Skip to content

Simple database using memory and can be extended by multiple storages

License

Notifications You must be signed in to change notification settings

Westixy/nodbstore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple database using memory and can be extended by multiple storages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published