Skip to content

Commit d91ac72

Browse files
refactor: migrate project to es6 class syntax (#581)
1 parent e881850 commit d91ac72

17 files changed

+452
-279
lines changed

src/services/belongs-to-updater.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1+
class BelongsToUpdater {
2+
constructor(model, association, opts, params, data) {
3+
this._model = model;
4+
this._params = params;
5+
this._data = data;
6+
}
17

2-
function BelongsToUpdater(model, association, opts, params, data) {
3-
this.perform = async () => {
8+
async perform() {
49
const updateParams = {};
5-
updateParams[params.associationName] = data.data ? data.data.id : null;
10+
updateParams[this._params.associationName] = this._data.data ? this._data.data.id : null;
611

7-
return model
8-
.findByIdAndUpdate(params.recordId, {
12+
return this._model
13+
.findByIdAndUpdate(this._params.recordId, {
914
$set: updateParams,
1015
}, {
1116
new: true,
1217
})
1318
.lean()
1419
.exec();
15-
};
20+
}
1621
}
1722

1823
module.exports = BelongsToUpdater;
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
function EmbeddedDocumentUpdater(model, params, association, record) {
2-
this.perform = () => {
3-
const { recordId } = params;
4-
const recordIndex = parseInt(params.recordIndex, 10);
1+
class EmbeddedDocumentUpdater {
2+
constructor(model, params, association, record) {
3+
this._model = model;
4+
this._params = params;
5+
this._association = association;
6+
this._record = record;
7+
}
58

6-
delete record._id;
9+
async perform() {
10+
const { recordId } = this._params;
11+
const recordIndex = parseInt(this._params.recordIndex, 10);
712

8-
const update = Object.keys(record).reduce((acc, value) => {
9-
acc.$set[`${association}.${recordIndex}.${value}`] = record[value];
13+
delete this._record._id;
14+
15+
const update = Object.keys(this._record).reduce((acc, value) => {
16+
acc.$set[`${this._association}.${recordIndex}.${value}`] = this._record[value];
1017
return acc;
1118
}, { $set: {} });
1219

13-
return model.findByIdAndUpdate(recordId, update);
14-
};
20+
return this._model.findByIdAndUpdate(recordId, update);
21+
}
1522
}
1623

1724
module.exports = EmbeddedDocumentUpdater;
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
class HasManyAssociator {
2+
constructor(model, association, opts, params, data) {
3+
this._model = model;
4+
this._params = params;
5+
this._data = data;
6+
}
17

2-
function HasManyAssociator(model, association, opts, params, data) {
3-
this.perform = () => {
8+
perform() {
49
const updateParams = {};
5-
updateParams[params.associationName] = {
6-
$each: data.data.map((document) => document.id),
10+
updateParams[this._params.associationName] = {
11+
$each: this._data.data.map((document) => document.id),
712
};
813

9-
return model
10-
.findByIdAndUpdate(params.recordId, {
14+
return this._model
15+
.findByIdAndUpdate(this._params.recordId, {
1116
$push: updateParams,
1217
}, {
1318
new: true,
1419
})
1520
.lean()
1621
.exec();
17-
};
22+
}
1823
}
1924

2025
module.exports = HasManyAssociator;
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11

2-
function HasManyDissociator(model, association, opts, params, data) {
3-
const isDelete = Boolean(params.delete);
2+
class HasManyDissociator {
3+
constructor(model, association, opts, params, data) {
4+
this._model = model;
5+
this._association = association;
6+
this._params = params;
7+
this._data = data;
8+
}
49

5-
this.perform = async () => {
6-
const documentIds = data.data.map((document) => document.id);
10+
async perform() {
11+
const isDelete = Boolean(this._params.delete);
12+
const documentIds = this._data.data.map((document) => document.id);
713
if (isDelete) {
8-
await association.deleteMany({ _id: { $in: documentIds } });
14+
await this._association.deleteMany({ _id: { $in: documentIds } });
915
}
1016

1117
const updateParams = {};
12-
updateParams[params.associationName] = { $in: documentIds };
18+
updateParams[this._params.associationName] = { $in: documentIds };
1319

14-
return model
15-
.findByIdAndUpdate(params.recordId, {
20+
return this._model
21+
.findByIdAndUpdate(this._params.recordId, {
1622
$pull: updateParams,
1723
}, {
1824
new: true,
1925
})
2026
.lean()
2127
.exec();
22-
};
28+
}
2329
}
2430

2531
module.exports = HasManyDissociator;

src/services/has-many-getter.js

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,46 @@ const SearchBuilder = require('./search-builder');
44
const utils = require('../utils/schema');
55
const FiltersParser = require('./filters-parser');
66

7-
function HasManyGetter(parentModel, childModel, opts, params) {
8-
const OBJECTID_REGEXP = /^[0-9a-fA-F]{24}$/;
9-
const schema = Interface.Schemas.schemas[utils.getModelName(childModel)];
10-
const searchBuilder = new SearchBuilder(childModel, opts, params);
11-
const filtersParser = new FiltersParser(childModel, params.timezone, opts);
12-
13-
function hasPagination() {
14-
return params.page && params.page.number;
7+
const OBJECTID_REGEXP = /^[0-9a-fA-F]{24}$/;
8+
9+
class HasManyGetter {
10+
constructor(parentModel, childModel, opts, params) {
11+
this._parentModel = parentModel;
12+
this._childModel = childModel;
13+
this._params = params;
14+
this._opts = opts;
15+
this._searchBuilder = new SearchBuilder(childModel, opts, params);
1516
}
1617

17-
function getLimit() {
18-
if (hasPagination()) {
19-
return parseInt(params.page.number, 10) * params.page.size;
18+
_hasPagination() {
19+
return this._params.page && this._params.page.number;
20+
}
21+
22+
_getLimit() {
23+
if (this._hasPagination()) {
24+
return parseInt(this._params.page.number, 10) * this._params.page.size;
2025
}
2126
return 5;
2227
}
2328

24-
function getSkip() {
25-
if (hasPagination()) {
26-
return (parseInt(params.page.number, 10) - 1) * params.page.size;
29+
_getSkip() {
30+
if (this._hasPagination()) {
31+
return (parseInt(this._params.page.number, 10) - 1) * this._params.page.size;
2732
}
2833
return 0;
2934
}
3035

31-
function getProjection() {
36+
_getProjection() {
3237
const projection = {};
33-
projection[params.associationName] = 1;
38+
projection[this._params.associationName] = 1;
3439
projection._id = 0; // eslint-disable-line
3540

3641
return projection;
3742
}
3843

39-
function handlePopulate(query) {
44+
_handlePopulate(query) {
45+
const schema = Interface.Schemas.schemas[utils.getModelName(this._childModel)];
46+
4047
_.each(schema.fields, (field) => {
4148
if (field.reference) {
4249
query.populate({
@@ -46,55 +53,55 @@ function HasManyGetter(parentModel, childModel, opts, params) {
4653
});
4754
}
4855

49-
async function buildConditions(recordIds) {
56+
async _buildConditions(recordIds) {
5057
const conditions = {
5158
$and: [{ _id: { $in: recordIds } }],
5259
};
5360

54-
if (params.search) {
55-
const conditionsSearch = await searchBuilder.getConditions();
61+
if (this._params.search) {
62+
const conditionsSearch = await this._searchBuilder.getConditions();
5663
conditions.$and.push(conditionsSearch);
5764
}
5865

59-
if (params.filters) {
60-
const newFilters = await filtersParser.replaceAllReferences(params.filters);
66+
if (this._params.filters) {
67+
const filtersParser = new FiltersParser(this._childModel, this._params.timezone, this._opts);
68+
const newFilters = await filtersParser.replaceAllReferences(this._params.filters);
6169
const newFiltersString = JSON.stringify(newFilters);
6270
conditions.$and.push(await filtersParser.perform(newFiltersString));
6371
}
6472

6573
return conditions;
6674
}
6775

68-
async function getRecordsAndRecordIds() {
69-
let id = params.recordId;
70-
if (OBJECTID_REGEXP.test(params.recordId)) {
71-
id = opts.Mongoose.Types.ObjectId(id);
76+
async _getRecordsAndRecordIds() {
77+
let id = this._params.recordId;
78+
if (OBJECTID_REGEXP.test(this._params.recordId)) {
79+
id = this._opts.Mongoose.Types.ObjectId(id);
7280
}
7381

74-
const parentRecords = await parentModel
82+
const parentRecords = await this._parentModel
7583
.aggregate()
7684
.match({ _id: id })
77-
.unwind(params.associationName)
78-
.project(getProjection())
85+
.unwind(this._params.associationName)
86+
.project(this._getProjection())
7987
.exec();
8088

81-
const childRecordIds = _.map(parentRecords, (record) => record[params.associationName]);
82-
const conditions = await buildConditions(childRecordIds);
83-
const query = childModel.find(conditions);
84-
handlePopulate(query);
89+
const childRecordIds = _.map(parentRecords, (record) => record[this._params.associationName]);
90+
const conditions = await this._buildConditions(childRecordIds);
91+
const query = this._childModel.find(conditions);
92+
this._handlePopulate(query);
8593

8694
const childRecords = await query;
8795
return [childRecords, childRecordIds];
8896
}
8997

90-
this.perform = async () => {
91-
const [childRecords, childRecordIds] = await getRecordsAndRecordIds();
92-
93-
let fieldSort = params.sort;
98+
async perform() {
99+
const [childRecords, childRecordIds] = await this._getRecordsAndRecordIds();
100+
let fieldSort = this._params.sort;
94101
let descending = false;
95102

96-
if (params.sort && (params.sort[0] === '-')) {
97-
fieldSort = params.sort.substring(1);
103+
if (this._params.sort && (this._params.sort[0] === '-')) {
104+
fieldSort = this._params.sort.substring(1);
98105
descending = true;
99106
}
100107

@@ -111,19 +118,21 @@ function HasManyGetter(parentModel, childModel, opts, params) {
111118
let sortedChildRecords = descending ? recordsSorted.reverse() : recordsSorted;
112119
let fieldsSearched = null;
113120

114-
if (params.search) {
115-
fieldsSearched = searchBuilder.getFieldsSearched();
121+
if (this._params.search) {
122+
fieldsSearched = this._searchBuilder.getFieldsSearched();
116123
}
117124

118-
sortedChildRecords = _.slice(sortedChildRecords, getSkip(), getSkip() + getLimit());
125+
sortedChildRecords = _.slice(
126+
sortedChildRecords, this._getSkip(), this._getSkip() + this._getLimit(),
127+
);
119128

120129
return [sortedChildRecords, fieldsSearched];
121-
};
130+
}
122131

123-
this.count = async () => {
124-
const recordsAndRecordIds = await getRecordsAndRecordIds();
132+
async count() {
133+
const recordsAndRecordIds = await this._getRecordsAndRecordIds();
125134
return recordsAndRecordIds[0].length;
126-
};
135+
}
127136
}
128137

129138
module.exports = HasManyGetter;

0 commit comments

Comments
 (0)