From df6a61155cc2cd8cdb1564619857e2932eaeae62 Mon Sep 17 00:00:00 2001 From: Massimo Candela Date: Thu, 12 Dec 2024 14:24:03 +0100 Subject: [PATCH] added methods getCollection and isMock --- README.md | 1 + src/BasicObj.js | 2 ++ src/Store.js | 22 ++++++++++++++++++++-- tests/store.find.test.js | 9 +++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 78d3be8..03c9317 100644 --- a/README.md +++ b/README.md @@ -641,6 +641,7 @@ The store has the following method. | findSync(type, filterFunction) | This method returns the objects in a synchronous way (no Promise). However, *it works only if you already performed an async operation (e.g., like refresh, load, find, subscribe) or if you set lazyLoad to false and the store had enough time to load.* | | hasChanged(type, object) | This method receives in input a model type and an object (optional). It returns a boolean, `true` if the object is dirty (it changed but it has not yet being persisted). If the object is not passed, the method checks if any of the objects of the specified model type has changed. | | didUpdate(context) | If you pass an object as a prop to a React component, the component will not refresh if the object changes. To address this, you can use this method inside the `componentDidUpdate` to check if any of the objects changed. See [example 6](#example-7---react-props-and-observability) | +| getCollection(type) | This method receives in input a model type . It returns all the raw (JSON) objects available for that model. | ### Insert vs. Mock diff --git a/src/BasicObj.js b/src/BasicObj.js index bdb1470..5cf1103 100644 --- a/src/BasicObj.js +++ b/src/BasicObj.js @@ -63,6 +63,8 @@ export class BasicObj { isDataflux = () => true; + isMock = () => false; + setId = (id) => { this.id = id; }; diff --git a/src/Store.js b/src/Store.js index 9ade366..9d01782 100644 --- a/src/Store.js +++ b/src/Store.js @@ -178,11 +178,15 @@ export default class Store { }); }; - hasChanged (type, object) { - + _validateTypeInput = (type) => { if (!this.models[type]) { throw new Error("Not valid model type"); } + } + + hasChanged (type, object) { + + this._validateTypeInput(type); const _hasChanged = (type, object) => { const obj = this.models[type].storedObjects[object.getId()]; @@ -224,6 +228,8 @@ export default class Store { } getDiff (type, ifLoaded) { + this._validateTypeInput(type); + return this._getPromise(type, ifLoaded) .then(() => this._getDiffSync(type)); }; @@ -394,8 +400,10 @@ export default class Store { } if (status === "mock") { + wrapper.isMock = () => true; wrapper.insert = () => { this.models[type].storedObjects[id].status = "new"; + wrapper.isMock = () => false; this.update([wrapper]); delete wrapper.insert; }; @@ -425,4 +433,14 @@ export default class Store { return item.promise; }; + + getCollection (type) { + return this._getPromise(type) + .then(() => { + return Object.values(this.models[type].storedObjects) + .map(i => i.object) + .sort((a, b) => a.getId().localeCompare(b.getId())) + .map(i => i.toJSON()); + }); + } } \ No newline at end of file diff --git a/tests/store.find.test.js b/tests/store.find.test.js index 5e5e5d8..fd04c4b 100644 --- a/tests/store.find.test.js +++ b/tests/store.find.test.js @@ -112,4 +112,13 @@ describe("Store find", function () { }); }).timeout(10000); + + it("getCollection", function (done) { + store.getCollection("author") + .then(collection => { + expect(collection.map(i => expectedAuthors.find(n => n.id === i.id)).filter(i => !i).length).to.equal(0); + done(); + }); + + }).timeout(10000); }); \ No newline at end of file