Ghost/core/server/models/base/utils.js
Daniel Lockyer e9b21fdbd1 Updated bson-objectid calls to match API change
refs c873899e49

- as of `bson-objectid` v2.0.0, this library exports the function
  to generate an ObjectID directly, and then you need to use `.toHexString()`
  to get the 24 character hex string - 6696f27d82
- this commit removes all uses of `.generate()` and replaces with this
  change
2021-04-21 16:23:52 +01:00

99 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 errors = require('@tryghost/errors');
/**
* 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]
*/
const 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 errors.NotFoundError({level: 'critical', help: effectedModelId});
}
fetchedModel.related(relation).on('creating', function (collection, data) {
data.id = ObjectId().toHexString();
});
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');
});
};
const 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 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;