Model

Kind: global class

model.methods

Kind: instance property of Model

model.table

The associated table name

Kind: instance property of Model

model.name

The model name

Kind: instance property of Model

model.setStatic

Deprecated

Set a new Model method

Kind: instance property of Model

Param Type Description
name string Name of the method
fn function The method

model.setMethod

Deprecated

Set a new Model Instance method

Kind: instance property of Model

Param Type Description
name string Name of the method
fn function The method

model.build(data, [options]) ⇒ ModelInstance

Build a Model instance

Kind: instance method of Model

Param Type Default Description
data Object The data to use to build the instance
[options] Object
[options.new] boolean true Whether the instance is new or already existing

model.save(data, [options]) ⇒ Promise

Save new items into the associated model table

Kind: instance method of Model
Resolve: Array.<ModelInstance>\|ModelInstance

Param Type Default Description
data Object | Array.<Object> The data to insert
[options] Object
[options.multiple] boolean false Run multiple insertions using a transaction (or the given connection)
[options.using] PoolConnection Use this connection

model.update(data, where, [options]) ⇒ Query

Update row(s) with new data

Kind: instance method of Model

Param Type Description
data Object The data that change
where Object Query expression for the WHERE condition. If not set or null, it updates each row.
[options] Object
[options.using] PoolConnection Use this connection

model.upsert(data, [options]) ⇒ Promise

Insert data or update the old row if a duplicate key conflict occurs in a UNIQUE index or PRIMARY KEY

Kind: instance method of Model
Resolve: Object

Param Type Description
data Object The data to be inserted or updated
[options] Object
[options.using] PoolConnection Use this connection

model.remove(where, [options]) ⇒ Query

Remove row(s) from the associated table

Kind: instance method of Model

Param Type Description
where Object Query expression for the WHERE condition. If not set or null, it removes all rows in the associated table
[options] Object
[options.using] PoolConnection Use this connection

model.at(where, [options]) ⇒ Query

Retrieve a single Model instance

Kind: instance method of Model

Param Type Description
where Object Query expression to find the row. If not set or null, it returns the first found row.
[options] Object
[options.attributes] Array.<string> The attributes to retrieve
[options.$] string The scope to use
[options.using] PoolConnection Use this connection

model.all(where, [options]) ⇒ Query

Retrieve a multiple Model instances

Kind: instance method of Model

Param Type Description
where Object Query expression to find the rows. If not set or null, it retrieves all rows.
[options] Object
[options.attributes] Array.<string> The attributes to retrieve
[options.$] string The scope to use
[options.limit] Number Limit the results
[options.using] PoolConnection Use this connection

model.sum(column, where, [options]) ⇒ Query

Get the sum of the numeric values of the column

Kind: instance method of Model

Param Type Description
column string The numeric column
where Object Query expression to filter rows. If not set or null, it gets the sum of all rows.
[options] Object
[options.using] PoolConnection Use this connection

model.min(column, where, [options]) ⇒ Query

Get the minimum numeric values of the column

Kind: instance method of Model

Param Type Description
column string The numeric column
where Object Query expression to filter rows. If not set or null, it gets the minimum of all rows.
[options] Object
[options.using] PoolConnection Use this connection

model.max(column, where, [options]) ⇒ Query

Get the maximum numeric values of the column

Kind: instance method of Model

Param Type Description
column string The numeric column
where Object Query expression to filter rows. If not set or null, it gets the maximum of all rows.
[options] Object
[options.using] PoolConnection Use this connection

model.count(where, [options]) ⇒ Query

Count the number of rows

Kind: instance method of Model

Param Type Description
where Object Query expression to filter rows. If not set or null, it counts all rows.
[options] Object
[options.using] PoolConnection Use this connection

model.index(fields, [options])

Add a new index on some fields

Kind: instance method of Model

Param Type Description
fields Array.<string> Name of the fields
[options] Object
[options.name] string name of the index, by default it is set to the concatenated fields with a '_'
[options.using] string method for the index ('hash', 'btree')
[options.unique] boolean unique index
[options.fulltext] boolean fulltext index
[options.spacial] boolean spacial index

model.query(query, [options]) ⇒ Promise

Send a SQL query

Kind: instance method of Model
Resolve: Array.<Object> - Results

Param Type Description
query Array.<*> | Object | string The query
query.sql string A query with parameters defined as ?
query.values Array.<*> An array of values to pass
[options] Object
[options.using] PoolConnection Use this connection

Example

User.query({ sql: 'SELECT ?? FROM ?? WHERE ?? = ?', values: [ 'id', User.table, 'id', 42 ] });
// SELECT `id` FROM `user` WHERE `id` = 42

User.query({ sql: 'INSERT INTO ?? SET ?', values: [ User.table, { name: 'Heisenberg', age: 42 } ] });
// INSERT INTO `user` SET `name` = 'Heisenberg', `age` = 42

User.query('SELECT id FROM `user` WHERE `id` = 42');
// SELECT `id` FROM `user` WHERE `id` = 42

User.query([ 'SELECT ?? FROM ?? WHERE ?? = ?', 'id', User.table, 'id', 42 ]);
// SELECT `id` FROM `user` WHERE `id` = 42

model.sync([options]) ⇒ Promise

Synchronize the Model with the database

Kind: instance method of Model

Param Type Default Description
[options] Object
[options.dropTable] boolean false Drop the table
[options.createTable] boolean true Create the table
[options.autoMigrate.addColumns] boolean true Add missing table columns
[options.autoMigrate.delColumns] boolean false Remove missing table columns
[options.using] PoolConnection Use this connection

model.createTable([options]) ⇒ Promise

Create the associated table if not exists

Kind: instance method of Model
Resolve: Object - The results

Param Type Description
[options] Object
[options.using] PoolConnection Use this connection

model.dropTable([options]) ⇒ Promise

Delete the associated table if exists

Kind: instance method of Model
Resolve: Object - The results

Param Type Description
[options] Object
[options.using] PoolConnection Use this connection

model.migrate([options]) ⇒ Promise

Add/remove columns from the table to match the current Model

Kind: instance method of Model
Resolve: Object - The results

Param Type Description
[options] Object
[options.using] PoolConnection Use this connection