Global

Members

em

export a ready to use instance, you can spawn a new Instance

Source:

Type Definitions

Condition

Type:
  • object | array
Source:
Example
// for AND operator:
{ age: 20, rank: 100, remark: null }
  => "age" = 20 AND "rank" = 100 AND "remark" IS NULL

// for OR operator:
[ { age: 20 }, { rank: 100, remark: null } ]
  => "age" = 20 OR ( "rank" = 100 AND "remark" IS NULL )

// for IN operator:
{ age: 20, rank: [ 20, 30, 40 ] }
  => "age" = 20 AND "rank" IN (20, 30, 40)

// for like (startsWith, endsWith, contains)
[ { age: 20, rank: em.gt(50) }, { firstName: em.startsWith("John") } ]
  => ("age" = 20 AND "rank" > 50) OR "firstName" LIKE "John%"

// add mathematical operations
{ "age": em.o("id").plus(10).subtract(1), "name": em.o("firstName").concate(' ').concate("lastName") }
  => "age" = "id" + 10 - 1

// since JSON only accept string as key, PSEUDO KEYs are introduced, starts with _ or $:

 { _COUNT: [ em.count(), em.gt(5) ] } // like HAVING clause
  =>  COUNT(*) > 5

 { $AGEID: [ { age: 20 }, { id: em.gt(20) } ], departmentId: 1 } // OR inside AND
  => ("age" = 20 OR "id" > 20) AND ("departmentId" = 1)

Entity

A object to contain Entity definition

Type:
  • object
Properties:
Name Type Description
tableName string
properties object.<string, Property>
propertyNames Array.<string>
updatableNames Array.<string>
inputableNames Array.<string>
primaryKeyNames Array.<string>
autoIncrementName string
Source:

InitOptions

Type:
  • object
Properties:
Name Type Attributes Default Description
modelsPath string <optional>
'./models'

path to model files folder

migrationsPath string <optional>
'./migrations'

path to migration files folder

dialect string <optional>
pg

'pg' is only option for now.

database string

ORIGIN database name, migration will be created base on it

connectionString string

need to replace database name with %s

Source:

InputOptions

Type:
  • object
Properties:
Name Type Attributes Description
data object

input data

method string

insert/update/refresh(In)

newErr Model~newErr <optional>

customize error

Source:

ModelErrorInfo

Type:
  • object
Properties:
Name Type Description
entityName string
entity Entity
propertyName string
property Property
reason string
Source:

Property

A object to contain Property definition Note allowNull property will cause a empty string validation for string property.

Type:
  • Column
Properties:
Name Type Attributes Default Description
columnName string
virtual boolean <optional>
false

only in memory, do not map to database

input boolean <optional>
true

accept input from user

dateFormat string <optional>
ISO8601

how to parse Date @see http://momentjs.com/docs/#/parsing/

autoTrim boolean | boolean <optional>
true

trim space characters for string type, pass 'length' to trancate by length property

index string | boolean <optional>

assign same index name to multiple columns will create a composite index

unique boolean <optional>

create a unique index or set existing index to unique

desc boolean <optional>
true

create a descending index

refer string <optional>

build up foreign key reference

referName string <optional>

specify different referNames to refer same table multiple times

onDelete string <optional>

specify onDelete action: 'CASCADE', 'SET NULL' ...

onUpdate string <optional>

specify onUpdate action

message string <optional>

error message when validation failure

VALIDATION array | boolean <optional>

isEmail: true, isInt: { min: 1 } validator

validators Array.<Validator> <optional>
Source:
Example
{
  id: { type: 'bigint', autoIncrement: true, primaryKey: true },
  account: { type: 'string', length: 50, allowNull: false }, // allowNull will reject '' for string type as well
  password: { type: 'string', virtual: true, isLength: { min: 5, 20 } },  // virtual property will not be mapped to db
  repassword: { type: 'string', virtual: true, validators: [ // this is how you define customize validators
    function(value) {
      return this.password === value;
    }
  ]},
  passwordHash: { type: 'string', length: 50, input: false }, // will be ignored in User.input convertion
  age: { type: 'int', isInt: { min: 18, max: 120 } },
  email: { type: 'string', isEmail: true },
}

Result

Type:
  • array
Properties:
Name Type Description
affectedRows number
Source:

SelectOptions

Type:
  • object
Properties:
Name Type Description
field string | Expression | array
join string
where Condition
orderby string | object | array
groupby string
having string
Source:
Example
// single field:
 { field: 'nick', where: { id: 1 } }
 { field: em.count(), where { id: em.gt(20) } }

 // multiple fields:
 { field: [ 'nick', 'firstName' ], where: { id: 1 } }

 // alias, assume in db we have a user's age is 22
 { field: { aged: em.o('age').plus(30) } }
   => {aged: 52}

 // order by single field ASC
 { field: 'name', order: 'id' }

 // order by multiple field ASC
 { field: 'name', order: [ 'name', 'id' ] }

 // join, select users by department title
 { field: '*', join: 'Department', where: { departmentId: em.o('Department.id'), title: 'Office' } }

 // order by desc:
 { field: 'name', order: { id: 'DESC' } }
 { field: 'name', order: { id: false } }

 // groupby and having:
 ```
 {
   field: 'deparmentId',
   groupby: 'deparmentId',
   having: {
     _: [ em.count, em.gt(4) ]
   }
 }
 ```