diff --git a/README.md b/README.md index 716b2d9..62e73de 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,13 @@ you can also find items via properties like pets.find({"type": "cat"}); // [{"id": "123fk91j7", "name": "Fluffy", "type": "cat"}] +and when it comes time to update you can do that to + + var fluffy = pets.find({"name": "Fluffy"})[0]; // one record + fluffy.age = 25; + + pets.update(fluffy); + and you can remove items via pets.remove("123fk91j7"); @@ -54,11 +61,12 @@ so, no records, right? ### API -The public API is very simple, you really only need 3 methods: `add`, `remove`, and `find`. +The public API is very simple, you really only need 4 methods: `add`, `update`, `remove`, and `find`. | Method | Description | |---|---| | `.add(object)` | Adds entry to collection and returns entry(s) added | +| `.update(object)` | Updates a record | | `.remove(id or object)` | Removes entry(s) from collection and returns removed | | `.find(id or object)` | find all, find by id, or find by filter, returns array of entries | | `.dump()` | saves records to JSON file | @@ -71,7 +79,7 @@ let cats = pets.find({"type": "cat"}); // [] cats.length; // 0 ``` -> NOTICE: `find` is special, changes based on what you pass in, an id, an object, or nothing at all +> NOTICE: `find` is special, changes based on what you pass in, an id, an object, or nothing at all - Additional [API Documentation](docs/api.md) ### Options @@ -102,3 +110,7 @@ Anyone is welcome to contribute, however, if you decide to get involved, please --- > [*]: with exception of a generated ID, if not provided + +## TODO + +- make reactive, so just doing `fluffy.age = 25` updates diff --git a/dist/record.js b/dist/record.js index ca16a95..1c9e208 100644 --- a/dist/record.js +++ b/dist/record.js @@ -1,5 +1,5 @@ -/*! Record.js v0.6.1 | MIT | https://github.com/n2geoff/record.js */ -(function (root, factory) { +/*! Record.js v0.7.0 | MIT | https://github.com/n2geoff/record.js */ +;(function (root, factory) { "use strict"; if (typeof module === "object" && module.exports) { module.exports = factory(root.Record); @@ -62,6 +62,24 @@ } } + update(entry) { + + var idx = this.records.indexOf(entry); + + if(this.records[idx].id === entry.id) { + + this.records.splice(idx, 1, entry); + + this._save(); + + return entry; + + } else { + return false; + } + + } + find(key) { if (!key) { diff --git a/dist/record.min.js b/dist/record.min.js index f4af631..4e201a9 100644 --- a/dist/record.min.js +++ b/dist/record.min.js @@ -1,2 +1,2 @@ -/*! Record.js v0.6.1 | MIT | https://github.com/n2geoff/record.js */ -!function(t,e){"use strict";"object"==typeof module&&module.exports?module.exports=e(t.Record):t.Record=e(t.Record)}(this,function(){"use strict";return class{constructor(t){if(this.store=(t||{}).store,this.debug=(t||{}).debug||!1,this.records=[],this.store&&localStorage){this._log("Initializing localStorage for "+this.store);let t=this._load()||[];this.records=[...t]}}_log(){this.debug&&console.log(...arguments)}add(t){if(Array.isArray(t)){let e=[];return t.forEach(()=>{t.id||(t.id=Math.random().toString(36).substr(2,9)),this.records.push(t),this.entries.push(t)}),this._save(),e}return t.id||(t.id=Math.random().toString(36).substr(2,9)),this.records.push(t),this._save(),t}find(t){if(!t)return this.records;if("string"==typeof t||"number"==typeof t)return this.records.filter(e=>e.id===t);let e=Object.keys(t);return this.records.filter(r=>-1!==e.indexOf("id")?r.id===t.id:e.every(e=>r[e]===t[e]))}remove(t){if(!t||Array.isArray(t))return this._log(console.error("remove() accepts a single object")),[];let e=this.find(t);return e.forEach(t=>{this.records.splice(this.records.indexOf(t),1)}),this._save(),e}clear(){this.records=[],this._save()}_save(){this.store&&localStorage&&localStorage.setItem(this.store,JSON.stringify(this.records))}_load(){if(this.store&&localStorage)return JSON.parse(localStorage.getItem(this.store))||[]}dump(){!function(t,e){let r=document.createElement("a"),s=new Blob([e],{type:"text/plain"});r.href=URL.createObjectURL(s),r.download=t,r.click()}(`${this.store||"data"}.json`,JSON.stringify(this._load(),null,4))}}}); \ No newline at end of file +/*! Record.js v0.7.0 | MIT | https://github.com/n2geoff/record.js */ +!function(t,e){"use strict";"object"==typeof module&&module.exports?module.exports=e(t.Record):t.Record=e(t.Record)}(this,function(){"use strict";return class{constructor(t){if(this.store=(t||{}).store,this.debug=(t||{}).debug||!1,this.records=[],this.store&&localStorage){this._log("Initializing localStorage for "+this.store);let t=this._load()||[];this.records=[...t]}}_log(){this.debug&&console.log(...arguments)}add(t){if(Array.isArray(t)){let e=[];return t.forEach(()=>{t.id||(t.id=Math.random().toString(36).substr(2,9)),this.records.push(t),this.entries.push(t)}),this._save(),e}return t.id||(t.id=Math.random().toString(36).substr(2,9)),this.records.push(t),this._save(),t}update(t){var e=this.records.indexOf(t);return this.records[e].id===t.id&&(this.records.splice(e,1,t),this._save(),t)}find(t){if(!t)return this.records;if("string"==typeof t||"number"==typeof t)return this.records.filter(e=>e.id===t);let e=Object.keys(t);return this.records.filter(r=>-1!==e.indexOf("id")?r.id===t.id:e.every(e=>r[e]===t[e]))}remove(t){if(!t||Array.isArray(t))return this._log(console.error("remove() accepts a single object")),[];let e=this.find(t);return e.forEach(t=>{this.records.splice(this.records.indexOf(t),1)}),this._save(),e}clear(){this.records=[],this._save()}_save(){this.store&&localStorage&&localStorage.setItem(this.store,JSON.stringify(this.records))}_load(){if(this.store&&localStorage)return JSON.parse(localStorage.getItem(this.store))||[]}dump(){!function(t,e){let r=document.createElement("a"),s=new Blob([e],{type:"text/plain"});r.href=URL.createObjectURL(s),r.download=t,r.click()}(`${this.store||"data"}.json`,JSON.stringify(this._load(),null,4))}}}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7922f12..6b15d66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "record.js", - "version": "0.6.1", + "version": "0.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c6fc73a..0fee9b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "record.js", - "version": "0.6.1", + "version": "0.7.0", "description": "dead-simple storage-collection library", "main": "src/record.js", "directories": { diff --git a/src/record.js b/src/record.js index 39ac0cd..4096644 100644 --- a/src/record.js +++ b/src/record.js @@ -1,5 +1,5 @@ /*! Record.js v0.6.1 | MIT | https://github.com/n2geoff/record.js */ -(function (root, factory) { +;(function (root, factory) { "use strict"; if (typeof module === "object" && module.exports) { module.exports = factory(root.Record); @@ -114,6 +114,33 @@ } } + /** + * Updates a Record + * + * @param {Object} entry + */ + update(entry) { + + // find entry index + var idx = this.records.indexOf(entry); + + // sanity check + if(this.records[idx].id === entry.id) { + + // update record + this.records.splice(idx, 1, entry); + + // save to storage + this._save(); + + return entry; + + } else { + return false; + } + + } + /** * Finds records in collection by id or object filter. * diff --git a/test/record.spec.js b/test/record.spec.js index c41deef..d2d08b0 100644 --- a/test/record.spec.js +++ b/test/record.spec.js @@ -34,6 +34,30 @@ test("Record.js", function(t) { t.end(); }); + t.test("should be able to update a record", function(t) { + + let plato = pets.find({"name": "plato"})[0]; + + t.equal(plato.age, undefined, "should not have age yet"); + + plato.age = 80; + + let update = pets.update(plato); + + t.equal(update.age, 80, "should exist in db now"); + t.end(); + }); + + t.test("should be able find pet", function(t) { + + let plato = pets.find({"age": 80})[0]; + + t.ok(plato, "found plato"); + + t.equal(plato.name, "plato", "yep, that him"); + t.end(); + }); + t.test("should find all matching records", function(t) { let dogs = pets.find({"type": "dog"});