Class: EmmoModel

EmmoModel(parentopt)

new EmmoModel(parentopt)

EmmoModel holds all entities definitions, and a Database Server for you.

EmmoModel is a EventEmitter, you can subscribe events by once/on

Parameters:
Name Type Attributes Description
parent EmmoModel <optional>
Source:

Members

entities :object.<string, Entity>

store all extra information than Model for runtime

Type:
Source:

models :object.<string, Model>

store all Models you ever defined.

Type:
Source:

Methods

all()

Perform operation for all databases

Source:

create(databaseopt) → {Promise}

create a database base on definition

Parameters:
Name Type Attributes Default Description
database string <optional>
ORIGIN
Source:
Fires:
Returns:
Type
Promise

define(name, properties, tableOptions|tableNameopt) → {Model}

define a new Model

Parameters:
Name Type Attributes Default Description
name string

singular

properties object.<string, Property>

KEY as property name

tableOptions|tableName string | object <optional>
names

plural

Source:
Returns:
Type
Model

dropCreate() → {Promise}

recreate ORIGIN database, useful for unit test scenario

Source:
Returns:
Type
Promise

getMigrator() → {Migrator}

lazy load migrator

Source:
Returns:
Type
Migrator

init(optionsOrConfigPathopt, store)

init

  1. should be fired up during app startup process.
  2. model need to require this file, so this prcoess can't be in constructor
  3. share definition among multiple EmmoModel
Parameters:
Name Type Attributes Description
optionsOrConfigPath InitOptions | string <optional>
store object

should provide add/remove/exists/getAll as ./lib/store.js did

Source:

remove(databaseopt) → {Promise}

remove a database from server

Parameters:
Name Type Attributes Default Description
database string <optional>
ORIGIN
Source:
Fires:
Returns:
Type
Promise

scope(databaseopt, job) → {promise}

this is where you perform database operations

  1. run operation over specific database em.scope(databasename, job);
  2. run operation over ORIGIN database em.scope(job);
  3. job is a function take a session instance to perform operation
  4. you must return a promise in job function so scope can release connection when finish
  5. ORIGIN normally refer to database in your_project/em.json file.
  6. you can perform TRANSCACTION in a scope.
Parameters:
Name Type Attributes Default Description
database string <optional>
ORIGIN

which database you want to operate

job EmmoModel~job

perform operation with session instance, need to return promise

Source:
Returns:
Type
promise
Example
var em = require('emmo-model');
 em.scope('db1', funciton(db) {
   return db.all('User');
 }).then(function(users) {
   console.log(users);
 });

spawn(options) → {EmmoModel}

create a new EmmoModel instance to a new server with same definition, like for backup/duplicate

Parameters:
Name Type Description
options InitOptions
Source:
Returns:
Type
EmmoModel

sync(databasesopt)

perform database structure synchoronization.

  1. missed databases will be created automatically.
  2. existing databases will be migrated smartly.
  3. whenever shit happens during creating process, it will be deleted.
  4. migration failure should not affect existing databases.
Parameters:
Name Type Attributes Default Description
databases string | array <optional>
ALL
Source:
Fires:
  • EmmoModel#event:created single database is created
  • EmmoModel#event:migrated single database is migrated
  • EmmoModel#event:synced single database is synced
  • EmmoModel#event:ready all databases are ready

transact()

Transaction support

Source:

Type Definitions

job(db)

Parameters:
Name Type Description
db Session
Source:

Events

created

when the database is created first time, you can plant seed data at this point, like insert admin user.

Parameters:
Name Type Description
database string
Source:

migrated

when the database is migrated, you can perform initialize against database here.

Parameters:
Name Type Description
databaseName string
Source:

ready

when all databases are created or migrated sucessfully, you can do some system bootstrap here.

Source:

removed

when database is removed

Parameters:
Name Type Description
database string
Source: