Coder Social home page Coder Social logo

nodbstore's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.