Skip to content

Commit a389faf

Browse files
author
Vincent Molinié
committed
chore: fix operators
1 parent a9e841b commit a389faf

File tree

8 files changed

+44
-41
lines changed

8 files changed

+44
-41
lines changed

src/services/composite-keys-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function CompositeKeysManager(model, schema, record) {
1717
};
1818

1919
this.getRecordsConditions = function getRecordsConditions(recordsIds, options) {
20-
const { OR } = new Operators(options);
20+
const { OR } = Operators.getInstance(options);
2121
return { [OR]: recordsIds.map((recordId) => this.getRecordConditions(recordId)) };
2222
};
2323

src/services/filters-parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { NoMatchingOperatorError } from './errors';
55
const { getReferenceSchema, getReferenceField } = require('../utils/query');
66

77
function FiltersParser(modelSchema, timezone, options) {
8-
this.OPERATORS = new Operators(options);
8+
this.OPERATORS = Operators.getInstance(options);
99
this.operatorDateParser = new BaseOperatorDateParser({ operators: this.OPERATORS, timezone });
1010

1111
this.perform = async (filtersString) =>

src/services/has-many-dissociator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const orm = require('../utils/orm');
44
const { ErrorHTTP422 } = require('./errors');
55

66
function HasManyDissociator(model, association, options, params, data) {
7-
const OPERATORS = new Operators(options);
7+
const OPERATORS = Operators.getInstance(options);
88
const isDelete = Boolean(params.delete);
99

1010
this.perform = () => {

src/services/resources-getter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function ResourcesGetter(model, options, params) {
1515
const queryBuilder = new QueryBuilder(model, options, params);
1616
let segmentScope;
1717
let segmentWhere;
18-
const OPERATORS = new Operators(options);
18+
const OPERATORS = Operators.getInstance(options);
1919
const primaryKey = _.keys(model.primaryKeys)[0];
2020
const filterParser = new FiltersParser(schema, params.timezone, options);
2121
let fieldNamesRequested;

src/services/search-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
1313
let associations = _.clone(model.associations);
1414
const hasSearchFields = schema.searchFields && _.isArray(schema.searchFields);
1515
let searchAssociationFields;
16-
const OPERATORS = new Operators(opts);
16+
const OPERATORS = Operators.getInstance(opts);
1717
const fieldsSearched = [];
1818
let hasExtendedConditions = false;
1919

src/services/value-stat-getter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import FiltersParser from './filters-parser';
55
import Orm from '../utils/orm';
66

77
function ValueStatGetter(model, params, options) {
8-
const OPERATORS = new Operators(options);
8+
const OPERATORS = Operators.getInstance(options);
99

1010
this.operatorDateParser = new BaseOperatorDateParser({
1111
operators: OPERATORS,

src/utils/operators.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
let instance = null;
1+
class Operators {
2+
static _instance = null;
23

3-
function Operators(options) {
4-
if (instance) return instance;
4+
constructor(options) {
5+
if (options && options.sequelize && options.sequelize.Op) {
6+
const { Op } = options.sequelize;
7+
this.AND = Op.and;
8+
this.CONTAINS = Op.contains;
9+
this.EQ = Op.eq;
10+
this.GT = Op.gt;
11+
this.GTE = Op.gte;
12+
this.IN = Op.in;
13+
this.LIKE = Op.like;
14+
this.LT = Op.lt;
15+
this.LTE = Op.lte;
16+
this.NE = Op.ne;
17+
this.NOT = Op.not;
18+
this.NOT_LIKE = Op.notLike;
19+
this.OR = Op.or;
20+
} else {
21+
this.AND = '$and';
22+
this.CONTAINS = '$contains';
23+
this.EQ = '$eq';
24+
this.GT = '$gt';
25+
this.GTE = '$gte';
26+
this.IN = '$in';
27+
this.LIKE = '$like';
28+
this.LT = '$lt';
29+
this.LTE = '$lte';
30+
this.NE = '$ne';
31+
this.NOT = '$not';
32+
this.NOT_LIKE = '$notLike';
33+
this.OR = '$or';
34+
}
535

6-
if (options && options.Sequelize && options.Sequelize.Op) {
7-
const { Op } = options.Sequelize;
8-
this.AND = Op.and;
9-
this.CONTAINS = Op.contains;
10-
this.EQ = Op.eq;
11-
this.GT = Op.gt;
12-
this.GTE = Op.gte;
13-
this.IN = Op.in;
14-
this.LIKE = Op.like;
15-
this.LT = Op.lt;
16-
this.LTE = Op.lte;
17-
this.NE = Op.ne;
18-
this.NOT = Op.not;
19-
this.NOT_LIKE = Op.notLike;
20-
this.OR = Op.or;
21-
} else {
22-
this.AND = '$and';
23-
this.CONTAINS = '$contains';
24-
this.EQ = '$eq';
25-
this.GT = '$gt';
26-
this.GTE = '$gte';
27-
this.IN = '$in';
28-
this.LIKE = '$like';
29-
this.LT = '$lt';
30-
this.LTE = '$lte';
31-
this.NE = '$ne';
32-
this.NOT = '$not';
33-
this.NOT_LIKE = '$notLike';
34-
this.OR = '$or';
36+
Operators._instance = this;
3537
}
3638

37-
instance = this;
38-
return this;
39+
static getInstance(options) {
40+
return Operators._instance || new Operators(options);
41+
}
3942
}
4043

4144
module.exports = Operators;

test/services/filters-parser.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('services > filters-parser', () => {
1212
sequelize: Sequelize,
1313
};
1414
const timezone = 'Europe/Paris';
15-
const OPERATORS = new Operators(sequelizeOptions);
15+
const OPERATORS = Operators.getInstance(sequelizeOptions);
1616
const defaultFiltersParser = new FiltersParser(schema, timezone, sequelizeOptions);
1717

1818
const getExpectedCondition = (field, conditions) => {

0 commit comments

Comments
 (0)