A client store is a single document defined by its schema. It can be of different types which means, you can choose to store different data in a different way and use the same API to interact with the data regardless of the type.
new ClientStore(storeName, schema, config)
Name of the store.
A schema instance of schema object.
Store configuration object
appName
: name of the app. Think about it like database and the stores like tables or documents in the database;version
: version of the store. Used forINDEXEDDB
andWEBSQL
store typestype
: Determines how the data is saved in the browser. Valid values accessed via importingStorageType
:LOCALSTORAGE
;WEBSQL
;INDEXEDDB
;MEMORYSTORAGE
description
: Used to describe the store and can be useful for developers;idKeyName
: A key name to override the default store identifier key name. Learn morecreatedDateKeyName
: A key name to override the default store create date key name. Learn morecreatedDateKeyName
: A key name to override the default store update date key name. Learn more
interface Config {
appName?: string;
version?: number;
type?: string;
description?: string;
idKeyName?: string;
createdDateKeyName?: string;
createdDateKeyName?: string;
}
Name | Description | Type |
---|---|---|
schema |
The Schema instance used to define the store data |
Schema |
ready |
Whether the store has initialized and its ready | boolean |
type |
How and where the data will be stored Default: MEMORYSTORAGE |
LOCALSTORAGE | WEBSQL | INDEXEDDB | MEMORYSTORAGE |
name |
The name you gave to the store when initializing it | string |
appName |
The name of the app you provided in configuration. Default: App |
string |
processing |
A flag whether the store is currently processing some action | boolean |
processingEvents |
A list of all events the store is currently processing | EventType[] |
idKeyName |
The name of the key in the items used as identifier. Default: _id |
string |
createdDateKeyName |
The name of the key used to contain the date when the item was created. Default: _createdDate |
string |
updatedDateKeyName |
The name of the key used to contain the date when the item was last updated. Default: __lastUpdatedDate |
string |
Name | Description | Async | Arguments | Return |
---|---|---|---|---|
size |
Get how many items are in the store | yes | None |
number |
subscribe |
Listen to all events of the store | no | subscriber: StoreSubscriber<T> |
UnSubscriber |
on |
Listen to a specific event | no | event: EventType.PROCESSING_EVENTS, handler: (event: EventType[]) => void |
UnSubscriber |
off |
Stop listen to a specific event | no | event: EventType.PROCESSING_EVENTS, handler: (event: EventType[]) => void |
void |
beforeChange |
Intercept all actions to the store | no | handler: BeforeChangeHandler<T> |
UnSubscriber |
intercept |
Intercept a specific action of the store | no | event: EventType, handler: InterceptEventHandler<T> |
UnSubscriber |
loadItems |
Bulk create or update items into the store | yes | dataList: Array<Partial<T>> |
Array<T> |
createItem |
Create an item in the store | yes | data: Partial<T> |
T |
updateItem |
Update an existing item in the store | yes | id: string, data: Partial<T> |
T |
getItems |
Get all items in the store | yes | None |
T[] |
getItem |
Get a specific item from the store | yes | id: string |
T |
removeItem |
Remove a specific item in the store | yes | id: string |
string |
clear |
Clear all items in the store | yes | None |
string[] |
findItem |
Find a specific item in the store | yes | cb?: (value: T, key: string) => boolean |
T |
findItems |
Find a group of items in the store | yes | cb?: (value: T, key: string) => boolean |
T[] |
Error thrown when the store name is not provider
new ClientStore()
Error thrown when the schema is invalid
new ClientStore("todo", null)
Error thrown when you provide something other than a function to the subscribe
method
Error thrown when you provide invalid event name to on
, off
, or interecept
methods
Error thrown when you provide something other than a function to the on
, off
, beforeChange
or interecept
methods
Error thrown when you provide non-object literal or empty object literal to perform createItem
, updateItem
and loadItems
Error thrown when a field value of the data is required but not provided or it is of a wrong type
const todoStore = new ClientStore("todo", {
$name: String,
description: "No Description"
})
const todoStore = new ClientStore<ToDo>("todo", {
$name: String,
description: "No Description",
complete: false
}, {
type: INDEXEDDB,
version: 2,
appName: "Test",
idKeyName: "id",
createdDateKeyName: "dateCreated",
updatedDateKeyName: "dateUpdated",
});