@common-stack/store-mongo 6.0.8-alpha.16 → 6.0.8-alpha.2
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/containers/container.d.ts +14 -0
- package/lib/containers/container.js +17 -0
- package/lib/containers/container.js.map +1 -0
- package/lib/containers/index.d.ts +1 -0
- package/lib/dataloaders/bulk-dataloader-v2.d.ts +40 -0
- package/lib/dataloaders/bulk-dataloader-v2.js +117 -0
- package/lib/dataloaders/bulk-dataloader-v2.js.map +1 -0
- package/lib/dataloaders/bulk-dataloader-v2.test.d.ts +1 -0
- package/lib/dataloaders/bulk-dataloader.d.ts +1 -1
- package/lib/dataloaders/bulk-dataloader.js +3 -3
- package/lib/dataloaders/bulk-dataloader.js.map +1 -1
- package/lib/dataloaders/bulk-dataloader.test.d.ts +1 -0
- package/lib/dataloaders/index.d.ts +1 -0
- package/lib/graphql/schema/base-services.graphql +253 -0
- package/lib/helpers/mongoose-connection.js.map +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -1
- package/lib/interfaces/base-repository.d.ts +1 -5
- package/lib/interfaces/base-service.d.ts +2 -1
- package/lib/interfaces/dataloaders.d.ts +1 -1
- package/lib/interfaces/getAllArgs.d.ts +135 -0
- package/lib/interfaces/getAllArgs.js +39 -0
- package/lib/interfaces/getAllArgs.js.map +1 -0
- package/lib/interfaces/index.d.ts +1 -5
- package/lib/interfaces/index.old.d.ts +3 -0
- package/lib/mixins/BaseServiceMixin.d.ts +45 -0
- package/lib/mixins/BaseServiceMixin.js +212 -0
- package/lib/mixins/BaseServiceMixin.js.map +1 -0
- package/lib/mixins/__tests__/BaseServiceMixin.test.d.ts +1 -0
- package/lib/mixins/base-service-mixin.d.ts +3 -2
- package/lib/mixins/base-service-mixin.js +29 -29
- package/lib/mixins/base-service-mixin.js.map +1 -1
- package/lib/mixins/index.d.ts +1 -0
- package/lib/module.d.ts +3 -0
- package/lib/module.js +4 -0
- package/lib/module.js.map +1 -0
- package/lib/services/BaseProxyService.d.ts +28 -0
- package/lib/services/BaseProxyService.js +52 -0
- package/lib/services/BaseProxyService.js.map +1 -0
- package/lib/services/BaseService.d.ts +23 -0
- package/lib/services/BaseService.js +64 -0
- package/lib/services/BaseService.js.map +1 -0
- package/lib/services/BaseService.test.d.ts +1 -0
- package/lib/services/ConnectionPoolManager.d.ts +54 -0
- package/lib/services/ConnectionPoolManager.js +163 -0
- package/lib/services/ConnectionPoolManager.js.map +1 -0
- package/lib/services/base-proxy-service.d.ts +3 -1
- package/lib/services/base-proxy-service.js +14 -11
- package/lib/services/base-proxy-service.js.map +1 -1
- package/lib/services/base-service.d.ts +3 -2
- package/lib/services/base-service.js +1 -1
- package/lib/services/base-service.js.map +1 -1
- package/lib/services/index.d.ts +3 -0
- package/lib/store/models/common-options-v2.d.ts +16 -0
- package/lib/store/models/common-options-v2.js +40 -0
- package/lib/store/models/common-options-v2.js.map +1 -0
- package/lib/store/models/common-options.d.ts +21 -0
- package/lib/store/models/common-options.js +33 -2
- package/lib/store/models/common-options.js.map +1 -1
- package/lib/store/models/index.d.ts +1 -0
- package/lib/store/repositories/BaseMongoRepository.d.ts +85 -0
- package/lib/store/repositories/BaseMongoRepository.js +424 -0
- package/lib/store/repositories/BaseMongoRepository.js.map +1 -0
- package/lib/store/repositories/BaseMongoRepository.test.d.ts +1 -0
- package/lib/store/repositories/CustomIdRepository.test.d.ts +1 -0
- package/lib/store/repositories/base-repository.d.ts +2 -1
- package/lib/store/repositories/base-repository.js +1 -1
- package/lib/store/repositories/base-repository.js.map +1 -1
- package/lib/store/repositories/index.d.ts +1 -0
- package/lib/templates/constants/SERVER_TYPES.ts.template +7 -0
- package/lib/templates/repositories/DataLoader.ts.template +211 -0
- package/lib/templates/repositories/DatabaseMigration.ts.template +13 -0
- package/lib/templates/repositories/IBaseMongoRepository.ts.template +284 -0
- package/lib/templates/repositories/IBaseService.ts.template +231 -0
- package/lib/templates/repositories/IBaseServiceMixin.ts.template +477 -0
- package/lib/templates/repositories/dbCommonTypes.ts.template +140 -0
- package/lib/templates/repositories/moleculerEventHandler.ts.template.toberemoved +118 -0
- package/lib/templates/repositories/mongoCommonTypes.ts.template +21 -0
- package/lib/templates/repositories/typedMoleculerService.ts.template.toberemoved +1188 -0
- package/lib/templates/repositories/zodToMoleculer.ts.template.toberemoved +133 -0
- package/package.json +21 -4
- package/LICENSE +0 -21
- package/lib/interfaces/base-repository.js +0 -5
- package/lib/interfaces/base-repository.js.map +0 -1
- package/lib/interfaces/generated-models.d.ts +0 -148
- package/lib/interfaces/generated-models.js +0 -24
- package/lib/interfaces/generated-models.js.map +0 -1
- package/lib/interfaces/get-all-args.d.ts +0 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-service.js","sources":["../../src/services/base-service.ts"],"sourcesContent":[
|
|
1
|
+
{"version":3,"file":"base-service.js","sources":["../../src/services/base-service.ts"],"sourcesContent":["import { Document, FilterQuery } from 'mongoose';\nimport { injectable } from 'inversify';\nimport { GetAllArgs } from '../interfaces';\nimport { IBaseRepository, IBaseService } from '../interfaces/index.old';\n\n@injectable()\nexport class BaseService<T, C = Omit<T, 'id'>, U = C> implements IBaseService<T, C, U> {\n constructor(protected repository: IBaseRepository<T>) {\n this.repository = repository;\n }\n\n async getAllWithCount(options: GetAllArgs<Document<T>>): Promise<{ data: T[]; totalCount: number }> {\n const totalCount = await this.count(options.criteria);\n const data = await this.getAll(options);\n return {\n totalCount,\n data,\n };\n }\n\n count(conditions?: FilterQuery<Document<T>>): Promise<number> {\n return this.repository.count(conditions);\n }\n\n get(conditions: string | FilterQuery<Document<T>>): Promise<T> {\n if (typeof conditions === 'string') {\n return this.repository.get({ id: conditions });\n }\n return this.repository.get(conditions);\n }\n\n getByName(name: string): Promise<T> {\n return this.repository.get({ name });\n }\n\n getAll(options: GetAllArgs<Document<T>>): Promise<T[]> {\n return this.repository.getAll(options);\n }\n\n getByIds(ids: string[]): Promise<T[]> {\n return this.repository.bulkGet(ids);\n }\n\n create(data: C): Promise<T> {\n return this.repository.create<C>(data);\n }\n\n bulkCreate(data: C[]): Promise<T[]> {\n return this.repository.bulkCreate<C>(data);\n }\n\n async update(id: string, data: Partial<U>, overwrite = false): Promise<T> {\n return this.repository.update<U>({ id }, data, { overwrite });\n }\n\n insert(data: (C | U) & { id?: string }, overwrite = true): Promise<T> {\n const { id, ...rest } = data;\n if (id) {\n const existing = this.repository.get({ id });\n if (existing) {\n return this.update(id, rest as U, overwrite);\n }\n }\n return this.create(rest as C);\n }\n\n delete(id: string | FilterQuery<Document<T>>): Promise<boolean> {\n if (typeof id === 'string') {\n return this.repository.delete({ id });\n }\n return this.repository.delete(id);\n }\n}\n"],"names":[],"mappings":"6EAMa,IAAA,WAAW,GAAjB,MAAM,WAAW,CAAA;AACE,IAAA,UAAA,CAAA;AAAtB,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,UAAU,EAAE,CAAC,CAAC;SAClD;QACD,OAAO,IAAI,CAAC,UAAU,CAA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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;SACzC;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"}
|
package/lib/services/index.d.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
export declare const commonModelSchemaOptions2: {
|
|
3
|
+
timestamps: boolean;
|
|
4
|
+
toJSON: {
|
|
5
|
+
virtuals: boolean;
|
|
6
|
+
transform(doc: any, ret: any): any;
|
|
7
|
+
};
|
|
8
|
+
toObject: {
|
|
9
|
+
virtuals: boolean;
|
|
10
|
+
getters: boolean;
|
|
11
|
+
transform(doc: any, ret: any): any;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export declare const addIdVirtualFields2: (schema: Schema) => Schema<any, import("mongoose").Model<any, any, any, any, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, {
|
|
15
|
+
[x: string]: any;
|
|
16
|
+
}>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const commonModelSchemaOptions2 = {
|
|
2
|
+
timestamps: true,
|
|
3
|
+
toJSON: {
|
|
4
|
+
virtuals: true,
|
|
5
|
+
transform(doc, ret) {
|
|
6
|
+
ret.id = ret._id.toString();
|
|
7
|
+
delete ret.__v;
|
|
8
|
+
// Uncomment if you don't want _id in output
|
|
9
|
+
// delete ret._id;
|
|
10
|
+
return ret;
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
toObject: {
|
|
14
|
+
virtuals: true,
|
|
15
|
+
getters: true,
|
|
16
|
+
transform(doc, ret) {
|
|
17
|
+
ret.id = ret._id.toString();
|
|
18
|
+
delete ret.__v;
|
|
19
|
+
// Uncomment if you don't want _id in output
|
|
20
|
+
// delete ret._id;
|
|
21
|
+
return ret;
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
const addIdVirtualFields2 = (schema) => {
|
|
26
|
+
// Add id virtual getter
|
|
27
|
+
schema.virtual('id').get(function () {
|
|
28
|
+
return this._id.toHexString();
|
|
29
|
+
});
|
|
30
|
+
// Ensure virtuals are included in JSON
|
|
31
|
+
schema.set('toJSON', {
|
|
32
|
+
virtuals: true,
|
|
33
|
+
transform: (_, ret) => {
|
|
34
|
+
ret.id = ret._id.toString();
|
|
35
|
+
delete ret.__v;
|
|
36
|
+
return ret;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return schema;
|
|
40
|
+
};export{addIdVirtualFields2,commonModelSchemaOptions2};//# sourceMappingURL=common-options-v2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-options-v2.js","sources":["../../../src/store/models/common-options-v2.ts"],"sourcesContent":["/* eslint-disable no-underscore-dangle */\n/* eslint-disable no-param-reassign */\nimport { Schema } from 'mongoose';\n\nexport const commonModelSchemaOptions2 = {\n timestamps: true,\n toJSON: {\n virtuals: true,\n transform(doc, ret) {\n ret.id = ret._id.toString();\n delete ret.__v;\n // Uncomment if you don't want _id in output\n // delete ret._id;\n return ret;\n },\n },\n toObject: {\n virtuals: true,\n getters: true,\n transform(doc, ret) {\n ret.id = ret._id.toString();\n delete ret.__v;\n // Uncomment if you don't want _id in output\n // delete ret._id;\n return ret;\n },\n },\n};\n\nexport const addIdVirtualFields2 = (schema: Schema) => {\n // Add id virtual getter\n schema.virtual('id').get(function () {\n return (this._id as any).toHexString();\n });\n\n // Ensure virtuals are included in JSON\n schema.set('toJSON', {\n virtuals: true,\n transform: (_, ret) => {\n ret.id = ret._id.toString();\n delete ret.__v;\n return ret;\n },\n });\n\n return schema;\n};\n"],"names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void;
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Applies the mongoose-lean-virtuals plugin to a schema.
|
|
15
|
+
* This allows .lean() queries to include virtual fields automatically.
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
* const MySchema = new Schema({ ... });
|
|
19
|
+
* applyLeanVirtuals(MySchema);
|
|
20
|
+
* // Now .lean() will include virtuals, or use .lean({ virtuals: true })
|
|
21
|
+
*/
|
|
22
|
+
export declare const applyLeanVirtuals: (schema: Schema) => Schema;
|
|
23
|
+
/**
|
|
24
|
+
* Applies common schema configurations including:
|
|
25
|
+
* - toJSON with virtuals
|
|
26
|
+
* - toObject with virtuals
|
|
27
|
+
* - mongoose-lean-virtuals plugin for .lean() queries
|
|
28
|
+
*
|
|
29
|
+
* Usage:
|
|
30
|
+
* const MySchema = new Schema({ ... });
|
|
31
|
+
* applyCommonSchemaPlugins(MySchema);
|
|
32
|
+
*/
|
|
33
|
+
export declare const applyCommonSchemaPlugins: (schema: Schema) => Schema;
|
|
13
34
|
export declare const addIdVirtualFields: (schema: Schema) => Schema<any, import("mongoose").Model<any, any, any, any, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, {
|
|
14
35
|
[x: string]: any;
|
|
15
36
|
}>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const commonModeSchemaOptions = {
|
|
1
|
+
import mongooseLeanVirtuals from'mongoose-lean-virtuals';const commonModeSchemaOptions = {
|
|
2
2
|
timestamps: true,
|
|
3
3
|
toJSON: {
|
|
4
4
|
virtuals: true,
|
|
@@ -12,9 +12,40 @@ const commonModeSchemaOptions = {
|
|
|
12
12
|
},
|
|
13
13
|
},
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Applies the mongoose-lean-virtuals plugin to a schema.
|
|
17
|
+
* This allows .lean() queries to include virtual fields automatically.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* const MySchema = new Schema({ ... });
|
|
21
|
+
* applyLeanVirtuals(MySchema);
|
|
22
|
+
* // Now .lean() will include virtuals, or use .lean({ virtuals: true })
|
|
23
|
+
*/
|
|
24
|
+
const applyLeanVirtuals = (schema) => {
|
|
25
|
+
schema.plugin(mongooseLeanVirtuals);
|
|
26
|
+
return schema;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Applies common schema configurations including:
|
|
30
|
+
* - toJSON with virtuals
|
|
31
|
+
* - toObject with virtuals
|
|
32
|
+
* - mongoose-lean-virtuals plugin for .lean() queries
|
|
33
|
+
*
|
|
34
|
+
* Usage:
|
|
35
|
+
* const MySchema = new Schema({ ... });
|
|
36
|
+
* applyCommonSchemaPlugins(MySchema);
|
|
37
|
+
*/
|
|
38
|
+
const applyCommonSchemaPlugins = (schema) => {
|
|
39
|
+
schema.set('toJSON', { virtuals: true });
|
|
40
|
+
schema.set('toObject', { virtuals: true, getters: true });
|
|
41
|
+
schema.plugin(mongooseLeanVirtuals);
|
|
42
|
+
return schema;
|
|
43
|
+
};
|
|
15
44
|
const addIdVirtualFields = (schema) => {
|
|
16
45
|
schema.set('toJSON', {
|
|
17
46
|
virtuals: true,
|
|
18
47
|
});
|
|
48
|
+
// Apply mongoose-lean-virtuals plugin so .lean() queries include virtuals
|
|
49
|
+
schema.plugin(mongooseLeanVirtuals);
|
|
19
50
|
return schema;
|
|
20
|
-
};export{addIdVirtualFields,commonModeSchemaOptions};//# sourceMappingURL=common-options.js.map
|
|
51
|
+
};export{addIdVirtualFields,applyCommonSchemaPlugins,applyLeanVirtuals,commonModeSchemaOptions};//# sourceMappingURL=common-options.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common-options.js","sources":["../../../src/store/models/common-options.ts"],"sourcesContent":[
|
|
1
|
+
{"version":3,"file":"common-options.js","sources":["../../../src/store/models/common-options.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport mongooseLeanVirtuals from 'mongoose-lean-virtuals';\n\nexport const commonModeSchemaOptions = {\n timestamps: true,\n toJSON: {\n virtuals: true,\n },\n toObject: {\n virtuals: true,\n getters: true,\n transform(doc, ret) {\n delete ret.__v;\n delete ret._id;\n },\n },\n};\n\n/**\n * Applies the mongoose-lean-virtuals plugin to a schema.\n * This allows .lean() queries to include virtual fields automatically.\n *\n * Usage:\n * const MySchema = new Schema({ ... });\n * applyLeanVirtuals(MySchema);\n * // Now .lean() will include virtuals, or use .lean({ virtuals: true })\n */\nexport const applyLeanVirtuals = (schema: Schema): Schema => {\n schema.plugin(mongooseLeanVirtuals);\n return schema;\n};\n\n/**\n * Applies common schema configurations including:\n * - toJSON with virtuals\n * - toObject with virtuals\n * - mongoose-lean-virtuals plugin for .lean() queries\n *\n * Usage:\n * const MySchema = new Schema({ ... });\n * applyCommonSchemaPlugins(MySchema);\n */\nexport const applyCommonSchemaPlugins = (schema: Schema): Schema => {\n schema.set('toJSON', { virtuals: true });\n schema.set('toObject', { virtuals: true, getters: true });\n schema.plugin(mongooseLeanVirtuals);\n return schema;\n};\n\nexport const addIdVirtualFields = (schema: Schema) => {\n schema.set('toJSON', {\n virtuals: true,\n });\n // Apply mongoose-lean-virtuals plugin so .lean() queries include virtuals\n schema.plugin(mongooseLeanVirtuals);\n return schema;\n};\n"],"names":[],"mappings":"yDAGa,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;AAEF;;;;;;;;AAQG;AACU,MAAA,iBAAiB,GAAG,CAAC,MAAc,KAAY;AACxD,IAAA,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACpC,IAAA,OAAO,MAAM,CAAC;AAClB,EAAE;AAEF;;;;;;;;;AASG;AACU,MAAA,wBAAwB,GAAG,CAAC,MAAc,KAAY;IAC/D,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AACzC,IAAA,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AAC1D,IAAA,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACpC,IAAA,OAAO,MAAM,CAAC;AAClB,EAAE;AAEW,MAAA,kBAAkB,GAAG,CAAC,MAAc,KAAI;AACjD,IAAA,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE;AACjB,QAAA,QAAQ,EAAE,IAAI;AACjB,KAAA,CAAC,CAAC;;AAEH,IAAA,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACpC,IAAA,OAAO,MAAM,CAAC;AAClB"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { CdmLogger } from '@cdm-logger/core';
|
|
2
|
+
import { Connection, FilterQuery, Model, PipelineStage, UpdateQuery } from 'mongoose';
|
|
3
|
+
import type { GetAllArgs, ISort, IBaseMongoRepository, IMongoOptions, AsDomainType, CreateType, UpdateType } from 'common/server';
|
|
4
|
+
export declare class BaseMongoRepository<SchemaType> implements IBaseMongoRepository<SchemaType> {
|
|
5
|
+
private modelFunc;
|
|
6
|
+
private options;
|
|
7
|
+
protected logger: CdmLogger.ILogger;
|
|
8
|
+
model: Model<SchemaType & {
|
|
9
|
+
id: string;
|
|
10
|
+
}>;
|
|
11
|
+
constructor(modelFunc: (db: Connection) => Model<SchemaType>, db: Connection, logger: CdmLogger.ILogger, options?: IMongoOptions);
|
|
12
|
+
/**
|
|
13
|
+
* Safely registers or retrieves a Mongoose model to prevent overwrite errors
|
|
14
|
+
* This is crucial for development environments where multiple containers might try to register the same model
|
|
15
|
+
*/
|
|
16
|
+
private safeModelRegistration;
|
|
17
|
+
/**
|
|
18
|
+
* Transforms a document to the domain model format
|
|
19
|
+
*/
|
|
20
|
+
protected transformDocument(doc: any): AsDomainType<SchemaType> | null;
|
|
21
|
+
/**
|
|
22
|
+
* Ensures the id field is present in a lean document
|
|
23
|
+
* This is a fallback for when .lean({ virtuals: true }) doesn't include the id virtual
|
|
24
|
+
*/
|
|
25
|
+
protected ensureIdField<T extends Record<string, any>>(doc: T): T & {
|
|
26
|
+
id: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Ensures id field is present in an array of lean documents
|
|
30
|
+
*/
|
|
31
|
+
protected ensureIdFields<T extends Record<string, any>>(docs: T[]): (T & {
|
|
32
|
+
id: string;
|
|
33
|
+
})[];
|
|
34
|
+
/**
|
|
35
|
+
* Transforms an array of documents
|
|
36
|
+
*/
|
|
37
|
+
protected transformDocuments(docs: SchemaType[]): AsDomainType<SchemaType>[];
|
|
38
|
+
/**
|
|
39
|
+
* Maps API criteria with 'id' to MongoDB criteria with '_id'
|
|
40
|
+
*/
|
|
41
|
+
private mapConditions;
|
|
42
|
+
/**
|
|
43
|
+
* Computes sort object from GetAllArgs sort parameter
|
|
44
|
+
* Handles both string-based and object-based sort specifications
|
|
45
|
+
*/
|
|
46
|
+
protected computeSort(sort: ISort | string | ISort[]): Record<string, 1 | -1>;
|
|
47
|
+
/**
|
|
48
|
+
* Prepares an aggregation pipeline with standard stages
|
|
49
|
+
* @param options Query options including filtering, sorting, pagination
|
|
50
|
+
* @param customPipeline Optional custom pipeline stages to include
|
|
51
|
+
* @returns Complete MongoDB aggregation pipeline
|
|
52
|
+
*/
|
|
53
|
+
protected preparePipeline(options: GetAllArgs<SchemaType>, customPipeline?: PipelineStage[]): PipelineStage[];
|
|
54
|
+
/**
|
|
55
|
+
* Execute an aggregation pipeline and transform the results
|
|
56
|
+
*/
|
|
57
|
+
protected aggregate(pipeline: PipelineStage[]): Promise<AsDomainType<SchemaType>[]>;
|
|
58
|
+
count(conditions?: FilterQuery<SchemaType>): Promise<number>;
|
|
59
|
+
get(conditions?: FilterQuery<SchemaType>, selectedFields?: string): Promise<AsDomainType<SchemaType> | null>;
|
|
60
|
+
find(conditions: Partial<FilterQuery<SchemaType>>, selectedFields?: string): Promise<AsDomainType<SchemaType> | null>;
|
|
61
|
+
getAll(options: GetAllArgs<SchemaType>): Promise<AsDomainType<SchemaType>[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Get all documents using aggregation pipeline
|
|
64
|
+
*/
|
|
65
|
+
getAllWithPipeline(options: GetAllArgs<SchemaType>, customPipeline?: PipelineStage[]): Promise<AsDomainType<SchemaType>[]>;
|
|
66
|
+
getAllWithCount(options: GetAllArgs<SchemaType>): Promise<{
|
|
67
|
+
data: AsDomainType<SchemaType>[];
|
|
68
|
+
totalCount: number;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* Get all documents with count using aggregation pipeline
|
|
72
|
+
*/
|
|
73
|
+
getAllWithCountAndPipeline(options: GetAllArgs<SchemaType>, customPipeline?: PipelineStage[]): Promise<{
|
|
74
|
+
data: AsDomainType<SchemaType>[];
|
|
75
|
+
totalCount: number;
|
|
76
|
+
}>;
|
|
77
|
+
create<T = CreateType<SchemaType>>(data: T): Promise<AsDomainType<SchemaType>>;
|
|
78
|
+
bulkCreate<T = CreateType<SchemaType>>(data: T[]): Promise<AsDomainType<SchemaType>[]>;
|
|
79
|
+
upsert<T = UpdateType<SchemaType>>(conditions: FilterQuery<SchemaType>, update: T, options?: Record<string, unknown>): Promise<AsDomainType<SchemaType>>;
|
|
80
|
+
update<T = UpdateType<SchemaType>>(criteria: FilterQuery<SchemaType>, update: T | UpdateQuery<SchemaType>, options?: Record<string, unknown>): Promise<AsDomainType<SchemaType>>;
|
|
81
|
+
bulkUpdate<T = UpdateType<SchemaType>>(criteria: FilterQuery<SchemaType>, update: T | UpdateQuery<SchemaType>, options?: Record<string, unknown>): Promise<AsDomainType<SchemaType>[]>;
|
|
82
|
+
delete(criteria: FilterQuery<SchemaType>): Promise<boolean>;
|
|
83
|
+
bulkDelete(criteria: FilterQuery<SchemaType>): Promise<number>;
|
|
84
|
+
bulkGet(ids: string[], selectedFields?: string): Promise<AsDomainType<SchemaType>[]>;
|
|
85
|
+
}
|