Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// NOTICE: Sequelize version previous to 4.4.2 generate a bad MSSQL query
// if users sort the collection on the primary key, so we prevent
// that.
const idField = _.keys(model.primaryKeys)[0];
if (Database.isMSSQL(opts) && _.includes([idField, `-${idField}`], params.sort)) {
const sequelizeVersion = opts.sequelize.version;
if (sequelizeVersion !== '4.4.2-forest') {
return null;
}
}
if (params.sort.indexOf('.') !== -1) {
// NOTICE: Sort on the belongsTo displayed field
const [associationName, fieldName] = params.sort.split('.');
const column = getReferenceField(
Schemas.schemas,
(aliasSchema || schema),
associationName,
fieldName,
);
return [[opts.sequelize.col(column), order]];
}
if (aliasName) {
return [[opts.sequelize.col(`${aliasName}.${Orm.getColumnName(aliasSchema, params.sort)}`), order]];
}
return [[params.sort, order]];
}
return null;
};
}
// NOTICE: Handle boolean for MySQL database
let modelName;
let field;
let fieldSplit;
let valueBoolean;
let fieldBoolean = false;
if (fieldName.indexOf(':') === -1) {
modelName = model.name;
} else {
fieldSplit = fieldName.split(':');
modelName = fieldSplit[0];
fieldName = fieldSplit[1];
}
const schema = Schemas.schemas[modelName];
if (schema) {
field = _.find(schema.fields, currentField => currentField.field === fieldName);
if (field && field.type === 'Boolean') {
fieldBoolean = true;
if (value.indexOf('true') > -1) {
valueBoolean = true;
} else if (value.indexOf('false') > -1) {
valueBoolean = false;
}
}
}
const condition = {};
if (value[0] === '!' && value[1] !== '*') {
value = value.substring(1);
if (fieldBoolean) {
function LeaderboardStatGetter(model, modelRelationship, params, options) {
const labelField = params.label_field;
const aggregate = params.aggregate.toUpperCase();
const aggregateField = params.aggregate_field;
const { limit } = params;
const schema = Schemas.schemas[model.name];
const schemaRelationship = Schemas.schemas[modelRelationship.name];
let associationAs = schema.name;
_.each(modelRelationship.associations, (association) => {
if (association.target.name === model.name && association.as) {
associationAs = association.as;
}
});
const groupBy = `${associationAs}.${labelField}`;
function getAggregateField() {
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
const fieldName = aggregateField || schemaRelationship.primaryKeys[0]
|| schemaRelationship.fields[0].field;
return `${schemaRelationship.name}.${Orm.getColumnName(schema, fieldName)}`;
function LineStatGetter(model, params, options) {
const schema = Schemas.schemas[model.name];
const timeRange = params.time_range.toLowerCase();
function getAggregateField() {
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
const fieldName = params.aggregate_field
|| schema.primaryKeys[0]
|| schema.fields[0].field;
return `${schema.name}.${Orm.getColumnName(schema, fieldName)}`;
}
function getGroupByDateField() {
return `${schema.name}.${Orm.getColumnName(schema, params.group_by_date_field)}`;
}
const groupByDateField = getGroupByDateField();
function ValueStatGetter(model, params, options) {
const OPERATORS = new Operators(options);
this.operatorDateParser = new BaseOperatorDateParser({
operators: OPERATORS,
timezone: params.timezone,
});
const schema = Schemas.schemas[model.name];
function getAggregate() {
return params.aggregate.toLowerCase();
}
function getAggregateField() {
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
const fieldName = params.aggregate_field
|| schema.primaryKeys[0]
|| schema.fields[0].field;
return `${schema.name}.${Orm.getColumnName(schema, fieldName)}`;
}
function getIncludes() {
const includes = [];
_.values(model.associations).forEach((association) => {
if (['HasOne', 'BelongsTo'].indexOf(association.associationType) > -1) {
function LeaderboardStatGetter(model, modelRelationship, params, options) {
const labelField = params.label_field;
const aggregate = params.aggregate.toUpperCase();
const aggregateField = params.aggregate_field;
const { limit } = params;
const schema = Schemas.schemas[model.name];
const schemaRelationship = Schemas.schemas[modelRelationship.name];
let associationAs = schema.name;
_.each(modelRelationship.associations, (association) => {
if (association.target.name === model.name && association.as) {
associationAs = association.as;
}
});
const groupBy = `${associationAs}.${labelField}`;
function getAggregateField() {
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
const fieldName = aggregateField || schemaRelationship.primaryKeys[0]
|| schemaRelationship.fields[0].field;
return `${schemaRelationship.name}.${Orm.getColumnName(schema, fieldName)}`;
}
function ResourcesGetter(model, options, params) {
const schema = Schemas.schemas[model.name];
const queryBuilder = new QueryBuilder(model, options, params);
let segmentScope;
let segmentWhere;
const OPERATORS = new Operators(options);
const primaryKey = _.keys(model.primaryKeys)[0];
const filterParser = new FiltersParser(schema, params.timezone, options);
let fieldNamesRequested;
let searchBuilder;
async function getFieldNamesRequested() {
if (!params.fields || !params.fields[model.name]) { return null; }
// NOTICE: Populate the necessary associations for filters
const associations = params.filters ? await filterParser.getAssociations(params.filters) : [];
if (params.sort && params.sort.includes('.')) {
const needsDateOnlyFormating = isVersionLessThan4(options.sequelize);
const schema = Schemas.schemas[model.name];
let associationSplit;
let associationCollection;
let associationField;
let associationSchema;
let field;
if (params.group_by_field.indexOf(':') === -1) {
field = _.find(schema.fields, (currentField) => currentField.field === params.group_by_field);
} else {
associationSplit = params.group_by_field.split(':');
associationCollection = model.associations[associationSplit[0]].target.name;
[, associationField] = associationSplit;
associationSchema = Schemas.schemas[associationCollection];
field = _.find(
associationSchema.fields,
(currentField) => currentField.field === associationField,
);
}
function getGroupByField() {
if (params.group_by_field.includes(':')) {
const [associationName, fieldName] = params.group_by_field.split(':');
return `${associationName}.${Orm.getColumnName(associationSchema, fieldName)}`;
}
return `${schema.name}.${Orm.getColumnName(schema, params.group_by_field)}`;
}
const groupByField = getGroupByField();
function PieStatGetter(model, params, options) {
const needsDateOnlyFormating = isVersionLessThan4(options.sequelize);
const schema = Schemas.schemas[model.name];
let associationSplit;
let associationCollection;
let associationField;
let associationSchema;
let field;
if (params.group_by_field.indexOf(':') === -1) {
field = _.find(schema.fields, (currentField) => currentField.field === params.group_by_field);
} else {
associationSplit = params.group_by_field.split(':');
associationCollection = model.associations[associationSplit[0]].target.name;
[, associationField] = associationSplit;
associationSchema = Schemas.schemas[associationCollection];
field = _.find(
associationSchema.fields,
(currentField) => currentField.field === associationField,
function QueryBuilder(model, opts, params) {
const schema = Schemas.schemas[model.name];
function hasPagination() {
return params.page && params.page.number;
}
this.getSkip = () => {
if (hasPagination()) {
return (Number.parseInt(params.page.number, 10) - 1) * this.getLimit();
}
return 0;
};
this.getLimit = () => {
if (hasPagination()) {
return Number.parseInt(params.page.size, 10) || 10;
}