Skip to content

futurist/modb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MODB

In Memory Object DataBase for JS.

npm Build Status JavaScript Style Guide

Install

npm i modb-js -S

Why

In JS, you should avoid below code:

const arr = new Array(10000)
// ... ... fill arr with some objects
const item = arr.find(x=>x.id=='someid')  // --> NEVER DO THIS!

Above code will froze(block) your application totally if the array is large enough.

Instead, you should need some form of In Memroy Database, this lib is good if you store Array of Object in memory as a small database.

Performance

With below sample array:

// create your arr as normal, each object need have an id
const arr = Array.from({length: 1e6}, (val,id) => ({
    id, n: Math.random()*100
}))

Test of find item for 10000 times:

const modb = require('modb-js')  // require the lib
const db = new modb(arr)  // create indexes

// A. vanilla .find
console.time('vanilla find')
for(let i=10000;i<20000;i++){
  arr.find(x=>x.id === i)
}
console.timeEnd('vanilla find')

// B. modb .find
console.time('modb find')
for(let i=10000;i<20000;i++){
  db.find('id', i)
}
console.timeEnd('modb find')

The result:

vanilla find: 2659.199ms

modb speed: 3.985ms

API

const db = new MODB(arr, [indexDef], [options])

Create modb instance from existing array.

Return: [Object] MODB instance, has data, index, indexDef, config key.

arr: [Array of objects] each object should have an id key.

indexDef: [Object] The key is index of the db, the value should below:

{unique: Boolean} true: The key is unique, and find result is 1 item.
{multiple: Boolean} true: The key is not unique, result is array of items.
{skip: Boolean} true: When create index, skip this key.

options: [Object] The key can be below:

{idKey: String} The default `id` key when create new MODB instance.
{notKey: String} The default `$not` key when find inverted results.

example

db = new modb(arr)  // default id is 'id', unique
db = new modb(arr, {a:{unique:true}, id:{skip:true}})  // using a as id, skip default 'id' as key.
db = new modb(arr, {'pid': {multiple: true}})  // create id as unique index, and pid as multiple index.

db.createIndex(key, def)

Create new index from exists modb.

Return: [Object]

{
  ok: 0/1,  // 1: Success; 0: Fail
}

key [String] The key of index to create.

def [Object] The definition of index, same as indexDef when create modb.

db.find(id, value, returnIndex)

Find result items, which id is value.

Return: [Object/Array] If the id is unique, return single object, else return array of objects.

id [String] The key of object to find.

value [Any] The value of object[id] to find.

returnIndex [Boolean] Return index of item in array instead of item value.

example

db.find('id', 5)  // result of arr.find(x=>x.id==5)
db.find('pid', 5)  // ALL results of arr.find(x=>x.pid==5)

db.findCond(object, returnIndex)

Find result items, from condition object.

Return: [Array] Always return array of objects.

object [Object] $and of each find result.

returnIndex [Boolean] Return index of item in array instead of item value.

example

db.findCond({'id': 5, 'pid': 3})  // ALL results of arr.find(x=>x.id==5 && x.pid==3)

db.findMany(condArr, returnIndex)

Find result items, from condition object/array of condition objects.

Return: [Array] Always return array of objects.

condArr [Object/Array] If it's array, return $or of each condition object.

returnIndex [Boolean] Return index of item in array instead of item value.

example

db.findMany([{'pid': 3}, {'id': 5}])  // ALL results of arr.find(x=>x.id==5 || x.pid==3)

db.insert(item)

Insert a new item into db.

Return: [Object]

{
  ok: 0/1,  // 1: Success; 0: Fail
}

item [Object] The object to insert.

example

db.insert({id: 6, x:11})  // insert a new item into db and arr

db.delete(id, value)

Delete items from db.

Return: [Object]

{
  ok: 0/1,  // 1: Success; 0: Fail
  deleted: object  // deleted items
}

id [String] The key of object to delete.

value [Any] The value of object[id] to delete.

example

db.delete('id', 6)  // delete item of id==6 from db and arr

db.update(id, value, newItem, [options])

Update item from db with newItem.

Return: [Object]

{
  ok: 0/1,  // 1: Success; 0: Fail
}

id [String] The key of object to update.

value [Any] The value of object[id] to update.

newItem [Object] The object to insert.

options [Object]

{
  upsert: Boolean,  // true: insert newItem if the key/value cannot be found
  replace: Boolean,  // true: replace exists item with newItem
                     // false: Merge into exists item with newItem
}

example

db.update('id', 6, {x:12})  // merge {x:12} into id:6 from db and arr
db.update('id', 6, {id:6, y:12}, {replace: true})  // replace newItem with id:6 from db and arr
db.update('id', 6, {id:6, y:12}, {upsert: true})  // merge newItem of id:6 from db and arr, if not found, insert it.

About

In Memory Object DataBase for JS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published