DoboAdapter

DoboAdapter class serves as a base class for all database adapters in the Dobo framework. It provides common functionality for managing models, records, and database operations. Child classes should implement the abstract methods to provide specific database functionality.

Constructor

new DoboAdapter()

Constructor.

Extends

Members

idField :DoboAdapter.TIdField

ID field configuration

maxChunkSize :number

Maximum chunk size for bulk operations

Type:
  • number

memory :boolean

Indicates whether the adapter uses in-memory storage

Type:
  • boolean

name :string

Adapter name

Type:
  • string

options :object

Adapter options

Type:
  • object

support :DoboAdapter.TSupport

Support configuration for the adapter

useUtc :boolean

Indicates whether to use UTC for datetime fields

Type:
  • boolean

Methods

(async) _attachHook(name, model, …args)

Attaches hooks to the model for various operations. It runs the appropriate hooks before and after the specified operation, allowing for custom behavior to be injected into the model's lifecycle.

Parameters:
NameTypeAttributesDescription
namestring

The name of the hook

modelDoboModel

The model instance to which the hook is being attached

argsany<repeatable>

Additional arguments to be passed to the hook

(async) _buildModel(model, options) → {Promise.<object>}

Wrapper for the buildModel method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
optionsobject
Returns:
Type: 
Promise.<object>

(async) _bulkCreateRecord(model, bodies, options) → {Promise.<void>}

Wrapper for the DoboAdapter#bulkCreateRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
bodiesArray.<object>
optionsobject
Returns:
Type: 
Promise.<void>

(async) _checkUnique(model, body, options) → {Promise.<void>}

Checks the uniqueness of fields with a unique index.

Parameters:
NameTypeDescription
modelDoboModel

The model instance to check

bodyobject

The data to be checked for uniqueness

optionsobject

Additional options, including the action being performed

Returns:
  • Resolves if unique, throws an error if not
Type: 
Promise.<void>

(async) _clearRecord(model, options) → {Promise.<object>}

Wrapper for the DoboAdapter#clearRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
optionsobject
Returns:
Type: 
Promise.<object>

(async) _countRecord(model, filter, options) → {Promise.<object>}

Wrapper for the DoboAdapter#countRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
filterobject
optionsobject
Returns:
Type: 
Promise.<object>

(async) _createAggregate(model, filter, params, options) → {Promise.<object>}

Wrapper for the DoboAdapter#createAggregate method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
filterobject
paramsobject
optionsobject
Returns:
Type: 
Promise.<object>

(async) _createHistogram(model, filter, params, options) → {Promise.<object>}

Wrapper for the DoboAdapter#createHistogram method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
filterobject
paramsobject
optionsobject
Returns:
Type: 
Promise.<object>

(async) _createRecord(model, input, options) → {Promise.<object>}

Wrapper for the DoboAdapter#createRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
inputobject
optionsobject
Returns:
Type: 
Promise.<object>

(async) _dropModel(model, options) → {Promise.<object>}

Wrapper for the dropModel method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
optionsobject
Returns:
Type: 
Promise.<object>

(async) _findAllRecord(model, filter, options) → {Promise.<object>}

Finds all records for the given model based on the provided filter. This method will only run if child adapter does not implement DoboAdapter#findAllRecord.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the records are being found

filterobject

The filter criteria for finding records

optionsobject

Additional options that may affect record finding

Returns:
  • The result of the record finding
Type: 
Promise.<object>

(async) _findRecord(model, filter, options) → {Promise.<object>}

Wrapper for the DoboAdapter#findRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
filterobject
optionsobject
Returns:
Type: 
Promise.<object>

(async) _getRecord(model, id, options) → {Promise.<object>}

Wrapper for the DoboAdapter#getRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
idstring | number
optionsobject
Returns:
Type: 
Promise.<object>

_getReturningFields(model, options) → {Array.<string>}

Get returning fields for a model based on the provided options. If the adapter supports returning fields, it will return the specified fields or all model properties. It ensures that the ID field is always included in the returned fields.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which to get the returning fields

optionsobject

Options that may include the fields to return

Returns:
  • Array of field names to be returned
Type: 
Array.<string>

(async) _modelExists(model, options) → {Promise.<boolean>}

Wrapper for the modelExists method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
optionsobject
Returns:
Type: 
Promise.<boolean>

(async) _prepBodyForCreate(model, body, options) → {Promise.<object>}

Prepares the body of a record for creation by populating default values for properties that are not set. It handles various types of default values, including functions, special strings (like 'now', 'uuid', etc.), and static values.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the body is being prepared

bodyobject

The data to be prepared for creation

optionsobject

Additional options that may affect the preparation

Returns:
  • The prepared body with default values populated
Type: 
Promise.<object>

(async) _prepIdForCreate(model, body, options) → {Promise.<void>}

Prepares the ID for a record before creation. It generates an ID if it is not set in the body.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the ID is being prepared

bodyobject

The data containing the ID

optionsobject

Additional options that may affect ID generation

Returns:
  • Resolves when the ID has been prepared
Type: 
Promise.<void>

(async) _removeRecord(model, id, options) → {Promise.<object>}

Wrapper for the DoboAdapter#removeRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
idstring | number
optionsobject
Returns:
Type: 
Promise.<object>

(async) _updateRecord(model, id, input, options) → {Promise.<object>}

Wrapper for the DoboAdapter#updateRecord method, called internally by model to make sure all adapter hooks are executed accordingly, and all inputs and outputs are sanitized.

Parameters:
NameTypeDescription
modelDoboModel
idstring | number
inputobject
optionsobject
Returns:
Type: 
Promise.<object>

(async) _upsertRecord(model, input, options) → {Promise.<object>}

Upserts a record for the given model. This method will only run if child adapter does not implement DoboAdapter#upsertRecord.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being upserted

inputobject

The input data for the upsert

optionsobject

Additional options that may affect record upserting

Returns:
  • The result of the record upsert
Type: 
Promise.<object>

(async) buildModel(model, options)

Builds the model in the database.

Must be implemented by child classes to provide specific database functionality for model building, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being built

optionsobject

Additional options that may affect model building

(async) bulkCreateRecord(model, bodies, options) → {Promise.<void>}

Bulk creates records for the given model.

Must be implemented by child classes to provide specific database functionality for bulk record creation, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the records are being created

bodiesArray.<object>

The array of input data for the new records

optionsobject

Additional options that may affect record creation

Returns:
  • Resolves when the records have been created
Type: 
Promise.<void>

(async) clearRecord(model, options) → {Promise.<object>}

Clears all records for the given model.

Must be implemented by child classes to provide specific database functionality for record clearing, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the records are being cleared

optionsobject

Additional options that may affect record clearing

Returns:
  • The result of the record clearing
Type: 
Promise.<object>

(async) connect(connection, noRebuild) → {Promise.<void>}

Connects to the database using the provided connection parameters.

Parameters:
NameTypeDescription
connection*

The connection parameters for the database

noRebuild*

Flag indicating whether to skip rebuilding the database schema

Returns:
  • Resolves when the connection is established
Type: 
Promise.<void>

(async) countRecord(model, filter, options) → {Promise.<object>}

Counts the records for the given model based on the provided filter.

Must be implemented by child classes to provide specific database functionality for record counting, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the records are being counted

filterobject

The filter criteria for counting records

optionsobject

Additional options that may affect record counting

Returns:
  • The result of the record counting
Type: 
Promise.<object>

(async) createAggregate(model, filter, params, options) → {Promise.<object>}

Creates an aggregate for the given model based on the provided filter and parameters.

Must be implemented by child classes to provide specific database functionality for aggregate creation, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the aggregate is being created

filterobject

The filter criteria for creating the aggregate

paramsobject

The parameters for the aggregate creation

optionsobject

Additional options that may affect aggregate creation

Returns:
  • The result of the aggregate creation
Type: 
Promise.<object>

(async) createHistogram(model, filter, params, options) → {Promise.<object>}

Creates a histogram for the given model based on the provided filter and parameters.

Must be implemented by child classes to provide specific database functionality for histogram creation, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the histogram is being created

filterobject

The filter criteria for creating the histogram

paramsobject

The parameters for the histogram creation

optionsobject

Additional options that may affect histogram creation

Returns:
  • The result of the histogram creation
Type: 
Promise.<object>

(async) createRecord(model, input, options) → {Promise.<object>}

Creates a new record for the given model.

Must be implemented by child classes to provide specific database functionality for record creation, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being created

inputobject

The input data for the new record

optionsobject

Additional options that may affect record creation

Returns:
  • The result of the record creation
Type: 
Promise.<object>

(async) dispose() → {Promise.<void>}

Disposes of the adapter, performing any necessary cleanup operations.

Returns:
  • Resolves when the adapter has been disposed
Type: 
Promise.<void>

(async) dropModel(model, options)

Drops the model from the database.

Must be implemented by child classes to provide specific database functionality for dropping a model, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being dropped

optionsobject

Additional options that may affect record dropping

(async) findRecord(model, filter, options) → {Promise.<object>}

Finds records for the given model based on the provided filter.

Must be implemented by child classes to provide specific database functionality for record finding, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the records are being found

filterobject

The filter criteria for finding records

optionsobject

Additional options that may affect record finding

Returns:
  • The result of the record finding
Type: 
Promise.<object>

getRealFields(model) → {Array.<string>}

Utility method to get the real fields of a model, excluding virtual fields. This is useful for operations that require only the actual stored properties of a model.

Parameters:
NameTypeDescription
model*
Returns:
  • Array of real field names
Type: 
Array.<string>

(async) getRecord(model, id, options) → {Promise.<object>}

Retrieves a record for the given model by its ID.

Must be implemented by child classes to provide specific database functionality for record retrieval, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being retrieved

idstring | number

The ID of the record to retrieve

optionsobject

Additional options that may affect record retrieval

Returns:
  • The result of the record retrieval
Type: 
Promise.<object>

getVirtualFields(model) → {Array.<string>}

Utility method to get the virtual fields of a model. This is useful for operations that need to work with computed or derived properties.

Parameters:
NameTypeDescription
modelDoboModel

The model instance

Returns:
  • Array of virtual field names
Type: 
Array.<string>

(async) modelExists(model, options)

Checks if the model exists in the database.

Must be implemented by child classes to provide specific database functionality for model existence checking, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance to check for existence

optionsobject

Additional options that may affect the existence check

(async) removeRecord(model, id, options) → {Promise.<object>}

Removes a record for the given model by its ID.

Must be implemented by child classes to provide specific database functionality for record removal, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being removed

idstring | number

The ID of the record to remove

optionsobject

Additional options that may affect record removal

Returns:
  • The result of the record removal
Type: 
Promise.<object>

sanitizeBody(model, body, partialopt) → {object}

Sanitizes the body of a record before creating or updating it. It ensures that all required fields are present and have valid values, and converts data types as necessary.

Parameters:
NameTypeAttributesDefaultDescription
modelDoboModel

The model instance for which the body is being sanitized

bodyobject

The body of the record to be sanitized

partialboolean<optional>
false

Indicates whether to perform a partial update

Returns:
  • Sanitized body
Type: 
object

(async) sanitizeConnection(conn) → {Promise.<void>}

Sanitize connection object

Parameters:
NameTypeDescription
connObject

Connection object

Returns:
Type: 
Promise.<void>

sanitizeRecord(model, record) → {object}

Sanitizes a record retrieved from the database, converting data types as necessary and ensuring that the record conforms to the model's schema.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being sanitized

recordobject

The record retrieved from the database

Returns:
  • Sanitized record
Type: 
object

(async) transaction(model, handler, …args)

Executes a transaction for the given model.

Must be implemented by child classes to provide specific database functionality for transactions, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeAttributesDescription
modelDoboModel

The model instance for which the transaction is being executed

handlerfunction

The transaction handler function

argsany<repeatable>

Additional arguments for the transaction handler

(async) updateRecord(model, id, input, options) → {Promise.<object>}

Updates a record for the given model by its ID.

Must be implemented by child classes to provide specific database functionality for record updating, or throw an error if the operation is not supported by the adapter.

Parameters:
NameTypeDescription
modelDoboModel

The model instance for which the record is being updated

idstring | number

The ID of the record to update

inputobject

The input data for the update

optionsobject

Additional options that may affect record updating

Returns:
  • The result of the record update
Type: 
Promise.<object>

Type Definitions

TIdField

Type:
  • object
Properties
NameTypeAttributesDefaultDescription
namestring<optional>
'_id'

The name of the ID field.

typestring<optional>
'string'

The data type of the ID field.

maxLengthnumber<optional>
50

The maximum length of the ID field.

requiredboolean<optional>
true

Indicates if the ID field is required.

indexstring<optional>
'primary'

The index type of the ID field.

TSupport

Type:
  • object
Properties
NameTypeAttributesDefaultDescription
propTypeobject<optional>
{}

An object indicating support for various property types.

Properties
NameTypeAttributesDefaultDescription
objectboolean<optional>
false

Indicates if object property type is supported.

arrayboolean<optional>
false

Indicates if array property type is supported.

datetimeboolean<optional>
true

Indicates if datetime property type is supported.

searchboolean<optional>
false

Indicates if search functionality is supported.

uniqueIndexboolean<optional>
false

Indicates if unique index functionality is supported.

nullableFieldboolean<optional>
true

Indicates if nullable fields are supported.

transactionboolean<optional>
false

Indicates if transaction functionality is supported.