itlab-internal-services 2.10.7 → 2.10.9
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/factories/index.d.ts +2 -0
- package/dist/factories/index.js +18 -0
- package/dist/factories/merge.factory.d.ts +61 -0
- package/dist/factories/merge.factory.js +81 -0
- package/dist/{virtuals.factory.d.ts → factories/virtuals.factory.d.ts} +1 -1
- package/dist/{virtuals.factory.js → factories/virtuals.factory.js} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interceptors/errors.interceptor.js +5 -0
- package/dist/pipes/id.pipe.d.ts +1 -1
- package/dist/pipes/id.pipe.js +1 -3
- package/package.json +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./merge.factory"), exports);
|
|
18
|
+
__exportStar(require("./virtuals.factory"), exports);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
26
|
+
import { Document, Model } from 'mongoose';
|
|
27
|
+
/**
|
|
28
|
+
* MergeFactory
|
|
29
|
+
* A factory for merging documents in a collection by replacing occurrences of oldId with newId in specified fields
|
|
30
|
+
*/
|
|
31
|
+
export declare class MergeFactory<T extends Document, Field extends keyof Omit<T, keyof Document>> {
|
|
32
|
+
private readonly logger;
|
|
33
|
+
private readonly model;
|
|
34
|
+
private readonly oldId;
|
|
35
|
+
private readonly newId;
|
|
36
|
+
private readonly fields;
|
|
37
|
+
/**
|
|
38
|
+
* MergeFactory constructor
|
|
39
|
+
* @param {Model<T>} model - The Mongoose model for the collection.
|
|
40
|
+
* @param {string} oldId - The id of the document to be merged.
|
|
41
|
+
* @param {string} newId - The id of the document to merge into.
|
|
42
|
+
* @param {...Field[]} fields - The fields to be merged.
|
|
43
|
+
*
|
|
44
|
+
* @description Creates a new MergeFactory instance.
|
|
45
|
+
*/
|
|
46
|
+
constructor(model: Model<T>, oldId: string, newId: string, ...fields: Field[]);
|
|
47
|
+
/**
|
|
48
|
+
* Merges documents in a collection by replacing occurrences of oldId with newId in specified fields.
|
|
49
|
+
*
|
|
50
|
+
* Iterates over all documents in the collection, applying an optional transformation to each item.
|
|
51
|
+
* For each specified field, if the field is a string and matches oldId, it is replaced with newId.
|
|
52
|
+
* If the field is an array, occurrences of oldId are replaced with newId, ensuring uniqueness.
|
|
53
|
+
*
|
|
54
|
+
* @param {Function} [transform] - Optional function to transform each item before merging fields.
|
|
55
|
+
* @returns {Promise<void>} Resolves when all documents have been processed and saved.
|
|
56
|
+
*
|
|
57
|
+
* @description Merges all documents in the collection by replacing occurrences of oldId with newId in
|
|
58
|
+
* the specified fields.
|
|
59
|
+
*/
|
|
60
|
+
merge(transform?: (item: Omit<T, keyof Document>) => Omit<T, keyof Document>): Promise<void>;
|
|
61
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MergeFactory = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const itlab_functions_1 = require("itlab-functions");
|
|
15
|
+
/**
|
|
16
|
+
* MergeFactory
|
|
17
|
+
* A factory for merging documents in a collection by replacing occurrences of oldId with newId in specified fields
|
|
18
|
+
*/
|
|
19
|
+
class MergeFactory {
|
|
20
|
+
/**
|
|
21
|
+
* MergeFactory constructor
|
|
22
|
+
* @param {Model<T>} model - The Mongoose model for the collection.
|
|
23
|
+
* @param {string} oldId - The id of the document to be merged.
|
|
24
|
+
* @param {string} newId - The id of the document to merge into.
|
|
25
|
+
* @param {...Field[]} fields - The fields to be merged.
|
|
26
|
+
*
|
|
27
|
+
* @description Creates a new MergeFactory instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor(model, oldId, newId, ...fields) {
|
|
30
|
+
this.logger = new common_1.Logger('MergeService');
|
|
31
|
+
this.model = model;
|
|
32
|
+
this.oldId = oldId;
|
|
33
|
+
this.newId = newId;
|
|
34
|
+
this.fields = fields;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Merges documents in a collection by replacing occurrences of oldId with newId in specified fields.
|
|
38
|
+
*
|
|
39
|
+
* Iterates over all documents in the collection, applying an optional transformation to each item.
|
|
40
|
+
* For each specified field, if the field is a string and matches oldId, it is replaced with newId.
|
|
41
|
+
* If the field is an array, occurrences of oldId are replaced with newId, ensuring uniqueness.
|
|
42
|
+
*
|
|
43
|
+
* @param {Function} [transform] - Optional function to transform each item before merging fields.
|
|
44
|
+
* @returns {Promise<void>} Resolves when all documents have been processed and saved.
|
|
45
|
+
*
|
|
46
|
+
* @description Merges all documents in the collection by replacing occurrences of oldId with newId in
|
|
47
|
+
* the specified fields.
|
|
48
|
+
*/
|
|
49
|
+
merge(transform) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const iterator = this.model.find().cursor();
|
|
52
|
+
// Iterate over all documents in the collection
|
|
53
|
+
for (let item = yield iterator.next(); item != null; item = yield iterator.next()) {
|
|
54
|
+
// Apply an optional transformation to each item before merging fields
|
|
55
|
+
if (transform) {
|
|
56
|
+
item = transform(item);
|
|
57
|
+
}
|
|
58
|
+
// Iterate over the specified fields
|
|
59
|
+
for (const field of this.fields) {
|
|
60
|
+
const value = item[field];
|
|
61
|
+
// If the field is a string, replace the oldId with the newId
|
|
62
|
+
if (typeof value === 'string' && value === this.oldId) {
|
|
63
|
+
item[field] = this.newId;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// If the field is an array, replace occurrences of oldId with newId, ensuring uniqueness
|
|
67
|
+
if (Array.isArray(value)) {
|
|
68
|
+
const newValue = [...new Set(value.filter((id) => id !== this.oldId).concat(this.newId))];
|
|
69
|
+
item[field] = newValue;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Save the modified document
|
|
74
|
+
yield item.save({ timestamps: false });
|
|
75
|
+
}
|
|
76
|
+
// Log a message when all documents have been processed and saved
|
|
77
|
+
this.logger.log(`Succesfully merged fields ${(0, itlab_functions_1.toList)(this.fields)}`);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.MergeFactory = MergeFactory;
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
25
|
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
26
26
|
import { Schema, VirtualTypeOptions } from 'mongoose';
|
|
27
|
-
import { HubResource } from '
|
|
27
|
+
import { HubResource } from '../hub-resource.enum';
|
|
28
28
|
/**
|
|
29
29
|
* Creates virtual fields on a schema.
|
|
30
30
|
*
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.VirtualsFactory = void 0;
|
|
4
4
|
const class_validator_1 = require("class-validator");
|
|
5
|
-
const hub_resource_enum_1 = require("
|
|
5
|
+
const hub_resource_enum_1 = require("../hub-resource.enum");
|
|
6
6
|
/**
|
|
7
7
|
* Creates virtual fields on a schema.
|
|
8
8
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './create-duplicate-checker.function';
|
|
2
2
|
export * from './exceptions';
|
|
3
|
+
export * from './factories';
|
|
3
4
|
export * from './favicon.controller';
|
|
4
5
|
export * from './guards';
|
|
5
6
|
export * from './http.logger';
|
|
@@ -14,4 +15,3 @@ export * from './pipes';
|
|
|
14
15
|
export * from './schema.transformer';
|
|
15
16
|
export * from './swagger.config';
|
|
16
17
|
export * from './transform';
|
|
17
|
-
export * from './virtuals.factory';
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./create-duplicate-checker.function"), exports);
|
|
18
18
|
__exportStar(require("./exceptions"), exports);
|
|
19
|
+
__exportStar(require("./factories"), exports);
|
|
19
20
|
__exportStar(require("./favicon.controller"), exports);
|
|
20
21
|
__exportStar(require("./guards"), exports);
|
|
21
22
|
__exportStar(require("./http.logger"), exports);
|
|
@@ -30,4 +31,3 @@ __exportStar(require("./pipes"), exports);
|
|
|
30
31
|
__exportStar(require("./schema.transformer"), exports);
|
|
31
32
|
__exportStar(require("./swagger.config"), exports);
|
|
32
33
|
__exportStar(require("./transform"), exports);
|
|
33
|
-
__exportStar(require("./virtuals.factory"), exports);
|
|
@@ -41,6 +41,11 @@ let ErrorsInterceptor = class ErrorsInterceptor {
|
|
|
41
41
|
message = [];
|
|
42
42
|
if (!Array.isArray(message))
|
|
43
43
|
message = [message];
|
|
44
|
+
// Convert string response to object
|
|
45
|
+
if (typeof err.response == 'string') {
|
|
46
|
+
message = [err.response];
|
|
47
|
+
err.response = { status: err.status, error: err.name };
|
|
48
|
+
}
|
|
44
49
|
// Mapping over the message array and transforming the string messages if necessary
|
|
45
50
|
err.response.message = message.map((m) => (typeof m === 'string' ? this.transform(m, []) : m));
|
|
46
51
|
// Removing duplicate messages
|
package/dist/pipes/id.pipe.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare class InvalidIdException extends HttpException {
|
|
|
16
16
|
* @param param - The parameter to use for the ID
|
|
17
17
|
* @returns The extracted ID
|
|
18
18
|
*/
|
|
19
|
-
export declare
|
|
19
|
+
export declare const Id: (param: string) => ParameterDecorator;
|
|
20
20
|
/**
|
|
21
21
|
* Creates an ApiId decorator with the given parameter
|
|
22
22
|
*
|
package/dist/pipes/id.pipe.js
CHANGED
|
@@ -53,9 +53,7 @@ exports.InvalidIdException = InvalidIdException;
|
|
|
53
53
|
* @param param - The parameter to use for the ID
|
|
54
54
|
* @returns The extracted ID
|
|
55
55
|
*/
|
|
56
|
-
|
|
57
|
-
return (0, common_1.Param)(param, new ParseIdPipe(param));
|
|
58
|
-
}
|
|
56
|
+
const Id = (param) => (0, common_1.Param)(param, new ParseIdPipe(param));
|
|
59
57
|
exports.Id = Id;
|
|
60
58
|
/**
|
|
61
59
|
* Creates an ApiId decorator with the given parameter
|