@itrunc/jsondb
v0.2.6
Published
Storage in filesystem with JSON file for NodeJS
Downloads
6
Readme
Classes
Typedefs
Model
Create / Get a model
Kind: global class
- Model
- new Model(folder, options)
- .saveMeta()
- .has(key, options) ⇒ boolean
- .getFilePath(key, options) ⇒ string
- .getMeta(key, options) ⇒ object | null
- .get(key, options) ⇒ object | null
- .mget(keys, options) ⇒ array
- .set(key, value, options)
- .find(comparator) ⇒ ModelFindReturns | null
- .findAll(comparator, options) ⇒ Array.<ModelFindReturns>
- .keysOf(comparator) ⇒ array
- .countOf(comparator) ⇒ number
- .mset(data, options)
- .del(key, options)
- .delAll()
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 |