0.2.6 • Published 1 year ago

@itrunc/jsondb v0.2.6

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Classes

Typedefs

Model

Create / Get a model

Kind: global class

new Model(folder, options)

Create a new model instance

ParamTypeDescription
folderStringPath to save data of the model
optionsModelConstructorOptions

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

ParamType
keystring
optionsModelHasOptions

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

ParamType
keystring
optionsModelHasOptions

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

ParamType
keystring
optionsModelHasOptions

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

ParamTypeDescription
keystringID of an object
optionsModelGetOptions

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

ParamTypeDescription
keysarrayID list
optionsModelGetOptions

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

ParamTypeDescription
keystringID of an object
valueobjectData to be saved in the JSON file
optionsModelSetOptions

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

ParamType
comparatorfunction

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

ParamType
comparatorfunction
optionsPaginateOptions

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

ParamType
comparatorfunction

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

ParamType
comparatorfunction

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

ParamType
dataarray | object
optionsModelSetOptions

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

ParamTypeDescription
keystringID of an object
optionsModelDelOptions

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

ParamTypeDescription
folderstringpath 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

ParamTypeDescription
namestringfolder name of the sub model
optionsModelConstructorOptions

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

ParamTypeDescription
namestringfolder name of the sub schema

Example

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

ModelConstructorOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
rulesstring"{}"Rules for the validators, refer to https://www.npmjs.com/package/async-validator
indexesstring"[]"Name of the fields to save value in meta for searching

ModelHasOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
eventbooleanfalseIndicates whether 'missed' event is triggered if not found

ModelGetOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
eventbooleanfalseIndicates whether event is triggered if not found
includeAllFieldsbooleanfalseIndicates whether the hidden fields are included in return

ModelDelOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
eventbooleanfalseIndicates whether event is triggered if not found
realbooleantrueIndicates whether the JSON file will be really removed, if false, JSON file won't be delete but just delete key in meta
saveMetabooleantrueIndicates whether meta file will be updated immediate

ModelSetOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
eventbooleanfalseIndicates whether event is triggered if not found
saveMetabooleantrueIndicates whether meta file will be updated immediate
whostring | undefinedFor createdBy and updatedBy
indexesobject{}Additional indexes to save in meta when saving the item

ModelFindReturns : Object

Kind: global typedef
Properties

NameTypeDescription
keystringID of the object
dataobjectThe data saved in JSON file
indexobjectThe data saved in meta

PaginateOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
offsetint0The first {offset} matched items are ignored
limitint0Page size, if it is 0 then no limit
0.2.6

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.15

3 years ago

0.1.16

3 years ago

0.1.19

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.2

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.9

3 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago