forest-express-sequelize
Version:
Official Express/Sequelize Liana for Forest
65 lines (63 loc) • 3.41 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
require("core-js/modules/es.promise.js");
require("core-js/modules/es.array.iterator.js");
var _lodash = require("lodash");
var _sequelizeCompatibility = _interopRequireDefault(require("../utils/sequelize-compatibility"));
var _primaryKeysManager = _interopRequireDefault(require("./primary-keys-manager"));
var _resourcesGetter = _interopRequireDefault(require("./resources-getter"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
class HasManyGetter extends _resourcesGetter.default {
constructor(model, association, lianaOptions, params, user) {
super(association, lianaOptions, params, user);
this._parentModel = model.unscoped();
}
async _getRecords() {
var _parentRecord$this$_p;
const options = await this._buildQueryOptions();
const parentRecord = await this._parentModel.findOne(options);
const records = (_parentRecord$this$_p = parentRecord === null || parentRecord === void 0 ? void 0 : parentRecord[this._params.associationName]) !== null && _parentRecord$this$_p !== void 0 ? _parentRecord$this$_p : [];
new _primaryKeysManager.default(this._model).annotateRecords(records);
return records;
}
async count() {
const options = await this._buildQueryOptions({
forCount: true
});
return this._parentModel.count(options);
}
async _buildQueryOptions(buildOptions = {}) {
const {
associationName,
recordId
} = this._params;
const [model, options] = await super._buildQueryOptions(_objectSpread(_objectSpread({}, buildOptions), {}, {
tableAlias: associationName
}));
const parentOptions = _sequelizeCompatibility.default.postProcess(this._parentModel, {
where: new _primaryKeysManager.default(this._parentModel).getRecordsConditions([recordId]),
include: [_objectSpread({
model,
as: associationName,
scope: false,
required: !!buildOptions.forCount
}, (0, _lodash.pick)(options, ['attributes', 'where', 'include']))]
});
if (!buildOptions.forCount) {
parentOptions.subQuery = false; // Why?
parentOptions.attributes = []; // Don't fetch parent attributes (perf)
parentOptions.offset = options.offset;
parentOptions.limit = options.limit;
// Order with the relation (https://github.com/sequelize/sequelize/issues/4553)
if (options.order) {
parentOptions.order = options.order.map(function (fields) {
return [associationName, ...fields];
});
}
}
return parentOptions;
}
}
module.exports = HasManyGetter;
;