Model
Model is a minimal and high level abstraction layer for SQL databases. It implements knex library. You need to install adapter for your database manually. For more info check initializing the library.
NOTE: Knexjs supports Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift, however, all adapters are not tested with the model. If some methods do not work as expected, feel free to open a github issue.
Creating a Model
To create a model import the base model class and pass a configuration object:
Configuration Object
All keys are mandatory unless stated differently.
key
type
description
table
string
name of the table in the database
config
object
sanitize (optional)
object
idField (optional)
string
primary key of the table, defaults to id
Methods
Model.find (query)
Finds data that matches provided query.
Model.findOne (query)
Finds data that matches provided query and returns only the first result.
Model.create (data)
Does a database insert with provided payload (data).
Model.update (data)
Updates a record by primary key. First argument is id (idField).
Model.upsert (data)
Upsert model will update existing data, or create a new record if there is no data with provided query. Upsert requires $where
param, which is basically the query for find
function. If nothing matches $where
query, new record is added.
CAUTION: If $where
matches multiple records, only the first one will be updated.
Model.delete (id)
Deletes a record from the database for provided id
(idField).
Data Filtering, Sorting and Pagination
All params can be used for find
or findAll
method, and in $where
for upsert
method.
Basic Filtering
Field level search:
key
sample
description
$in
{ id: { $in: [1,2] } }
finds entries that have id 1 or 2
$nin
{ id: { $nin: [1,2] } }
finds entries that do not have id 1 or 2
$lt
{ id: { $lt: 5 } }
finds entries that have id less than 5
$lte
{ id: { $lte: 5 } }
finds entries that have id less than or equal 5
$gt
{ id: { $gt: 5 } }
finds entries that have id greater than 5
$gte
{ id: { $gte: 5 } }
finds entries that have id greater than or equal 5
$between
{ id: { $between: [1, 5] } }
finds entries that have ids between 1 and 5 (including 1 and 5)
$notBetween
{ id: { $notBetween: [1, 5] } }
finds entries that do not have ids between 1 and 5 (excluding 1 and 5)
$null
{ id: { $null: true }}
finds entries where id is null (use $null: false
to invert the results)
$or
{ $or: { email: 't@cd.com', username: 'dan' } }
finds entries where email is t@cd.com
or username is dan
Pagination
Pagination is done by utilizing $limit
and $skip
. By default, $limit
is set to 10.
Sorting Data
Data can be sorted by one property, using $sort
. Direction is optional and defaults to desc
.
Sanitization
You can define sanitize
function in constructor, to sanitize data before executing the method (applies to all methods). Sanitize functions must be sync.
Extending the Model
At some point you may need to utilize some of the knex methods or create custom methods. You can achieve this by extending the model.
Last updated