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> |
Members
entities :object.<string, Entity>
store all extra information than Model for runtime
Type:
- object.<string, Entity>
models :object.<string, Model>
store all Models you ever defined.
Type:
- object.<string, Model>
Methods
all()
Perform operation for all databases
create(databaseopt) → {Promise}
create a database base on definition
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
database |
string |
<optional> |
ORIGIN |
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 |
Returns:
- Type
- Model
dropCreate() → {Promise}
recreate ORIGIN database, useful for unit test scenario
Returns:
- Type
- Promise
getMigrator() → {Migrator}
lazy load migrator
Returns:
- Type
- Migrator
init(optionsOrConfigPathopt, store)
init
- should be fired up during app startup process.
- model need to require this file, so this prcoess can't be in constructor
- 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 |
remove(databaseopt) → {Promise}
remove a database from server
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
database |
string |
<optional> |
ORIGIN |
Fires:
Returns:
- Type
- Promise
scope(databaseopt, job) → {promise}
this is where you perform database operations
- run operation over specific database em.scope(databasename, job);
- run operation over ORIGIN database em.scope(job);
- job is a function take a session instance to perform operation
- you must return a promise in job function so scope can release connection when finish
- ORIGIN normally refer to
database
in your_project/em.json file. - 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 |
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 |
Returns:
- Type
- EmmoModel
sync(databasesopt)
perform database structure synchoronization.
- missed databases will be created automatically.
- existing databases will be migrated smartly.
- whenever shit happens during creating process, it will be deleted.
- migration failure should not affect existing databases.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
databases |
string | array |
<optional> |
ALL |
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
Type Definitions
job(db)
Parameters:
Name | Type | Description |
---|---|---|
db |
Session |
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 |
migrated
when the database is migrated, you can perform initialize against database here.
Parameters:
Name | Type | Description |
---|---|---|
databaseName |
string |
ready
when all databases are created or migrated sucessfully, you can do some system bootstrap here.
removed
when database is removed
Parameters:
Name | Type | Description |
---|---|---|
database |
string |