Simple database working on memory and can have multiple storage
it will be use for small data that need to be stored easily with a simple api
npm i nodbstore
Only in memory
const nodbstore = require('nodbstore')
// create a database (a simple table)
const db = new nodbstore()
// O(1)
const entry = db.put({ some: 'data', and: ['more', 'if', 'you', 'want'] })
// O(1)
const editedEntry = db.put({ _id: entry._id, add: 'a field', some: undefined })
// O(1)
const deletedEntry = db.remove(entry._id)
// O(1)
const gettedEntry = db.get(entry._id)
// O(n)
// stop when it finded one
const entry = db.findOne(entry => entry.some === 'data')
// start from the end
const entry = db.findOne(entry => entry.some === 'data', true)
// return null if it doesnt find something
// O(n)
// always return an array
const entries = db.find(entry => entry.some === 'data')
// to JSON string
db.toJson()
// to JS Object !! thats not a clone !
db.toObj()
// if you want a clone :
JSON.parse(db.toJson())
// from JSON string
db.loadJson(jsonString)
// from exported db object !! it will edit directly the object
db.loadObj(exportedDbObject)
// wana work from a clone
db.loadJson(JSON.stringify(exportedDbObject))
class NewStore extends nodbstore.Storage {
write(json) { // trigger when some data are updated
// you can add test if you want to skip write in some case
if (console.log === undefined) return
this.nodb // the instance of the database
console.log(json)
json === this.nodb.toJson() // -> true
}
// can be omited
load() { // called manuall or with nodb.loadFromStore( store )
this.nodb.loadJson(/* json of db obtained with love */)
}
// can be omited
init() { // trigger when store is added to nodb
this.data.that.I.will.use.later = 'some'
// or do WTFYW
}
}
db.addStore(new NewStore())