Dobo

Dobo Database Framework for Bajo.

See ecosystem for available drivers & tools

Constructor

new Dobo()

Members

connections :Array.<Object>

Type:
  • Array.<Object>

drivers :Array.<Object>

Type:
  • Array.<Object>

features :Array.<Object>

Type:
  • Array.<Object>

schemas :Array.<Object>

Type:
  • Array.<Object>

(static, constant) aggregateTypes :Array.<string>

Type:
  • Array.<string>
Default Value
  • ['count', 'avg', 'min', 'max', 'sum']

(static, constant) alias :string

Type:
  • string
Default Value
  • 'db'

(static, constant) propType :TPropType

Methods

(async) init()

(async) modelCreate(name, optionsopt)

Create a new model:

  • read corresponding schema
  • attempt to create table/database/collection accordingly
Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

optionsObject<optional>
{}

Options object

(async) modelDrop(name, optionsopt)

Drop database model

Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

optionsObject<optional>
{}

Options object

(async) modelExists(name, thrownopt, optionsopt) → {boolean}

Check if model exists already

Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

thrownboolean<optional>
false

If true throw error if not exists instead of just silent

optionsObject<optional>
{}

Options object

Returns:
Type: 
boolean

(async) pickRecord(optionsopt) → {Object}

Pick only fields defined from a record

Parameters:
NameTypeAttributesDefaultDescription
optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
recordObject

Record to pick fields from

fieldsArray

Array of field names to be picked

schemaObject

Associated record's schema

hiddenObject<optional>
[]

Additional fields to be hidden in addition the one defined in schema

forceNoHiddenboolean<optional>

Force ALL fields to be picked, thus ignoring hidden fields

Returns:
Type: 
Object

(async) prepPagination(filteropt, schema, options) → {TRecordPagination}

Prepare records pagination:

  • making sure records limit is obeyed
  • making sure page is a positive value
  • if skip is given, recalculate limit to use skip instead of page number
  • Build sort info
Parameters:
NameTypeAttributesDefaultDescription
filterObject<optional>
{}

Filter object

schemaObject

Model's schema

optionsObject

Options

Returns:
Type: 
TRecordPagination

(async) recordCount(name, filteropt, optionsopt) → {TRecordCountResult|number}

Return the number of records found by given filter

Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

filterTRecordFilter<optional>
{}

Filter object

optionsTRecordCountOptions<optional>
{}
Returns:

Return number of records if options.dataOnly is set. TRecordCountResult otherwise

Type: 
TRecordCountResult | number

(async) recordCreate(name, body, optionsopt) → {TRecordCreateResult|Object}

Create a new record

Example:

const { recordCreate } = this.app.dobo
const { body } = {
  id: 'ID',
  name: 'Indonesia',
  iso3: 'IDN'
}
const result = await recordCreate('CdbCountry', body)
Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

bodyObject

Data to be saved

optionsTRecordCreateOptions<optional>
{}
Returns:

Returns newly created record if options.dataOnly is set. TRecordCreateResult otherwise

Type: 
TRecordCreateResult | Object

(async) recordFind(name, filteropt, optionsopt) → {TRecordFindResult|Array.<Object>}

Find records by model's name and given filter

Example: find records from model CdbCountry where its id is 'ID' or 'MY', sorted by name in ascending order and return only its id, name and iso3

const { recordFind } = this.app.dobo
const query = { id: { $in: ['ID', 'MY'] } }
const sort = { name: 1 }
const fields = ['id', 'name', 'iso3']
const result = await recordFind('CdbCountry', { query, sort }, { fields })
Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

filterObject<optional>
{}

Filter object

optionsTRecordFindOptions<optional>
{}
Returns:

Return array of records if options.dataOnly is set. TRecordFindResult otherwise

Type: 
TRecordFindResult | Array.<Object>

(async) recordFindAll(name, filteropt, optionsopt) → {TRecordFindResult|Array.<Object>}

Find all records by model's name and given filter.

The total number of records returned is limited by hardLimit value set in config file.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

filterObject<optional>
{}

Filter object

optionsTRecordFindOptions<optional>
{}
Returns:

Return array of records if options.dataOnly is set. TRecordFindResult otherwise

Type: 
TRecordFindResult | Array.<Object>

(async) recordFindOne(name, filteropt, optionsopt) → {TRecordGetResult|Object}

Find the first record by model's name and given filter.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

filterObject<optional>
{}

Filter object

optionsTRecordFindOptions<optional>
{}
Returns:

Return record's object if options.dataOnly is set. TRecordGetResult otherwise

Type: 
TRecordGetResult | Object

(async) recordGet(name, optionsopt) → {TRecordGetResult|Object}

Get record by model's name and record ID

Example:

const { recordGet } = this.app.dobo
const fields = ['id', 'name', 'iso3']
const result = await recordGet('CdbCountry', 'ID', { fields })
Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

string | number

Record's ID

optionsTRecordGetOptions<optional>
{}
Returns:

Return record's object if options.dataOnly is set. TRecordGetResult otherwise

Type: 
TRecordGetResult | Object

(async) recordRemove(name, id, optionsopt) → {TRecordRemoveResult|Object}

Remove existing record by it's ID. All attachments bound to this record will also be removed forever.

Example:

const { recordRemove } = this.app.dobo
const result = await recordRemove('CdbCountry', 'ID')
Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

idstring | number

Record's ID

optionsTRecordRemoveOptions<optional>
{}
Returns:

Return the removed record if options.dataOnly is set. TRecordRemoveResult otherwise

Type: 
TRecordRemoveResult | Object

(async) recordUpdate(name, id, body, optionsopt) → {TRecordUpdateResult|Object}

Update a record by it's ID and body payload

Example:

const { recordUpdate } = this.app.dobo
const { body } = {
  name: 'Republic of Indonesia',
  phoneCode: '+62'
}
const result = await recordUpdate('CdbCountry', 'ID', body)
Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

idstring | number

Record's ID

bodyObject

Body payload

optionsTRecordUpdateOptions<optional>
{}
Returns:

Returns updated record if options.dataOnly is set. TRecordUpdateResult otherwise

Type: 
TRecordUpdateResult | Object

(async) recordUpsert(name, body, optionsopt) → {TRecordUpdateResult|TRecordCreateResult|Object}

Update a record by payload's ID. If no record is found by given ID, a new one will be created instead.

Missing ID in payload always results a new record creation.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Model's name

bodyObject

Body payload

optionsTRecordUpsertOptions<optional>
{}
Returns:

Returns updated/newly created record if options.dataOnly is set. TRecordUpdateResult or TRecordCreateResult otherwise

Type: 
TRecordUpdateResult | TRecordCreateResult | Object

(async) start(connsopt, noRebuildopt)

Start plugin

Parameters:
NameTypeAttributesDefaultDescription
connsstring | Array<optional>
all

Which connections should be run on start

noRebuildboolean<optional>
false

Set true to ALWAYS rebuild model on start. Yes, only set it to true if you REALLY know what you're doing!!!

(async, static) sanitizeBody(optionsopt) → {Object}

Sanitize payload body against schema

Parameters:
NameTypeAttributesDefaultDescription
optionsObject<optional>
{}
Properties
NameTypeAttributesDefaultDescription
bodyObject<optional>
{}
schemaObject<optional>
{}
partialboolean<optional>
false
strictboolean<optional>
false
extFieldsArray<optional>
[]
Returns:
Type: 
Object

(static) sanitizeDate(value, optionsopt) → {string|Date}

Sanitize value as a date/time value. Parse/format string using dayjs format

Parameters:
NameTypeAttributesDefaultDescription
valuenumber | string

Value to sanitize

optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
silentboolean<optional>
true

If true (default) and value isn't valid, returns empty

inputFormatstring<optional>

If provided, parse value using this option

outputFormatstring<optional>

If not provided or native, returns Javascript Date. Otherwise returns formatted date/time string

Returns:
Type: 
string | Date

(static) sanitizeId(id, schema) → {number|string}

Sanitize id according it's schema

Parameters:
NameTypeDescription
idnumber | string
schemaObject
Returns:
Type: 
number | string

(async, static) validate(value, joiSchema, optionsopt) → {Object}

Validate value against JOI schema

Parameters:
NameTypeAttributesDefaultDescription
valueObject

value to validate

joiSchemaObject

JOI schema

optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
nsstring<optional>
dobo

Scope's namespace

fieldsArray<optional>
[]
extFieldsArray<optional>
[]
paramsObject<optional>
{}

Validation parameters. See config and JOI validate's options

Returns:
Type: 
Object