Simple JSON in-memory auto-persistent database for server and client.
- Simple JS object, no extraneous API
- Auto-persisted using on-change
- Saves to a json file on server, and uses localStorage in browser
- Works in browser, with React or Preact, or without
Note: Uses ES6 features (Proxy), use only where browser/env supports it
npm i undbconstundb=require('undb')const[db,onChange]=undb({path: './store.json',/* in node */path: 'namespace',/* in browser */initial: {something: false}})onChange(()=>{// Fires whenever db changes})constundb=require('undb')const[db,onChange]=undb(options)dbDeeply observed JS object that triggers auto-save feature when modifiedonChange[fn]Called wheneverdbchangesoptionspath[str]Path to use for persistence. Should be a filename on server, or a "namespace" on clientinitial[obj]Initial database structureclear[bool]Deletes all localstorage items except current pathdebounce[num]DebounceonChangebefore[bool]MakeonChangefire before the value has been updated indbread[fn=>obj]Intercept the read function. Must return a data object.fnis the default read functionwrite[cb=>null]Intercept the write function. Must callcbonChange[obj]Options passed to on-change
const{ link }=require('undb/common/utils')conststate1Tuple=undb(options)conststate2Tuple=undb(options)link(state1Tuple,state2Tuple);const{ link }=require('undb/common/utils')const[state1,state2]=link({},{});const[state1,onChange,link]=undb(options)const[state2]=link({})link[fn]Link 2 or more states. Changing one will change the other(s).@param {...[state, onChange]|initial}Takes either the[state, onChange]tuple or aninitialobject to create a new reactive state object from.@returns state[]Returns an array corresponding to the input arguments, each item either being the input[state](from the tuple) or a newly created one.
connect[fn=>fn=>Component]Connects theonChangeto a<Component>so that it re-renders wheneveronChangeis fired
constconnect=require('undb/browser/connect')constReactiveComponent=connect(onChange)(<Component>)useState[obj=>obj]React State Hook alternative that updates whenstateobject is modified
const{ useState }=require('undb/browser/hooks')conststate=useState({counter: 0})createUseState[fn=>fn]Create auseStatehook for existingstate
const{ createUseState }=require('undb/browser/hooks')const[state,onChange]=undb(options)constuseState=createUseState(onChange)Note: Re-renders entire React App
store.jsconstundb=require('undb')const[store,onChange]=undb({path: 'my-app'})exports.store=storeexports.onChange=onChange
components/App.jsconst{ store }=require('../store')module.exports=()=>[<h1>Hello {store.name}!</h1>,<inputonChange={e=>store.name=e.target.value}> ]
main.jsconstReact=require('react')const{ onChange }=require('./store')constApp=require('./components/App')onChange(()=>React.render(<App>))
components/App.jsconstundb=require('undb')constconnect=require('undb/browser/connect')const[state,onChange]=undb()constApp=()=>[<h1>Hello {state.name}!</h1>,<inputonInput={e=>state.name=e.target.value}> ] module.exports = connect(onChange)(App)
main.jsconstReact=require('react')constApp=require('./components/App')React.render(<App>)
React State Hook Alternative
components/App.jsconst{ useState }=require('undb/browser/hooks')constApp=()=>{conststate=useState({counter: 0})return(<buttononClick={()=>state.counter++}> `You have pressed this button ${state.counter} times` </button>)}