Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.perform = (model, key, value) => {
const schema = Interface.Schemas.schemas[utils.getModelName(model)];
let parseFct = val => val;
let ret = null;
const fieldValues = key.split(':');
const fieldName = fieldValues[0];
const subfieldName = fieldValues[1];
// Mongoose Aggregate don't parse the value automatically.
let field = _.find(schema.fields, { field: fieldName });
const isEmbeddedField = !!field.type.fields;
if (isEmbeddedField) {
field = _.find(field.type.fields, { field: subfieldName });
}
if (field) {
function HasManyGetter(model, association, opts, params) {
const queryBuilder = new QueryBuilder(model, opts, params);
const schema = Interface.Schemas.schemas[association.name];
const primaryKeyModel = _.keys(model.primaryKeys)[0];
function getFieldNamesRequested() {
if (!params.fields || !params.fields[association.name]) { return null; }
// NOTICE: Force the primaryKey retrieval to store the records properly in
// the client.
const primaryKeyArray = [_.keys(association.primaryKeys)[0]];
return _.union(primaryKeyArray, params.fields[association.name].split(','));
}
const fieldNamesRequested = getFieldNamesRequested();
const searchBuilder = new SearchBuilder(association, opts, params, fieldNamesRequested);
const where = searchBuilder.perform(params.associationName);
const include = queryBuilder.getIncludes(association, fieldNamesRequested);
function FiltersParser(model, timezone, options) {
const schema = Interface.Schemas.schemas[utils.getModelName(model)];
const parseInteger = (value) => Number.parseInt(value, 10);
const parseDate = (value) => new Date(value);
const parseBoolean = (value) => {
if (value === 'true') { return true; }
if (value === 'false') { return false; }
return typeof value === 'boolean' ? value : null;
};
const parseString = (value) => {
// NOTICE: Check if the value is a real ObjectID. By default, the isValid method returns true
// for a random string with length 12 (example: 'Black Friday').
const { ObjectId } = options.mongoose.Types;
if (ObjectId.isValid(value) && ObjectId(value).toString() === value) {
return ObjectId(value);
}
return value;
_.each(associations, (association) => {
if (!fieldNamesRequested
|| (fieldNamesRequested.indexOf(association.as) !== -1)) {
if (['HasOne', 'BelongsTo'].indexOf(association.associationType) > -1) {
const modelAssociation = association.target;
const schemaAssociation = Interface.Schemas.schemas[modelAssociation.name];
const fieldsAssociation = schemaAssociation.fields;
_.each(fieldsAssociation, (field) => {
if (field.isVirtual) { return; } // NOTICE: Ignore Smart Fields.
if (field.integration) { return; } // NOTICE: Ignore integration fields.
if (field.reference) { return; } // NOTICE: Ignore associations.
if (field.isSearchable === false) { return; }
if (hasSearchFields
&& !_.includes(searchAssociationFields, `${association.as}.${field.field}`)) {
return;
}
let condition = {};
const columnName = field.columnName || field.field;
const column = opts.sequelize.col(`${association.as}.${columnName}`);
function HasManyGetter(model, association, opts, params) {
const OBJECTID_REGEXP = /^[0-9a-fA-F]{24}$/;
const schema = Interface.Schemas.schemas[utils.getModelName(association)];
const searchBuilder = new SearchBuilder(association, opts, params);
function hasPagination() {
return params.page && params.page.number;
}
function getLimit() {
if (hasPagination()) {
return parseInt(params.page.number, 10) * params.page.size;
}
return 5;
}
function getSkip() {
if (hasPagination()) {
return (parseInt(params.page.number, 10) - 1) * params.page.size;
function ResourceGetter(model, params) {
const schema = Interface.Schemas.schemas[utils.getModelName(model)];
function handlePopulate(query) {
_.each(schema.fields, (field) => {
if (field.reference) {
query.populate(field.field);
}
});
}
this.perform = () =>
new P((resolve, reject) => {
const query = model.findById(params.recordId);
handlePopulate(query);
query
function ResourceUpdater(model, params, newRecord) {
const schema = Interface.Schemas.schemas[model.name];
this.perform = () => {
const compositeKeysManager = new CompositeKeysManager(model, schema, newRecord);
return new ResourceFinder(model, params)
.perform()
.then((record) => {
if (record) {
_.each(newRecord, (value, attribute) => {
record[attribute] = value;
});
return record.validate()
.catch((error) => { throw new ErrorHTTP422(error.message); })
.then(() => record.save());
}
function ResourcesGetter(model, opts, params) {
const schema = Interface.Schemas.schemas[utils.getModelName(model)];
const queryBuilder = new QueryBuilder(model, params, opts);
let fieldsSearched = null;
function getSegment() {
if (schema.segments && params.segment) {
return _.find(schema.segments, (currentSegment) => currentSegment.name === params.segment);
}
return null;
}
function getSegmentCondition() {
const segment = getSegment();
if (segment && segment.where && typeof segment.where === 'function') {
return segment.where().then((where) => ({ where }));
}
function ResourceCreator(Model, params) {
const schema = Interface.Schemas.schemas[utils.getModelName(Model)];
function create() {
return new P((resolve, reject) => {
if ('_id' in params) { delete params._id; }
new Model(params)
.save((err, record) => {
if (err) { return reject(err); }
return resolve(record);
});
});
}
function fetch(record) {
return new P((resolve, reject) => {
const query = Model.findById(record.id);
function ResourceCreator(model, params) {
const schema = Interface.Schemas.schemas[model.name];
this.perform = function perform() {
const promises = [];
const recordCreated = model.build(params);
if (model.associations) {
_.forOwn(model.associations, (association, name) => {
if (association.associationType === 'BelongsTo') {
promises.push(recordCreated[`set${_.upperFirst(name)}`](params[name], { save: false }));
}
});
}
return P.all(promises)
.then(() => recordCreated.validate()
.catch((error) => {