createStore()
Create the object relationship store.
Basic usage
import { createStore, createRelationalObject } from "@jjmyers/object-relationship-store";
const user = createRelationalObject("user");
const image = createRelationalObject("image");
const store = createStore({
relationalCreators: [
user,
image,
],
identifier: {
user: o => "username" in o,
image: o => "uri" in o,
}
});
Properties
relationalCreators
Expects an Array of ORS.RelationalCreator objects.
createStore({
relationalCreators: [
user,
image,
]
});
identifier
Expects an object where the keys are the names of the ORS.RelationalCreator objects and value is an identifier Function that returns a boolean. This Function will be used to identify an object when it is passed into the store.
createStore({
identifier: {
user: object => "username" in object,
image: object => "uri" in object,
}
});
indexes
Expects an array of ORS.RelationalObjectIndex objects
import { createStore, createRelationalObjectIndex } from "@jjmyers/object-relationship-store";
const postFeed = createRelationalObjectIndex("postFeed", [post]);
createStore({
indexes: [
postFeed
]
});
Return values
const store = createStore({
// ...properties
});
store.mutate()
store.mutateWhere()
store.select()
store.selectIndex()
store.destroy()
store.purge()
store.getReferences()
store.getState()
store.restore()
store.save()
store.subscribe()
.mutate(payload)
Push a payload into the store. Can create, update or delete objects in the store
Find out more about store.mutate()
.mutateWhere(selector, callback)
Push a payload into the store. Can create, update or delete objects in the store. The difference here is that it take in a where clause.
.select(selector)
Select data from the store
Find out more about store.select()
.selectIndex(index, options)
Select data from an index in the store
Find out more about store.selectIndex()
.destroy(name)
Delete all objects associated with the name.
All user object in the store will be deleted.
store.destroy("user")
.purge()
Delete all objects in the store.
store.purge()
.getReferences()
Returns all object references
Don't use this method, it will be removed in a future version. It's here just for debugging purposes.
store.getReferences()
.getState()
Returns all the objects in the store
Don't use this method, to get data from the store. Use store.select().
store.getState()
.save(callback)
Save the current state of the store.
Use store.restore() to restore the saved state.
store.save(currentState => {
// Save currentState to local storage.
})
.restore(savedState)
Restore state from the saved state
Use store.save() to get savedState
let savedState = null
store.save(currentState => {
// Save currentState to local storage.
savedState = currentState
})
store.restore(savedState)
.subscribe(listener)
Used to subscribe to changes in the store.
This method was created to help when integrating this library with React's useSyncExternalStore()
function listener() {}
store.subscribe(listener)
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
relationalCreators | ORS.RelationalCreator[] | undefined | An array of ORS.RelationalCreator objects. |
identifier | Record<string, (object) => boolean> | undefined | Identifier function that will be called on objects to determine the type of the object. |
indexes | ORS.RelationalObjectIndex[] | undefined | Identifier function that will be called on objects to determine the type of the object. |
Return values
| Name | Description |
|---|---|
.mutate(payload) | Push a payload into the store. Can create, update or delete objects in the store. |
.mutateWhere(selector, callback) | Push a payload into the store. Can create, update or delete objects in the store. The difference here is that it take in a `where` clause. |
.mutateWhere(selector, callback) | Push a payload into the store. Can create, update or delete objects in the store. The difference here is that it take in a `where` clause. |
.select(selector) | Select data from the store. |
.selectIndex(index, options) | Select data from the store. |
.destroy(name) | Delete all objects associated with the name. |
.purge() | Delete all objects in the store. |
.getReferences() | Returns all object references Don't use this method, it will be removed in a future version. |
.getState() | Returns all the objects in the store. |
.save(callback) | Save the current state of the store. |
.restore(savedState) | Restore state from the saved state. |
.subscribe(listener) | Used to subscribe to changes in the store. |