nodbstore

0.0.2 • Public • Published

NoDBStore

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

Getting started

npm i nodbstore

create the db

Only in memory

const nodbstore = require('nodbstore')
// create a database (a simple table)
const db = new nodbstore()

insert an element

// O(1)
const entry = db.put({ some: 'data', and: ['more', 'if', 'you', 'want'] })

edit an element

// O(1)
const editedEntry = db.put({ _id: entry._id, add: 'a field', some: undefined })

remove an element

// O(1)
const deletedEntry = db.remove(entry._id)

get an entry

// O(1)
const gettedEntry = db.get(entry._id)

find an element

// 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

find all entries

// O(n)
// always return an array
const entries = db.find(entry => entry.some === 'data')

export

// to JSON string
db.toJson()
// to JS Object !! thats not a clone !
db.toObj()
 
// if you want a clone :
JSON.parse(db.toJson())

import

// 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))

add a storage

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())

Package Sidebar

Install

npm i nodbstore

Weekly Downloads

5

Version

0.0.2

License

MIT

Unpacked Size

10.1 kB

Total Files

4

Last publish

Collaborators

  • westixy