1.0.0 • Published 5 years ago

apdb v1.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

Apdb

Red-black-tree-based data storage (for small program / light application / node / browser ), efficient and lightweight, support for persistence and temporary storage!

Usage

Install

npm i apdb --save

1. Import

  1. for nodejs
    import apdb from 'apdb/node';
  2. for wechat mini app
    import apdb from 'apdb/wechat';
  3. for web browser
    import apdb from 'apdb/web';

2. Initial

const apdb = new apdb( params );

params object

keydescdefault
tablestring storage table name'default'

3. Insert

const result = await apdb.insert( params );

result boolean

if insert successful, result is true

params object

keydescdefault
keystring primary keyDate.now() + Math.random()
valueobject valuenull
updateboolean update current tree to storage, improve multi-line insert performancetrue

demo

await apdb.insert({
  key: 'apdb key', 
  value: {
    name: 'apdb',
    author: 'echosoar'
  }
});

4. Select

const result = await apdb.select( params );

result object

keydesc
totalnumber Total number of data
resultarray all lines satisfying conditions
resultn.keyprimary key
resultn.valuevalue

params object

keydescdefault
tablestring storage table name'default'
keyarray string which fields are returned[]: return all fields
whereobject conditions for select, key value pair; use $key as key to select primary keynull
orderobject depend on certain columns for sorting, the value can be desc or ascnull
limitobjectnull
limit.startnumber limit which line to start returning0
limit.sizenumber limit number of returnsnull : return all lines

demo

const list = await apdb.select({
  where: {
    $key: /8/
  },
  order: {
    num: 'desc'
  },
  limit: {
    size: 5
  }
});

5. Update

const result = await apdb.update( params );

result number

result is the amount of updated lines

params object

keydescdefault
tablestring storage table name'default'
whereobject conditions for update, key value pair; use $key as key to select primary keynull
valueobject new value{}
newValueboolean if ture, the original value columns will be discardedfalse

demo

const updateCount = await apdb.update({
    where: {
      num: 302910
    },
    value: { 
      up: 'test new value'
    },
});
  

6. Delete

const result = await apdb.delete( params );

result number

result is the amount of deleted lines

params object

keydescdefault
tablestring storage table name'default'
whereobject conditions for delete, key value pair; use $key as key to select primary keynull

demo

const deleteCount = await apdb.delete({
    where: {
      num: 302909
    }
});

Performance

Node environment

typeamount(lines)operatetime usage(ms)space usage(MB)
json1,000,000init2000-
json1,000,000insert14451130.8
msgpack1,000,000init7281-
msgpack1,000,000insert16288100.7
- all1,000,000order desc1729
- all1,000,000order asc1746
- all1,000,000select where normal103
- all1,000,000select where regexp7197

Each line format

{
  key: "1545802142928test13",
  value: {
    "num":302910,
    "key":"1545801896834test302910"
  }
}
1.0.0

5 years ago