lodat (Local Database)
Powerful persistent store for Javascript. Compatible with (localStorage, sessionStorage, AsyncStorage, and many more)
Installation
npm install lodat --save
Getting started
Making a simple counter app using Lodat
import lodat from "lodat"; // create lodat db with some optionsconst db = ;// handle db updatingdb; { return ;} { ;} { const count = await db; documentbodyinnerHTML = `Counter: `;} ;
Using schema
import lodat from "lodat"; const db = ; { return contexttodos;} { contexttodos;} const newTodo = await db; db;
Querying entity
{ return contexttodos;} { return contexttodos;} { return contexttodos;}
Storing data in cookie
import CookieStorage from "cookie-storage"; const db = ;
Examples
Performance testing (TBD)
Lodat is about 10x faster than lowdb Add and update 1000 todos
References
lodat(options)
-
options:
name type description name string database name (def = '') storage Storage object specify storage type will be used (def = memoryStorage) debounce number specify writing debouncing (def = 0, no debouncing) initial any specify default data init Generator Function specify an action will be executed in initializing phase schemas string[] list of schema names schemas { prop: 'schema name' } schema mappings -
Return: Database instance
Database props
name | return | description |
---|---|---|
exec(executor, payload?) | Promise | execute an executor with specified payload. Executor must be Generator Function |
subscribe(listener) | Unsubscribe function | add a change listener. It will be called any time an data manipulated |
clear() | void | clear all data |
flush() | void | flush all pending writes to storage |
Context props
name | type/return | description |
---|---|---|
schema(name) | Schema object | get specified schema by its name |
get(name) | Yield | get value of specified database prop |
set(name, value) | Yield | set value of specified database prop |
set(name, reducerFn) | Yield | set value of specified database prop using reducerFn, the reducerFn retrieves previous value as first argument |
exec(executor, payload?) | Yield | execute an executor with specified payload. Executor must be Generator Function |
fork(executor, payload?) | Yield | execute an executor with specified payload and dont block current execution. Executor must be Generator Function |
Schema props
name | type/return | description |
---|---|---|
name | string | schema name |
create(props) | Yield | create new entity with specified props |
exist(entityKey) | Yield | check entity existing by its key |
remove(...entityKeys) | Yield | remove multiple entities by their keys |
count() | Yield | return a number of entity in the schema |
all() | Yield<Entity[]> | return all entities in the schema |
all(limit) | Yield<Entity[]> | return first N entities in the schema |
all(entityKeys) | Yield<Entity[]> | return all entities that matches given keys |
all(predicateFn) | Yield<Entity[]> | return all entities that matches given predicateFn |
all(predicateFn, limit) | Yield<Entity[]> | return first N entities that matches given predicateFn |
get(entityKey) | Yield | get entity by key |
get(predicateFn) | Yield | get first entity that matches given predicateFn |
clear() | Yield | clear all entities in the schema |
update(entity, props) | Yield | update specified entity with new props, if no props changed, nothing to write to storage |
subscribe(listener) | Unsubscribe function | add a change listener. It will be called any time an entity manipulated |
Generator Function
Lodat uses generator function to control reading or writing data flows and async execution (no async/await needed). Below is simple generator function.
{ // a generatorContext provides many util methods, please refer Context props section for further info // retrieve return value from yield expression const returnValueOfDoSomething = generatorContext; // retrieve resolved value from promise const resolvedValue = ;}