mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
22e13acd65
- All var declarations are now const or let as per ES6 - All comma-separated lists / chained declarations are now one declaration per line - This is for clarity/readability but also made running the var-to-const/let switch smoother - ESLint rules updated to match How this was done: - npm install -g jscodeshift - git clone https://github.com/cpojer/js-codemod.git - git clone git@github.com:TryGhost/Ghost.git shallow-ghost - cd shallow-ghost - jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2 - jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2 - yarn - yarn test - yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode - grunt test-regression - sorted!
101 lines
3.3 KiB
JavaScript
101 lines
3.3 KiB
JavaScript
/**
|
|
* # Utils
|
|
* Parts of the model code which can be split out and unit tested
|
|
*/
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
const ObjectId = require('bson-objectid');
|
|
const common = require('../../lib/common');
|
|
let attach;
|
|
let detach;
|
|
|
|
/**
|
|
* Attach wrapper (please never call attach manual!)
|
|
*
|
|
* We register the creating event to be able to hook into the model creation process of Bookshelf.
|
|
* We need to load the model again, because of a known bookshelf issue:
|
|
* see https://github.com/tgriesser/bookshelf/issues/629
|
|
* (withRelated option causes a null value for the foreign key)
|
|
*
|
|
* roles [1,2]
|
|
* roles [{id: 1}, {id: 2}]
|
|
* roles [{role_id: 1}]
|
|
* roles [BookshelfModel]
|
|
*/
|
|
attach = function attach(Model, effectedModelId, relation, modelsToAttach, options) {
|
|
options = options || {};
|
|
|
|
let fetchedModel;
|
|
const localOptions = {transacting: options.transacting};
|
|
|
|
return Model.forge({id: effectedModelId}).fetch(localOptions)
|
|
.then(function successFetchedModel(_fetchedModel) {
|
|
fetchedModel = _fetchedModel;
|
|
|
|
if (!fetchedModel) {
|
|
throw new common.errors.NotFoundError({level: 'critical', help: effectedModelId});
|
|
}
|
|
|
|
fetchedModel.related(relation).on('creating', function (collection, data) {
|
|
data.id = ObjectId.generate();
|
|
});
|
|
|
|
return Promise.resolve(modelsToAttach)
|
|
.then(function then(models) {
|
|
models = _.map(models, function mapper(model) {
|
|
if (model.id) {
|
|
return model.id;
|
|
} else if (!_.isObject(model)) {
|
|
return model.toString();
|
|
} else {
|
|
return model;
|
|
}
|
|
});
|
|
|
|
return fetchedModel.related(relation).attach(models, localOptions);
|
|
});
|
|
})
|
|
.finally(function () {
|
|
if (!fetchedModel) {
|
|
return;
|
|
}
|
|
|
|
fetchedModel.related(relation).off('creating');
|
|
});
|
|
};
|
|
|
|
detach = function detach(Model, effectedModelId, relation, modelsToAttach, options) {
|
|
options = options || {};
|
|
|
|
let fetchedModel;
|
|
const localOptions = {transacting: options.transacting};
|
|
|
|
return Model.forge({id: effectedModelId}).fetch(localOptions)
|
|
.then(function successFetchedModel(_fetchedModel) {
|
|
fetchedModel = _fetchedModel;
|
|
|
|
if (!fetchedModel) {
|
|
throw new common.errors.NotFoundError({level: 'critical', help: effectedModelId});
|
|
}
|
|
|
|
return Promise.resolve(modelsToAttach)
|
|
.then(function then(models) {
|
|
models = _.map(models, function mapper(model) {
|
|
if (model.id) {
|
|
return model.id;
|
|
} else if (!_.isObject(model)) {
|
|
return model.toString();
|
|
} else {
|
|
return model;
|
|
}
|
|
});
|
|
|
|
return fetchedModel.related(relation).detach(models, localOptions);
|
|
});
|
|
});
|
|
};
|
|
|
|
module.exports.attach = attach;
|
|
module.exports.detach = detach;
|