It’s often desirable to apply model definitions to the underlying relational database to provision or update schema objects so that they stay synchronized with the model definitions. In LoopBack, this is called auto-migration. Implementing auto-migration is optional for connector.
There are two variations:
See Creating a database schema from models for a general introduction to auto-migration auto-update.
These are the two top-level functions that actually create the database schema and call the other functions.
For both functions, the parameters are:
models (optional): a string model name or an array of string model names. If not present, apply to all modelscb: callback functionMySQL.prototype.autoupdate = function (models, cb) {
// ...
};
MySQL.prototype.automigrate = function (models, cb) {
// ...
};
The automigrate and autoupdate operations are usually mapped to a sequence of data definition language (DDL) statements.
First, define a few helper functions.
Define a function to create a database table for a model.
Parameters:
model: Model namecb: Callback functionMySQL.prototype.createTable = function (model, cb) {
// ...
};
Define a function to check if the specified models exist.
Parameters:
models (optional): a string model name or an array of string model names. If not present, apply to all modelscb: callback functionMySQL.prototype.isActual = function(models, cb) {
// ...
};
MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) {
// ...
};
Define functions to create column and index definitions for models and model properties.
MySQL.prototype.buildColumnDefinitions =
MySQL.prototype.propertiesSQL = function (model) {
// ...
};
MySQL.prototype.buildIndex = function(model, property) {
// ...
};
MySQL.prototype.buildIndexes = function(model) {
// ...
};
MySQL.prototype.buildColumnDefinition = function(model, prop) {
// ...
};
MySQL.prototype.columnDataType = function (model, property) {
// ...
};