@digitaldefiance/node-express-suite 3.12.16 → 3.13.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/package.json +1 -1
- package/src/application-base.d.ts +50 -47
- package/src/application-base.d.ts.map +1 -1
- package/src/application-base.js +180 -229
- package/src/application-base.js.map +1 -1
- package/src/application.d.ts +2 -1
- package/src/application.d.ts.map +1 -1
- package/src/application.js +8 -4
- package/src/application.js.map +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.d.ts.map +1 -1
- package/src/index.js +3 -1
- package/src/index.js.map +1 -1
- package/src/interfaces/document-store.d.ts +38 -0
- package/src/interfaces/document-store.d.ts.map +1 -0
- package/src/interfaces/document-store.js +8 -0
- package/src/interfaces/document-store.js.map +1 -0
- package/src/interfaces/failable-result.d.ts +1 -6
- package/src/interfaces/failable-result.d.ts.map +1 -1
- package/src/interfaces/failable-result.js +0 -5
- package/src/interfaces/failable-result.js.map +1 -1
- package/src/interfaces/index.d.ts +2 -0
- package/src/interfaces/index.d.ts.map +1 -1
- package/src/interfaces/index.js +2 -0
- package/src/interfaces/index.js.map +1 -1
- package/src/interfaces/storage/client-session.d.ts +22 -0
- package/src/interfaces/storage/client-session.d.ts.map +1 -0
- package/src/interfaces/storage/client-session.js +3 -0
- package/src/interfaces/storage/client-session.js.map +1 -0
- package/src/interfaces/storage/collection.d.ts +80 -0
- package/src/interfaces/storage/collection.d.ts.map +1 -0
- package/src/interfaces/storage/collection.js +3 -0
- package/src/interfaces/storage/collection.js.map +1 -0
- package/src/interfaces/storage/database-lifecycle-hooks.d.ts +43 -0
- package/src/interfaces/storage/database-lifecycle-hooks.d.ts.map +1 -0
- package/src/interfaces/storage/database-lifecycle-hooks.js +3 -0
- package/src/interfaces/storage/database-lifecycle-hooks.js.map +1 -0
- package/src/interfaces/storage/database.d.ts +30 -0
- package/src/interfaces/storage/database.d.ts.map +1 -0
- package/src/interfaces/storage/database.js +3 -0
- package/src/interfaces/storage/database.js.map +1 -0
- package/src/interfaces/storage/document-types.d.ts +413 -0
- package/src/interfaces/storage/document-types.d.ts.map +1 -0
- package/src/interfaces/storage/document-types.js +8 -0
- package/src/interfaces/storage/document-types.js.map +1 -0
- package/src/interfaces/storage/index.d.ts +6 -0
- package/src/interfaces/storage/index.d.ts.map +1 -0
- package/src/interfaces/storage/index.js +9 -0
- package/src/interfaces/storage/index.js.map +1 -0
- package/src/services/index.d.ts +4 -0
- package/src/services/index.d.ts.map +1 -1
- package/src/services/index.js +4 -0
- package/src/services/index.js.map +1 -1
- package/src/services/mongoose-collection.d.ts +52 -0
- package/src/services/mongoose-collection.d.ts.map +1 -0
- package/src/services/mongoose-collection.js +326 -0
- package/src/services/mongoose-collection.js.map +1 -0
- package/src/services/mongoose-database.d.ts +64 -0
- package/src/services/mongoose-database.d.ts.map +1 -0
- package/src/services/mongoose-database.js +121 -0
- package/src/services/mongoose-database.js.map +1 -0
- package/src/services/mongoose-document-store.d.ts +109 -0
- package/src/services/mongoose-document-store.d.ts.map +1 -0
- package/src/services/mongoose-document-store.js +264 -0
- package/src/services/mongoose-document-store.js.map +1 -0
- package/src/services/mongoose-session-adapter.d.ts +39 -0
- package/src/services/mongoose-session-adapter.d.ts.map +1 -0
- package/src/services/mongoose-session-adapter.js +63 -0
- package/src/services/mongoose-session-adapter.js.map +1 -0
- package/src/utils/default-mongo-uri-validator.d.ts +15 -0
- package/src/utils/default-mongo-uri-validator.d.ts.map +1 -0
- package/src/utils/default-mongo-uri-validator.js +46 -0
- package/src/utils/default-mongo-uri-validator.js.map +1 -0
- package/src/utils.d.ts +21 -2
- package/src/utils.d.ts.map +1 -1
- package/src/utils.js +124 -49
- package/src/utils.js.map +1 -1
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Mongoose collection adapter implementing ICollection.
|
|
4
|
+
* Wraps a mongoose Model to conform to the shared ICollection interface
|
|
5
|
+
* from brightchain-lib.
|
|
6
|
+
* @module services/mongoose-collection
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.MongooseCollection = void 0;
|
|
10
|
+
const mongoose_types_1 = require("@digitaldefiance/mongoose-types");
|
|
11
|
+
const mongoose_session_adapter_1 = require("./mongoose-session-adapter");
|
|
12
|
+
/**
|
|
13
|
+
* Extract the native mongodb ClientSession from WriteOptions if present.
|
|
14
|
+
* The IClientSession in options may be a MongooseSessionAdapter wrapping
|
|
15
|
+
* a native session.
|
|
16
|
+
*/
|
|
17
|
+
function extractNativeSession(options) {
|
|
18
|
+
if (!options?.session)
|
|
19
|
+
return undefined;
|
|
20
|
+
if (options.session instanceof mongoose_session_adapter_1.MongooseSessionAdapter) {
|
|
21
|
+
return options.session.nativeSession;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Adapts a mongoose Model to the ICollection<T> interface.
|
|
27
|
+
* Delegates CRUD, query, index, and bulk operations to the underlying
|
|
28
|
+
* mongoose model. Schema validation methods are no-ops since mongoose
|
|
29
|
+
* handles schema validation internally.
|
|
30
|
+
*/
|
|
31
|
+
class MongooseCollection {
|
|
32
|
+
_model;
|
|
33
|
+
_writeConcern = { w: 1 };
|
|
34
|
+
_readPreference = 'primary';
|
|
35
|
+
constructor(connection, collectionName) {
|
|
36
|
+
// Get existing model or create a permissive one with a flexible schema
|
|
37
|
+
try {
|
|
38
|
+
this._model = connection.model(collectionName);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Model doesn't exist yet — create with a permissive schema
|
|
42
|
+
const schema = new mongoose_types_1.Schema({}, { strict: false, versionKey: false });
|
|
43
|
+
this._model = connection.model(collectionName, schema);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// ── CRUD ──
|
|
47
|
+
async insertOne(doc, options) {
|
|
48
|
+
const session = extractNativeSession(options);
|
|
49
|
+
const created = await this._model.create([doc], { session });
|
|
50
|
+
const first = created[0];
|
|
51
|
+
return {
|
|
52
|
+
acknowledged: true,
|
|
53
|
+
insertedId: String(first._id),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async insertMany(docs, options) {
|
|
57
|
+
const session = extractNativeSession(options);
|
|
58
|
+
const created = await this._model.create(docs, { session });
|
|
59
|
+
const insertedIds = {};
|
|
60
|
+
for (let i = 0; i < created.length; i++) {
|
|
61
|
+
insertedIds[i] = String(created[i]._id);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
acknowledged: true,
|
|
65
|
+
insertedCount: created.length,
|
|
66
|
+
insertedIds,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async findOne(filter, options) {
|
|
70
|
+
const session = extractNativeSession(options);
|
|
71
|
+
const query = this._model.findOne((filter ?? {}), options?.projection, { session, lean: true });
|
|
72
|
+
if (options?.sort) {
|
|
73
|
+
query.sort(options.sort);
|
|
74
|
+
}
|
|
75
|
+
const result = await query.exec();
|
|
76
|
+
return result ?? null;
|
|
77
|
+
}
|
|
78
|
+
async find(filter, options) {
|
|
79
|
+
const session = extractNativeSession(options);
|
|
80
|
+
const query = this._model.find((filter ?? {}), options?.projection, { session, lean: true });
|
|
81
|
+
if (options?.sort) {
|
|
82
|
+
query.sort(options.sort);
|
|
83
|
+
}
|
|
84
|
+
if (options?.skip !== undefined) {
|
|
85
|
+
query.skip(options.skip);
|
|
86
|
+
}
|
|
87
|
+
if (options?.limit !== undefined) {
|
|
88
|
+
query.limit(options.limit);
|
|
89
|
+
}
|
|
90
|
+
const results = await query.exec();
|
|
91
|
+
return results;
|
|
92
|
+
}
|
|
93
|
+
async findById(id) {
|
|
94
|
+
const result = await this._model.findById(id).lean().exec();
|
|
95
|
+
return result ?? null;
|
|
96
|
+
}
|
|
97
|
+
async updateOne(filter, update, options) {
|
|
98
|
+
const session = extractNativeSession(options);
|
|
99
|
+
const result = await this._model.updateOne(filter, update, { session, upsert: options?.upsert });
|
|
100
|
+
return {
|
|
101
|
+
acknowledged: result.acknowledged,
|
|
102
|
+
matchedCount: result.matchedCount,
|
|
103
|
+
modifiedCount: result.modifiedCount,
|
|
104
|
+
upsertedCount: result.upsertedCount,
|
|
105
|
+
upsertedId: result.upsertedId ? String(result.upsertedId) : undefined,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
async updateMany(filter, update, options) {
|
|
109
|
+
const session = extractNativeSession(options);
|
|
110
|
+
const result = await this._model.updateMany(filter, update, { session, upsert: options?.upsert });
|
|
111
|
+
return {
|
|
112
|
+
acknowledged: result.acknowledged,
|
|
113
|
+
matchedCount: result.matchedCount,
|
|
114
|
+
modifiedCount: result.modifiedCount,
|
|
115
|
+
upsertedCount: result.upsertedCount,
|
|
116
|
+
upsertedId: result.upsertedId ? String(result.upsertedId) : undefined,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
async deleteOne(filter, options) {
|
|
120
|
+
const session = extractNativeSession(options);
|
|
121
|
+
const result = await this._model.deleteOne(filter, { session });
|
|
122
|
+
return {
|
|
123
|
+
acknowledged: result.acknowledged,
|
|
124
|
+
deletedCount: result.deletedCount,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async deleteMany(filter, options) {
|
|
128
|
+
const session = extractNativeSession(options);
|
|
129
|
+
const result = await this._model.deleteMany(filter, { session });
|
|
130
|
+
return {
|
|
131
|
+
acknowledged: result.acknowledged,
|
|
132
|
+
deletedCount: result.deletedCount,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
async replaceOne(filter, doc, options) {
|
|
136
|
+
const session = extractNativeSession(options);
|
|
137
|
+
const result = await this._model.replaceOne(filter, doc, { session, upsert: options?.upsert });
|
|
138
|
+
return {
|
|
139
|
+
acknowledged: result.acknowledged,
|
|
140
|
+
matchedCount: result.matchedCount,
|
|
141
|
+
modifiedCount: result.modifiedCount,
|
|
142
|
+
upsertedCount: result.upsertedCount,
|
|
143
|
+
upsertedId: result.upsertedId ? String(result.upsertedId) : undefined,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
// ── Query ──
|
|
147
|
+
async countDocuments(filter) {
|
|
148
|
+
return this._model
|
|
149
|
+
.countDocuments((filter ?? {}))
|
|
150
|
+
.exec();
|
|
151
|
+
}
|
|
152
|
+
async estimatedDocumentCount() {
|
|
153
|
+
return this._model.estimatedDocumentCount().exec();
|
|
154
|
+
}
|
|
155
|
+
async distinct(field, filter) {
|
|
156
|
+
const result = await this._model
|
|
157
|
+
.distinct(String(field), (filter ?? {}))
|
|
158
|
+
.exec();
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
async aggregate(pipeline) {
|
|
162
|
+
// Use the native collection's aggregate to avoid mongoose PipelineStage type constraints
|
|
163
|
+
const cursor = this._model.collection.aggregate(pipeline);
|
|
164
|
+
const result = await cursor.toArray();
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
// ── Indexes ──
|
|
168
|
+
async createIndex(spec, options) {
|
|
169
|
+
const indexName = await this._model.collection.createIndex(spec, {
|
|
170
|
+
unique: options?.unique,
|
|
171
|
+
name: options?.name,
|
|
172
|
+
sparse: options?.sparse,
|
|
173
|
+
background: options?.background,
|
|
174
|
+
expireAfterSeconds: options?.expireAfterSeconds,
|
|
175
|
+
});
|
|
176
|
+
return indexName;
|
|
177
|
+
}
|
|
178
|
+
async dropIndex(name) {
|
|
179
|
+
await this._model.collection.dropIndex(name);
|
|
180
|
+
}
|
|
181
|
+
listIndexes() {
|
|
182
|
+
// Synchronous listing is not directly supported by mongoose.
|
|
183
|
+
// Return an empty array; callers needing async index listing
|
|
184
|
+
// should use the native collection directly.
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
// ── Bulk operations ──
|
|
188
|
+
async bulkWrite(operations, options) {
|
|
189
|
+
const session = extractNativeSession(options);
|
|
190
|
+
const mongooseOps = operations.map((op) => {
|
|
191
|
+
if ('insertOne' in op) {
|
|
192
|
+
return {
|
|
193
|
+
insertOne: {
|
|
194
|
+
document: op.insertOne.document,
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
if ('updateOne' in op) {
|
|
199
|
+
return {
|
|
200
|
+
updateOne: {
|
|
201
|
+
filter: op.updateOne.filter,
|
|
202
|
+
update: op.updateOne.update,
|
|
203
|
+
upsert: op.updateOne.upsert,
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if ('updateMany' in op) {
|
|
208
|
+
return {
|
|
209
|
+
updateMany: {
|
|
210
|
+
filter: op.updateMany.filter,
|
|
211
|
+
update: op.updateMany.update,
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
if ('deleteOne' in op) {
|
|
216
|
+
return {
|
|
217
|
+
deleteOne: { filter: op.deleteOne.filter },
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
if ('deleteMany' in op) {
|
|
221
|
+
return {
|
|
222
|
+
deleteMany: {
|
|
223
|
+
filter: op.deleteMany.filter,
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
if ('replaceOne' in op) {
|
|
228
|
+
return {
|
|
229
|
+
replaceOne: {
|
|
230
|
+
filter: op.replaceOne.filter,
|
|
231
|
+
replacement: op.replaceOne.replacement,
|
|
232
|
+
upsert: op.replaceOne.upsert,
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
return op;
|
|
237
|
+
});
|
|
238
|
+
const result = await this._model.bulkWrite(mongooseOps, { session, ordered: options?.ordered });
|
|
239
|
+
return {
|
|
240
|
+
acknowledged: true,
|
|
241
|
+
insertedCount: result.insertedCount,
|
|
242
|
+
matchedCount: result.matchedCount,
|
|
243
|
+
modifiedCount: result.modifiedCount,
|
|
244
|
+
deletedCount: result.deletedCount,
|
|
245
|
+
upsertedCount: result.upsertedCount,
|
|
246
|
+
insertedIds: {},
|
|
247
|
+
upsertedIds: {},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
// ── Change streams ──
|
|
251
|
+
watch(listener) {
|
|
252
|
+
const changeStream = this._model.watch();
|
|
253
|
+
changeStream.on('change', (change) => {
|
|
254
|
+
listener({
|
|
255
|
+
operationType: change['operationType'],
|
|
256
|
+
documentKey: change['documentKey'],
|
|
257
|
+
fullDocument: change['fullDocument'],
|
|
258
|
+
updateDescription: change['updateDescription'],
|
|
259
|
+
ns: change['ns'],
|
|
260
|
+
timestamp: new Date(),
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
return () => {
|
|
264
|
+
void changeStream.close();
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// ── Schema validation (no-ops for mongoose) ──
|
|
268
|
+
setSchema(_schema) {
|
|
269
|
+
// No-op: mongoose handles schema validation internally
|
|
270
|
+
}
|
|
271
|
+
getSchema() {
|
|
272
|
+
// No-op: mongoose handles schema validation internally
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
removeSchema() {
|
|
276
|
+
// No-op: mongoose handles schema validation internally
|
|
277
|
+
}
|
|
278
|
+
validateDoc(_doc) {
|
|
279
|
+
// No validation errors — mongoose handles validation internally
|
|
280
|
+
return [];
|
|
281
|
+
}
|
|
282
|
+
// ── Write concern / Read preference ──
|
|
283
|
+
getWriteConcern() {
|
|
284
|
+
return this._writeConcern;
|
|
285
|
+
}
|
|
286
|
+
setWriteConcern(wc) {
|
|
287
|
+
this._writeConcern = wc;
|
|
288
|
+
}
|
|
289
|
+
getReadPreference() {
|
|
290
|
+
return this._readPreference;
|
|
291
|
+
}
|
|
292
|
+
setReadPreference(rp) {
|
|
293
|
+
this._readPreference = rp;
|
|
294
|
+
}
|
|
295
|
+
// ── Text index ──
|
|
296
|
+
createTextIndex(options) {
|
|
297
|
+
const indexSpec = {};
|
|
298
|
+
for (const field of Object.keys(options.fields)) {
|
|
299
|
+
indexSpec[field] = 'text';
|
|
300
|
+
}
|
|
301
|
+
const indexName = options.name ?? 'text_index';
|
|
302
|
+
void this._model.collection.createIndex(indexSpec, {
|
|
303
|
+
name: indexName,
|
|
304
|
+
weights: options.fields,
|
|
305
|
+
default_language: options.defaultLanguage,
|
|
306
|
+
});
|
|
307
|
+
return indexName;
|
|
308
|
+
}
|
|
309
|
+
dropTextIndex() {
|
|
310
|
+
// Attempt to drop the text index; best-effort
|
|
311
|
+
void this._model.collection.dropIndex('text_index').catch(() => {
|
|
312
|
+
// Ignore errors if the index doesn't exist
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
hasTextIndex() {
|
|
316
|
+
// Synchronous check is not possible with mongoose.
|
|
317
|
+
// Return false; callers needing accurate info should check asynchronously.
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
// ── Lifecycle ──
|
|
321
|
+
async drop() {
|
|
322
|
+
await this._model.collection.drop();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
exports.MongooseCollection = MongooseCollection;
|
|
326
|
+
//# sourceMappingURL=mongoose-collection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoose-collection.js","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-node-express-suite/src/services/mongoose-collection.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AA8BH,oEAAyD;AAEzD,yEAAoE;AAEpE;;;;GAIG;AACH,SAAS,oBAAoB,CAC3B,OAAkE;IAElE,IAAI,CAAC,OAAO,EAAE,OAAO;QAAE,OAAO,SAAS,CAAC;IACxC,IAAI,OAAO,CAAC,OAAO,YAAY,iDAAsB,EAAE,CAAC;QACtD,OAAO,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;IACvC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;GAKG;AACH,MAAa,kBAAkB;IAGrB,MAAM,CAAW;IACjB,aAAa,GAAiB,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;IACvC,eAAe,GAAmB,SAAS,CAAC;IAEp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C;QAC/C,KAAK,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CACrC,SAA4C,EAC5C;YACE,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,OAAO,CAAC,MAAM;YACvB,gBAAgB,EAAE,OAAO,CAAC,eAAe;SAC1C,CACF,CAAC;QACF,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,aAAa;QACX,8CAA8C;QAC9C,KAAK,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;YAC7D,2CAA2C;QAC7C,CAAC,CAAC,CAAC;IACL,CAAC;IAED,YAAY;QACV,mDAAmD;QACnD,2EAA2E;QAC3E,OAAO,KAAK,CAAC;IACf,CAAC;IAED,kBAAkB;IAElB,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IACtC,CAAC;CACF;AAvZD,gDAuZC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Mongoose database adapter implementing IDatabase.
|
|
3
|
+
* Wraps a mongoose connection to conform to the shared IDatabase interface
|
|
4
|
+
* from brightchain-lib, enabling application code to work with mongoose
|
|
5
|
+
* through the unified database contract.
|
|
6
|
+
* @module services/mongoose-database
|
|
7
|
+
*/
|
|
8
|
+
import type { BsonDocument, CollectionOptions, IClientSession, ICollection, IDatabase } from '../interfaces/storage';
|
|
9
|
+
import type { Connection } from '@digitaldefiance/mongoose-types';
|
|
10
|
+
/**
|
|
11
|
+
* Adapts a mongoose connection to the IDatabase interface.
|
|
12
|
+
* Allows existing mongoose-based applications to use the unified
|
|
13
|
+
* IDatabase contract from brightchain-lib.
|
|
14
|
+
*/
|
|
15
|
+
export declare class MongooseDatabase implements IDatabase {
|
|
16
|
+
private _connection;
|
|
17
|
+
/**
|
|
18
|
+
* @param connection - Optional mongoose connection to wrap.
|
|
19
|
+
* Defaults to the global mongoose.connection if not provided.
|
|
20
|
+
*/
|
|
21
|
+
constructor(connection?: Connection);
|
|
22
|
+
/**
|
|
23
|
+
* Get the underlying mongoose connection for direct access when needed.
|
|
24
|
+
*/
|
|
25
|
+
get connection(): Connection;
|
|
26
|
+
/**
|
|
27
|
+
* Connect to MongoDB via mongoose.
|
|
28
|
+
* If a URI is provided, establishes a new connection.
|
|
29
|
+
* If already connected, this is a no-op.
|
|
30
|
+
*/
|
|
31
|
+
connect(uri?: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Disconnect from MongoDB.
|
|
34
|
+
*/
|
|
35
|
+
disconnect(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Whether the mongoose connection is in the 'connected' state.
|
|
38
|
+
*/
|
|
39
|
+
isConnected(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Get or create a collection by name, returning an ICollection adapter.
|
|
42
|
+
*/
|
|
43
|
+
collection<T extends BsonDocument = BsonDocument>(name: string, _options?: CollectionOptions): ICollection<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Start a client session for transaction support.
|
|
46
|
+
* Wraps the mongoose session in an IClientSession adapter.
|
|
47
|
+
*/
|
|
48
|
+
startSession(): IClientSession;
|
|
49
|
+
/**
|
|
50
|
+
* Execute a callback within a mongoose transaction with retry logic.
|
|
51
|
+
* Handles session lifecycle: start → transaction → commit/abort → end.
|
|
52
|
+
*/
|
|
53
|
+
withTransaction<R>(fn: (session: IClientSession) => Promise<R>): Promise<R>;
|
|
54
|
+
/**
|
|
55
|
+
* List all collection names known to this connection.
|
|
56
|
+
*/
|
|
57
|
+
listCollections(): string[];
|
|
58
|
+
/**
|
|
59
|
+
* Drop a collection by name.
|
|
60
|
+
* @returns true if the collection was dropped, false if it didn't exist.
|
|
61
|
+
*/
|
|
62
|
+
dropCollection(name: string): Promise<boolean>;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=mongoose-database.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoose-database.d.ts","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-node-express-suite/src/services/mongoose-database.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,cAAc,EACd,WAAW,EACX,SAAS,EACV,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAIlE;;;;GAIG;AACH,qBAAa,gBAAiB,YAAW,SAAS;IAChD,OAAO,CAAC,WAAW,CAAa;IAEhC;;;OAGG;gBACS,UAAU,CAAC,EAAE,UAAU;IAInC;;OAEG;IACH,IAAI,UAAU,IAAI,UAAU,CAE3B;IAED;;;;OAIG;IACG,OAAO,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAO1C;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAMjC;;OAEG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,UAAU,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAC9C,IAAI,EAAE,MAAM,EACZ,QAAQ,CAAC,EAAE,iBAAiB,GAC3B,WAAW,CAAC,CAAC,CAAC;IAIjB;;;OAGG;IACH,YAAY,IAAI,cAAc;IAW9B;;;OAGG;IACG,eAAe,CAAC,CAAC,EACrB,EAAE,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC,CAAC,GAC1C,OAAO,CAAC,CAAC,CAAC;IAeb;;OAEG;IACH,eAAe,IAAI,MAAM,EAAE;IAI3B;;;OAGG;IACG,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAQrD"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Mongoose database adapter implementing IDatabase.
|
|
4
|
+
* Wraps a mongoose connection to conform to the shared IDatabase interface
|
|
5
|
+
* from brightchain-lib, enabling application code to work with mongoose
|
|
6
|
+
* through the unified database contract.
|
|
7
|
+
* @module services/mongoose-database
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.MongooseDatabase = void 0;
|
|
11
|
+
const tslib_1 = require("tslib");
|
|
12
|
+
const mongoose_types_1 = tslib_1.__importDefault(require("@digitaldefiance/mongoose-types"));
|
|
13
|
+
const mongoose_collection_1 = require("./mongoose-collection");
|
|
14
|
+
const mongoose_session_adapter_1 = require("./mongoose-session-adapter");
|
|
15
|
+
/**
|
|
16
|
+
* Adapts a mongoose connection to the IDatabase interface.
|
|
17
|
+
* Allows existing mongoose-based applications to use the unified
|
|
18
|
+
* IDatabase contract from brightchain-lib.
|
|
19
|
+
*/
|
|
20
|
+
class MongooseDatabase {
|
|
21
|
+
_connection;
|
|
22
|
+
/**
|
|
23
|
+
* @param connection - Optional mongoose connection to wrap.
|
|
24
|
+
* Defaults to the global mongoose.connection if not provided.
|
|
25
|
+
*/
|
|
26
|
+
constructor(connection) {
|
|
27
|
+
this._connection = connection ?? mongoose_types_1.default.connection;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the underlying mongoose connection for direct access when needed.
|
|
31
|
+
*/
|
|
32
|
+
get connection() {
|
|
33
|
+
return this._connection;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Connect to MongoDB via mongoose.
|
|
37
|
+
* If a URI is provided, establishes a new connection.
|
|
38
|
+
* If already connected, this is a no-op.
|
|
39
|
+
*/
|
|
40
|
+
async connect(uri) {
|
|
41
|
+
if (uri) {
|
|
42
|
+
await mongoose_types_1.default.connect(uri);
|
|
43
|
+
this._connection = mongoose_types_1.default.connection;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Disconnect from MongoDB.
|
|
48
|
+
*/
|
|
49
|
+
async disconnect() {
|
|
50
|
+
if (this._connection.readyState !== 0) {
|
|
51
|
+
await mongoose_types_1.default.disconnect();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Whether the mongoose connection is in the 'connected' state.
|
|
56
|
+
*/
|
|
57
|
+
isConnected() {
|
|
58
|
+
return this._connection.readyState === 1;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get or create a collection by name, returning an ICollection adapter.
|
|
62
|
+
*/
|
|
63
|
+
collection(name, _options) {
|
|
64
|
+
return new mongoose_collection_1.MongooseCollection(this._connection, name);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Start a client session for transaction support.
|
|
68
|
+
* Wraps the mongoose session in an IClientSession adapter.
|
|
69
|
+
*/
|
|
70
|
+
startSession() {
|
|
71
|
+
// mongoose.connection.startSession() is async, but IDatabase declares
|
|
72
|
+
// startSession() as synchronous. We create a deferred session adapter
|
|
73
|
+
// that initializes the native session lazily on first transaction use.
|
|
74
|
+
//
|
|
75
|
+
// For the synchronous contract, we use the underlying client directly.
|
|
76
|
+
const client = this._connection.getClient();
|
|
77
|
+
const nativeSession = client.startSession();
|
|
78
|
+
return new mongoose_session_adapter_1.MongooseSessionAdapter(nativeSession);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Execute a callback within a mongoose transaction with retry logic.
|
|
82
|
+
* Handles session lifecycle: start → transaction → commit/abort → end.
|
|
83
|
+
*/
|
|
84
|
+
async withTransaction(fn) {
|
|
85
|
+
const adapter = this.startSession();
|
|
86
|
+
adapter.startTransaction();
|
|
87
|
+
try {
|
|
88
|
+
const result = await fn(adapter);
|
|
89
|
+
await adapter.commitTransaction();
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
await adapter.abortTransaction();
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
adapter.endSession();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* List all collection names known to this connection.
|
|
102
|
+
*/
|
|
103
|
+
listCollections() {
|
|
104
|
+
return Object.keys(this._connection.collections);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Drop a collection by name.
|
|
108
|
+
* @returns true if the collection was dropped, false if it didn't exist.
|
|
109
|
+
*/
|
|
110
|
+
async dropCollection(name) {
|
|
111
|
+
try {
|
|
112
|
+
await this._connection.dropCollection(name);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.MongooseDatabase = MongooseDatabase;
|
|
121
|
+
//# sourceMappingURL=mongoose-database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoose-database.js","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-node-express-suite/src/services/mongoose-database.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;AASH,6FAAuD;AAEvD,+DAA2D;AAC3D,yEAAoE;AAEpE;;;;GAIG;AACH,MAAa,gBAAgB;IACnB,WAAW,CAAa;IAEhC;;;OAGG;IACH,YAAY,UAAuB;QACjC,IAAI,CAAC,WAAW,GAAG,UAAU,IAAI,wBAAQ,CAAC,UAAU,CAAC;IACvD,CAAC;IAED;;OAEG;IACH,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,OAAO,CAAC,GAAY;QACxB,IAAI,GAAG,EAAE,CAAC;YACR,MAAM,wBAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC5B,IAAI,CAAC,WAAW,GAAG,wBAAQ,CAAC,UAAU,CAAC;QACzC,CAAC;IACH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU;QACd,IAAI,IAAI,CAAC,WAAW,CAAC,UAAU,KAAK,CAAC,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AC/B,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;YAC5C,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF;AA/GD,4CA+GC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Mongoose implementation of the IDocumentStore interface.
|
|
3
|
+
* Wraps existing mongoose logic previously embedded in BaseApplication.
|
|
4
|
+
* @module services/mongoose-document-store
|
|
5
|
+
*/
|
|
6
|
+
import mongoose, { Model } from '@digitaldefiance/mongoose-types';
|
|
7
|
+
import { MongoMemoryReplSet } from 'mongodb-memory-server';
|
|
8
|
+
import { IBaseDocument } from '../documents/base';
|
|
9
|
+
import { Environment } from '../environment';
|
|
10
|
+
import { IApplication } from '../interfaces/application';
|
|
11
|
+
import { IConstants } from '../interfaces/constants';
|
|
12
|
+
import { IDocumentStore } from '../interfaces/document-store';
|
|
13
|
+
import { IFailableResult } from '../interfaces/failable-result';
|
|
14
|
+
import { SchemaMap } from '../types';
|
|
15
|
+
import type { PlatformID } from '@digitaldefiance/node-ecies-lib';
|
|
16
|
+
import type { BaseApplication } from '../application-base';
|
|
17
|
+
/**
|
|
18
|
+
* Mongoose implementation of IDocumentStore.
|
|
19
|
+
* Extracts and preserves the existing mongoose logic from BaseApplication.
|
|
20
|
+
* @template TID - Platform-specific ID type extending PlatformID
|
|
21
|
+
* @template TModelDocs - Record mapping model names to their document types
|
|
22
|
+
* @template TInitResults - Type of database initialization results
|
|
23
|
+
* @template TConstants - Application constants type
|
|
24
|
+
*/
|
|
25
|
+
export declare class MongooseDocumentStore<TID extends PlatformID, TModelDocs extends Record<string, IBaseDocument<any, TID>>, TInitResults, TConstants extends IConstants = IConstants> implements IDocumentStore<TID, TModelDocs> {
|
|
26
|
+
/**
|
|
27
|
+
* Mongoose database instance
|
|
28
|
+
*/
|
|
29
|
+
private _db?;
|
|
30
|
+
/**
|
|
31
|
+
* Schema map for all models
|
|
32
|
+
*/
|
|
33
|
+
private _schemaMap?;
|
|
34
|
+
/**
|
|
35
|
+
* In-memory MongoDB instance for development
|
|
36
|
+
*/
|
|
37
|
+
private _devDatabase?;
|
|
38
|
+
/**
|
|
39
|
+
* Function to create the schema map given a Mongoose connection
|
|
40
|
+
*/
|
|
41
|
+
private readonly _schemaMapFactory;
|
|
42
|
+
/**
|
|
43
|
+
* Function to initialize the database with default data
|
|
44
|
+
*/
|
|
45
|
+
private readonly _databaseInitFunction;
|
|
46
|
+
/**
|
|
47
|
+
* Function to create a hash from the database initialization results (for logging purposes)
|
|
48
|
+
*/
|
|
49
|
+
private readonly _initResultHashFunction;
|
|
50
|
+
/**
|
|
51
|
+
* Application environment
|
|
52
|
+
*/
|
|
53
|
+
private readonly _environment;
|
|
54
|
+
/**
|
|
55
|
+
* Application constants
|
|
56
|
+
*/
|
|
57
|
+
private readonly _constants;
|
|
58
|
+
constructor(schemaMapFactory: (connection: mongoose.Connection) => SchemaMap<TID, TModelDocs>, databaseInitFunction: (application: BaseApplication<TID, TModelDocs, TInitResults>) => Promise<IFailableResult<TInitResults>>, initResultHashFunction: (initResults: TInitResults) => string, environment: Environment<TID>, constants?: TConstants);
|
|
59
|
+
/**
|
|
60
|
+
* Get the underlying mongoose instance (for backward compatibility).
|
|
61
|
+
*/
|
|
62
|
+
get db(): typeof mongoose;
|
|
63
|
+
/** @inheritdoc */
|
|
64
|
+
get schemaMap(): SchemaMap<TID, TModelDocs> | undefined;
|
|
65
|
+
/** @inheritdoc */
|
|
66
|
+
get devDatabase(): MongoMemoryReplSet | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Validate MongoDB URI to prevent SSRF attacks.
|
|
69
|
+
* Delegates to the standalone defaultMongoUriValidator for reuse
|
|
70
|
+
* across both IDatabase and IDocumentStore paths.
|
|
71
|
+
*/
|
|
72
|
+
private validateMongoUri;
|
|
73
|
+
/**
|
|
74
|
+
* Connect to MongoDB and initialize schemas.
|
|
75
|
+
* Extracted from BaseApplication.connectDatabase.
|
|
76
|
+
* @inheritdoc
|
|
77
|
+
*/
|
|
78
|
+
connect(uri?: string): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Disconnect from database.
|
|
81
|
+
* Extracted from BaseApplication.disconnectDatabase.
|
|
82
|
+
* @inheritdoc
|
|
83
|
+
*/
|
|
84
|
+
disconnect(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Whether the store is currently connected and ready for operations.
|
|
87
|
+
* @inheritdoc
|
|
88
|
+
*/
|
|
89
|
+
isConnected(): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Retrieve a model/collection handle by name.
|
|
92
|
+
* Delegates to ModelRegistry.
|
|
93
|
+
* @inheritdoc
|
|
94
|
+
*/
|
|
95
|
+
getModel<T extends IBaseDocument<any, TID>>(modelName: string): Model<T>;
|
|
96
|
+
/**
|
|
97
|
+
* Set up an in-memory MongoDB instance for development.
|
|
98
|
+
* Extracted from BaseApplication.setupDevDatabase.
|
|
99
|
+
* @inheritdoc
|
|
100
|
+
*/
|
|
101
|
+
setupDevStore(): Promise<string>;
|
|
102
|
+
/**
|
|
103
|
+
* Initialize the development database with default data.
|
|
104
|
+
* Extracted from BaseApplication.initializeDevDatabase.
|
|
105
|
+
* @inheritdoc
|
|
106
|
+
*/
|
|
107
|
+
initializeDevStore<TResult = TInitResults>(app: IApplication<TID>): Promise<TResult>;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=mongoose-document-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoose-document-store.d.ts","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-node-express-suite/src/services/mongoose-document-store.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAC;AAQlE,OAAO,EAAE,kBAAkB,E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