1.0.0 • Published 7 years ago

sr-store v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

sr-store

A javascript class which wraps localStorage, but can also work in standalone.

Install

Usage

Create a store

// all data will be saved to localStorage, under the key 'collection'
// and will auto read data in localStorage, populate it.
let store = new Store(window.localStorage, 'collections')
store.getName() === 'collections'


let store = new Store({})
// That pass an invalid localStorage makes store work in memory,
// omit the name, store will use 'db' as the name.
store.getName() === 'db'
store.inMemory === true

Insert

store.insert({name: 'Alex'})
// {
//  collections: [{name: 'Alex'}]
// }
store.insert([{name: 'Billy'},{name: 'Chuck'}])
// {
//  collections: [{name: 'Alex'}, {name: 'Billy'}, {name: 'Chuck'}]
// }

Remove

store.remove({name: 'Alex'}) 
// return n which is the number of records removed
// {
//  collections: [{name: 'Billy'},{name: 'Chuck'}]
// }
store.remove({})
// pass an empty object will remove all records 
// {
//  collections: []
// }

Destroy

// delete all data and namespace in localstorage
store.destroy()
// {
//  
// }

Find

store.find({name: 'Alex'}) 
// return an array
store.find({})
// return an array including all records

Update

// {
//  collections: [{name: 'Alex'}]
// }
store.update({name: 'Alex'}, {name: 'Alex with updated name'}) 
// return n which is the number of records updated
// Note: the first argument must be an object to query records

License

MIT