Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions lib/executor.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,6 +30,7 @@ module.exports = function execute(app, instructions, callback) {

setupDataSources(app, instructions);
setupModels(app, instructions);

setupMiddleware(app, instructions);
setupComponents(app, instructions);

Expand DownExpand Up@@ -164,15 +165,38 @@ function setupDataSources(app, instructions) {
function setupModels(app, instructions) {
defineMixins(app, instructions);
defineModels(app, instructions);
attachModelsToApp(app, instructions);
customizeModels(app, instructions);
}

function attachModelsToApp(app, instructions) {
instructions.models.forEach(function(data) {
// Skip base models that are not exported to the app
if (!data.config) return;

debug('Attaching model %s to app', data.name);
app.model(data._model, data.config);
});
}

function customizeModels(app, instructions) {
instructions.models.forEach(function(data) {
var model = data._model;

if (data.sourceFile) {
debug('Loading customization script %s', data.sourceFile);
var code = require(data.sourceFile);
if (typeof code === 'function' && model) {
debug('Customizing model %s', data.name);
code(model);
} else {
debug('Skipping model file %s - `module.exports` is not a function',
data.sourceFile);
}
}
});
}

function defineMixins(app, instructions) {
var modelBuilder = (app.registry || app.loopback).modelBuilder;
var BaseClass = app.loopback.Model;
Expand DownExpand Up@@ -212,17 +236,6 @@ function defineModels(app, instructions) {
} else {
debug('Creating new model %s %j', name, data.definition);
model = registry.createModel(data.definition);
if (data.sourceFile) {
debug('Loading customization script %s', data.sourceFile);
var code = require(data.sourceFile);
if (typeof code === 'function') {
debug('Customizing model %s', name);
code(model);
} else {
debug('Skipping model file %s - `module.exports` is not a function',
data.sourceFile);
}
}
}

data._model = model;
Expand Down