kimsible / mudb

Small NodeJS JSON database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mudb

mudb is a small JSON database with a very simple API

API

open()

const mudb = require('mudb')

async () => {
  const db = await mudb.open('path/to/json')
  ...
}

openSync()

Use only before starting a server or outside of a request handler.

const mudb = require('mudb')

const db =  mudb.openSync('path/to/json')

async () => {
  ...
}
  • put()

await db.put({_id: 'EIndiizUIU828', title: 'A small json database', ... }).save()
  • get()

Single filter

db.get({_id: 'EIdiizUIU828'}) // [{_id: 'EIndiizUIU828', title: 'A small json database', ... }]

Filter type can be a function, a string, a number, a boolean or a regexp

db.get({rate: rate => rate < 5}) // return all items with a rate lower than 5
db.get({title: /toto/}) // return all items with a title containing "toto"
db.get({active: true}) // return all items where active is true
db.get({rate: 4.5}) // return all items where rate is 4.5

Multiple filters

db.get({title: /toto/}, {rate: 4.5}, {...}, ...)

Select specific keys

db.get(['active', 'rate'], {title: /toto/}, {...}, ...) // [{active: true, rate: 5}, ...]

Sort result

db.get(...).sort((a, b) => a.date - b.date) // by oldest date
db.get(...).sort((a, b) => b.date - a.date) // by newest date
db.get(...).reverse() // by newest added

Limit result

db.get(...).filter((_, index) => index < 15) // limit to 15 items
db.get(...).reverse().filter((_, index) => index < 10) // by newest added and limit to 10 items
  • del()

await db.del({_id: 'EIdiizUIU828'}).save()
  • size

db.size // 1
  • dump

db.dump // ['item1', { _id: 'item2'}, ['item3'], ...]

drop()

const mudb = require('mudb')

async () => {
  await mudb.drop('path/to/json')
}

About

Small NodeJS JSON database

License:GNU Affero General Public License v3.0


Languages

Language:JavaScript 91.0%Language:Shell 9.0%