DoboModel

Model class defintion.

Constructor

new DoboModel()

Constructor

Members

adapter :string

Model's adapter name

Type:
  • string

Methods

(async, static) _simpleLookup(value, lookupValue, optionsopt) → {*|null|undefined}

Internal method to perform a simple lookup based on the provided value and lookupValue.

Parameters:
NameTypeAttributesDefaultDescription
valuestring | object | Array

The value to look up

lookupValueobject

The lookup values to replace in the query

optionsobject<optional>
{}

Additional options for the lookup

Returns:

Returns the result of the lookup or null if not found or undefined if the value is not a string, object, or array

Type: 
* | null | undefined

(static) action(name, …args) → {DoboAction}

Invoke a model's action. It will create a new instance of DoboAction and return it.

Parameters:
NameTypeAttributesDescription
namestring

The name of the action to invoke

argsany<repeatable>

Arguments to pass to the action

Returns:

Returns an instance of DoboAction

Type: 
DoboAction

(async, static) build(opts) → {DoboModel.TResultRecord|DoboModel.TRecord}

Build a model's definition based on its properties and rules.

Parameters:
NameTypeDescription
optsDoboModel.TOptions

The options for building the model

(async, static) buildFromDbModel(optionsopt) → {object|boolean}

Build JOI model from database model's properties and rules

Parameters:
NameTypeAttributesDefaultDescription
optionsobject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
fieldsArray<optional>
[]

If not empty, only these fields will be included in the JOI model

ruleobject<optional>
{}

Custom rules to override model's rules

extFieldsArray<optional>
[]

Additional fields to include in the JOI model

partialboolean<optional>
false

If true, only the fields present in the body will be validated

Returns:

Returns JOI model object or false if no properties to validate

Type: 
object | boolean

(async, static) buildPropValues(prop, opts) → {Array.<DoboModel.TPropertyValue>}

Builds the values for a given property if prop.values is set:

  • If a string, it will be treated as a handler and executed to get the values.
  • If a function, it will be executed to get the values.
  • If an array, it will be used as is.

Values are returned as an array of DoboModel.TPropertyValues. If a request object is provided in the options, the text will be translated using the request's translation function.

Parameters:
NameTypeDescription
propobject

The property definition

optsDoboModel.TOptions

Additional options for building the property values

Returns:

Returns an array of property values

Type: 
Array.<DoboModel.TPropertyValue>

(async, static) clearRecord(optsopt) → {DoboAction|DoboModel.TResultRecord|DoboModel.TRecord}

Clears all records from the model's underlying data store.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDefaultDescription
optsDoboModel.TOptions<optional>
{}

Options object.

(async, static) countRecord(filteropt, optsopt) → {DoboAction|DoboModel.TResultRecord|DoboModel.TRecord}

Counts the number of records in the model's underlying data store that match the provided filter criteria.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDefaultDescription
filterDoboModel.TFilter<optional>
{}

The filter criteria to apply when counting records.

optsDoboModel.TOptions<optional>
{}

Options object.

(async, static) createRecord(body, optsopt) → {DoboAction|DoboModel.TResultCreateRecord|DoboModel.TRecord}

Creates a new record in the model's underlying data store with the provided data.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDescription
bodyobject

The data for the new record to be created

optsDoboModel.TOptions<optional>

Options object

See
  • DoboModel.THookBeforeCreateRecord
  • DoboModel.THookAfterCreateRecord

(async, static) drop(optsopt) → {DoboAction|DoboModel.TResult|boolean}

Drops the model from the underlying data store.

Parameters:
NameTypeAttributesDescription
optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeDropModel
  • DoboModel.THookAfterDropModel
Returns:
Type: 
DoboAction | DoboModel.TResult | boolean

(async, static) findAllRecord(filter, optsopt) → {DoboAction|DoboModel.TResultFindRecord|Array.<DoboModel.TRecord>}

Finds all records from the model's underlying data store based on the provided filter.

Parameters:
NameTypeAttributesDescription
filterDoboModel.TFilter

The filter parameters for finding the records.

optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeFindAllRecord
  • DoboModel.THookAfterFindAllRecord

(async, static) findOneRecord(filter, optsopt) → {DoboAction|DoboModel.TResultFindRecord|Array.<DoboModel.TRecord>}

Finds a single record from the model's underlying data store based on the provided filter.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDescription
filterDoboModel.TFilter

The filter parameters for finding the record.

optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeFindRecord

(async, static) findRecord(filteropt, optsopt) → {DoboAction|DoboModel.TResultFindRecord|Array.<DoboModel.TRecord>}

Finds records in the model's underlying data store that match the provided filter criteria.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDefaultDescription
filterDoboModel.TFilter<optional>
{}

The filter criteria to apply when finding records.

optsDoboModel.TOptions<optional>
{}

Options object.

See
  • DoboModel.THookBeforeFindRecord
  • DoboModel.THookAfterFindRecord

(static) getIndexes() → {Array.<object>}

Returns the model's indexes.

Returns:

Returns an array of index objects defined in the model

Type: 
Array.<object>

(static) getNonVirtualProperties(namesOnlyopt) → {Array.<(object|string)>}

Get the model's non-virtual properties.

Parameters:
NameTypeAttributesDefaultDescription
namesOnlyboolean<optional>
false

If true, return only property names instead of full property objects

Returns:

Returns an array of non-virtual properties or property names based on the namesOnly option

Type: 
Array.<(object|string)>

(static) getProperties(optionsopt) → {Array.<(object|string)>}

Get the model's properties based on the given options.

Parameters:
NameTypeAttributesDefaultDescription
optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
noVirtualboolean<optional>
false

If true, exclude virtual properties

namesOnlyboolean<optional>
false

If true, return only property names instead of full property objects

Returns:

Returns an array of properties or property names based on the options

Type: 
Array.<(object|string)>

(static) getProperty(name) → {Object|null}

Get a property by its name.

Parameters:
NameTypeDescription
namestring

The name of the property to retrieve

Returns:

Returns the property object if found, otherwise undefined

Type: 
Object | null

(async, static) getRecord(id, optsopt) → {DoboAction|DoboModel.TResultGetRecord|DoboModel.TRecord}

Gets a record from the model's underlying data store based on the provided ID.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDescription
idstring | number

The ID of the record to be retrieved.

optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeGetRecord
  • DoboModel.THookAfterGetRecord

(static) getVirtualProperties(namesOnlyopt) → {Array.<(object|string)>}

Get the model's virtual properties.

Parameters:
NameTypeAttributesDefaultDescription
namesOnlyboolean<optional>
false

If true, return only property names instead of full property objects

Returns:

Returns an array of virtual properties or property names based on the namesOnly option

Type: 
Array.<(object|string)>

(static) hasProperty(name) → {boolean}

Checks if the model has a property with the given name.

Parameters:
NameTypeDescription
namestring

The name of the property to check

Returns:

Returns true if the property exists, otherwise false

Type: 
boolean

(async, static) isExists(optsopt) → {DoboAction|DoboModel.TResult|boolean}

Checks if the model exists in the underlying data store.

Parameters:
NameTypeAttributesDescription
optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeModelExists
  • DoboModel.THookAfterModelExists
Returns:
Type: 
DoboAction | DoboModel.TResult | boolean

(async, static) removeRecord(id, optsopt) → {DoboAction|DoboModel.TResultRemoveRecord|DoboModel.TRecord}

Removes a record from the model's underlying data store based on the provided ID.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDefaultDescription
idstring | number

The ID of the record to be removed.

optsDoboModel.TOptions<optional>
{}

Options object.

See
  • DoboModel.THookBeforeRemoveRecord
  • DoboModel.THookAfterRemoveRecord

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

Sanitize the payload body based on the model's properties and rules.

Parameters:
NameTypeAttributesDefaultDescription
optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
bodyObject<optional>
{}

Body object to sanitize

partialboolean<optional>
false

If true, only the fields present in the body will be sanitized

strictboolean<optional>
false

If true, only the fields defined in the model's properties will be included in the sanitized body

extFieldsArray<optional>
[]

Additional fields to include in the sanitized body

truncateStringboolean<optional>
false

If true, string fields will be truncated to their maximum length defined in the model's properties

actionstring<optional>

The action being performed (e.g., 'createRecord', 'updateRecord', etc.)

allPropsboolean<optional>
false

If true, all properties including virtual ones will be considered for sanitization

Returns:

Returns sanitized body object

Type: 
Object

(async, static) sanitizeFixture(paramsopt, optsopt) → {Object}

Sanitize a fixture object by resolving lookup values and ensuring proper data types.

Parameters:
NameTypeAttributesDefaultDescription
paramsObject<optional>
{}

Parameters object

Properties
NameTypeAttributesDefaultDescription
bodyObject<optional>
{}

Body object to sanitize

lookupValueObject<optional>
{}

Lookup values for resolving references in the body

noLookupboolean<optional>
false

If true, skip lookup resolution

optsDoboModel.TOptions<optional>
{}

Options object for additional configurations

Returns:

Returns sanitized body object

Type: 
Object

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

Sanitize the given ID based on the model's properties.

Parameters:
NameTypeDescription
idstring | number

The ID to sanitize

Returns:

Returns the sanitized ID

Type: 
string | number

(async, static) sanitizeRecord(recordopt, optsopt) → {Object}

Sanitize a record object based on the model's properties and rules.

Parameters:
NameTypeAttributesDefaultDescription
recordObject<optional>

Record object

optsDoboModel.TOptions<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
fieldsArray<optional>

If not empty, only these fields will be included in the sanitized record

hiddenArray.<string><optional>
[]

List of fields to hide from the sanitized record

forceNoHiddenboolean<optional>
false

If true, force ALL fields to be picked, thus ignoring hidden fields. If an array, force all fields except those in the array to be picked.

fmtboolean<optional>
false

If true, add a _fmt property to the sanitized record with formatted values automatically based on the model's properties and rules

reqObject<optional>

Request object, used for formatting values based on request context

Returns:

Returns sanitized record object

Type: 
Object

(async, static) sanitizeRef(obj, fatalopt)

Sanitize a reference object based on the model's properties and rules.

Parameters:
NameTypeAttributesDefaultDescription
obj*

The reference object to sanitize

fatalboolean<optional>
true

Whether to throw an error if sanitization fails

(async, static) updateRecord(id, body, optsopt) → {DoboAction|DoboModel.TResultUpdateRecord|DoboModel.TRecord}

Updates an existing record in the model's underlying data store based on the provided ID and data.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDescription
idstring | number

The ID of the record to be updated.

bodyobject

The data to update the record with.

optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeUpdateRecord
  • DoboModel.THookAfterUpdateRecord

(async, static) upsertRecord(body, optsopt) → {DoboAction|DoboModel.TResultCreateRecord|DoboModel.TResultUpdateRecord|DoboModel.TRecord}

Upserts a record in the model's underlying data store. If a record with the specified ID exists, it updates that record; otherwise, it creates a new record.

If no arguments are provided, it automatically turns into a chainable DoboAction object.

Parameters:
NameTypeAttributesDescription
bodyobject

The data for the record to be upserted.

optsDoboModel.TOptions<optional>

Options object.

See
  • DoboModel.THookBeforeUpsertRecord
  • DoboModel.THookAfterUpsertRecord

(async, static) validate(body, joiModel, optionsopt) → {Object}

Validate body object against JOI model

Parameters:
NameTypeAttributesDefaultDescription
bodyObject

Body object to validate

joiModelObject

JOI model

optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
extFieldsArray<optional>
[]
partialboolean<optional>
false

If true, only the fields present in the body will be validated

paramsObject<optional>
{}

Validation parameters. See config and JOI validate's options

Returns:
Type: 
Object

Type Definitions

TFilter

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
querystring | Object<optional>
{}

Query definition. See query-language for more

limitnumber

Maximum number of records to return

pagenumber

Which page to return

skipnumber

Records to skip

sortDoboModel.TSort

Sort order info

See
  • Dobo#recordFind
  • Dobo#recordFindOne
  • Dobo#recordFindAll

TOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
noHookboolean<optional>
false

If true, no model's hook will be executed

noModelHookboolean<optional>
false

If true, no model's hook will be executed

noDynHookboolean<optional>
false

If true, no dynamic hook will be executed

noValidationboolean<optional>
false

If true, no validation of data payload performed

noCheckUniqueboolean<optional>
false

If true, no unique validation for ID performed

noBodySanitizerboolean<optional>
false

If true, accept data payload as is without sanitization

noRecordSanitizerboolean<optional>
false

If true, accept result payload as is without sanitization

noResultboolean<optional>
false

If true, returns nothing

truncateStringboolean<optional>
true

If true (default), string is truncated to its model's maxLength

partialboolean<optional>

If true, only updated values are saved. Otherwise replace all existing values with given payload. Defaults to true for updateRecord

dataOnlyboolean<optional>
true

If true (default) returns only record's object. Otherwise DoboModel.TResult

TPagination

Type:
  • Object
Properties
NameTypeAttributesDescription
limitnumber

Number of records per page

pagenumber

Page number

skipnumber<optional>

Records to skip. If not provided, it will be calculated based on limit and page

TPropertyType

Type:
  • Object
Properties
NameTypeDefaultDescription
integerObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
number
smallintObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
number
textObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
string
textTypestring<optional>
string
valuesArray.<string><optional>
['text', 'mediumtext', 'longtext']
stringObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
string
maxLengthmaxLength<optional>
255
minLengthminLength<optional>
0
floatObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
number
doubleObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
number
booleanObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
boolean
datetimeObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
date
dateObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
date
timeObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
date
timestampObject
Properties
NameTypeAttributesDefaultDescription
validatorstring<optional>
timestamp
objectObject{}
arrayObject{}

TPropertyValue

Property value type definition. Used for building property values in a model which is an array of this object type.

Type:
  • Object
Properties
NameTypeDescription
valuestring

The actual value of the property

textstring

The display text for the property value, which can be translated based on the request context

TRecord

The record returned from a model's action. It is an object where:

  • Key represents the property name
  • Value represents its value according to the property's data type

This record is then merged with DoboModel.TRecordFormatted and DoboModel.TRecordRef to form the final record returned from a model's action.

Example
{
  id: 1,
  item: "Laptop",
  createdAt: "2024-01-01T00:00:00.000Z",
  isActive: true,
  price: 2500.00,
  _fmt: {
    id: "1",
    item: "Laptop",
    createdAt: "Jan 1, 2024, 12:00 AM",
    isActive: "True",
    price: "2,500.00"
  },
  _ref: {
    user: {
      id: 1,
      name: "John Doe"
    }
  }
}

TRecordFormatted

Formatted version of a record's property values. It is an object where:

  • Key represents the property name
  • Value represents its formatted value according to the property's data type or formatting function. Data type is ALWAYS in string.

This record is then wrapped in a _fmt property.

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
_fmtObject<optional>
{}

Wrapper

Example
{
  _fmt: {
    id: "1",
    item: "Laptop",
    createdAt: "Jan 1, 2024, 12:00 AM",
    isActive: "True",
    price: "2,500.00"
  }
}

TRecordRef

Reference record related to a record's result. It is an object where:

  • Key represents the name of the referenced record. Normally, it is the referenced model's name in camelCase format.
  • Value represents its reference record. It can be an object or an array of objects, depending on the relationship type.

This record is then wrapped in a _ref property in the result object.

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
_refObject<optional>
{}

Wrapper

Properties
NameTypeAttributesDescription
.Object | Array<optional>

Reference record

Example
{
  _ref: {
    user: {
      id: 1,
      name: "John Doe"
    },
    posts: [
      {
        id: 1,
        title: "Post Title"
      },
      {
        id: 2,
        title: "Another Post Title"
      }
    ]
  }
}

TResultCreateRecord

The result returned from a model's createRecord action if dataOnly is set to false.

Type:
  • Object
Properties
NameTypeDescription
dataDoboModel.TRecord

The data returned from the action

TResultFindRecord

The result returned from a model's find* actions if dataOnly is set to false.

Type:
  • Object
Properties
NameTypeAttributesDescription
dataArray.<DoboModel.TRecord>

The data returned from the action

countnumber<optional>

The total number of records found, if applicable

cachedboolean<optional>

Indicates if the result was retrieved from cache, if applicable. Only available if bajoCache is used

warningsArray<optional>

Any warnings generated during the action, if applicable

TResultGetRecord

The result returned from a model's getRecord action if dataOnly is set to false.

Type:
  • Object
Properties
NameTypeDescription
dataDoboModel.TRecord

The data returned from the action

TResultRecord

The result returned from a model's action other than find*, {create|update|remove}Record if dataOnly is set to false.

Type:
  • Object
Properties
NameTypeDescription
dataArray.<DoboModel.TRecord>

The data returned from the action

TResultRemoveRecord

The result returned from a model's removeRecord action if dataOnly is set to false.

Type:
  • Object
Properties
NameTypeDescription
oldDataDoboModel.TRecord

The removed data returned from the action

TResultUpdateRecord

The result returned from a model's updateRecord action if dataOnly is set to false.

Type:
  • Object
Properties
NameTypeDescription
dataDoboModel.TRecord

The updated data returned from the action

oldDataDoboModel.TRecord

The previous data before the update

TSort

Key value pairs used as sort order in pagination and sorting of records. It is an object where:

  • Key represents model's field name
  • Value represents its sort order
Type:
Example
// to sort by firstName (ascending) and lastName (descending)
const sort = {
  firstName: 1,
  lastName: -1
}

TSortOrder

Sort order key used in pagination and sorting of records. It can be either:

  • 1 for ascending order (the default, if none is provided) or
  • -1 for descending order.
Type:
  • string

TValidator

All available validators to check against model's properties and rules

Type:
  • Object
Properties
NameTypeDescription
stringTValidatorString
numberTValidatorNumber
booleanTValidatorBoolean
dateTValidatorDate
timestampTValidatorTimestamp
arrayTValidatorArray

TValidatorArray

List of available array validators to check against model's properties and rules

Type:
  • Array.<string>
Properties
NameTypeDefaultDescription
0stringlength
1stringmax
2stringmin

TValidatorBoolean

List of available boolean validators to check against model's properties and rules

Type:
  • Array.<string>
Properties
NameTypeDefaultDescription
0stringfalsy
1stringsensitive
2stringtruthy

TValidatorDate

List of available date validators to check against model's properties and rules

Type:
  • Array.<string>
Properties
NameTypeDefaultDescription
0stringgreater
1stringiso
2stringless
3stringmax
4stringmin

TValidatorNumber

List of available number validators to check against model's properties and rules

Type:
  • Array.<string>
Properties
NameTypeDefaultDescription
0stringgreat
1stringless
2stringmax
3stringmin
4stringmultiple
5stringnegative
6stringport
7stringpositive
8stringsign
9stringunsafe

TValidatorString

List of available string validators to check against model's properties and rules

Type:
  • Array.<string>
Properties
NameTypeDefaultDescription
0stringalphanum
1stringbase64
2stringcase
3stringcreditCard
4stringdataUri
5stringemail
6stringguid
7stringuuid
8stringhex
9stringhostname
10stringinsenstive
11stringip
12stringisoDate
13stringisoDuration
14stringlength
15stringlowercase
16stringmax
17stringmin
18stringnormalize
19stringpattern
20stringregex
21stringreplace
22stringtoken
23stringtrim
24stringtruncate
25stringupercase
26stringuri

TValidatorTimestamp

List of available timestamp validators to check against model's properties and rules

Type:
  • Array.<string>
Properties
NameTypeDefaultDescription
0stringtimestamp