Model

Introduced in 4.2.0

Stability: 1 - Experimental

const Model = require('kado/lib/Model')

The Model is a super class made for building new data models used for interacting with data sets produced by data queries.

By default the Model class is a dry extension of the Mapper library provides the data access and writing methods needed for a data model. Specific methods should be then added to the model. Queries shall be static while operations on a record should be methods of the model.

Example Model

const Model = require('kado/lib/Model')
class MyModel extends Model {
  static getData () { return [{ test: 'test' }] }
  print () { console.log(`${this.test} is working`)}
}

Class: Model

This is the Model class which extends Mapper.

static Model.assertDate(date, msg = 'Invalid date')

static Model.assertEmail(email, msg = 'Invalid email')

static Model.assertId(id, msg = 'Invalid id')

static Model.byIdQuery(table, id, fields, idField)

static Model.deleteQuery(table)

static Model.fieldBoolean (defaultValue = false)

static Model.fieldDate (nullable = false, writable = true, defaultValue)

static Model.fieldNumber (defaultValue = '0', signed = true, nullable=false, length)

static Model.fieldPositiveNumber (defaultValue = '0', nullable = false, length)

This always returns an unsigned field.

static Model.fieldPrimary (autoIncrement = true, nullable = true)

static Model.fieldString (defaultValue = null, nullable = true, length)

static Model.fieldText (defaultValue = '', nullable = false)

static Model.filterFields(fields, options)

Available Options

static Model.insertQuery(table, fields)

static Model.listQuery(table)

static async Model.save(Model, db, id, fields, data)

This method is used to save records to the database, it will handle updating records that already exist or creating records that dont exist. This is useful when creating administration functions that need to create / edit records.

static Model.searchQuery(table, phrase, fields)

static Model.updateQuery(table, fields)