@common-stack/store-mongo 0.5.21 → 0.5.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dataloaders/bulk-dataloader.cjs +19 -0
- package/lib/dataloaders/bulk-dataloader.cjs.map +1 -0
- package/lib/dataloaders/bulk-dataloader.mjs +19 -0
- package/lib/dataloaders/bulk-dataloader.mjs.map +1 -0
- package/lib/helpers/mongoose-connection.cjs +18 -0
- package/lib/helpers/mongoose-connection.cjs.map +1 -0
- package/lib/helpers/mongoose-connection.mjs +18 -0
- package/lib/helpers/mongoose-connection.mjs.map +1 -0
- package/lib/index.cjs +1 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.mjs +1 -0
- package/lib/index.mjs.map +1 -0
- package/lib/interfaces/base-repository.cjs +5 -0
- package/lib/interfaces/base-repository.cjs.map +1 -0
- package/lib/interfaces/base-repository.mjs +5 -0
- package/lib/interfaces/base-repository.mjs.map +1 -0
- package/lib/interfaces/generated-models.cjs +29 -0
- package/lib/interfaces/generated-models.cjs.map +1 -0
- package/lib/interfaces/generated-models.mjs +29 -0
- package/lib/interfaces/generated-models.mjs.map +1 -0
- package/lib/interfaces/mongoose-settings.d.ts +1 -5
- package/lib/mixins/base-service-mixin.cjs +123 -0
- package/lib/mixins/base-service-mixin.cjs.map +1 -0
- package/lib/mixins/base-service-mixin.mjs +123 -0
- package/lib/mixins/base-service-mixin.mjs.map +1 -0
- package/lib/services/base-proxy-service.cjs +44 -0
- package/lib/services/base-proxy-service.cjs.map +1 -0
- package/lib/services/base-proxy-service.mjs +44 -0
- package/lib/services/base-proxy-service.mjs.map +1 -0
- package/lib/services/base-service.cjs +60 -0
- package/lib/services/base-service.cjs.map +1 -0
- package/lib/services/base-service.mjs +60 -0
- package/lib/services/base-service.mjs.map +1 -0
- package/lib/store/models/common-options.cjs +20 -0
- package/lib/store/models/common-options.cjs.map +1 -0
- package/lib/store/models/common-options.mjs +20 -0
- package/lib/store/models/common-options.mjs.map +1 -0
- package/lib/store/repositories/base-repository.cjs +224 -0
- package/lib/store/repositories/base-repository.cjs.map +1 -0
- package/lib/store/repositories/base-repository.mjs +224 -0
- package/lib/store/repositories/base-repository.mjs.map +1 -0
- package/package.json +8 -4
- package/lib/index.js +0 -1124
- package/lib/index.js.map +0 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';var tslib=require('tslib'),inversify=require('inversify');exports.BaseService = class BaseService {
|
|
2
|
+
constructor(repository) {
|
|
3
|
+
this.repository = repository;
|
|
4
|
+
this.repository = repository;
|
|
5
|
+
}
|
|
6
|
+
async getAllWithCount(options) {
|
|
7
|
+
const totalCount = await this.count(options.criteria);
|
|
8
|
+
const data = await this.getAll(options);
|
|
9
|
+
return {
|
|
10
|
+
totalCount,
|
|
11
|
+
data,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
count(conditions) {
|
|
15
|
+
return this.repository.count(conditions);
|
|
16
|
+
}
|
|
17
|
+
get(conditions) {
|
|
18
|
+
if (typeof conditions === 'string') {
|
|
19
|
+
return this.repository.get({ id: conditions });
|
|
20
|
+
}
|
|
21
|
+
return this.repository.get(conditions);
|
|
22
|
+
}
|
|
23
|
+
getName(name) {
|
|
24
|
+
return this.repository.get({ name });
|
|
25
|
+
}
|
|
26
|
+
getAll(options) {
|
|
27
|
+
return this.repository.getAll(options);
|
|
28
|
+
}
|
|
29
|
+
getByIds(ids) {
|
|
30
|
+
return this.repository.bulkGet(ids);
|
|
31
|
+
}
|
|
32
|
+
create(data) {
|
|
33
|
+
return this.repository.create(data);
|
|
34
|
+
}
|
|
35
|
+
bulkCreate(data) {
|
|
36
|
+
return this.repository.bulkCreate(data);
|
|
37
|
+
}
|
|
38
|
+
async update(id, data, overwrite = false) {
|
|
39
|
+
return this.repository.update({ id }, data, { overwrite });
|
|
40
|
+
}
|
|
41
|
+
insert(data, overwrite = true) {
|
|
42
|
+
const { id } = data, rest = tslib.__rest(data, ["id"]);
|
|
43
|
+
if (id) {
|
|
44
|
+
const existing = this.repository.get({ id });
|
|
45
|
+
if (existing) {
|
|
46
|
+
return this.update(id, rest, overwrite);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return this.create(rest);
|
|
50
|
+
}
|
|
51
|
+
delete(id) {
|
|
52
|
+
if (typeof id === 'string') {
|
|
53
|
+
return this.repository.delete({ id });
|
|
54
|
+
}
|
|
55
|
+
return this.repository.delete(id);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.BaseService = tslib.__decorate([
|
|
59
|
+
inversify.injectable()
|
|
60
|
+
], exports.BaseService);//# sourceMappingURL=base-service.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-service.cjs","sources":["../../src/services/base-service.ts"],"sourcesContent":[null],"names":["BaseService","__rest","__decorate","injectable"],"mappings":"uEAKaA,mBAAW,GAAjB,MAAM,WAAW,CAAA;AACpB,IAAA,WAAA,CAAsB,UAA8B,EAAA;QAA9B,IAAU,CAAA,UAAA,GAAV,UAAU,CAAoB;AAChD,QAAA,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;KAChC;IAED,MAAM,eAAe,CAAC,OAAgC,EAAA;QAClD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACxC,OAAO;YACH,UAAU;YACV,IAAI;SACP,CAAC;KACL;AAED,IAAA,KAAK,CAAC,UAAqC,EAAA;QACvC,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;KAC5C;AAED,IAAA,GAAG,CAAC,UAA6C,EAAA;AAC7C,QAAA,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;AAChC,YAAA,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,UAA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AE,EAAE,IAAS,EAAE,SAAS,CAAC,CAAC;AAChD,aAAA;AACJ,SAAA;AACD,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,IAAS,CAAC,CAAC;KACjC;AAED,IAAA,MAAM,CAAC,EAAqC,EAAA;AACxC,QAAA,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE;YACxB,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AACzC,SAAA;QACD,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;KACrC;EACJ;AAlEYD,mBAAW,GAAAE,gBAAA,CAAA;AADvB,IAAAC,oBAAU,EAAE;AACA,CAAA,EAAAH,mBAAW,CAkEvB"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {__decorate,__rest}from'tslib';import {injectable}from'inversify';let BaseService = class BaseService {
|
|
2
|
+
constructor(repository) {
|
|
3
|
+
this.repository = repository;
|
|
4
|
+
this.repository = repository;
|
|
5
|
+
}
|
|
6
|
+
async getAllWithCount(options) {
|
|
7
|
+
const totalCount = await this.count(options.criteria);
|
|
8
|
+
const data = await this.getAll(options);
|
|
9
|
+
return {
|
|
10
|
+
totalCount,
|
|
11
|
+
data,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
count(conditions) {
|
|
15
|
+
return this.repository.count(conditions);
|
|
16
|
+
}
|
|
17
|
+
get(conditions) {
|
|
18
|
+
if (typeof conditions === 'string') {
|
|
19
|
+
return this.repository.get({ id: conditions });
|
|
20
|
+
}
|
|
21
|
+
return this.repository.get(conditions);
|
|
22
|
+
}
|
|
23
|
+
getName(name) {
|
|
24
|
+
return this.repository.get({ name });
|
|
25
|
+
}
|
|
26
|
+
getAll(options) {
|
|
27
|
+
return this.repository.getAll(options);
|
|
28
|
+
}
|
|
29
|
+
getByIds(ids) {
|
|
30
|
+
return this.repository.bulkGet(ids);
|
|
31
|
+
}
|
|
32
|
+
create(data) {
|
|
33
|
+
return this.repository.create(data);
|
|
34
|
+
}
|
|
35
|
+
bulkCreate(data) {
|
|
36
|
+
return this.repository.bulkCreate(data);
|
|
37
|
+
}
|
|
38
|
+
async update(id, data, overwrite = false) {
|
|
39
|
+
return this.repository.update({ id }, data, { overwrite });
|
|
40
|
+
}
|
|
41
|
+
insert(data, overwrite = true) {
|
|
42
|
+
const { id } = data, rest = __rest(data, ["id"]);
|
|
43
|
+
if (id) {
|
|
44
|
+
const existing = this.repository.get({ id });
|
|
45
|
+
if (existing) {
|
|
46
|
+
return this.update(id, rest, overwrite);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return this.create(rest);
|
|
50
|
+
}
|
|
51
|
+
delete(id) {
|
|
52
|
+
if (typeof id === 'string') {
|
|
53
|
+
return this.repository.delete({ id });
|
|
54
|
+
}
|
|
55
|
+
return this.repository.delete(id);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
BaseService = __decorate([
|
|
59
|
+
injectable()
|
|
60
|
+
], BaseService);export{BaseService};//# sourceMappingURL=base-service.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-service.mjs","sources":["../../src/services/base-service.ts"],"sourcesContent":[null],"names":[],"mappings":"yEAKa,IAAA,WAAW,GAAjB,MAAM,WAAW,CAAA;AACpB,IAAA,WAAA,CAAsB,UAA8B,EAAA;QAA9B,IAAU,CAAA,UAAA,GAAV,UAAU,CAAoB;AAChD,QAAA,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;KAChC;IAED,MAAM,eAAe,CAAC,OAAgC,EAAA;QAClD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACxC,OAAO;YACH,UAAU;YACV,IAAI;SACP,CAAC;KACL;AAED,IAAA,KAAK,CAAC,U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AK,IAAI,EAAtB,CAAe,IAAA,CAAA,CAAO,CAAC;AAC7B,QAAA,IAAI,EAAE,EAAE;AACJ,YAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AAC7C,YAAA,IAAI,QAAQ,EAAE;gBACV,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,IAAS,EAAE,SAAS,CAAC,CAAC;AAChD,aAAA;AACJ,SAAA;AACD,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,IAAS,CAAC,CAAC;KACjC;AAED,IAAA,MAAM,CAAC,EAAqC,EAAA;AACxC,QAAA,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE;YACxB,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AACzC,SAAA;QACD,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;KACrC;EACJ;AAlEY,WAAW,GAAA,UAAA,CAAA;AADvB,IAAA,UAAU,EAAE;AACA,CAAA,EAAA,WAAW,CAkEvB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';const commonModeSchemaOptions = {
|
|
2
|
+
timestamps: true,
|
|
3
|
+
toJSON: {
|
|
4
|
+
virtuals: true,
|
|
5
|
+
},
|
|
6
|
+
toObject: {
|
|
7
|
+
virtuals: true,
|
|
8
|
+
getters: true,
|
|
9
|
+
transform(doc, ret) {
|
|
10
|
+
delete ret.__v;
|
|
11
|
+
delete ret._id;
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
const addIdVirtualFields = (schema) => {
|
|
16
|
+
schema.set('toJSON', {
|
|
17
|
+
virtuals: true,
|
|
18
|
+
});
|
|
19
|
+
return schema;
|
|
20
|
+
};exports.addIdVirtualFields=addIdVirtualFields;exports.commonModeSchemaOptions=commonModeSchemaOptions;//# sourceMappingURL=common-options.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-options.cjs","sources":["../../../src/store/models/common-options.ts"],"sourcesContent":[null],"names":[],"mappings":"aAEa,MAAA,uBAAuB,GAAG;AACnC,IAAA,UAAU,EAAE,IAAI;AAChB,IAAA,MAAM,EAAE;AACJ,QAAA,QAAQ,EAAE,IAAI;AACjB,KAAA;AACD,IAAA,QAAQ,EAAE;AACN,QAAA,QAAQ,EAAE,IAAI;AACd,QAAA,OAAO,EAAE,IAAI;QACb,SAAS,CAAC,GAAG,EAAE,GAAG,EAAA;YACd,OAAO,GAAG,CAAC,GAAG,CAAC;YACf,OAAO,GAAG,CAAC,GAAG,CAAC;SAClB;AACJ,KAAA;EACH;AAEW,MAAA,kBAAkB,GAAG,CAAC,MAAc,KAAI;AACjD,IAAA,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE;AACjB,QAAA,QAAQ,EAAE,IAAI;AACjB,KAAA,CAAC,CAAC;AACH,IAAA,OAAO,MAAM,CAAC;AAClB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const commonModeSchemaOptions = {
|
|
2
|
+
timestamps: true,
|
|
3
|
+
toJSON: {
|
|
4
|
+
virtuals: true,
|
|
5
|
+
},
|
|
6
|
+
toObject: {
|
|
7
|
+
virtuals: true,
|
|
8
|
+
getters: true,
|
|
9
|
+
transform(doc, ret) {
|
|
10
|
+
delete ret.__v;
|
|
11
|
+
delete ret._id;
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
const addIdVirtualFields = (schema) => {
|
|
16
|
+
schema.set('toJSON', {
|
|
17
|
+
virtuals: true,
|
|
18
|
+
});
|
|
19
|
+
return schema;
|
|
20
|
+
};export{addIdVirtualFields,commonModeSchemaOptions};//# sourceMappingURL=common-options.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-options.mjs","sources":["../../../src/store/models/common-options.ts"],"sourcesContent":[null],"names":[],"mappings":"AAEa,MAAA,uBAAuB,GAAG;AACnC,IAAA,UAAU,EAAE,IAAI;AAChB,IAAA,MAAM,EAAE;AACJ,QAAA,QAAQ,EAAE,IAAI;AACjB,KAAA;AACD,IAAA,QAAQ,EAAE;AACN,QAAA,QAAQ,EAAE,IAAI;AACd,QAAA,OAAO,EAAE,IAAI;QACb,SAAS,CAAC,GAAG,EAAE,GAAG,EAAA;YACd,OAAO,GAAG,CAAC,GAAG,CAAC;YACf,OAAO,GAAG,CAAC,GAAG,CAAC;SAClB;AACJ,KAAA;EACH;AAEW,MAAA,kBAAkB,GAAG,CAAC,MAAc,KAAI;AACjD,IAAA,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE;AACjB,QAAA,QAAQ,EAAE,IAAI;AACjB,KAAA,CAAC,CAAC;AACH,IAAA,OAAO,MAAM,CAAC;AAClB"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
'use strict';var tslib=require('tslib'),lodash=require('lodash'),mongoose=require('mongoose'),inversify=require('inversify'),baseRepository=require('../../interfaces/base-repository.cjs');require('../../interfaces/generated-models.cjs');var BaseRepository_1;
|
|
2
|
+
exports.BaseRepository = BaseRepository_1 = class BaseRepository {
|
|
3
|
+
constructor(modelFunc, db, logger, options) {
|
|
4
|
+
this.modelFunc = modelFunc;
|
|
5
|
+
this.model = modelFunc(db);
|
|
6
|
+
this.options = options;
|
|
7
|
+
this.logger = logger.child({ className: BaseRepository_1.name });
|
|
8
|
+
}
|
|
9
|
+
computeSort(sort) {
|
|
10
|
+
if (lodash.isObject(sort)) {
|
|
11
|
+
return { [sort === null || sort === void 0 ? void 0 : sort.key]: sort.value.toLowerCase() === 'asc' ? 1 : -1 };
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
preparePipeLine(options) {
|
|
16
|
+
const { criteria, selectedFields, sort, limit, skip } = options;
|
|
17
|
+
// map id to mongoose _id
|
|
18
|
+
const mappedCriteria = Object.entries(criteria || {}).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [key]: mongoose.Types.ObjectId.isValid(value) ? new mongoose.Types.ObjectId(value) : value })), { id: undefined });
|
|
19
|
+
const { id } = mappedCriteria, rest = tslib.__rest(mappedCriteria, ["id"]);
|
|
20
|
+
const projectedFields = selectedFields === null || selectedFields === void 0 ? void 0 : selectedFields.split(' ').reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: 1 })), {});
|
|
21
|
+
return [
|
|
22
|
+
{ $match: Object.assign({}, rest, id ? { _id: id } : {}) },
|
|
23
|
+
...(sort ? [{ $sort: this.computeSort(sort) }] : []),
|
|
24
|
+
...((selectedFields === null || selectedFields === void 0 ? void 0 : selectedFields.length) ? [{
|
|
25
|
+
$project: projectedFields
|
|
26
|
+
}] : []),
|
|
27
|
+
{ $skip: skip || baseRepository.PAGINATION_OPTIONS.skip },
|
|
28
|
+
{ $limit: limit || baseRepository.PAGINATION_OPTIONS.limit },
|
|
29
|
+
{ $addFields: { id: '$_id' } },
|
|
30
|
+
{ $project: { _id: 0 } },
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
// public async getAll(options: GetAllArgs<D>): Promise<T[]> {
|
|
34
|
+
// try {
|
|
35
|
+
// return this.model.aggregate(this.preparePipeLine(options));
|
|
36
|
+
// } catch (e) {
|
|
37
|
+
// this.logger.error(`Unable to retrieve Model with options ${JSON.stringify(options)}`);
|
|
38
|
+
// throw e;
|
|
39
|
+
// }
|
|
40
|
+
// }
|
|
41
|
+
async getAll(options) {
|
|
42
|
+
try {
|
|
43
|
+
const { criteria, selectedFields, sort, limit, skip } = options;
|
|
44
|
+
// map id to mongoose _id
|
|
45
|
+
const _a = criteria || { id: undefined }, { id } = _a, rest = tslib.__rest(_a, ["id"]);
|
|
46
|
+
const sortBy = lodash.isObject(sort) ? { [sort === null || sort === void 0 ? void 0 : sort.key]: sort.value } : { createdAt: 1 };
|
|
47
|
+
const response = await this.model
|
|
48
|
+
.find(Object.assign(Object.assign({}, rest), (id ? { _id: id } : {})), selectedFields)
|
|
49
|
+
.sort(sortBy)
|
|
50
|
+
.limit(limit || baseRepository.PAGINATION_OPTIONS.limit)
|
|
51
|
+
.skip(skip || baseRepository.PAGINATION_OPTIONS.skip)
|
|
52
|
+
.exec();
|
|
53
|
+
return response.map((i) => i === null || i === void 0 ? void 0 : i.toObject());
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(options)}`);
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async getAllWithCount(options) {
|
|
61
|
+
const data = await this.getAll(options);
|
|
62
|
+
const totalCount = await this.count(options.criteria);
|
|
63
|
+
return { totalCount, data };
|
|
64
|
+
}
|
|
65
|
+
// public async getAllWithCount(options: GetAllArgs<D>): Promise<{ data: T[], totalCount: number }> {
|
|
66
|
+
// try {
|
|
67
|
+
// const pipeline: PipelineStage[] = this.preparePipeLine(options);
|
|
68
|
+
// pipeline.push({
|
|
69
|
+
// $facet: {
|
|
70
|
+
// data: [{
|
|
71
|
+
// $group: {
|
|
72
|
+
// _id: null,
|
|
73
|
+
// items: { $push: '$$ROOT' }
|
|
74
|
+
// }
|
|
75
|
+
// }],
|
|
76
|
+
// count: [{ $count: 'count' }],
|
|
77
|
+
// },
|
|
78
|
+
// })
|
|
79
|
+
// const result = await this.model.aggregate(pipeline);
|
|
80
|
+
// const data = result[0]?.data[0]?.items || [];
|
|
81
|
+
// const totalCount = result[0]?.count[0]?.count || 0;
|
|
82
|
+
// return { data, totalCount };
|
|
83
|
+
// } catch (e) {
|
|
84
|
+
// this.logger.error(`Unable to retrieve Model with options ${JSON.stringify(options)}`);
|
|
85
|
+
// throw e;
|
|
86
|
+
// }
|
|
87
|
+
// }
|
|
88
|
+
// eslint-disable-next-line class-methods-use-this
|
|
89
|
+
mapConditions(conditions) {
|
|
90
|
+
const { id: _id } = conditions, remaining = tslib.__rest(conditions, ["id"]);
|
|
91
|
+
return Object.assign(Object.assign({}, (_id ? { _id } : {})), remaining);
|
|
92
|
+
}
|
|
93
|
+
async count(conditions) {
|
|
94
|
+
return this.model.count(conditions).exec();
|
|
95
|
+
}
|
|
96
|
+
async get(conditions, selectedFields) {
|
|
97
|
+
try {
|
|
98
|
+
const response = await this.model.findOne(this.mapConditions(conditions), selectedFields).exec();
|
|
99
|
+
return response === null || response === void 0 ? void 0 : response.toObject();
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(conditions)}`);
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async bulkGet(ids) {
|
|
107
|
+
try {
|
|
108
|
+
const results = await this.model.find().setOptions({ batchSize: 100 }).where('_id').in(ids).exec();
|
|
109
|
+
return results.map((i) => i.toObject());
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(ids)}`);
|
|
113
|
+
throw e;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async find(conditions, selectedFields) {
|
|
117
|
+
try {
|
|
118
|
+
const response = await this.model.findOne(conditions, selectedFields).exec();
|
|
119
|
+
return response === null || response === void 0 ? void 0 : response.toObject();
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(conditions)}`);
|
|
123
|
+
throw e;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async create(data) {
|
|
127
|
+
try {
|
|
128
|
+
const response = await this.model.create(data);
|
|
129
|
+
return response.toObject();
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
this.logger.error(`Unable to create Model with data ${JSON.stringify(data)} due to ${e === null || e === void 0 ? void 0 : e.message}`);
|
|
133
|
+
throw e;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async bulkCreate(data) {
|
|
137
|
+
try {
|
|
138
|
+
const response = await this.model.insertMany(data, {
|
|
139
|
+
lean: true,
|
|
140
|
+
ordered: true,
|
|
141
|
+
});
|
|
142
|
+
return response;
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
this.logger.error(`Unable to bulk create due to error`, e.message);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async upsert(conditions, update, options) {
|
|
149
|
+
return this.update(conditions, update, Object.assign({ upsert: true }, options));
|
|
150
|
+
}
|
|
151
|
+
async bulkUpdate(criteria, update, options) {
|
|
152
|
+
try {
|
|
153
|
+
const { id } = criteria, rest = tslib.__rest(criteria, ["id"]);
|
|
154
|
+
const processedCriteria = id ? Object.assign({ _id: id }, rest) : criteria;
|
|
155
|
+
const res = await this.model.updateMany(processedCriteria, update);
|
|
156
|
+
if (res) {
|
|
157
|
+
const response = await this.model
|
|
158
|
+
.find(processedCriteria)
|
|
159
|
+
.exec();
|
|
160
|
+
return response.map((i) => i === null || i === void 0 ? void 0 : i.toObject());
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.logger.error(`Unable to Bulk Update with criteria ${JSON.stringify(criteria)} and data ${JSON.stringify(update)}`);
|
|
164
|
+
throw new Error('Unable to do bulk update');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
this.logger.error(`Unable to Bulk Update with criteria ${JSON.stringify(criteria)} and data ${JSON.stringify(update)}`);
|
|
169
|
+
throw e;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async update(criteria, update, options) {
|
|
173
|
+
try {
|
|
174
|
+
const { id } = criteria, rest = tslib.__rest(criteria, ["id"]);
|
|
175
|
+
const processedCriteria = id ? Object.assign({ _id: id }, rest) : criteria;
|
|
176
|
+
const mergedOptions = Object.assign({ new: true, useFindAndModify: false }, options);
|
|
177
|
+
if (id) {
|
|
178
|
+
return (await this.model
|
|
179
|
+
.findByIdAndUpdate(id, update, mergedOptions)
|
|
180
|
+
.exec());
|
|
181
|
+
}
|
|
182
|
+
return (await this.model
|
|
183
|
+
.findOneAndUpdate(processedCriteria, update, mergedOptions)
|
|
184
|
+
.exec());
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
this.logger.error(`Unable to Update with criteria ${JSON.stringify(criteria)} and data ${JSON.stringify(update)}`);
|
|
188
|
+
throw e;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async delete(criteria) {
|
|
192
|
+
try {
|
|
193
|
+
let deleted;
|
|
194
|
+
if (criteria === null || criteria === void 0 ? void 0 : criteria.id) {
|
|
195
|
+
deleted = await this.model.findByIdAndDelete(criteria.id);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
deleted = await this.model.findOneAndDelete(this.mapConditions(criteria));
|
|
199
|
+
}
|
|
200
|
+
return !!deleted;
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
this.logger.error(`Unable to delete the model with criteria`, criteria);
|
|
204
|
+
throw e;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async bulkDelete(criteria) {
|
|
208
|
+
try {
|
|
209
|
+
const deleted = await this.model.deleteMany(this.mapConditions(criteria));
|
|
210
|
+
return (deleted === null || deleted === void 0 ? void 0 : deleted.deletedCount) || 0;
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
this.logger.error(`Unable to delete the model with criteria`, criteria);
|
|
214
|
+
throw e;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
exports.BaseRepository = BaseRepository_1 = tslib.__decorate([
|
|
219
|
+
inversify.injectable(),
|
|
220
|
+
tslib.__param(0, inversify.unmanaged()),
|
|
221
|
+
tslib.__param(1, inversify.unmanaged()),
|
|
222
|
+
tslib.__param(2, inversify.unmanaged()),
|
|
223
|
+
tslib.__param(3, inversify.unmanaged())
|
|
224
|
+
], exports.BaseRepository);//# sourceMappingURL=base-repository.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-repository.cjs","sources":["../../../src/store/repositories/base-repository.ts"],"sourcesContent":[null],"names":["BaseRepository","isObject","Types","__rest","PAGINATION_OPTIONS","__decorate","injectable","__param","unmanaged"],"mappings":";AAOaA,sBAAc,GAApB,gBAAA,GAAA,MAAM,cAAc,CAAA;AAOvB,IAAA,WAAA,CAEY,SAAuC,EAE3C,EAAc,EAEd,MAAqB,EAErB,OAAuB,EAAA;QANnB,IAAS,CAAA,SAAA,GAAT,SAAS,CAA8B;AAQ/C,QAAA,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,EAAE,CAAC,CAAC;AAC3B,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AACvB,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,gBAAc,CAAC,IAAI,EAAE,CAAC,CAAC;KAClE;AAGO,IAAA,WAAW,CAAC,IAA2B,EAAA;AAC3C,QAAA,IAAIC,eAAQ,CAAC,IAAI,CAAC,EAAE;AAChB,YAAA,OAAO,EAAE,CAAC,IAAI,KAAA,IAAA,IAAJ,IAAI,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAJ,IAAI,CAAE,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,KAAK,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAA;AACtE,SAAA;AACD,QAAA,OAAO,IAAI,CAAC;KACf;AAED,IAAA,eAAe,CAAC,OAAsB,EAAA;AAClC,QAAA,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;;AAEhE,QAAA,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE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H,sBAAc,GAAA,gBAAA,GAAAK,gBAAA,CAAA;AAD1B,IAAAC,oBAAU,EAAE;IASJC,aAAA,CAAA,CAAA,EAAAC,mBAAS,EAAE,CAAA;IAEXD,aAAA,CAAA,CAAA,EAAAC,mBAAS,EAAE,CAAA;IAEXD,aAAA,CAAA,CAAA,EAAAC,mBAAS,EAAE,CAAA;IAEXD,aAAA,CAAA,CAAA,EAAAC,mBAAS,EAAE,CAAA;AAdP,CAAA,EAAAR,sBAAc,CA4Q1B"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import {__decorate,__param,__rest}from'tslib';import {isObject}from'lodash';import {Types}from'mongoose';import {injectable,unmanaged}from'inversify';import {PAGINATION_OPTIONS}from'../../interfaces/base-repository.mjs';import'../../interfaces/generated-models.mjs';var BaseRepository_1;
|
|
2
|
+
let BaseRepository = BaseRepository_1 = class BaseRepository {
|
|
3
|
+
constructor(modelFunc, db, logger, options) {
|
|
4
|
+
this.modelFunc = modelFunc;
|
|
5
|
+
this.model = modelFunc(db);
|
|
6
|
+
this.options = options;
|
|
7
|
+
this.logger = logger.child({ className: BaseRepository_1.name });
|
|
8
|
+
}
|
|
9
|
+
computeSort(sort) {
|
|
10
|
+
if (isObject(sort)) {
|
|
11
|
+
return { [sort === null || sort === void 0 ? void 0 : sort.key]: sort.value.toLowerCase() === 'asc' ? 1 : -1 };
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
preparePipeLine(options) {
|
|
16
|
+
const { criteria, selectedFields, sort, limit, skip } = options;
|
|
17
|
+
// map id to mongoose _id
|
|
18
|
+
const mappedCriteria = Object.entries(criteria || {}).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [key]: Types.ObjectId.isValid(value) ? new Types.ObjectId(value) : value })), { id: undefined });
|
|
19
|
+
const { id } = mappedCriteria, rest = __rest(mappedCriteria, ["id"]);
|
|
20
|
+
const projectedFields = selectedFields === null || selectedFields === void 0 ? void 0 : selectedFields.split(' ').reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: 1 })), {});
|
|
21
|
+
return [
|
|
22
|
+
{ $match: Object.assign({}, rest, id ? { _id: id } : {}) },
|
|
23
|
+
...(sort ? [{ $sort: this.computeSort(sort) }] : []),
|
|
24
|
+
...((selectedFields === null || selectedFields === void 0 ? void 0 : selectedFields.length) ? [{
|
|
25
|
+
$project: projectedFields
|
|
26
|
+
}] : []),
|
|
27
|
+
{ $skip: skip || PAGINATION_OPTIONS.skip },
|
|
28
|
+
{ $limit: limit || PAGINATION_OPTIONS.limit },
|
|
29
|
+
{ $addFields: { id: '$_id' } },
|
|
30
|
+
{ $project: { _id: 0 } },
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
// public async getAll(options: GetAllArgs<D>): Promise<T[]> {
|
|
34
|
+
// try {
|
|
35
|
+
// return this.model.aggregate(this.preparePipeLine(options));
|
|
36
|
+
// } catch (e) {
|
|
37
|
+
// this.logger.error(`Unable to retrieve Model with options ${JSON.stringify(options)}`);
|
|
38
|
+
// throw e;
|
|
39
|
+
// }
|
|
40
|
+
// }
|
|
41
|
+
async getAll(options) {
|
|
42
|
+
try {
|
|
43
|
+
const { criteria, selectedFields, sort, limit, skip } = options;
|
|
44
|
+
// map id to mongoose _id
|
|
45
|
+
const _a = criteria || { id: undefined }, { id } = _a, rest = __rest(_a, ["id"]);
|
|
46
|
+
const sortBy = isObject(sort) ? { [sort === null || sort === void 0 ? void 0 : sort.key]: sort.value } : { createdAt: 1 };
|
|
47
|
+
const response = await this.model
|
|
48
|
+
.find(Object.assign(Object.assign({}, rest), (id ? { _id: id } : {})), selectedFields)
|
|
49
|
+
.sort(sortBy)
|
|
50
|
+
.limit(limit || PAGINATION_OPTIONS.limit)
|
|
51
|
+
.skip(skip || PAGINATION_OPTIONS.skip)
|
|
52
|
+
.exec();
|
|
53
|
+
return response.map((i) => i === null || i === void 0 ? void 0 : i.toObject());
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(options)}`);
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async getAllWithCount(options) {
|
|
61
|
+
const data = await this.getAll(options);
|
|
62
|
+
const totalCount = await this.count(options.criteria);
|
|
63
|
+
return { totalCount, data };
|
|
64
|
+
}
|
|
65
|
+
// public async getAllWithCount(options: GetAllArgs<D>): Promise<{ data: T[], totalCount: number }> {
|
|
66
|
+
// try {
|
|
67
|
+
// const pipeline: PipelineStage[] = this.preparePipeLine(options);
|
|
68
|
+
// pipeline.push({
|
|
69
|
+
// $facet: {
|
|
70
|
+
// data: [{
|
|
71
|
+
// $group: {
|
|
72
|
+
// _id: null,
|
|
73
|
+
// items: { $push: '$$ROOT' }
|
|
74
|
+
// }
|
|
75
|
+
// }],
|
|
76
|
+
// count: [{ $count: 'count' }],
|
|
77
|
+
// },
|
|
78
|
+
// })
|
|
79
|
+
// const result = await this.model.aggregate(pipeline);
|
|
80
|
+
// const data = result[0]?.data[0]?.items || [];
|
|
81
|
+
// const totalCount = result[0]?.count[0]?.count || 0;
|
|
82
|
+
// return { data, totalCount };
|
|
83
|
+
// } catch (e) {
|
|
84
|
+
// this.logger.error(`Unable to retrieve Model with options ${JSON.stringify(options)}`);
|
|
85
|
+
// throw e;
|
|
86
|
+
// }
|
|
87
|
+
// }
|
|
88
|
+
// eslint-disable-next-line class-methods-use-this
|
|
89
|
+
mapConditions(conditions) {
|
|
90
|
+
const { id: _id } = conditions, remaining = __rest(conditions, ["id"]);
|
|
91
|
+
return Object.assign(Object.assign({}, (_id ? { _id } : {})), remaining);
|
|
92
|
+
}
|
|
93
|
+
async count(conditions) {
|
|
94
|
+
return this.model.count(conditions).exec();
|
|
95
|
+
}
|
|
96
|
+
async get(conditions, selectedFields) {
|
|
97
|
+
try {
|
|
98
|
+
const response = await this.model.findOne(this.mapConditions(conditions), selectedFields).exec();
|
|
99
|
+
return response === null || response === void 0 ? void 0 : response.toObject();
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(conditions)}`);
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async bulkGet(ids) {
|
|
107
|
+
try {
|
|
108
|
+
const results = await this.model.find().setOptions({ batchSize: 100 }).where('_id').in(ids).exec();
|
|
109
|
+
return results.map((i) => i.toObject());
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(ids)}`);
|
|
113
|
+
throw e;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async find(conditions, selectedFields) {
|
|
117
|
+
try {
|
|
118
|
+
const response = await this.model.findOne(conditions, selectedFields).exec();
|
|
119
|
+
return response === null || response === void 0 ? void 0 : response.toObject();
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
this.logger.error(`Unable to retrieve Model with criteria ${JSON.stringify(conditions)}`);
|
|
123
|
+
throw e;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async create(data) {
|
|
127
|
+
try {
|
|
128
|
+
const response = await this.model.create(data);
|
|
129
|
+
return response.toObject();
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
this.logger.error(`Unable to create Model with data ${JSON.stringify(data)} due to ${e === null || e === void 0 ? void 0 : e.message}`);
|
|
133
|
+
throw e;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async bulkCreate(data) {
|
|
137
|
+
try {
|
|
138
|
+
const response = await this.model.insertMany(data, {
|
|
139
|
+
lean: true,
|
|
140
|
+
ordered: true,
|
|
141
|
+
});
|
|
142
|
+
return response;
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
this.logger.error(`Unable to bulk create due to error`, e.message);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async upsert(conditions, update, options) {
|
|
149
|
+
return this.update(conditions, update, Object.assign({ upsert: true }, options));
|
|
150
|
+
}
|
|
151
|
+
async bulkUpdate(criteria, update, options) {
|
|
152
|
+
try {
|
|
153
|
+
const { id } = criteria, rest = __rest(criteria, ["id"]);
|
|
154
|
+
const processedCriteria = id ? Object.assign({ _id: id }, rest) : criteria;
|
|
155
|
+
const res = await this.model.updateMany(processedCriteria, update);
|
|
156
|
+
if (res) {
|
|
157
|
+
const response = await this.model
|
|
158
|
+
.find(processedCriteria)
|
|
159
|
+
.exec();
|
|
160
|
+
return response.map((i) => i === null || i === void 0 ? void 0 : i.toObject());
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.logger.error(`Unable to Bulk Update with criteria ${JSON.stringify(criteria)} and data ${JSON.stringify(update)}`);
|
|
164
|
+
throw new Error('Unable to do bulk update');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
this.logger.error(`Unable to Bulk Update with criteria ${JSON.stringify(criteria)} and data ${JSON.stringify(update)}`);
|
|
169
|
+
throw e;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async update(criteria, update, options) {
|
|
173
|
+
try {
|
|
174
|
+
const { id } = criteria, rest = __rest(criteria, ["id"]);
|
|
175
|
+
const processedCriteria = id ? Object.assign({ _id: id }, rest) : criteria;
|
|
176
|
+
const mergedOptions = Object.assign({ new: true, useFindAndModify: false }, options);
|
|
177
|
+
if (id) {
|
|
178
|
+
return (await this.model
|
|
179
|
+
.findByIdAndUpdate(id, update, mergedOptions)
|
|
180
|
+
.exec());
|
|
181
|
+
}
|
|
182
|
+
return (await this.model
|
|
183
|
+
.findOneAndUpdate(processedCriteria, update, mergedOptions)
|
|
184
|
+
.exec());
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
this.logger.error(`Unable to Update with criteria ${JSON.stringify(criteria)} and data ${JSON.stringify(update)}`);
|
|
188
|
+
throw e;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async delete(criteria) {
|
|
192
|
+
try {
|
|
193
|
+
let deleted;
|
|
194
|
+
if (criteria === null || criteria === void 0 ? void 0 : criteria.id) {
|
|
195
|
+
deleted = await this.model.findByIdAndDelete(criteria.id);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
deleted = await this.model.findOneAndDelete(this.mapConditions(criteria));
|
|
199
|
+
}
|
|
200
|
+
return !!deleted;
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
this.logger.error(`Unable to delete the model with criteria`, criteria);
|
|
204
|
+
throw e;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async bulkDelete(criteria) {
|
|
208
|
+
try {
|
|
209
|
+
const deleted = await this.model.deleteMany(this.mapConditions(criteria));
|
|
210
|
+
return (deleted === null || deleted === void 0 ? void 0 : deleted.deletedCount) || 0;
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
this.logger.error(`Unable to delete the model with criteria`, criteria);
|
|
214
|
+
throw e;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
BaseRepository = BaseRepository_1 = __decorate([
|
|
219
|
+
injectable(),
|
|
220
|
+
__param(0, unmanaged()),
|
|
221
|
+
__param(1, unmanaged()),
|
|
222
|
+
__param(2, unmanaged()),
|
|
223
|
+
__param(3, unmanaged())
|
|
224
|
+
], BaseRepository);export{BaseRepository};//# sourceMappingURL=base-repository.mjs.map
|