2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
const bookshelf = require('bookshelf');
|
|
|
|
const pluralize = require('pluralize');
|
|
|
|
|
|
|
|
// Local helpers.
|
|
|
|
const utils = require('./utils/');
|
|
|
|
|
|
|
|
// Strapi helpers for models.
|
2016-10-31 12:51:05 +01:00
|
|
|
const utilsModels = require('strapi-utils').models;
|
2016-03-18 11:12:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bookshelf hook
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function (strapi) {
|
|
|
|
const hook = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default options
|
|
|
|
*/
|
|
|
|
|
|
|
|
defaults: {
|
2017-01-04 17:21:24 +01:00
|
|
|
defaultConnection: 'default',
|
|
|
|
host: 'localhost'
|
2016-03-18 11:12:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the hook
|
|
|
|
*/
|
|
|
|
|
2016-11-07 16:31:34 +01:00
|
|
|
initialize: cb => {
|
2016-03-18 11:12:50 +01:00
|
|
|
let globalName;
|
|
|
|
|
2016-07-06 12:16:31 +02:00
|
|
|
// Make sure the Knex hook is present since Knex needs it.
|
2017-01-04 17:21:24 +01:00
|
|
|
if (!strapi.hooks.knex) {
|
2016-07-06 12:16:31 +02:00
|
|
|
strapi.log.error('Impossible to load the ORM without the query builder!');
|
|
|
|
strapi.log.error('You need to install the Strapi query builder with `$ npm install strapi-knex --save`.');
|
|
|
|
strapi.stop(1);
|
|
|
|
}
|
|
|
|
|
2016-03-18 11:12:50 +01:00
|
|
|
// Only run this logic after the Knex has finished to load.
|
2016-11-07 16:31:34 +01:00
|
|
|
strapi.after('hook:knex:loaded', () => {
|
2016-03-18 11:12:50 +01:00
|
|
|
// Initialize collections
|
|
|
|
_.set(strapi, 'bookshelf.collections', {});
|
|
|
|
|
|
|
|
// Return callback if there is no model
|
|
|
|
if (_.isEmpty(strapi.models)) {
|
2016-07-06 12:16:31 +02:00
|
|
|
return cb();
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
_.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-bookshelf'}), (connection, connectionName) => {
|
|
|
|
// Apply defaults
|
|
|
|
_.defaults(connection.settings, strapi.hooks.bookshelf.defaults);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Select models concerned by this connection
|
|
|
|
const models = _.pickBy(strapi.models, {connection: connectionName});
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Return callback if there is no model
|
|
|
|
if (_.isEmpty(models)) {
|
|
|
|
return cb();
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
const loadedHook = _.after(_.size(models), () => {
|
|
|
|
cb();
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Parse every registered model.
|
|
|
|
_.forEach(models, (definition, model) => {
|
|
|
|
globalName = _.upperFirst(_.camelCase(definition.globalId));
|
2016-03-22 15:50:33 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Make sure the model has a table name.
|
|
|
|
// If not, use the model name.
|
|
|
|
if (_.isEmpty(definition.tableName)) {
|
|
|
|
definition.tableName = model;
|
2016-03-22 15:50:33 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Add some informations about ORM & client connection
|
|
|
|
definition.orm = 'bookshelf';
|
|
|
|
definition.client = _.get(connection.settings, 'client');
|
|
|
|
|
|
|
|
// Register the final model for Bookshelf.
|
|
|
|
const loadedModel = {
|
|
|
|
tableName: definition.tableName,
|
|
|
|
hasTimestamps: _.get(definition, 'options.timestamps') || true
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize the global variable with the
|
|
|
|
// capitalized model name.
|
|
|
|
global[globalName] = {};
|
|
|
|
|
|
|
|
// Call this callback function after we are done parsing
|
|
|
|
// all attributes for relationships-- see below.
|
|
|
|
const done = _.after(_.size(definition.attributes), () => {
|
|
|
|
try {
|
|
|
|
// Initialize lifecycle callbacks.
|
|
|
|
loadedModel.initialize = () => {
|
|
|
|
const self = this;
|
|
|
|
const lifecycle = {
|
|
|
|
creating: 'beforeCreate',
|
|
|
|
created: 'afterCreate',
|
|
|
|
destroying: 'beforeDestroy',
|
|
|
|
destroyed: 'afterDestroy',
|
|
|
|
updating: 'beforeUpdate',
|
|
|
|
updated: 'afterUpdate',
|
|
|
|
fetching: 'beforeFetch',
|
|
|
|
fetched: 'afterFetch',
|
|
|
|
saving: 'beforeSave',
|
|
|
|
saved: 'afterSave'
|
|
|
|
};
|
|
|
|
|
|
|
|
_.forEach(lifecycle, (fn, key) => {
|
|
|
|
if (_.isFunction(strapi.models[model.toLowerCase()][fn])) {
|
|
|
|
self.on(key, strapi.models[model.toLowerCase()][fn]);
|
|
|
|
}
|
|
|
|
});
|
2016-03-18 11:12:50 +01:00
|
|
|
};
|
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
const ORM = bookshelf(connection.settings);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
global[globalName] = ORM.Model.extend(loadedModel);
|
|
|
|
global[pluralize(globalName)] = ORM.Collection.extend({
|
|
|
|
model: global[globalName]
|
|
|
|
});
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Push model to strapi global variables.
|
|
|
|
strapi.bookshelf.collections[globalName.toLowerCase()] = global[globalName];
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Push attributes to be aware of model schema.
|
|
|
|
strapi.bookshelf.collections[globalName.toLowerCase()]._attributes = definition.attributes;
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
loadedHook();
|
|
|
|
} catch (err) {
|
|
|
|
strapi.log.error('Impossible to register the `' + model + '` model.');
|
|
|
|
strapi.log.error(err);
|
|
|
|
strapi.stop();
|
|
|
|
}
|
|
|
|
});
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
if (_.isEmpty(definition.attributes)) {
|
|
|
|
done();
|
|
|
|
}
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
// Add every relationships to the loaded model for Bookshelf.
|
|
|
|
// Basic attributes don't need this-- only relations.
|
|
|
|
_.forEach(definition.attributes, (details, name) => {
|
|
|
|
const verbose = _.get(utilsModels.getNature(details, name), 'verbose') || '';
|
|
|
|
|
|
|
|
// Build associations key
|
|
|
|
if (!_.isEmpty(verbose)) {
|
|
|
|
utilsModels.defineAssociations(globalName, definition, details, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (verbose) {
|
|
|
|
case 'hasOne':
|
|
|
|
const FK = _.findKey(strapi.models[details.model].attributes, details => {
|
|
|
|
if (details.hasOwnProperty('model') && details.model === model && details.hasOwnProperty('via') && details.via === name) {
|
|
|
|
return details;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
loadedModel[name] = () => {
|
|
|
|
return this.hasOne(global[_.capitalize(details.model)], FK);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'hasMany':
|
|
|
|
loadedModel[name] = () => {
|
|
|
|
return this.hasMany(global[_.capitalize(details.collection)], details.via);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'belongsTo':
|
|
|
|
loadedModel[name] = () => {
|
|
|
|
return this.belongsTo(global[_.capitalize(details.model)], name);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'belongsToMany':
|
|
|
|
const tableName = _.map(_.sortBy([strapi.models[details.collection].attributes[details.via], details], 'collection'), table => {
|
|
|
|
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
|
|
|
|
}).join('__');
|
|
|
|
|
|
|
|
const relationship = _.clone(strapi.models[details.collection].attributes[details.via]);
|
|
|
|
|
|
|
|
// Force singular foreign key
|
|
|
|
relationship.attribute = pluralize.singular(relationship.collection);
|
|
|
|
details.attribute = pluralize.singular(details.collection);
|
|
|
|
|
|
|
|
// Define PK column
|
|
|
|
details.column = utils.getPK(model, strapi.models);
|
|
|
|
relationship.column = utils.getPK(details.collection, strapi.models);
|
|
|
|
|
|
|
|
// Sometimes the many-to-many relationships
|
|
|
|
// is on the same keys on the same models (ex: `friends` key in model `User`)
|
|
|
|
if (details.attribute + '_' + details.column === relationship.attribute + '_' + relationship.column) {
|
|
|
|
relationship.attribute = pluralize.singular(details.via);
|
|
|
|
}
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
loadedModel[name] = () => {
|
|
|
|
return this.belongsToMany(global[_.capitalize(details.collection)], tableName, relationship.attribute + '_' + relationship.column, details.attribute + '_' + details.column);
|
|
|
|
};
|
|
|
|
break;
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2017-01-04 17:21:24 +01:00
|
|
|
done();
|
|
|
|
});
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return hook;
|
|
|
|
};
|