Skip to content

Commit

Permalink
added methods getCollection and isMock
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Dec 12, 2024
1 parent e912dbd commit df6a611
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/BasicObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class BasicObj {

isDataflux = () => true;

isMock = () => false;

setId = (id) => {
this.id = id;
};
Expand Down
22 changes: 20 additions & 2 deletions src/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down Expand Up @@ -224,6 +228,8 @@ export default class Store {
}

getDiff (type, ifLoaded) {
this._validateTypeInput(type);

return this._getPromise(type, ifLoaded)
.then(() => this._getDiffSync(type));
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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());
});
}
}
9 changes: 9 additions & 0 deletions tests/store.find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit df6a611

Please sign in to comment.