mudb is a small JSON database with a very simple API
const mudb = require('mudb')
async () => {
const db = await mudb.open('path/to/json')
...
}
Use only before starting a server or outside of a request handler.
const mudb = require('mudb')
const db = mudb.openSync('path/to/json')
async () => {
...
}
await db.put({_id: 'EIndiizUIU828', title: 'A small json database', ... }).save()
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
await db.del({_id: 'EIdiizUIU828'}).save()
db.size // 1
db.dump // ['item1', { _id: 'item2'}, ['item3'], ...]
const mudb = require('mudb')
async () => {
await mudb.drop('path/to/json')
}