npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modb-js

v1.0.2

Published

In Memory Object DataBase for JS.

Downloads

3

Readme

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.