@smartsoft001/mongo 2.76.0 → 2.80.0
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/index.cjs +722 -0
- package/package.json +23 -3
- package/src/lib/mongo.config.d.ts +10 -0
- package/src/lib/mongo.module.d.ts +7 -0
- package/src/lib/mongo.unitofwork.d.ts +12 -0
- package/src/lib/mongo.utils.d.ts +2 -0
- package/src/lib/repositories/attachment.repository.d.ts +27 -0
- package/src/lib/repositories/interfaces.d.ts +21 -0
- package/src/lib/repositories/item.repository.d.ts +51 -0
- package/.eslintrc +0 -13
- package/jest.config.ts +0 -27
- package/project.json +0 -48
- package/src/lib/mongo.config.ts +0 -10
- package/src/lib/mongo.module.ts +0 -32
- package/src/lib/mongo.unitofwork.spec.ts +0 -84
- package/src/lib/mongo.unitofwork.ts +0 -58
- package/src/lib/mongo.utils.spec.ts +0 -53
- package/src/lib/mongo.utils.ts +0 -17
- package/src/lib/repositories/attachment.repository.spec.ts +0 -259
- package/src/lib/repositories/attachment.repository.ts +0 -115
- package/src/lib/repositories/interfaces.ts +0 -28
- package/src/lib/repositories/item.repository.spec.ts +0 -1269
- package/src/lib/repositories/item.repository.ts +0 -582
- package/tsconfig.json +0 -13
- package/tsconfig.lib.json +0 -11
- package/tsconfig.spec.json +0 -20
- /package/src/{index.ts → index.d.ts} +0 -0
- /package/{test-setup.ts → test-setup.d.ts} +0 -0
package/index.cjs
ADDED
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
29
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
30
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
31
|
+
if (decorator = decorators[i])
|
|
32
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
33
|
+
if (kind && result)
|
|
34
|
+
__defProp(target, key, result);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// packages/shared/mongo/src/index.ts
|
|
39
|
+
var src_exports = {};
|
|
40
|
+
__export(src_exports, {
|
|
41
|
+
MongoConfig: () => MongoConfig,
|
|
42
|
+
MongoItemRepository: () => MongoItemRepository,
|
|
43
|
+
MongoModule: () => MongoModule
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
|
|
47
|
+
// packages/shared/models/src/lib/symbols.ts
|
|
48
|
+
var SYMBOL_MODEL = Symbol.for("smartsoft:model");
|
|
49
|
+
var SYMBOL_FIELD = Symbol.for("smartsoft:field");
|
|
50
|
+
|
|
51
|
+
// packages/shared/models/src/lib/decorators/model/model.decorator.ts
|
|
52
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
53
|
+
|
|
54
|
+
// packages/shared/models/src/lib/decorators/field/field.decorator.ts
|
|
55
|
+
var import_reflect_metadata2 = require("reflect-metadata");
|
|
56
|
+
|
|
57
|
+
// packages/shared/utils/src/lib/services/password/password.service.ts
|
|
58
|
+
var md5_ = __toESM(require("md5"));
|
|
59
|
+
|
|
60
|
+
// packages/shared/utils/src/lib/services/object/object.service.ts
|
|
61
|
+
var import_flatted = require("flatted");
|
|
62
|
+
var ObjectService = class {
|
|
63
|
+
/***
|
|
64
|
+
* Create object with data
|
|
65
|
+
* @param data {object} - data to set
|
|
66
|
+
* @param type {type} - new type
|
|
67
|
+
* @return - new type object
|
|
68
|
+
*/
|
|
69
|
+
static createByType(data, type) {
|
|
70
|
+
if (!data)
|
|
71
|
+
return data;
|
|
72
|
+
try {
|
|
73
|
+
if (data instanceof type)
|
|
74
|
+
return data;
|
|
75
|
+
} catch (e) {
|
|
76
|
+
console.warn(e);
|
|
77
|
+
}
|
|
78
|
+
const result = new type();
|
|
79
|
+
Object.keys(data).forEach((key) => {
|
|
80
|
+
result[key] = data[key];
|
|
81
|
+
});
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
/***
|
|
85
|
+
* Remove object type from data
|
|
86
|
+
* @param obj {object} - object
|
|
87
|
+
* @return - object without type
|
|
88
|
+
*/
|
|
89
|
+
static removeTypes(obj) {
|
|
90
|
+
if (!obj)
|
|
91
|
+
return obj;
|
|
92
|
+
const result = {};
|
|
93
|
+
Object.keys(obj).forEach((key) => {
|
|
94
|
+
if (obj[key] && obj[key].constructor && !(obj[key] instanceof Date)) {
|
|
95
|
+
let stringValue = "";
|
|
96
|
+
try {
|
|
97
|
+
stringValue = JSON.stringify(obj[key]);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
console.warn("can't stringify without circular package");
|
|
100
|
+
stringValue = (0, import_flatted.stringify)(obj[key]);
|
|
101
|
+
}
|
|
102
|
+
result[key] = JSON.parse(stringValue);
|
|
103
|
+
} else {
|
|
104
|
+
result[key] = obj[key];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// packages/shared/utils/src/lib/services/guid/guid.service.ts
|
|
112
|
+
var import_guid_typescript = require("guid-typescript");
|
|
113
|
+
|
|
114
|
+
// packages/shared/utils/src/lib/services/array/array.service.ts
|
|
115
|
+
var _ = __toESM(require("lodash"));
|
|
116
|
+
|
|
117
|
+
// packages/shared/models/src/lib/utils.ts
|
|
118
|
+
function getModelFieldKeys(type) {
|
|
119
|
+
if (!type["__fields"])
|
|
120
|
+
return [];
|
|
121
|
+
return Object.keys(type["__fields"]);
|
|
122
|
+
}
|
|
123
|
+
function getModelFieldOptions(instance, fieldKey) {
|
|
124
|
+
return Reflect.getMetadata(SYMBOL_FIELD, instance, fieldKey);
|
|
125
|
+
}
|
|
126
|
+
function getModelFieldsWithOptions(instance) {
|
|
127
|
+
const keys = getModelFieldKeys(instance.constructor);
|
|
128
|
+
return keys.map((item) => {
|
|
129
|
+
return {
|
|
130
|
+
key: item,
|
|
131
|
+
options: getModelFieldOptions(instance, item)
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// packages/shared/domain-core/src/lib/repositories.ts
|
|
137
|
+
var IUnitOfWork = class {
|
|
138
|
+
};
|
|
139
|
+
var IItemRepository = class {
|
|
140
|
+
};
|
|
141
|
+
var IAttachmentRepository = class {
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// packages/shared/mongo/src/lib/mongo.config.ts
|
|
145
|
+
var MongoConfig = class {
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// packages/shared/mongo/src/lib/mongo.unitofwork.ts
|
|
149
|
+
var import_common = require("@nestjs/common");
|
|
150
|
+
var import_mongodb = require("mongodb");
|
|
151
|
+
|
|
152
|
+
// packages/shared/mongo/src/lib/mongo.utils.ts
|
|
153
|
+
function getMongoUrl(config) {
|
|
154
|
+
let url;
|
|
155
|
+
if (config.username && config.password)
|
|
156
|
+
url = `mongodb://${config.username}:${config.password}@${config.host}:${config.port}`;
|
|
157
|
+
else
|
|
158
|
+
url = `mongodb://${config.host}:${config.port}`;
|
|
159
|
+
url = url + "?authSource=" + config.database;
|
|
160
|
+
if (config.host.indexOf("ondigitalocean.com") > -1) {
|
|
161
|
+
url = url.replace("mongodb://", "mongodb+srv://");
|
|
162
|
+
url = url + "&tls=true";
|
|
163
|
+
}
|
|
164
|
+
return url;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// packages/shared/mongo/src/lib/mongo.unitofwork.ts
|
|
168
|
+
var MongoUnitOfWork = class extends IUnitOfWork {
|
|
169
|
+
constructor(config) {
|
|
170
|
+
super();
|
|
171
|
+
this.config = config;
|
|
172
|
+
}
|
|
173
|
+
async scope(definition) {
|
|
174
|
+
const client = await import_mongodb.MongoClient.connect(this.getUrl());
|
|
175
|
+
const session = client.startSession();
|
|
176
|
+
const transactionOptions = {
|
|
177
|
+
readPreference: "primary",
|
|
178
|
+
readConcern: { level: "local" },
|
|
179
|
+
writeConcern: { w: "majority" }
|
|
180
|
+
};
|
|
181
|
+
let error = null;
|
|
182
|
+
try {
|
|
183
|
+
await session.withTransaction(async () => {
|
|
184
|
+
await definition({
|
|
185
|
+
session,
|
|
186
|
+
connection: client
|
|
187
|
+
});
|
|
188
|
+
}, transactionOptions);
|
|
189
|
+
} catch (e) {
|
|
190
|
+
error = e;
|
|
191
|
+
} finally {
|
|
192
|
+
await session.endSession();
|
|
193
|
+
await client.close();
|
|
194
|
+
}
|
|
195
|
+
if (error)
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
getUrl() {
|
|
199
|
+
return getMongoUrl(this.config);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
MongoUnitOfWork = __decorateClass([
|
|
203
|
+
(0, import_common.Injectable)()
|
|
204
|
+
], MongoUnitOfWork);
|
|
205
|
+
|
|
206
|
+
// packages/shared/mongo/src/lib/repositories/attachment.repository.ts
|
|
207
|
+
var import_common2 = require("@nestjs/common");
|
|
208
|
+
var import_mongodb2 = require("mongodb");
|
|
209
|
+
var mongo = __toESM(require("mongodb"));
|
|
210
|
+
var MongoAttachmentRepository = class extends IAttachmentRepository {
|
|
211
|
+
constructor(config) {
|
|
212
|
+
super();
|
|
213
|
+
this.config = config;
|
|
214
|
+
}
|
|
215
|
+
async upload(data, options) {
|
|
216
|
+
const client = await import_mongodb2.MongoClient.connect(this.getUrl());
|
|
217
|
+
return await new Promise((res, rej) => {
|
|
218
|
+
const db = client.db(this.config.database);
|
|
219
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
220
|
+
bucketName: this.config.collection
|
|
221
|
+
});
|
|
222
|
+
const writeStream = bucket.openUploadStreamWithId(
|
|
223
|
+
data.id,
|
|
224
|
+
data.fileName,
|
|
225
|
+
{
|
|
226
|
+
contentType: data.mimeType
|
|
227
|
+
}
|
|
228
|
+
);
|
|
229
|
+
if (options?.streamCallback)
|
|
230
|
+
options.streamCallback(writeStream);
|
|
231
|
+
data.stream.pipe(writeStream);
|
|
232
|
+
writeStream.on("error", (error) => {
|
|
233
|
+
rej(error);
|
|
234
|
+
});
|
|
235
|
+
writeStream.on("finish", () => {
|
|
236
|
+
res();
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
async getInfo(id) {
|
|
241
|
+
const client = await import_mongodb2.MongoClient.connect(this.getUrl());
|
|
242
|
+
const db = client.db(this.config.database);
|
|
243
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
244
|
+
bucketName: this.config.collection
|
|
245
|
+
});
|
|
246
|
+
const items = await bucket.find({
|
|
247
|
+
_id: id
|
|
248
|
+
}).toArray();
|
|
249
|
+
if (!items || items.length === 0)
|
|
250
|
+
return null;
|
|
251
|
+
return {
|
|
252
|
+
fileName: items[0].filename,
|
|
253
|
+
contentType: items[0].contentType,
|
|
254
|
+
length: items[0].length
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
async getStream(id, options) {
|
|
258
|
+
const client = await import_mongodb2.MongoClient.connect(this.getUrl());
|
|
259
|
+
const db = client.db(this.config.database);
|
|
260
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
261
|
+
bucketName: this.config.collection
|
|
262
|
+
});
|
|
263
|
+
return bucket.openDownloadStream(id, options);
|
|
264
|
+
}
|
|
265
|
+
async delete(id) {
|
|
266
|
+
const client = await import_mongodb2.MongoClient.connect(this.getUrl());
|
|
267
|
+
const db = client.db(this.config.database);
|
|
268
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
269
|
+
bucketName: this.config.collection
|
|
270
|
+
});
|
|
271
|
+
await bucket.delete(id);
|
|
272
|
+
await client.close();
|
|
273
|
+
}
|
|
274
|
+
getUrl() {
|
|
275
|
+
return getMongoUrl(this.config);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
MongoAttachmentRepository = __decorateClass([
|
|
279
|
+
(0, import_common2.Injectable)()
|
|
280
|
+
], MongoAttachmentRepository);
|
|
281
|
+
|
|
282
|
+
// packages/shared/mongo/src/lib/repositories/item.repository.ts
|
|
283
|
+
var import_common3 = require("@nestjs/common");
|
|
284
|
+
var import_mongodb3 = require("mongodb");
|
|
285
|
+
var import_rxjs = require("rxjs");
|
|
286
|
+
var import_operators = require("rxjs/operators");
|
|
287
|
+
var MongoItemRepository = class extends IItemRepository {
|
|
288
|
+
constructor(config) {
|
|
289
|
+
super();
|
|
290
|
+
this.config = config;
|
|
291
|
+
}
|
|
292
|
+
async create(item, user, repoOptions) {
|
|
293
|
+
await this.collectionContext(async (collection) => {
|
|
294
|
+
try {
|
|
295
|
+
await collection.insertOne(this.getModelToCreate(item, user), {
|
|
296
|
+
session: repoOptions?.transaction?.session
|
|
297
|
+
});
|
|
298
|
+
this.logChange("create", item, repoOptions, user, null).then();
|
|
299
|
+
} catch (errInsert) {
|
|
300
|
+
this.logChange("create", item, repoOptions, user, errInsert).then();
|
|
301
|
+
throw errInsert;
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
async clear(user, repoOptions) {
|
|
306
|
+
await this.collectionContext(async (collection) => {
|
|
307
|
+
try {
|
|
308
|
+
await collection.deleteMany(
|
|
309
|
+
{},
|
|
310
|
+
{ session: repoOptions?.transaction?.session }
|
|
311
|
+
);
|
|
312
|
+
this.logChange("clear", null, repoOptions, user, null).then();
|
|
313
|
+
} catch (errClear) {
|
|
314
|
+
this.logChange("clear", null, repoOptions, user, errClear).then();
|
|
315
|
+
throw errClear;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
async createMany(list, user, repoOptions) {
|
|
320
|
+
await this.collectionContext(async (collection) => {
|
|
321
|
+
try {
|
|
322
|
+
await collection.insertMany(
|
|
323
|
+
list.map((item) => this.getModelToCreate(item, user)),
|
|
324
|
+
{ session: repoOptions?.transaction?.session }
|
|
325
|
+
);
|
|
326
|
+
this.logChange("createMany", null, repoOptions, user, null).then();
|
|
327
|
+
} catch (errInsert) {
|
|
328
|
+
this.logChange("createMany", null, repoOptions, user, errInsert).then();
|
|
329
|
+
throw errInsert;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
async update(item, user, repoOptions) {
|
|
334
|
+
await this.collectionContext(async (collection) => {
|
|
335
|
+
try {
|
|
336
|
+
const info = await this.getInfo(item.id, collection);
|
|
337
|
+
await collection.replaceOne(
|
|
338
|
+
{ _id: item.id },
|
|
339
|
+
this.getModelToUpdate(item, user, info),
|
|
340
|
+
{ session: repoOptions?.transaction?.session }
|
|
341
|
+
);
|
|
342
|
+
this.logChange("update", item, repoOptions, user, null).then();
|
|
343
|
+
} catch (errInsert) {
|
|
344
|
+
this.logChange("update", item, repoOptions, user, errInsert).then();
|
|
345
|
+
throw errInsert;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
async updatePartial(item, user, repoOptions) {
|
|
350
|
+
await this.collectionContext(async (collection) => {
|
|
351
|
+
try {
|
|
352
|
+
const info = await this.getInfo(item.id, collection);
|
|
353
|
+
await collection.updateOne(
|
|
354
|
+
{ _id: item.id },
|
|
355
|
+
{
|
|
356
|
+
$set: this.getModelToUpdate(item, user, info)
|
|
357
|
+
},
|
|
358
|
+
{ session: repoOptions?.transaction?.session }
|
|
359
|
+
);
|
|
360
|
+
this.logChange("updatePartial", item, repoOptions, user, null).then();
|
|
361
|
+
} catch (errUpdate) {
|
|
362
|
+
this.logChange(
|
|
363
|
+
"updatePartial",
|
|
364
|
+
item,
|
|
365
|
+
repoOptions,
|
|
366
|
+
user,
|
|
367
|
+
errUpdate
|
|
368
|
+
).then();
|
|
369
|
+
throw errUpdate;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async updatePartialManyByCriteria(criteria, set, user, repoOptions) {
|
|
374
|
+
await this.collectionContext(async (collection) => {
|
|
375
|
+
try {
|
|
376
|
+
this.convertIdInCriteria(criteria);
|
|
377
|
+
await collection.updateMany(
|
|
378
|
+
criteria,
|
|
379
|
+
{
|
|
380
|
+
$set: {
|
|
381
|
+
...set,
|
|
382
|
+
"__info.update": {
|
|
383
|
+
username: user?.username,
|
|
384
|
+
date: /* @__PURE__ */ new Date()
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
{ session: repoOptions?.transaction?.session }
|
|
389
|
+
);
|
|
390
|
+
this.logChange(
|
|
391
|
+
"updatePartialManyByCriteria",
|
|
392
|
+
{
|
|
393
|
+
...criteria,
|
|
394
|
+
set
|
|
395
|
+
},
|
|
396
|
+
repoOptions,
|
|
397
|
+
user,
|
|
398
|
+
null
|
|
399
|
+
).then();
|
|
400
|
+
} catch (errUpdate) {
|
|
401
|
+
this.logChange(
|
|
402
|
+
"updatePartialManyByCriteria",
|
|
403
|
+
{
|
|
404
|
+
...criteria,
|
|
405
|
+
set
|
|
406
|
+
},
|
|
407
|
+
repoOptions,
|
|
408
|
+
user,
|
|
409
|
+
errUpdate
|
|
410
|
+
).then();
|
|
411
|
+
throw errUpdate;
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
updatePartialManyBySpecification(spec, set, user, repoOptions) {
|
|
416
|
+
return this.updatePartialManyByCriteria(
|
|
417
|
+
spec.criteria,
|
|
418
|
+
set,
|
|
419
|
+
user,
|
|
420
|
+
repoOptions
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
async delete(id, user, repoOptions) {
|
|
424
|
+
await this.collectionContext(async (collection) => {
|
|
425
|
+
try {
|
|
426
|
+
await collection.deleteOne(
|
|
427
|
+
{ _id: id },
|
|
428
|
+
{ session: repoOptions?.transaction?.session }
|
|
429
|
+
);
|
|
430
|
+
this.logChange(
|
|
431
|
+
"delete",
|
|
432
|
+
{
|
|
433
|
+
id
|
|
434
|
+
},
|
|
435
|
+
repoOptions,
|
|
436
|
+
user,
|
|
437
|
+
null
|
|
438
|
+
).then();
|
|
439
|
+
} catch (errDelete) {
|
|
440
|
+
this.logChange(
|
|
441
|
+
"delete",
|
|
442
|
+
{
|
|
443
|
+
id
|
|
444
|
+
},
|
|
445
|
+
repoOptions,
|
|
446
|
+
user,
|
|
447
|
+
errDelete
|
|
448
|
+
).then();
|
|
449
|
+
throw errDelete;
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
async getById(id, repoOptions) {
|
|
454
|
+
return await this.collectionContext(async (collection) => {
|
|
455
|
+
const item = await collection.findOne(
|
|
456
|
+
{ _id: id },
|
|
457
|
+
{
|
|
458
|
+
session: repoOptions?.transaction?.session
|
|
459
|
+
}
|
|
460
|
+
);
|
|
461
|
+
return this.getModelToResult(item);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
async getByCriteria(criteria, options = {}) {
|
|
465
|
+
return await this.collectionContext(async (collection) => {
|
|
466
|
+
this.convertIdInCriteria(criteria);
|
|
467
|
+
this.generateSearch(criteria);
|
|
468
|
+
const totalCount = await this.getCount(criteria, collection);
|
|
469
|
+
const aggregate = [];
|
|
470
|
+
if (criteria) {
|
|
471
|
+
aggregate.push({ $match: criteria });
|
|
472
|
+
}
|
|
473
|
+
if (options?.sort) {
|
|
474
|
+
aggregate.push({ $sort: options.sort });
|
|
475
|
+
}
|
|
476
|
+
if (options?.skip) {
|
|
477
|
+
aggregate.push({ $skip: options.skip });
|
|
478
|
+
}
|
|
479
|
+
if (options?.limit) {
|
|
480
|
+
aggregate.push({ $limit: options.limit });
|
|
481
|
+
}
|
|
482
|
+
if (options?.project) {
|
|
483
|
+
aggregate.push({ $project: options.project });
|
|
484
|
+
}
|
|
485
|
+
if (options?.min) {
|
|
486
|
+
aggregate.push({ $min: options.min });
|
|
487
|
+
}
|
|
488
|
+
if (options?.max) {
|
|
489
|
+
aggregate.push({ $max: options.max });
|
|
490
|
+
}
|
|
491
|
+
if (options?.group) {
|
|
492
|
+
aggregate.push({ $group: options.group });
|
|
493
|
+
}
|
|
494
|
+
const list = await collection.aggregate(aggregate, {
|
|
495
|
+
allowDiskUse: options?.allowDiskUse,
|
|
496
|
+
session: options?.session
|
|
497
|
+
}).toArray();
|
|
498
|
+
return {
|
|
499
|
+
data: list.map((item) => this.getModelToResult(item)),
|
|
500
|
+
totalCount
|
|
501
|
+
};
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
getBySpecification(spec, options = {}) {
|
|
505
|
+
return this.getByCriteria(spec.criteria, options);
|
|
506
|
+
}
|
|
507
|
+
async countByCriteria(criteria) {
|
|
508
|
+
return await this.collectionContext(async (collection) => {
|
|
509
|
+
this.convertIdInCriteria(criteria);
|
|
510
|
+
this.generateSearch(criteria);
|
|
511
|
+
return await this.getCount(criteria, collection);
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
countBySpecification(spec) {
|
|
515
|
+
return this.countByCriteria(spec.criteria);
|
|
516
|
+
}
|
|
517
|
+
changesByCriteria(criteria) {
|
|
518
|
+
let stream;
|
|
519
|
+
let client;
|
|
520
|
+
return new import_rxjs.Observable((observer) => {
|
|
521
|
+
(async () => {
|
|
522
|
+
try {
|
|
523
|
+
client = await import_mongodb3.MongoClient.connect(this.getUrl());
|
|
524
|
+
const db = client.db(this.config.database);
|
|
525
|
+
const collection = db.collection(this.config.collection);
|
|
526
|
+
const pipeline = criteria.id ? [
|
|
527
|
+
{
|
|
528
|
+
$match: {
|
|
529
|
+
"documentKey._id": criteria.id
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
] : [];
|
|
533
|
+
stream = collection.watch(pipeline).on("change", (result) => {
|
|
534
|
+
observer.next({
|
|
535
|
+
id: result["documentKey"]["_id"],
|
|
536
|
+
type: this.mapChangeType(result.operationType),
|
|
537
|
+
data: result.operationType === "update" ? result["updateDescription"] : this.getModelToResult(result["fullDocument"])
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
} catch (err) {
|
|
541
|
+
observer.error(err);
|
|
542
|
+
}
|
|
543
|
+
})();
|
|
544
|
+
}).pipe(
|
|
545
|
+
(0, import_operators.finalize)(async () => {
|
|
546
|
+
console.log("Stop watch");
|
|
547
|
+
await stream.close();
|
|
548
|
+
await client.close();
|
|
549
|
+
}),
|
|
550
|
+
(0, import_operators.share)()
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
async getContext(handler) {
|
|
554
|
+
const client = await import_mongodb3.MongoClient.connect(this.getUrl());
|
|
555
|
+
const db = client.db(this.config.database);
|
|
556
|
+
try {
|
|
557
|
+
const result = await handler(db);
|
|
558
|
+
await client.close();
|
|
559
|
+
return result;
|
|
560
|
+
} catch (e) {
|
|
561
|
+
await client.close();
|
|
562
|
+
throw e;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
async getCount(criteria, collection) {
|
|
566
|
+
this.convertIdInCriteria(criteria);
|
|
567
|
+
return await collection.countDocuments(criteria);
|
|
568
|
+
}
|
|
569
|
+
async getInfo(id, collection) {
|
|
570
|
+
const array = await collection.aggregate([{ $match: { _id: id } }, { $project: { __info: 1 } }]).toArray();
|
|
571
|
+
return array[0] ? array[0]["__info"] : {};
|
|
572
|
+
}
|
|
573
|
+
getModelToCreate(item, user) {
|
|
574
|
+
const result = ObjectService.removeTypes(item);
|
|
575
|
+
result["_id"] = result.id;
|
|
576
|
+
delete result.id;
|
|
577
|
+
result["__info"] = {
|
|
578
|
+
create: {
|
|
579
|
+
username: user ? user.username : null,
|
|
580
|
+
date: /* @__PURE__ */ new Date()
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
return result;
|
|
584
|
+
}
|
|
585
|
+
mapChangeType(dbType) {
|
|
586
|
+
const map = {
|
|
587
|
+
insert: "create",
|
|
588
|
+
update: "update",
|
|
589
|
+
delete: "delete"
|
|
590
|
+
};
|
|
591
|
+
return map[dbType];
|
|
592
|
+
}
|
|
593
|
+
getModelToUpdate(item, user, info) {
|
|
594
|
+
const result = ObjectService.removeTypes(item);
|
|
595
|
+
result["_id"] = result.id;
|
|
596
|
+
delete result.id;
|
|
597
|
+
result["__info"] = {
|
|
598
|
+
...info,
|
|
599
|
+
update: {
|
|
600
|
+
username: user ? user.username : null,
|
|
601
|
+
date: /* @__PURE__ */ new Date()
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
return result;
|
|
605
|
+
}
|
|
606
|
+
getModelToResult(item) {
|
|
607
|
+
if (!item)
|
|
608
|
+
return null;
|
|
609
|
+
const result = ObjectService.removeTypes(item);
|
|
610
|
+
result["id"] = result._id;
|
|
611
|
+
delete result._id;
|
|
612
|
+
delete result["__info"];
|
|
613
|
+
return result;
|
|
614
|
+
}
|
|
615
|
+
getUrl() {
|
|
616
|
+
return getMongoUrl(this.config);
|
|
617
|
+
}
|
|
618
|
+
async logChange(type, item, options, user, error) {
|
|
619
|
+
const client = await import_mongodb3.MongoClient.connect(this.getUrl());
|
|
620
|
+
const db = client.db(this.config.database);
|
|
621
|
+
try {
|
|
622
|
+
await db.collection("changes").insertOne({
|
|
623
|
+
type,
|
|
624
|
+
collection: this.config.collection,
|
|
625
|
+
item,
|
|
626
|
+
options,
|
|
627
|
+
user,
|
|
628
|
+
error,
|
|
629
|
+
date: /* @__PURE__ */ new Date()
|
|
630
|
+
});
|
|
631
|
+
} catch (e) {
|
|
632
|
+
console.warn(e);
|
|
633
|
+
} finally {
|
|
634
|
+
await client.close();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
generateSearch(criteria) {
|
|
638
|
+
if (!criteria["$search"])
|
|
639
|
+
return;
|
|
640
|
+
if (this.config.type) {
|
|
641
|
+
const modelFields = getModelFieldsWithOptions(
|
|
642
|
+
new this.config.type()
|
|
643
|
+
).filter((i) => i.options.search);
|
|
644
|
+
if (modelFields.length) {
|
|
645
|
+
const searchArray = [];
|
|
646
|
+
modelFields.forEach((val) => {
|
|
647
|
+
const res = {};
|
|
648
|
+
res[val.key] = {
|
|
649
|
+
$regex: this.convertRegex(criteria["$search"]),
|
|
650
|
+
$options: "i"
|
|
651
|
+
};
|
|
652
|
+
searchArray.push(res);
|
|
653
|
+
});
|
|
654
|
+
if (!criteria["$or"])
|
|
655
|
+
criteria["$or"] = searchArray;
|
|
656
|
+
else if (criteria["$or"] && !criteria["$and"]) {
|
|
657
|
+
criteria["$and"] = [{ $or: criteria["$or"] }, { $or: searchArray }];
|
|
658
|
+
delete criteria["$or"];
|
|
659
|
+
} else if (criteria["$and"]) {
|
|
660
|
+
criteria["$and"] = [...criteria["$and"], { $or: searchArray }];
|
|
661
|
+
}
|
|
662
|
+
delete criteria["$search"];
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
const customCriteria = {
|
|
667
|
+
$text: { $search: ' "' + this.convertRegex(criteria["$search"]) + '" ' }
|
|
668
|
+
};
|
|
669
|
+
delete criteria["$search"];
|
|
670
|
+
criteria = {
|
|
671
|
+
...criteria,
|
|
672
|
+
...customCriteria
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
convertIdInCriteria(criteria) {
|
|
676
|
+
if (criteria["id"]) {
|
|
677
|
+
criteria["_id"] = criteria["id"];
|
|
678
|
+
delete criteria["id"];
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
convertRegex(val) {
|
|
682
|
+
return val.toString().replace(/\*/g, "[*]");
|
|
683
|
+
}
|
|
684
|
+
async collectionContext(callback, repoOptions) {
|
|
685
|
+
const client = repoOptions?.transaction?.connection ? repoOptions.transaction.connection : await import_mongodb3.MongoClient.connect(this.getUrl());
|
|
686
|
+
const db = client.db(this.config.database);
|
|
687
|
+
let result;
|
|
688
|
+
try {
|
|
689
|
+
result = await callback(db.collection(this.config.collection));
|
|
690
|
+
} finally {
|
|
691
|
+
if (!repoOptions?.transaction)
|
|
692
|
+
await client.close();
|
|
693
|
+
}
|
|
694
|
+
return result;
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
MongoItemRepository = __decorateClass([
|
|
698
|
+
(0, import_common3.Injectable)()
|
|
699
|
+
], MongoItemRepository);
|
|
700
|
+
|
|
701
|
+
// packages/shared/mongo/src/lib/mongo.module.ts
|
|
702
|
+
var MongoModule = class _MongoModule {
|
|
703
|
+
static forRoot(config) {
|
|
704
|
+
const providers = [
|
|
705
|
+
{ provide: MongoConfig, useValue: config },
|
|
706
|
+
{ provide: IItemRepository, useClass: MongoItemRepository },
|
|
707
|
+
{ provide: IAttachmentRepository, useClass: MongoAttachmentRepository },
|
|
708
|
+
{ provide: IUnitOfWork, useClass: MongoUnitOfWork }
|
|
709
|
+
];
|
|
710
|
+
return {
|
|
711
|
+
module: _MongoModule,
|
|
712
|
+
providers,
|
|
713
|
+
exports: providers
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
718
|
+
0 && (module.exports = {
|
|
719
|
+
MongoConfig,
|
|
720
|
+
MongoItemRepository,
|
|
721
|
+
MongoModule
|
|
722
|
+
});
|