Skip to main content

store.mutate()

To make any changes to the data in the store, store.mutate() will be used.

  • Create data
  • Update data
  • Delete data

Usage

Create an object

store.mutate({
id: 1,
username: "the_overlord"
})

Update an object

store.mutate({
id: 1,
username: "Updated!"
})

Delete an object

store.mutate({
id: 1,
__identify__: "user",
__destroy__: true,
})

Payload

The payload is an object that we want to create/update in the store.

const payload = { id: 1, username: "the_overlord" }
store.mutate(payload)

Payloads have 4 special fields

  • __identify__ a string, to tell the store what type of object this is.
  • __indexes__ a string[], to tell the store which indexes this object belongs to.
  • __removeFromIndexes__ a string[], to tell the store which indexes we should remove this object from.
  • __destroy__ a boolean, to tell the store that we want to delete this object from the store.

__identify__

There are two ways an the store can identify the type of an object.

  • Through the identifier
  • By adding __identify__ in the object

Adding data will be faster when using __identify__ in the object compaired to the identifier function.

If __identify__ is found in the object, the store will not use the identifier function.

Here the identifier function will not know that this object is a user object, so we add __identify__ in the object to tell the store that this is a user object.

const store = createStore({
// ...properties
identifier: {
user: o => "username" in o,
}
});

store.mutate({
id: 1,
age: 25,
__identify__: "user",
})

__indexes__

When we add some object to the store or create a new object, we may need to add the objects to an index

store.mutate({
id: 1,
age: 25,
__indexes__: ["postFeed-home"]
})

When using an index, we folllow this naming structure [indexName]-[uniqueKey]

This way we can use the same index for multiple pages.

Example:

// We have a comments page, for each post.
const commentsIndex = createRelationalObjectIndex("commentsIndex", [comment]);

// Here is how we will select data in this index
// Post ID will be passed dynamically.
// So for each post comments page, we will maintain a seperate index of comments.
store.selectIndex(`commentsIndex-${postId}`, options);

// Add one comment to the store and to the index 'commentsIndex'
store.mutate({
// ...comment
__indexes__: [`commentsIndex-${postId}`]
})

__removeFromIndexes__

To remove an object from an index but not from the store, we use __removeFromIndexes__.

note

Over here we passed the id of the comment, and used __identify__ to identify the object as a comment and __removeFromIndexes__ to remove it from the index.

store.mutate({
id: 10,
__identify__: "comment",
__removeFromIndexes__: [`commentsIndex-${postId}`]
})

__destroy__

To delete an object from the store, pass __destroy__ as true.

note

All references to this object's primaryKey will be removed.

All orphaned children will be deleted.

To keep the orphaned children and references, you must do a soft delete. (probably using a field like 'isDeleted' to know if the object is deleted.)

store.mutate({
id: 10,
__identify__: "comment",
__destroy__: true
})

API

Properties

PropertyTypeDefaultDescription
__identify__stringundefinedUsed to define the type of the object when the identifier function will not work.
__destroy__booleanundefinedWhen true the object will be deleted from the store. All references to the object and all orphaned children will be deleted.
__indexes__string[]undefinedWhen an object is being created in the store, the object will also be added to all these indexes.
__removeFromIndexes__string[]undefinedWhen set, the object will be removed from all matching indexes.