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

@itrunc/jsondb

v0.2.6

Published

Storage in filesystem with JSON file for NodeJS

Downloads

73

Readme

Classes

Typedefs

Model

Create / Get a model

Kind: global class

new Model(folder, options)

Create a new model instance

| Param | Type | Description | | --- | --- | --- | | folder | String | Path to save data of the model | | options | ModelConstructorOptions | |

Example

const model = new Model('path/to/model', {
  rules: { name: [{ required: true }] },
  indexes: ['type']
})

model.saveMeta()

Save cached meta and mapping to filesystem

Kind: instance method of Model
Example

const model = new Model()
model.saveMeta()

model.has(key, options) ⇒ boolean

Check existence of a object with specific key.

Kind: instance method of Model

| Param | Type | | --- | --- | | key | string | | options | ModelHasOptions |

Example

const model = new Model()
model.on('missed', key => console.log(`${key} is missing`))
model.has('test')

model.getFilePath(key, options) ⇒ string

Get file path of the object with specific key.

Kind: instance method of Model
Returns: string - - If the object not found, empty string will be returned

| Param | Type | | --- | --- | | key | string | | options | ModelHasOptions |

Example

const model = new Model()
const file = model.getFilePath('test')

model.getMeta(key, options) ⇒ object | null

Get meta of the object with specific key.

Kind: instance method of Model
Returns: object | null - - If the object not found, null will be returned

| Param | Type | | --- | --- | | key | string | | options | ModelHasOptions |

Example

const model = new Model()
const meta = model.getMeta('test')

model.get(key, options) ⇒ object | null

Get object with specific key, null will be returned if object not existed

Kind: instance method of Model

| Param | Type | Description | | --- | --- | --- | | key | string | ID of an object | | options | ModelGetOptions | |

Example

const model = new Model()
model.on('error', (func, err, { key } = {}) => console.log(func, key, err))
const data = model.get('key1')
console.log(data)

model.mget(keys, options) ⇒ array

Get objects with list of ID

Kind: instance method of Model

| Param | Type | Description | | --- | --- | --- | | keys | array | ID list | | options | ModelGetOptions | |

Example

const model = new Model()
model.on('error', (func, err, { key } = {}) => console.log(func, key, err))
const data = model.mget(['key1', 'key2'])
console.log(data)

model.set(key, value, options)

Create of update an object with specific key

Kind: instance method of Model

| Param | Type | Description | | --- | --- | --- | | key | string | ID of an object | | value | object | Data to be saved in the JSON file | | options | ModelSetOptions | |

Example

const model = new Model()
model.on('error', (func, err, { key, value, index } = {}) => console.log(func, key, err, value, index))
model.on('set', (key, value, index, old) => console.log(key, value, index, old))
model.set('key1', { name: 'Ben' }).then(data => {
  console.log('Data is saved', data)
}).catch(error => {
  console.error(error)
}) 

model.find(comparator) ⇒ ModelFindReturns | null

Get the first object which the comparator returns true

Kind: instance method of Model

| Param | Type | | --- | --- | | comparator | function |

Example

const model = new Model()
const item = model.find(item => item.id === 'key1')
console.log(item)

model.findAll(comparator, options) ⇒ Array.<ModelFindReturns>

Get all objects which the comparator returns true

Kind: instance method of Model

| Param | Type | | --- | --- | | comparator | function | | options | PaginateOptions |

Example

const model = new Model()
const list = model.findAll(item => item.role === 'admin').map(({ id, data, index }) => {
  return { id, ...data, ...index }
})
console.log(list)

model.keysOf(comparator) ⇒ array

Keys of item matches criteria

Kind: instance method of Model

| Param | Type | | --- | --- | | comparator | function |

Example

const model = new Model()
const keys = model.keysOf(item => item.role === 'admin')
console.log(keys)

model.countOf(comparator) ⇒ number

Count of item matches criteria

Kind: instance method of Model

| Param | Type | | --- | --- | | comparator | function |

Example

const model = new Model()
const count = model.countOf(item => item.role === 'admin')
console.log(count)

model.mset(data, options)

Bulk create or update objects

Kind: instance method of Model

| Param | Type | | --- | --- | | data | array | object | | options | ModelSetOptions |

Example

const model = new Model()
model.mset([
  { id: 'item1', name: 'Peter' },
  { name: 'John' }
]).then(list => {
  console.log('List of objects been saved', list)
}).catch(console.error)

model.del(key, options)

Delete object with specific key

Kind: instance method of Model

| Param | Type | Description | | --- | --- | --- | | key | string | ID of an object | | options | ModelDelOptions | |

Example

const model = new Model()
model.on('deleted', (key, data) => console.log('deleted', key, data))
model.on('error', (func, err, { key } = {}) => console.log(func, key, err))
model.del('key1').then(obj => {
  console.log('Object been deleted', obj)
}).catch(console.error)

model.delAll()

Delete all objects

Kind: instance method of Model
Example

const model = new Model()
model.delAll()

Schema

Create / Get a schema

Kind: global class

new Schema(folder)

Create a new schema instance

| Param | Type | Description | | --- | --- | --- | | folder | string | path to save the schema |

Example

const schema = new Schema('path/to/schema')

schema.model(name, options) ⇒ Model

Create or get an instance of sub model

Kind: instance method of Schema

| Param | Type | Description | | --- | --- | --- | | name | string | folder name of the sub model | | options | ModelConstructorOptions | |

Example

const schema = new Schema()
const model = schema.model('test')

schema.schema(name) ⇒ Schema

Create or get an instance of sub schema

Kind: instance method of Schema

| Param | Type | Description | | --- | --- | --- | | name | string | folder name of the sub schema |

Example

const schema = new Schema()
const sub = schema.schema('test')

ModelConstructorOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [rules] | string | "{}" | Rules for the validators, refer to https://www.npmjs.com/package/async-validator | | [indexes] | string | "[]" | Name of the fields to save value in meta for searching |

ModelHasOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [event] | boolean | false | Indicates whether 'missed' event is triggered if not found |

ModelGetOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [event] | boolean | false | Indicates whether event is triggered if not found | | [includeAllFields] | boolean | false | Indicates whether the hidden fields are included in return |

ModelDelOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [event] | boolean | false | Indicates whether event is triggered if not found | | [real] | boolean | true | Indicates whether the JSON file will be really removed, if false, JSON file won't be delete but just delete key in meta | | [saveMeta] | boolean | true | Indicates whether meta file will be updated immediate |

ModelSetOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [event] | boolean | false | Indicates whether event is triggered if not found | | [saveMeta] | boolean | true | Indicates whether meta file will be updated immediate | | [who] | string | undefined | | For createdBy and updatedBy | | [indexes] | object | {} | Additional indexes to save in meta when saving the item |

ModelFindReturns : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | key | string | ID of the object | | data | object | The data saved in JSON file | | index | object | The data saved in meta |

PaginateOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [offset] | int | 0 | The first {offset} matched items are ignored | | [limit] | int | 0 | Page size, if it is 0 then no limit |