Schema

Schema - adapter-specific classes factory.

All classes in single schema shares same adapter type and one database connection


param name - type of schema adapter (mysql, mongoose, sequelize, redis)
param settings - any database-specific settings which we need to establish connection (of course it depends on specific adapter)

  • host
  • port
  • username
  • password
  • database
  • debug Boolean = false


example Schema creation, waiting for connection callback

var schema = new Schema('mysql', { database: 'myapp_test' });
schema.define(...);
schema.on('connected', function () {
    // work with database
});
Source code
Instance methods Helper methods

Schema.prototype.define

Declared as function defineClass(className, properties, settings)

Define class


param String className
param Object properties - hash of class properties in format {property: Type, property2: Type2, ...} or {property: {type: Type}, property2: {type: Type2}, ...}
param Object settings - other configuration of class
return newly created class


example simple case

var User = schema.defind('User', {
    email: String,
    password: String,
    birthDate: Date,
    activated: Boolean
});


example more advanced case

var User = schema.defind('User', {
    email: { type: String, limit: 150, index: true },
    password: { type: String, limit: 50 },
    birthDate: Date,
    registrationDate: {type: Date, default: function () { return new Date }},
    activated: { type: Boolean, default: false }
});
Source code

Schema.prototype.defineProperty

Declared as function (model, prop, params)

Define single property named prop on model


param String model - name of model
param String prop - name of propery
param Object params - property settings

Source code

Schema.prototype.automigrate

Declared as function (cb)

Drop each model table and re-create. This method make sense only for sql adapters.

Warning! All data will be lost! Use autoupdate if you need your data.
Source code

Schema.prototype.autoupdate

Declared as function (cb)

Update existing database tables. This method make sense only for sql adapters.

Source code

Schema.prototype.isActual

Declared as function (cb)

Check whether migrations needed This method make sense only for sql adapters.

Source code

Schema.prototype.log

Declared as function (sql, t)

Log benchmarked message. Do not redefine this method, if you need to grab chema logs, use schema.on('log', ...) emitter event


private used by adapters

Source code

Schema.prototype.freeze

Declared as function freeze()

Freeze schema. Behavior depends on adapter

Source code

Schema.prototype.tableName

Declared as function (modelName)

Return table name for specified modelName
param String modelName

Source code

Schema.prototype.defineForeignKey

Declared as function defineForeignKey(className, key)

Define foreign key
param String className
param String key - name of key field

Source code

Schema.prototype.disconnect

Declared as function disconnect()

Close database connection

Source code

hiddenProperty

Declared as function hiddenProperty(where, property, value)

Define hidden property

Source code

© 1602 Software