Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function eagerScope(modelClass, expr, scopes, filters = null,
prepend = false, isRoot = true) {
if (isRoot) {
expr = expr?.isObjectionRelationExpression
? expr.clone()
: RelationExpression.create(expr)
} else {
// Only add the scope if it's not already defined by the eager statement and
// if it's actually available as a filter in the model's namedFilters list.
for (const scope of scopes) {
if (!expr.args.includes(scope) &&
(modelClass.namedFilters[scope] || filters?.[scope])) {
expr.args[prepend ? 'unshift' : 'push'](scope)
}
}
}
if (expr.numChildren > 0) {
const relations = modelClass.getRelations()
for (const child of Object.values(expr.children)) {
const relation = relations[child.name]
if (!relation) {
throw new RelationError(
throw new errors.GeneralError('You must provide an Objection Model');
}
const whitelist = Object.values(OPERATORS).concat(options.whitelist || []);
super(Object.assign({
id: options.model.idColumn || 'id',
whitelist
}, options));
this.idSeparator = options.idSeparator || ',';
this.jsonSchema = options.model.jsonSchema;
this.allowedEager = options.allowedEager && RelationExpression.create(options.allowedEager);
this.namedEagerFilters = options.namedEagerFilters;
this.eagerFilters = options.eagerFilters;
this.allowedInsert = options.allowedInsert && RelationExpression.create(options.allowedInsert);
this.insertGraphOptions = options.insertGraphOptions;
this.createUseUpsertGraph = options.createUseUpsertGraph;
this.allowedUpsert = options.allowedUpsert && RelationExpression.create(options.allowedUpsert);
this.upsertGraphOptions = options.upsertGraphOptions;
}
mergeRelations (optionRelations, paramRelations) {
if (!paramRelations) {
return optionRelations;
}
if (!optionRelations) {
return RelationExpression.create(paramRelations);
}
return optionRelations.merge(paramRelations);
}
const whitelist = Object.values(OPERATORS).concat(options.whitelist || []);
super(Object.assign({
id: options.model.idColumn || 'id',
whitelist
}, options));
this.idSeparator = options.idSeparator || ',';
this.jsonSchema = options.model.jsonSchema;
this.allowedEager = options.allowedEager && RelationExpression.create(options.allowedEager);
this.namedEagerFilters = options.namedEagerFilters;
this.eagerFilters = options.eagerFilters;
this.allowedInsert = options.allowedInsert && RelationExpression.create(options.allowedInsert);
this.insertGraphOptions = options.insertGraphOptions;
this.createUseUpsertGraph = options.createUseUpsertGraph;
this.allowedUpsert = options.allowedUpsert && RelationExpression.create(options.allowedUpsert);
this.upsertGraphOptions = options.upsertGraphOptions;
}
constructor (options) {
if (!options.model) {
throw new errors.GeneralError('You must provide an Objection Model');
}
const whitelist = Object.values(OPERATORS).concat(options.whitelist || []);
super(Object.assign({
id: options.model.idColumn || 'id',
whitelist
}, options));
this.idSeparator = options.idSeparator || ',';
this.jsonSchema = options.model.jsonSchema;
this.allowedEager = options.allowedEager && RelationExpression.create(options.allowedEager);
this.namedEagerFilters = options.namedEagerFilters;
this.eagerFilters = options.eagerFilters;
this.allowedInsert = options.allowedInsert && RelationExpression.create(options.allowedInsert);
this.insertGraphOptions = options.insertGraphOptions;
this.createUseUpsertGraph = options.createUseUpsertGraph;
this.allowedUpsert = options.allowedUpsert && RelationExpression.create(options.allowedUpsert);
this.upsertGraphOptions = options.upsertGraphOptions;
}
const DESERIALIZED_ARRAY_OPERATORS = [
'between',
'not between',
'?|',
'?&'
];
const NON_COMPARISON_OPERATORS = [
'@>',
'?',
'<@',
'?|',
'?&'
];
const NO_RELATIONS = RelationExpression.create('[]');
/**
* Class representing an feathers adapter for Objection.js ORM.
* @param {object} options
* @param {string} [options.id='id'] - database id field
* @param {string} [options.idSeparator=','] - id field primary keys separator char
* @param {object} options.model - an Objection model
* @param {object} options.paginate
* @param {object} options.events
* @param {string} options.allowedEager - Objection eager loading string.
*/
class Service extends AdapterService {
constructor (options) {
if (!options.model) {
throw new errors.GeneralError('You must provide an Objection Model');
}