Model

Base Model class which should be used as an extension for database entities.

Static members

.belongsTo(Target, [foreignKey])

Creates a many-to-one relation between the current Model and a target.

belongsTo(Target, [foreignKey])

Creates a many-to-one relation between the current Model and a target.

Parameters

  • ( string | Model ) Target :

    Name or static reference to the joinable table's Model.

  • string = foreignKey :

    Foreign key in this Model.

Returns

Relation
.blacklistedProps

List of properties which shall not be present in database entities. The blacklist takes precedence over any whitelist rule.

blacklistedProps

List of properties which shall not be present in database entities. The blacklist takes precedence over any whitelist rule.

.hasMany(Target, [foreignKey])

Creates a one-to-many relation between the current Model and a target.

hasMany(Target, [foreignKey])

Creates a one-to-many relation between the current Model and a target.

Parameters

  • ( string | Model ) Target :

    Name or static reference to the joinable table's Model.

  • string = foreignKey :

    Foreign key in the target Model.

Returns

Relation
.hasOne(Target, [foreignKey])

Creates a one-to-one relation between the current Model and a target.

hasOne(Target, [foreignKey])

Creates a one-to-one relation between the current Model and a target.

Parameters

  • ( string | Model ) Target :

    Name or static reference to the joinable table's Model.

  • string = foreignKey :

    Foreign key in the target Model.

Returns

Relation
.idAttribute

idAttribute

.jsonSchema

JSON Schema to be used for validating instances of the Model. Validation happens automatically before executing queries.

jsonSchema

JSON Schema to be used for validating instances of the Model. Validation happens automatically before executing queries.

.primaryKey

Primary key of the Model, used for instance identification.

primaryKey

Primary key of the Model, used for instance identification.

.query

Returns a new QueryBuilder instance which corresponds to the current Model.

query

Returns a new QueryBuilder instance which corresponds to the current Model.

Returns

QueryBuilder
.tableName

Case-sensitive name of the database table which corresponds to the Model.

tableName

Case-sensitive name of the database table which corresponds to the Model.

.whitelistedProps

List of properties which should exclusively be present in database entities. If the list is empty, then every enumerable property of the instance are considered to be database entities.

whitelistedProps

List of properties which should exclusively be present in database entities. If the list is empty, then every enumerable property of the instance are considered to be database entities.

Instance members

#constructor([props], [isNew])

Creates a new Model instance.

constructor([props], [isNew])

Creates a new Model instance.

Parameters

  • Object = props (default {}) :

    Initial properties of the instance.

  • boolean = isNew (default true) :

    True if the instance is not yet stored persistently in the database.

#del

Queues the deletion of the current Model from the database.

del

Queues the deletion of the current Model from the database.

Returns

QueryBuilder

Throws

  • InexistentDbObjectError :
#fetchRelated(props)

Queues fetching the given related Models of the current instance.

fetchRelated(props)

Queues fetching the given related Models of the current instance.

Parameters

  • ... string props :

    Relation attributes to be fetched.

Returns

QueryBuilder
#save

Queues saving (creating or updating) the current Model in the database.

save

Queues saving (creating or updating) the current Model in the database.

Returns

QueryBuilder

Throws

  • EmptyDbObjectError :
#validate

Validates all the enumerable properties of the current instance.

validate

Validates all the enumerable properties of the current instance.

Throws

  • ValidationError :

KnexOrm

Entry class for accessing the functionality of Knex-ORM.

Properties

DbObjectAlreadyRegisteredError

Extends Error

An error which gets thrown when an attempt is made to register a database object multiple times.

Properties

  • string name :

    Name of the database object in question.

EmptyDbObjectError

Extends Error

An error which gets thrown when an attempt is made to store an empty database object.

InexistentDbObjectError

Extends Error

An error which gets thrown when an attempt is made to modify an inexistent database object.

QueryBuilder

Represents a query builder which corresponds to a static Model reference. Inherits every query method of the Knex query builder.

Instance members

#then([onFulfilled], [onRejected])

Executes the query.

then([onFulfilled], [onRejected])

Executes the query.

Parameters

Returns

Promise .< Object >
#toString([separator])

Gets the list of raw queries to be executed, joined by a string separator.

toString([separator])

Gets the list of raw queries to be executed, joined by a string separator.

Parameters

  • string = separator (default \n) :

    Separator string to be used for joining multiple raw query strings.

Returns

string
#withRelated(props)

Queues fetching the given related Models of the queryable instance(s).

withRelated(props)

Queues fetching the given related Models of the queryable instance(s).

Parameters

  • ... string props :

    Relation attributes to be fetched.

Returns

QueryBuilder

RelationError

Extends Error

An error which gets thrown when a Relation does not behave as expected.

ValidationError

Extends Error

An error which gets thrown when a Model cannot be successfully validated against its JSON Schema.

Properties

  • ? Object data :

    Detailed information about why the validation has failed.