@trailmix-cms/db 0.0.1
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/dist/audited.base-collection.d.ts +26 -0
- package/dist/audited.base-collection.d.ts.map +1 -0
- package/dist/audited.base-collection.js +151 -0
- package/dist/audited.base-collection.js.map +1 -0
- package/dist/collection.decorator.d.ts +3 -0
- package/dist/collection.decorator.d.ts.map +1 -0
- package/dist/collection.decorator.js +9 -0
- package/dist/collection.decorator.js.map +1 -0
- package/dist/collection.factory.d.ts +14 -0
- package/dist/collection.factory.d.ts.map +1 -0
- package/dist/collection.factory.js +29 -0
- package/dist/collection.factory.js.map +1 -0
- package/dist/collections/audit.collection.d.ts +93 -0
- package/dist/collections/audit.collection.d.ts.map +1 -0
- package/dist/collections/audit.collection.js +69 -0
- package/dist/collections/audit.collection.js.map +1 -0
- package/dist/collections/index.d.ts +9 -0
- package/dist/collections/index.d.ts.map +1 -0
- package/dist/collections/index.js +15 -0
- package/dist/collections/index.js.map +1 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +26 -0
- package/dist/config.js.map +1 -0
- package/dist/connection.factory.d.ts +24 -0
- package/dist/connection.factory.d.ts.map +1 -0
- package/dist/connection.factory.js +71 -0
- package/dist/connection.factory.js.map +1 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +18 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/internal-collection-names.d.ts +5 -0
- package/dist/constants/internal-collection-names.d.ts.map +1 -0
- package/dist/constants/internal-collection-names.js +7 -0
- package/dist/constants/internal-collection-names.js.map +1 -0
- package/dist/database.module.d.ts +3 -0
- package/dist/database.module.d.ts.map +1 -0
- package/dist/database.module.js +43 -0
- package/dist/database.module.js.map +1 -0
- package/dist/database.service.d.ts +17 -0
- package/dist/database.service.d.ts.map +1 -0
- package/dist/database.service.js +50 -0
- package/dist/database.service.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/build-collection-token.d.ts +3 -0
- package/dist/utils/build-collection-token.d.ts.map +1 -0
- package/dist/utils/build-collection-token.js +11 -0
- package/dist/utils/build-collection-token.js.map +1 -0
- package/dist/utils/codes.d.ts +8 -0
- package/dist/utils/codes.d.ts.map +1 -0
- package/dist/utils/codes.js +62 -0
- package/dist/utils/codes.js.map +1 -0
- package/dist/utils/default.d.ts +8 -0
- package/dist/utils/default.d.ts.map +1 -0
- package/dist/utils/default.js +19 -0
- package/dist/utils/default.js.map +1 -0
- package/dist/utils/environment.d.ts +2 -0
- package/dist/utils/environment.d.ts.map +1 -0
- package/dist/utils/environment.js +5 -0
- package/dist/utils/environment.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/record.d.ts +19 -0
- package/dist/utils/record.d.ts.map +1 -0
- package/dist/utils/record.js +19 -0
- package/dist/utils/record.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Filter, Collection, ClientSession, FindOptions, ObjectId, UpdateFilter, Document, WithId, OptionalUnlessRequiredId } from 'mongodb';
|
|
2
|
+
import { AuditContext, Base } from '@trailmixcms/models';
|
|
3
|
+
import { Creatable } from './utils';
|
|
4
|
+
import { InternalCollectionName } from './constants';
|
|
5
|
+
import { AuditCollection } from './collections/audit.collection';
|
|
6
|
+
import { DatabaseService } from './database.service';
|
|
7
|
+
import { ZodType } from 'zod';
|
|
8
|
+
export declare abstract class AuditedCollection<T extends Base.BaseEntity & Document> {
|
|
9
|
+
protected readonly collection: Collection<T>;
|
|
10
|
+
protected readonly databaseService: DatabaseService;
|
|
11
|
+
protected readonly auditCollection: AuditCollection;
|
|
12
|
+
protected abstract readonly collectionName: InternalCollectionName | string;
|
|
13
|
+
protected abstract readonly entitySchema: ZodType<OptionalUnlessRequiredId<T>>;
|
|
14
|
+
constructor(collection: Collection<T>, databaseService: DatabaseService, auditCollection: AuditCollection);
|
|
15
|
+
get(id: ObjectId): Promise<WithId<T> | null>;
|
|
16
|
+
find(filter: Filter<T>, options?: FindOptions): Promise<WithId<T>[]>;
|
|
17
|
+
findOne(query: Filter<T>): Promise<WithId<T> | null>;
|
|
18
|
+
findOneAndUpdate(query: Filter<T>, update: UpdateFilter<T>, auditContext: AuditContext.Model, session?: ClientSession): Promise<WithId<T>>;
|
|
19
|
+
deleteMany(query: Filter<T>, auditContext: AuditContext.Model, session?: ClientSession): Promise<import("mongodb").DeleteResult>;
|
|
20
|
+
deleteOne(entityId: ObjectId, auditContext: AuditContext.Model, session?: ClientSession): Promise<import("mongodb").DeleteResult>;
|
|
21
|
+
insertOne(params: Creatable<T>, auditContext: AuditContext.Model, session?: ClientSession): Promise<OptionalUnlessRequiredId<T> & {
|
|
22
|
+
_id: import("mongodb").InferIdType<T>;
|
|
23
|
+
}>;
|
|
24
|
+
upsertOne(query: Filter<T>, update: UpdateFilter<T>, auditContext: AuditContext.Model, session?: ClientSession): Promise<WithId<T>>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=audited.base-collection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audited.base-collection.d.ts","sourceRoot":"","sources":["../src/audited.base-collection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,wBAAwB,EAA2B,MAAM,SAAS,CAAC;AACtK,OAAO,EAAS,YAAY,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAiB,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,sBAAsB,EAAE,MAAM,a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AAE,OAAO,CAAC,EAAE,aAAa;IAqBvF,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,aAAa;;;IA2BzF,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,aAAa;CA0BvH"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AuditedCollection = void 0;
|
|
7
|
+
const models_1 = require("@trailmixcms/models");
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
10
|
+
class AuditedCollection {
|
|
11
|
+
collection;
|
|
12
|
+
databaseService;
|
|
13
|
+
auditCollection;
|
|
14
|
+
constructor(collection, databaseService, auditCollection) {
|
|
15
|
+
this.collection = collection;
|
|
16
|
+
this.databaseService = databaseService;
|
|
17
|
+
this.auditCollection = auditCollection;
|
|
18
|
+
}
|
|
19
|
+
get(id) {
|
|
20
|
+
return this.collection.findOne({ _id: id });
|
|
21
|
+
}
|
|
22
|
+
find(filter, options) {
|
|
23
|
+
return this.collection.find(filter, options).toArray();
|
|
24
|
+
}
|
|
25
|
+
findOne(query) {
|
|
26
|
+
return this.collection.findOne(query);
|
|
27
|
+
}
|
|
28
|
+
async findOneAndUpdate(query, update, auditContext, session) {
|
|
29
|
+
const encoded = !!models_1.AuditContext.modelSchema.encode(auditContext);
|
|
30
|
+
if (!encoded) {
|
|
31
|
+
throw new Error('Failed to encode audit context');
|
|
32
|
+
}
|
|
33
|
+
return this.databaseService.withTransaction({ session }, async (session) => {
|
|
34
|
+
const result = await this.collection.findOneAndUpdate(query, (0, lodash_merge_1.default)(update, {
|
|
35
|
+
$set: {
|
|
36
|
+
updated_at: new Date(),
|
|
37
|
+
}
|
|
38
|
+
}), {
|
|
39
|
+
session,
|
|
40
|
+
returnDocument: 'after'
|
|
41
|
+
});
|
|
42
|
+
if (!result) {
|
|
43
|
+
throw new Error(`Failed to find and update "${this.collectionName}" with query ${JSON.stringify(query)}`);
|
|
44
|
+
}
|
|
45
|
+
const auditRecord = {
|
|
46
|
+
entity_id: result._id,
|
|
47
|
+
entity_type: this.collectionName,
|
|
48
|
+
action: 'update',
|
|
49
|
+
context: auditContext,
|
|
50
|
+
};
|
|
51
|
+
await this.auditCollection.insertOne(auditRecord, session);
|
|
52
|
+
return result;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async deleteMany(query, auditContext, session) {
|
|
56
|
+
const encoded = !!models_1.AuditContext.modelSchema.encode(auditContext);
|
|
57
|
+
if (!encoded) {
|
|
58
|
+
throw new Error('Failed to encode audit context');
|
|
59
|
+
}
|
|
60
|
+
return this.databaseService.withTransaction({ session }, async (session) => {
|
|
61
|
+
const items = await this.collection.find(query, { session }).toArray();
|
|
62
|
+
const result = await this.collection.deleteMany({ _id: { $in: items.map(item => item._id) } }, { session });
|
|
63
|
+
if (result.deletedCount !== items.length) {
|
|
64
|
+
throw new Error(`Failed to delete ${items.length} of ${items.length} "${this.collectionName}" items with query ${JSON.stringify(query)}`);
|
|
65
|
+
}
|
|
66
|
+
for (const item of items) {
|
|
67
|
+
const auditRecord = {
|
|
68
|
+
entity_id: item._id,
|
|
69
|
+
entity_type: this.collectionName,
|
|
70
|
+
action: 'delete',
|
|
71
|
+
context: auditContext,
|
|
72
|
+
};
|
|
73
|
+
await this.auditCollection.insertOne(auditRecord, session);
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async deleteOne(entityId, auditContext, session) {
|
|
79
|
+
const encoded = !!models_1.AuditContext.modelSchema.encode(auditContext);
|
|
80
|
+
if (!encoded) {
|
|
81
|
+
throw new Error('Failed to encode audit context');
|
|
82
|
+
}
|
|
83
|
+
return this.databaseService.withTransaction({ session }, async (session) => {
|
|
84
|
+
const result = await this.collection.deleteOne({ _id: entityId }, { session });
|
|
85
|
+
if (result.deletedCount === 0) {
|
|
86
|
+
throw new Error(`Failed to delete "${this.collectionName}" with id ${entityId}`);
|
|
87
|
+
}
|
|
88
|
+
const auditRecord = {
|
|
89
|
+
entity_id: entityId,
|
|
90
|
+
entity_type: this.collectionName,
|
|
91
|
+
action: 'delete',
|
|
92
|
+
context: auditContext,
|
|
93
|
+
};
|
|
94
|
+
await this.auditCollection.insertOne(auditRecord, session);
|
|
95
|
+
return result;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async insertOne(params, auditContext, session) {
|
|
99
|
+
const encoded = !!models_1.AuditContext.modelSchema.encode(auditContext);
|
|
100
|
+
if (!encoded) {
|
|
101
|
+
throw new Error('Failed to encode audit context');
|
|
102
|
+
}
|
|
103
|
+
return this.databaseService.withTransaction({ session }, async (session) => {
|
|
104
|
+
const entity = (0, utils_1.ensureCreated)(params);
|
|
105
|
+
const encoded = !!this.entitySchema.encode(entity);
|
|
106
|
+
if (!encoded) {
|
|
107
|
+
throw new Error('Failed to encode entity');
|
|
108
|
+
}
|
|
109
|
+
const { insertedId } = await this.collection.insertOne(entity, session);
|
|
110
|
+
const result = {
|
|
111
|
+
...entity,
|
|
112
|
+
_id: insertedId
|
|
113
|
+
};
|
|
114
|
+
const auditRecord = {
|
|
115
|
+
entity_id: insertedId,
|
|
116
|
+
entity_type: this.collectionName,
|
|
117
|
+
action: 'create',
|
|
118
|
+
context: auditContext,
|
|
119
|
+
};
|
|
120
|
+
await this.auditCollection.insertOne(auditRecord, session);
|
|
121
|
+
return result;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
async upsertOne(query, update, auditContext, session) {
|
|
125
|
+
const encoded = !!models_1.AuditContext.modelSchema.encode(auditContext);
|
|
126
|
+
if (!encoded) {
|
|
127
|
+
throw new Error('Failed to encode audit context');
|
|
128
|
+
}
|
|
129
|
+
return this.databaseService.withTransaction({ session }, async (session) => {
|
|
130
|
+
const updateFilter = (0, lodash_merge_1.default)(update, {
|
|
131
|
+
$setOnInsert: {
|
|
132
|
+
created_at: new Date(),
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
const result = await this.collection.findOneAndUpdate(query, updateFilter, { upsert: true, session, returnDocument: 'after' });
|
|
136
|
+
if (!result) {
|
|
137
|
+
throw new Error('Failed to upsert record');
|
|
138
|
+
}
|
|
139
|
+
const auditRecord = {
|
|
140
|
+
entity_id: result._id,
|
|
141
|
+
entity_type: this.collectionName,
|
|
142
|
+
action: 'update',
|
|
143
|
+
context: auditContext,
|
|
144
|
+
};
|
|
145
|
+
await this.auditCollection.insertOne(auditRecord, session);
|
|
146
|
+
return result;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.AuditedCollection = AuditedCollection;
|
|
151
|
+
//# sourceMappingURL=audited.base-collection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audited.base-collection.js","sourceRoot":"","sources":["../src/audited.base-collection.ts"],"names":[],"mappings":";;;;;;AACA,gDAAgE;AAChE,mCAAmD;AAKnD,gEAAiC;AAEjC,MAAsB,iBAAiB;IAKZ;IACA;IACA;IAHvB,YACuB,UAAyB,EACzB,eAAgC,EAChC,eAAgC;QAFhC,eAAU,GAAV,UAAU,CAAe;QACzB,oBAAe,GAAf,eAAe,CAAiB;QAChC,oBAAe,GAAf,eAAe,CAAiB;IACnD,CAAC;IAEL,GAAG,CAAC,EAAY;QACZ,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,EAAE,EAAe,CAAC,CAAC;IAC7D,CAAC;I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O,IAAI,CAAC,eAAe,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;YACvE,MAAM,YAAY,GACd,IAAA,sBAAK,EAAC,MAAM,EAAE;gBACV,YAAY,EAAE;oBACV,UAAU,EAAE,IAAI,IAAI,EAAE;iBACD;aAC5B,CAAC,CAAC;YACP,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,CAAE,CAAC;YAChI,IAAI,CAAC,MAAM,EAAE,CAAC;gBACV,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;YAC/C,CAAC;YACD,MAAM,WAAW,GAA4B;gBACzC,SAAS,EAAE,MAAM,CAAC,GAAG;gBACrB,WAAW,EAAE,IAAI,CAAC,cAAc;gBAChC,MAAM,EAAE,QAAQ;gBAChB,OAAO,EAAE,YAAY;aACxB,CAAC;YACF,MAAM,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;YAC3D,OAAO,MAAM,CAAC;QAClB,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AApJD,8CAoJC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.decorator.d.ts","sourceRoot":"","sources":["../src/collection.decorator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAG1D,wBAAgB,kBAAkB,CAAC,cAAc,EAAE,MAAM,GAAG,sBAAsB,0CAEjF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentCollection = DocumentCollection;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const build_collection_token_1 = require("./utils/build-collection-token");
|
|
6
|
+
function DocumentCollection(collectionName) {
|
|
7
|
+
return (0, common_1.Inject)((0, build_collection_token_1.buildCollectionToken)(collectionName));
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=collection.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.decorator.js","sourceRoot":"","sources":["../src/collection.decorator.ts"],"names":[],"mappings":";;AAIA,gDAEC;AAND,2CAAwC;AAExC,2EAAsE;AAEtE,SAAgB,kBAAkB,CAAC,cAA+C;IAC9E,OAAO,IAAA,eAAM,EAAC,IAAA,6CAAoB,EAAC,cAAc,CAAC,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Collection, CollectionOptions, Document } from 'mongodb';
|
|
2
|
+
import { DatabaseService } from './database.service';
|
|
3
|
+
import { InternalCollectionName } from './constants';
|
|
4
|
+
export type CollectionSetup<T extends Document> = (collection: Collection<T>) => void;
|
|
5
|
+
export type CollectionFactoryOptions = {
|
|
6
|
+
disableDefaultIndexes?: boolean;
|
|
7
|
+
mongodbCollectionOptions?: CollectionOptions;
|
|
8
|
+
};
|
|
9
|
+
export declare function collectionFactory<T extends Document>(collectionName: InternalCollectionName | string, options?: CollectionFactoryOptions): {
|
|
10
|
+
provide: string;
|
|
11
|
+
useFactory: (databaseService: DatabaseService) => Promise<Collection<T>>;
|
|
12
|
+
inject: (typeof DatabaseService)[];
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=collection.factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.factory.d.ts","sourceRoot":"","sources":["../src/collection.factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAKrD,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,QAAQ,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAEtF,MAAM,MAAM,wBAAwB,GAAG;IACnC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,wBAAwB,CAAC,EAAE,iBAAiB,CAAC;CAChD,CAAA;AAED,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,QAAQ,EAAE,cAAc,EAAE,sBAAsB,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,wBAAwB;;kCAI5G,eAAe;;EAkB3C"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.collectionFactory = collectionFactory;
|
|
4
|
+
const database_service_1 = require("./database.service");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const build_collection_token_1 = require("./utils/build-collection-token");
|
|
7
|
+
const logger = new common_1.Logger('CollectionFactory');
|
|
8
|
+
function collectionFactory(collectionName, options) {
|
|
9
|
+
const provider = {
|
|
10
|
+
provide: (0, build_collection_token_1.buildCollectionToken)(collectionName),
|
|
11
|
+
useFactory: async (databaseService) => {
|
|
12
|
+
logger.verbose(`providing collection_${collectionName}`);
|
|
13
|
+
const collection = databaseService.db.collection(collectionName, options?.mongodbCollectionOptions);
|
|
14
|
+
if (!options?.disableDefaultIndexes) {
|
|
15
|
+
logger.verbose(`creating default indexes for collection_${collectionName}`);
|
|
16
|
+
await collection.createIndex({
|
|
17
|
+
created_at: 1,
|
|
18
|
+
});
|
|
19
|
+
await collection.createIndex({
|
|
20
|
+
updated_at: 1,
|
|
21
|
+
}, { sparse: true });
|
|
22
|
+
}
|
|
23
|
+
return collection;
|
|
24
|
+
},
|
|
25
|
+
inject: [database_service_1.DatabaseService]
|
|
26
|
+
};
|
|
27
|
+
return provider;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=collection.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.factory.js","sourceRoot":"","sources":["../src/collection.factory.ts"],"names":[],"mappings":";;AAeA,8CAsBC;AApCD,yDAAqD;AACrD,2CAAkD;AAElD,2EAAsE;AAEtE,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,mBAAmB,CAAC,CAAC;AAS/C,SAAgB,iBAAiB,CAAqB,cAA+C,EAAE,OAAkC;IACrI,MAAM,QAAQ,GAAG;QACb,OAAO,EAAE,IAAA,6CAAoB,EAAC,cAAc,CAAC;QAC7C,UAAU,EAAE,KAAK,EACb,eAAgC,EAClC,EAAE;YACA,MAAM,CAAC,OAAO,CAAC,wBAAwB,cAAc,EAAE,CAAC,CAAA;YACxD,MAAM,UAAU,GAAG,eAAe,CAAC,EAAE,CAAC,UAAU,CAAI,cAAc,EAAE,OAAO,EAAE,wBAAwB,CAAC,CAAC;YACvG,IAAI,CAAC,OAAO,EAAE,qBAAqB,EAAE,CAAC;gBAClC,MAAM,CAAC,OAAO,CAAC,2CAA2C,cAAc,EAAE,CAAC,CAAA;gBAC3E,MAAM,UAAU,CAAC,WAAW,CAAC;oBACzB,UAAU,EAAE,CAAC;iBAChB,CAAC,CAAC;gBACH,MAAM,UAAU,CAAC,WAAW,CAAC;oBACzB,UAAU,EAAE,CAAC;iBAChB,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YACzB,CAAC;YACD,OAAO,UAAU,CAAC;QACtB,CAAC;QACD,MAAM,EAAE,CAAC,kCAAe,CAAC;KAC5B,CAAC;IACF,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Filter, Collection, ClientSession, FindOptions, UpdateFilter } from 'mongodb';
|
|
2
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
3
|
+
import { Audit } from '@trailmixcms/models';
|
|
4
|
+
import { Creatable } from '../utils';
|
|
5
|
+
type Record = Audit.Entity;
|
|
6
|
+
export declare class AuditCollection implements OnModuleInit {
|
|
7
|
+
protected readonly collection: Collection<Record>;
|
|
8
|
+
private readonly logger;
|
|
9
|
+
readonly collectionName: "audit";
|
|
10
|
+
readonly entitySchema: import("zod").ZodObject<{
|
|
11
|
+
_id: import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodCustom<import("bson").ObjectId, import("bson").ObjectId>>;
|
|
12
|
+
created_at: import("zod").ZodCodec<import("zod").ZodISODateTime, import("zod").ZodDate>;
|
|
13
|
+
updated_at: import("zod").ZodOptional<import("zod").ZodCodec<import("zod").ZodISODateTime, import("zod").ZodDate>>;
|
|
14
|
+
entity_id: import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodCustom<import("bson").ObjectId, import("bson").ObjectId>>;
|
|
15
|
+
entity_type: import("zod").ZodString;
|
|
16
|
+
action: import("zod").ZodEnum<{
|
|
17
|
+
create: "create";
|
|
18
|
+
update: "update";
|
|
19
|
+
delete: "delete";
|
|
20
|
+
}>;
|
|
21
|
+
context: import("zod").ZodObject<{
|
|
22
|
+
account_id: import("zod").ZodOptional<import("zod").ZodCodec<import("zod").ZodString, import("zod").ZodCustom<import("bson").ObjectId, import("bson").ObjectId>>>;
|
|
23
|
+
anonymous: import("zod").ZodOptional<import("zod").ZodBoolean>;
|
|
24
|
+
system: import("zod").ZodBoolean;
|
|
25
|
+
source: import("zod").ZodOptional<import("zod").ZodString>;
|
|
26
|
+
message: import("zod").ZodOptional<import("zod").ZodString>;
|
|
27
|
+
}, import("zod/v4/core").$strip>;
|
|
28
|
+
}, import("zod/v4/core").$strip>;
|
|
29
|
+
constructor(collection: Collection<Record>);
|
|
30
|
+
onModuleInit(): Promise<void>;
|
|
31
|
+
find(filter: Filter<Record>, options?: FindOptions): Promise<import("mongodb").WithId<{
|
|
32
|
+
_id: import("bson").ObjectId;
|
|
33
|
+
created_at: Date;
|
|
34
|
+
entity_id: import("bson").ObjectId;
|
|
35
|
+
entity_type: string;
|
|
36
|
+
action: "create" | "update" | "delete";
|
|
37
|
+
context: {
|
|
38
|
+
system: boolean;
|
|
39
|
+
account_id?: import("bson").ObjectId | undefined;
|
|
40
|
+
anonymous?: boolean | undefined;
|
|
41
|
+
source?: string | undefined;
|
|
42
|
+
message?: string | undefined;
|
|
43
|
+
};
|
|
44
|
+
updated_at?: Date | undefined;
|
|
45
|
+
}>[]>;
|
|
46
|
+
findOne(query: Filter<Record>): Promise<import("mongodb").WithId<{
|
|
47
|
+
_id: import("bson").ObjectId;
|
|
48
|
+
created_at: Date;
|
|
49
|
+
entity_id: import("bson").ObjectId;
|
|
50
|
+
entity_type: string;
|
|
51
|
+
action: "create" | "update" | "delete";
|
|
52
|
+
context: {
|
|
53
|
+
system: boolean;
|
|
54
|
+
account_id?: import("bson").ObjectId | undefined;
|
|
55
|
+
anonymous?: boolean | undefined;
|
|
56
|
+
source?: string | undefined;
|
|
57
|
+
message?: string | undefined;
|
|
58
|
+
};
|
|
59
|
+
updated_at?: Date | undefined;
|
|
60
|
+
}> | null>;
|
|
61
|
+
insertOne(params: Creatable<Record>, session?: ClientSession): Promise<{
|
|
62
|
+
_id: import("bson").ObjectId;
|
|
63
|
+
created_at: Date;
|
|
64
|
+
entity_id: import("bson").ObjectId;
|
|
65
|
+
entity_type: string;
|
|
66
|
+
action: "create" | "update" | "delete";
|
|
67
|
+
context: {
|
|
68
|
+
system: boolean;
|
|
69
|
+
account_id?: import("bson").ObjectId | undefined;
|
|
70
|
+
anonymous?: boolean | undefined;
|
|
71
|
+
source?: string | undefined;
|
|
72
|
+
message?: string | undefined;
|
|
73
|
+
};
|
|
74
|
+
updated_at?: Date | undefined;
|
|
75
|
+
}>;
|
|
76
|
+
findOneAndUpdate(query: Filter<Record>, update: UpdateFilter<Record>, session?: ClientSession): Promise<import("mongodb").WithId<{
|
|
77
|
+
_id: import("bson").ObjectId;
|
|
78
|
+
created_at: Date;
|
|
79
|
+
entity_id: import("bson").ObjectId;
|
|
80
|
+
entity_type: string;
|
|
81
|
+
action: "create" | "update" | "delete";
|
|
82
|
+
context: {
|
|
83
|
+
system: boolean;
|
|
84
|
+
account_id?: import("bson").ObjectId | undefined;
|
|
85
|
+
anonymous?: boolean | undefined;
|
|
86
|
+
source?: string | undefined;
|
|
87
|
+
message?: string | undefined;
|
|
88
|
+
};
|
|
89
|
+
updated_at?: Date | undefined;
|
|
90
|
+
}> | null>;
|
|
91
|
+
}
|
|
92
|
+
export {};
|
|
93
|
+
//# sourceMappingURL=audit.collection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.collection.d.ts","sourceRoot":"","sources":["../../src/collections/audit.collection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvF,OAAO,EAAsB,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE5C,OAAO,EAAE,SAAS,EAAiB,MAAM,UAAU,CAAC;AAGpD,KAAK,MAAM,GAAG,KAAK,CAAC,MAAM,CAAA;AAG1B,qBACa,eAAgB,YAAW,YAAY;IAMR,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,MAAM,CAAC;IALzF,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqC;IAC5D,SAAgB,cAAc,UAAkB;IAChD,SAAgB,YAAY;;;;;;;;;;;;;;;;;;qCAAsB;gBAGS,UAAU,EAAE,UAAU,CAAC,MAAM,CAAC;IAGnF,YAAY;IAOlB,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW;;;;;;;;;;;;;;;IAIlD,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC;;;;;;;;;;;;;;;IAIvB,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa;;;;;;;;;;;;;;;IAc5D,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa;;;;;;;;;;;;;;;CAGtG"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AuditCollection = void 0;
|
|
16
|
+
const mongodb_1 = require("mongodb");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const models_1 = require("@trailmixcms/models");
|
|
19
|
+
const collection_decorator_1 = require("../collection.decorator");
|
|
20
|
+
const utils_1 = require("../utils");
|
|
21
|
+
const constants_1 = require("../constants");
|
|
22
|
+
const collectionName = constants_1.InternalCollectionName.Audit;
|
|
23
|
+
let AuditCollection = class AuditCollection {
|
|
24
|
+
collection;
|
|
25
|
+
logger = new common_1.Logger(this.constructor.name);
|
|
26
|
+
collectionName = collectionName;
|
|
27
|
+
entitySchema = models_1.Audit.entitySchema;
|
|
28
|
+
constructor(collection) {
|
|
29
|
+
this.collection = collection;
|
|
30
|
+
}
|
|
31
|
+
async onModuleInit() {
|
|
32
|
+
this.logger.verbose(`creating custom indexes for collection_${collectionName}`);
|
|
33
|
+
await this.collection.createIndex({ entity_id: 1, created_at: 1 });
|
|
34
|
+
await this.collection.createIndex({ entity_type: 1, created_at: 1 });
|
|
35
|
+
await this.collection.createIndex({ account_id: 1, created_at: 1 });
|
|
36
|
+
}
|
|
37
|
+
find(filter, options) {
|
|
38
|
+
return this.collection.find(filter, options).toArray();
|
|
39
|
+
}
|
|
40
|
+
;
|
|
41
|
+
findOne(query) {
|
|
42
|
+
return this.collection.findOne(query);
|
|
43
|
+
}
|
|
44
|
+
;
|
|
45
|
+
async insertOne(params, session) {
|
|
46
|
+
const entity = (0, utils_1.ensureCreated)(params);
|
|
47
|
+
const encoded = !!models_1.Audit.entitySchema.encode(entity);
|
|
48
|
+
if (!encoded) {
|
|
49
|
+
throw new Error('Failed to encode audit record');
|
|
50
|
+
}
|
|
51
|
+
const { insertedId } = await this.collection.insertOne(entity, session);
|
|
52
|
+
const result = {
|
|
53
|
+
...entity,
|
|
54
|
+
_id: insertedId
|
|
55
|
+
};
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
;
|
|
59
|
+
async findOneAndUpdate(query, update, session) {
|
|
60
|
+
return this.collection.findOneAndUpdate(query, update, { session, returnDocument: 'after' });
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
exports.AuditCollection = AuditCollection;
|
|
64
|
+
exports.AuditCollection = AuditCollection = __decorate([
|
|
65
|
+
(0, common_1.Injectable)(),
|
|
66
|
+
__param(0, (0, collection_decorator_1.DocumentCollection)(collectionName)),
|
|
67
|
+
__metadata("design:paramtypes", [mongodb_1.Collection])
|
|
68
|
+
], AuditCollection);
|
|
69
|
+
//# sourceMappingURL=audit.collection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.collection.js","sourceRoot":"","sources":["../../src/collections/audit.collection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qCAAuF;AACvF,2CAAkE;AAClE,gDAA4C;AAC5C,kEAA6D;AAC7D,oCAAoD;AACpD,4CAAsD;AAGtD,MAAM,cAAc,GAAG,kCAAsB,CAAC,KAAK,CAAC;AAG7C,IAAM,eAAe,GAArB,MAAM,eAAe;IAMmC;IAL1C,MAAM,GAAG,IAAI,eAAM,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAC5C,cAAc,GAAG,cAAc,CAAC;IAChC,YAAY,GAAG,cAAK,CAAC,YAAY,CAAC;IAElD,YAC2D,UAA8B;QAA9B,eAAU,GAAV,UAAU,CAAoB;IACrF,CAAC;IAEL,KAAK,CAAC,YAAY;QACd,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,0CAA0C,cAAc,EAAE,CAAC,CAAA;QAC/E,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;QACnE,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;QACrE,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;IACxE,CAAC;IAED,IAAI,CAAC,MAAsB,EAAE,OAAqB;QAC9C,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;IAC3D,CAAC;IAAA,CAAC;IAEF,OAAO,CAAC,KAAqB;QACzB,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAAA,CAAC;IAEF,KAAK,CAAC,SAAS,CAAC,MAAyB,EAAE,OAAuB;QAC9D,MAAM,MAAM,GAAG,IAAA,qBAAa,EAAe,MAAM,CAAC,CAAC;QACnD,MAAM,OAAO,GAAG,CAAC,CAAC,cAAK,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpD,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;QACrD,CAAC;QACD,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACxE,MAAM,MAAM,GAAW;YACnB,GAAG,MAAM;YACT,GAAG,EAAE,UAAU;SAClB,CAAA;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;IAAA,CAAC;IAEF,KAAK,CAAC,gBAAgB,CAAC,KAAqB,EAAE,MAA4B,EAAE,OAAuB;QAC/F,OAAO,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC;IACjG,CAAC;CACJ,CAAA;AAzCY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;IAOJ,WAAA,IAAA,yCAAkB,EAAC,cAAc,CAAC,CAAA;qCAAgC,oBAAU;GANxE,eAAe,CAyC3B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AuditCollection } from './audit.collection';
|
|
2
|
+
export { AuditCollection } from './audit.collection';
|
|
3
|
+
export declare const collectionServices: (typeof AuditCollection)[];
|
|
4
|
+
export declare const mongoDbCollectionProviders: {
|
|
5
|
+
provide: string;
|
|
6
|
+
useFactory: (databaseService: import("..").DatabaseService) => Promise<import("mongodb").Collection<import("bson").Document>>;
|
|
7
|
+
inject: typeof import("..").DatabaseService[];
|
|
8
|
+
}[];
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAIrD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,eAAO,MAAM,kBAAkB,4BAE9B,CAAC;AAEF,eAAO,MAAM,0BAA0B;;;;GAEtC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mongoDbCollectionProviders = exports.collectionServices = exports.AuditCollection = void 0;
|
|
4
|
+
const audit_collection_1 = require("./audit.collection");
|
|
5
|
+
const collection_factory_1 = require("../collection.factory");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
|
+
var audit_collection_2 = require("./audit.collection");
|
|
8
|
+
Object.defineProperty(exports, "AuditCollection", { enumerable: true, get: function () { return audit_collection_2.AuditCollection; } });
|
|
9
|
+
exports.collectionServices = [
|
|
10
|
+
audit_collection_1.AuditCollection,
|
|
11
|
+
];
|
|
12
|
+
exports.mongoDbCollectionProviders = [
|
|
13
|
+
(0, collection_factory_1.collectionFactory)(constants_1.InternalCollectionName.Audit),
|
|
14
|
+
];
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":";;;AAAA,yDAAqD;AACrD,8DAA0D;AAC1D,4CAAsD;AAEtD,uDAAqD;AAA5C,mHAAA,eAAe,OAAA;AAEX,QAAA,kBAAkB,GAAG;IAC9B,kCAAe;CAClB,CAAC;AAEW,QAAA,0BAA0B,GAAG;IACtC,IAAA,sCAAiB,EAAC,kCAAsB,CAAC,KAAK,CAAC;CAClD,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const configuration: () => {
|
|
2
|
+
onModuleInit: boolean;
|
|
3
|
+
MONGODB_CONNECTION_STRING: string;
|
|
4
|
+
MONGODB_DATABASE_NAME: string;
|
|
5
|
+
GENERATE_SPEC: boolean;
|
|
6
|
+
isDevelopment: boolean;
|
|
7
|
+
isDev: boolean;
|
|
8
|
+
isTest: boolean;
|
|
9
|
+
isProduction: boolean;
|
|
10
|
+
isProd: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type AppConfig = ReturnType<typeof configuration>;
|
|
13
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa;;;;;;;;;;CAmBzB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configuration = void 0;
|
|
4
|
+
const envalid_1 = require("envalid");
|
|
5
|
+
const configuration = () => {
|
|
6
|
+
const config = (0, envalid_1.cleanEnv)(process.env, {
|
|
7
|
+
MONGODB_CONNECTION_STRING: (0, envalid_1.str)({
|
|
8
|
+
desc: 'Connection string to mongodb',
|
|
9
|
+
...(process.env.GENERATE_SPEC === 'true' ? { default: '', allowEmpty: true } : {}),
|
|
10
|
+
}),
|
|
11
|
+
MONGODB_DATABASE_NAME: (0, envalid_1.str)({
|
|
12
|
+
desc: 'Database Name',
|
|
13
|
+
default: 'main'
|
|
14
|
+
}),
|
|
15
|
+
GENERATE_SPEC: (0, envalid_1.bool)({
|
|
16
|
+
desc: 'Generate OpenAPI spec',
|
|
17
|
+
default: false
|
|
18
|
+
}),
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
...config,
|
|
22
|
+
onModuleInit: false // Fix issue where nestjs checks for onModuleInit hook and fails if it doesn't exist
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
exports.configuration = configuration;
|
|
26
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;AAAA,qCAA8C;AAEvC,MAAM,aAAa,GAAG,GAAG,EAAE;IAC9B,MAAM,MAAM,GAAG,IAAA,kBAAQ,EAAC,OAAO,CAAC,GAAG,EAAE;QACjC,yBAAyB,EAAE,IAAA,aAAG,EAAC;YAC3B,IAAI,EAAE,8BAA8B;YACpC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACrF,CAAC;QACF,qBAAqB,EAAE,IAAA,aAAG,EAAC;YACvB,IAAI,EAAE,eAAe;YACrB,OAAO,EAAE,MAAM;SAClB,CAAC;QACF,aAAa,EAAE,IAAA,cAAI,EAAC;YAChB,IAAI,EAAE,uBAAuB;YAC7B,OAAO,EAAE,KAAK;SACjB,CAAC;KACL,CAAC,CAAC;IACH,OAAO;QACH,GAAG,MAAM;QACT,YAAY,EAAE,KAAK,CAAC,oFAAoF;KAC3G,CAAC;AACN,CAAC,CAAC;AAnBW,QAAA,aAAa,iBAmBxB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as mongodb from 'mongodb';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
3
|
+
import { AppConfig } from './config';
|
|
4
|
+
export declare const connectionFactory: {
|
|
5
|
+
provide: string;
|
|
6
|
+
inject: {
|
|
7
|
+
new (internalConfig?: Record<string, any>): ConfigService<{
|
|
8
|
+
onModuleInit: boolean;
|
|
9
|
+
MONGODB_CONNECTION_STRING: string;
|
|
10
|
+
MONGODB_DATABASE_NAME: string;
|
|
11
|
+
GENERATE_SPEC: boolean;
|
|
12
|
+
isDevelopment: boolean;
|
|
13
|
+
isDev: boolean;
|
|
14
|
+
isTest: boolean;
|
|
15
|
+
isProduction: boolean;
|
|
16
|
+
isProd: boolean;
|
|
17
|
+
}, false>;
|
|
18
|
+
}[];
|
|
19
|
+
useFactory: (configService: ConfigService<AppConfig>) => Promise<{
|
|
20
|
+
client: mongodb.MongoClient;
|
|
21
|
+
db: mongodb.Db;
|
|
22
|
+
}>;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=connection.factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.factory.d.ts","sourceRoot":"","sources":["../src/connection.factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAKrC,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;gCAIP,aAAa,CAAC,SAAS,CAAC;gBAIvB,OAAO,CAAC,WAAW;YACvB,OAAO,CAAC,EAAE;;CA2B7B,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.connectionFactory = void 0;
|
|
37
|
+
const mongodb = __importStar(require("mongodb"));
|
|
38
|
+
const config_1 = require("@nestjs/config");
|
|
39
|
+
const common_1 = require("@nestjs/common");
|
|
40
|
+
const logger = new common_1.Logger('ConnectionFactory');
|
|
41
|
+
exports.connectionFactory = {
|
|
42
|
+
provide: 'DB_CONNECTION',
|
|
43
|
+
inject: [(config_1.ConfigService)],
|
|
44
|
+
useFactory: async (configService) => {
|
|
45
|
+
if (configService.get('GENERATE_SPEC')) {
|
|
46
|
+
const mockedConnection = {
|
|
47
|
+
client: {
|
|
48
|
+
close: () => { }
|
|
49
|
+
},
|
|
50
|
+
db: {
|
|
51
|
+
collection: () => ({
|
|
52
|
+
createIndex: () => { },
|
|
53
|
+
findOne: () => ({}),
|
|
54
|
+
insertOne: () => { },
|
|
55
|
+
countDocuments: () => 0,
|
|
56
|
+
}),
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return mockedConnection;
|
|
60
|
+
}
|
|
61
|
+
const connectionString = configService.get('MONGODB_CONNECTION_STRING');
|
|
62
|
+
const databaseName = configService.get('MONGODB_DATABASE_NAME');
|
|
63
|
+
const client = new mongodb.MongoClient(connectionString);
|
|
64
|
+
logger.verbose('Connecting to mongodb...');
|
|
65
|
+
await client.connect();
|
|
66
|
+
logger.verbose('Connected to mongodb.');
|
|
67
|
+
const db = client.db(databaseName, { ignoreUndefined: true });
|
|
68
|
+
return { client, db };
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
//# sourceMappingURL=connection.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.factory.js","sourceRoot":"","sources":["../src/connection.factory.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAmC;AACnC,2CAA+C;AAE/C,2CAAwC;AAExC,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,mBAAmB,CAAC,CAAC;AAElC,QAAA,iBAAiB,GAAG;IAC7B,OAAO,EAAE,eAAe;IACxB,MAAM,EAAE,CAAC,CAAA,sBAAwB,CAAA,CAAC;IAClC,UAAU,EAAE,KAAK,EACb,aAAuC,EACzC,EAAE;QACA,IAAI,aAAa,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC;YACrC,MAAM,gBAAgB,GAGlB;gBACA,MAAM,EAAE;oBACJ,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC;iBACnB;gBACD,EAAE,EAAE;oBACA,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC;wBACf,WAAW,EAAE,GAAG,EAAE,GAAG,CAAC;wBACtB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;wBAC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