@nextage/nx-frame-be 1.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/README.md +1 -0
- package/README.md.old +45 -0
- package/build/app.d.ts +0 -0
- package/build/app.js +1 -0
- package/build/aws/aws-cluster-manager.d.ts +28 -0
- package/build/aws/aws-cluster-manager.js +105 -0
- package/build/aws/aws-email-manager.d.ts +24 -0
- package/build/aws/aws-email-manager.js +57 -0
- package/build/aws/aws-queue-manager.d.ts +37 -0
- package/build/aws/aws-queue-manager.js +100 -0
- package/build/aws/aws.interfaces.d.ts +39 -0
- package/build/aws/aws.interfaces.js +2 -0
- package/build/aws/index.d.ts +4 -0
- package/build/aws/index.js +20 -0
- package/build/common/base/__test__/base.model.test.d.ts +1 -0
- package/build/common/base/__test__/base.model.test.js +283 -0
- package/build/common/base/base.controller.d.ts +124 -0
- package/build/common/base/base.controller.js +134 -0
- package/build/common/base/base.interfaces.d.ts +81 -0
- package/build/common/base/base.interfaces.js +2 -0
- package/build/common/base/base.model.d.ts +384 -0
- package/build/common/base/base.model.js +730 -0
- package/build/common/base/folded.model.d.ts +41 -0
- package/build/common/base/folded.model.js +215 -0
- package/build/common/base/mongo-utils.d.ts +122 -0
- package/build/common/base/mongo-utils.js +242 -0
- package/build/common/base/mongo.types.d.ts +20 -0
- package/build/common/base/mongo.types.js +2 -0
- package/build/common/base/sql-utils.d.ts +20 -0
- package/build/common/base/sql-utils.js +69 -0
- package/build/common/constants.d.ts +27 -0
- package/build/common/constants.js +70 -0
- package/build/common/crypto/crypto.classes.d.ts +100 -0
- package/build/common/crypto/crypto.classes.js +146 -0
- package/build/common/crypto/crypto.constants.d.ts +68 -0
- package/build/common/crypto/crypto.constants.js +57 -0
- package/build/common/crypto/crypto.error.d.ts +4 -0
- package/build/common/crypto/crypto.error.js +10 -0
- package/build/common/crypto/crypto.interfaces.d.ts +46 -0
- package/build/common/crypto/crypto.interfaces.js +2 -0
- package/build/common/crypto/crypto.utils.d.ts +139 -0
- package/build/common/crypto/crypto.utils.js +380 -0
- package/build/common/crypto/index.d.ts +3 -0
- package/build/common/crypto/index.js +19 -0
- package/build/common/enums.d.ts +61 -0
- package/build/common/enums.js +75 -0
- package/build/common/errors.d.ts +13 -0
- package/build/common/errors.js +24 -0
- package/build/common/express/express.interfaces.d.ts +20 -0
- package/build/common/express/express.interfaces.js +2 -0
- package/build/common/express/express.utils.d.ts +31 -0
- package/build/common/express/express.utils.js +85 -0
- package/build/common/express/index.d.ts +2 -0
- package/build/common/express/index.js +18 -0
- package/build/common/graphql/__test__/generator.schema.test.d.ts +1 -0
- package/build/common/graphql/__test__/generator.schema.test.js +391 -0
- package/build/common/graphql/directives.d.ts +18 -0
- package/build/common/graphql/directives.js +56 -0
- package/build/common/graphql/generator copy.d.ts +41 -0
- package/build/common/graphql/generator copy.js +385 -0
- package/build/common/graphql/generator.d.ts +42 -0
- package/build/common/graphql/generator.js +385 -0
- package/build/common/graphql/index.d.ts +4 -0
- package/build/common/graphql/index.js +21 -0
- package/build/common/graphql/interfaces.d.ts +67 -0
- package/build/common/graphql/interfaces.js +2 -0
- package/build/common/graphql/loader.d.ts +22 -0
- package/build/common/graphql/loader.js +186 -0
- package/build/common/graphql/scalars/index.d.ts +10 -0
- package/build/common/graphql/scalars/index.js +6 -0
- package/build/common/graphql/scalars/resolvers.d.ts +8 -0
- package/build/common/graphql/scalars/resolvers.js +71 -0
- package/build/common/graphql/scalars/schema.d.ts +1 -0
- package/build/common/graphql/scalars/schema.js +12 -0
- package/build/common/graphql/types/index.d.ts +3 -0
- package/build/common/graphql/types/index.js +6 -0
- package/build/common/graphql/types/schema.d.ts +1 -0
- package/build/common/graphql/types/schema.js +98 -0
- package/build/common/graphql/utils.d.ts +18 -0
- package/build/common/graphql/utils.js +40 -0
- package/build/common/grid-fs/gridfs-base.model.d.ts +53 -0
- package/build/common/grid-fs/gridfs-base.model.js +95 -0
- package/build/common/grid-fs/gridfs.interfaces.d.ts +41 -0
- package/build/common/grid-fs/gridfs.interfaces.js +2 -0
- package/build/common/index.d.ts +10 -0
- package/build/common/index.js +32 -0
- package/build/common/interfaces.d.ts +26 -0
- package/build/common/interfaces.js +10 -0
- package/build/common/manager/bulk-manager.d.ts +59 -0
- package/build/common/manager/bulk-manager.js +103 -0
- package/build/common/manager/crypto-manager.d.ts +88 -0
- package/build/common/manager/crypto-manager.js +200 -0
- package/build/common/manager/index.d.ts +5 -0
- package/build/common/manager/index.js +21 -0
- package/build/common/manager/pubsub-manager.d.ts +20 -0
- package/build/common/manager/pubsub-manager.js +38 -0
- package/build/common/manager/shutdown-manager.d.ts +27 -0
- package/build/common/manager/shutdown-manager.js +118 -0
- package/build/common/manager/socket-io-cli-manager.d.ts +53 -0
- package/build/common/manager/socket-io-cli-manager.js +91 -0
- package/build/common/manager/tunnel-manager.d.ts +28 -0
- package/build/common/manager/tunnel-manager.js +49 -0
- package/build/common/models/coded-entity.controller.d.ts +5 -0
- package/build/common/models/coded-entity.controller.js +9 -0
- package/build/common/models/coded-entity.model.d.ts +25 -0
- package/build/common/models/coded-entity.model.js +51 -0
- package/build/common/models/user.model.d.ts +56 -0
- package/build/common/models/user.model.js +72 -0
- package/build/common/types.d.ts +136 -0
- package/build/common/types.js +16 -0
- package/build/common/utils.d.ts +552 -0
- package/build/common/utils.js +1100 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +18 -0
- package/build/test/env.d.ts +0 -0
- package/build/test/env.js +5 -0
- package/build/test/setup.d.ts +6 -0
- package/build/test/setup.js +59 -0
- package/package.json +90 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { NxContext } from '../interfaces';
|
|
2
|
+
import { BaseModel } from './base.model';
|
|
3
|
+
import { FilterParams, FoldedModelAttrs } from './base.interfaces';
|
|
4
|
+
import { FoldedMongoDoc, BaseMongoModel } from './base.interfaces';
|
|
5
|
+
export declare class FoldedModel<TAttrs extends FoldedModelAttrs, TDoc extends FoldedMongoDoc, TMongoModel extends BaseMongoModel<TAttrs, TDoc>> extends BaseModel<TAttrs, TDoc, TMongoModel> {
|
|
6
|
+
childrenAttrs: string[];
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {*} item
|
|
10
|
+
*/
|
|
11
|
+
beforeCreate(item: TAttrs, context: NxContext): Promise<TAttrs>;
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {*} item
|
|
15
|
+
*/
|
|
16
|
+
beforeUpdate(item: TAttrs, context: NxContext): Promise<TAttrs>;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param {*} params
|
|
20
|
+
* @param {*} context
|
|
21
|
+
*/
|
|
22
|
+
updateFilters(params: FilterParams, context: NxContext): Promise<FilterParams | null>;
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param {*} params
|
|
26
|
+
* @param {*} context
|
|
27
|
+
*/
|
|
28
|
+
foldedList(params: FilterParams, context: NxContext): Promise<TDoc[]>;
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @param {*} item
|
|
32
|
+
*/
|
|
33
|
+
beforeCopy(attrs: TAttrs, context: NxContext): Promise<TAttrs>;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param {*} item
|
|
37
|
+
* @param {*} param1
|
|
38
|
+
* @param {*} context
|
|
39
|
+
*/
|
|
40
|
+
share(items: TDoc[], { users, organizations }: FoldedModelAttrs, context: NxContext): Promise<boolean>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
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.FoldedModel = void 0;
|
|
13
|
+
const errors_1 = require("../errors");
|
|
14
|
+
const base_model_1 = require("./base.model");
|
|
15
|
+
const mongo_utils_1 = require("./mongo-utils");
|
|
16
|
+
class FoldedModel extends base_model_1.BaseModel {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this.childrenAttrs = [];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param {*} item
|
|
24
|
+
*/
|
|
25
|
+
beforeCreate(item, context) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
var _a;
|
|
28
|
+
if (!((_a = context.user) === null || _a === void 0 ? void 0 : _a.id))
|
|
29
|
+
throw new errors_1.ForbiddenError();
|
|
30
|
+
item.createdAt = new Date();
|
|
31
|
+
item.ownerId = context.user.id;
|
|
32
|
+
item.folder = !!item.folder;
|
|
33
|
+
if (item.folderId) {
|
|
34
|
+
const folder = yield this.findOneNative({ _id: (0, mongo_utils_1.toObjectId)(item.folderId) });
|
|
35
|
+
if ((folder === null || folder === void 0 ? void 0 : folder.ownerId.toString()) !== context.user.id.toString())
|
|
36
|
+
delete item.folderId;
|
|
37
|
+
return item;
|
|
38
|
+
}
|
|
39
|
+
return Promise.resolve(item);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param {*} item
|
|
45
|
+
*/
|
|
46
|
+
beforeUpdate(item, context) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
item.updatedAt = new Date();
|
|
49
|
+
delete item.ownerId;
|
|
50
|
+
return Promise.resolve(item);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
*
|
|
55
|
+
* @param {*} params
|
|
56
|
+
* @param {*} context
|
|
57
|
+
*/
|
|
58
|
+
updateFilters(params, context) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
var _a;
|
|
61
|
+
if (!((_a = context.user) === null || _a === void 0 ? void 0 : _a.id))
|
|
62
|
+
return Promise.resolve(null);
|
|
63
|
+
if (!params)
|
|
64
|
+
params = {};
|
|
65
|
+
if (params.folderId)
|
|
66
|
+
params.folderId = (0, mongo_utils_1.toObjectId)(params.folderId);
|
|
67
|
+
this.manageFilterArrayParams(params);
|
|
68
|
+
// if admin no restrictions
|
|
69
|
+
// if (context.user.isAdmin()) {
|
|
70
|
+
// return Promise.resolve(params);
|
|
71
|
+
// }
|
|
72
|
+
let matchFilter = {};
|
|
73
|
+
if (params._id)
|
|
74
|
+
matchFilter._id = params._id;
|
|
75
|
+
// params.locked = { $exists: false };
|
|
76
|
+
params.$or = [
|
|
77
|
+
// user is owner
|
|
78
|
+
{ ownerId: context.user.id },
|
|
79
|
+
// element is public
|
|
80
|
+
{ public: true },
|
|
81
|
+
// element has no owner, good if organization not set or matching,
|
|
82
|
+
// users share not set or matching
|
|
83
|
+
{
|
|
84
|
+
$and: [
|
|
85
|
+
{ ownerId: { $exists: false } },
|
|
86
|
+
{
|
|
87
|
+
$and: [
|
|
88
|
+
{
|
|
89
|
+
$or: [
|
|
90
|
+
{ users: { $exists: false } },
|
|
91
|
+
{ users: { $eq: null } },
|
|
92
|
+
{ users: { $eq: [] } },
|
|
93
|
+
{ 'users._id': context.user.id.toString() }
|
|
94
|
+
]
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
$or: [
|
|
98
|
+
{ organizations: { $exists: false } },
|
|
99
|
+
{ organizations: { $eq: null } },
|
|
100
|
+
{ organizations: { $eq: [] } }
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
},
|
|
107
|
+
// user is not the owner but query is shared with him or belonging organization
|
|
108
|
+
{
|
|
109
|
+
$and: [
|
|
110
|
+
{ ownerId: { $ne: context.user.id } },
|
|
111
|
+
{ $or: [{ 'users._id': context.user.id.toString() }] }
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
];
|
|
115
|
+
// if no admin check for user organizations
|
|
116
|
+
return Promise.resolve(params);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
* @param {*} params
|
|
122
|
+
* @param {*} context
|
|
123
|
+
*/
|
|
124
|
+
foldedList(params, context) {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
try {
|
|
127
|
+
const filter = yield this.updateFilters(params, context);
|
|
128
|
+
if (!filter)
|
|
129
|
+
return [];
|
|
130
|
+
let childrenAttrs = { _id: '$_id', name: '$name', title: '$title', locked: '$locked', folder: '$folder', folderId: '$folderId', users: '$users', organizations: '$organizations', ownerId: '$ownerId' };
|
|
131
|
+
if (this.childrenAttrs)
|
|
132
|
+
this.childrenAttrs.forEach(ca => childrenAttrs[ca] = `$${ca}`);
|
|
133
|
+
let pipeline = [
|
|
134
|
+
{ $match: filter },
|
|
135
|
+
{ $sort: { folder: -1, name: 1, title: 1 } },
|
|
136
|
+
{
|
|
137
|
+
$group: {
|
|
138
|
+
_id: '$folderId',
|
|
139
|
+
children: { $push: childrenAttrs }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
const foldedItems = yield this.mgModel.aggregate(pipeline);
|
|
144
|
+
if (!foldedItems)
|
|
145
|
+
return [];
|
|
146
|
+
let root = foldedItems.find(g => g._id === null);
|
|
147
|
+
// if user has no items in root (only shared from other folders)
|
|
148
|
+
if (!root) {
|
|
149
|
+
root = { _id: null, children: [] };
|
|
150
|
+
foldedItems.push(root);
|
|
151
|
+
}
|
|
152
|
+
return foldedItems.filter(folder => {
|
|
153
|
+
if (!folder._id)
|
|
154
|
+
return true;
|
|
155
|
+
// check if element is a child of other folders, if not put its children inside root folder
|
|
156
|
+
let hasParent = !!foldedItems.filter(fi => fi !== folder && !!fi.children.find((child) => child._id.toString() === folder._id.toString())).length;
|
|
157
|
+
if (!hasParent) {
|
|
158
|
+
root.children = root.children.concat(folder.children);
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
*
|
|
171
|
+
* @param {*} item
|
|
172
|
+
*/
|
|
173
|
+
beforeCopy(attrs, context) {
|
|
174
|
+
var _a;
|
|
175
|
+
attrs.ownerId = (_a = context.user) === null || _a === void 0 ? void 0 : _a.id;
|
|
176
|
+
delete attrs.users;
|
|
177
|
+
delete attrs.groups;
|
|
178
|
+
delete attrs.organizations;
|
|
179
|
+
return super.beforeCopy(attrs, context);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
*
|
|
183
|
+
* @param {*} item
|
|
184
|
+
* @param {*} param1
|
|
185
|
+
* @param {*} context
|
|
186
|
+
*/
|
|
187
|
+
share(items_1, _a, context_1) {
|
|
188
|
+
return __awaiter(this, arguments, void 0, function* (items, { users, organizations }, context) {
|
|
189
|
+
let update = {};
|
|
190
|
+
if (Array.isArray(users))
|
|
191
|
+
update.users = users;
|
|
192
|
+
if (Array.isArray(organizations))
|
|
193
|
+
update.organizations = organizations;
|
|
194
|
+
if (!Object.keys(update).length)
|
|
195
|
+
return Promise.resolve(false);
|
|
196
|
+
// if (!item.folder)
|
|
197
|
+
// return this.updateNative({ _id: toObjectId(item._id) }, { $set: update });
|
|
198
|
+
let pipeline = [
|
|
199
|
+
{ $match: { _id: { $in: items.map(i => (0, mongo_utils_1.toObjectId)(i.id)) } } },
|
|
200
|
+
{ $graphLookup: { from: this.collectionName, startWith: '$_id', connectFromField: '_id', connectToField: 'folderId', as: 'children' } }
|
|
201
|
+
];
|
|
202
|
+
const foundData = yield this.aggregate(pipeline, {}, context);
|
|
203
|
+
if (!(foundData === null || foundData === void 0 ? void 0 : foundData[0]))
|
|
204
|
+
return false;
|
|
205
|
+
const ids = [];
|
|
206
|
+
foundData.forEach(fi => {
|
|
207
|
+
ids.push(fi.id);
|
|
208
|
+
fi.children.forEach((ch) => ids.push(ch._id));
|
|
209
|
+
});
|
|
210
|
+
const res = yield this.bulkUpdate(ids, update, context);
|
|
211
|
+
return !!res;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.FoldedModel = FoldedModel;
|
|
@@ -0,0 +1,122 @@
|
|
|
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 mongoose from 'mongoose';
|
|
27
|
+
import { BaseDBItem } from '../types';
|
|
28
|
+
import { ModelDef } from './mongo.types';
|
|
29
|
+
import { BaseMongoDoc, BaseMongoModel } from './base.interfaces';
|
|
30
|
+
import { BaseModel } from './base.model';
|
|
31
|
+
export declare const codedEntitySchema: {
|
|
32
|
+
code: {
|
|
33
|
+
type: StringConstructor;
|
|
34
|
+
required: boolean;
|
|
35
|
+
unique: boolean;
|
|
36
|
+
};
|
|
37
|
+
name: {
|
|
38
|
+
type: StringConstructor;
|
|
39
|
+
required: boolean;
|
|
40
|
+
};
|
|
41
|
+
description: StringConstructor;
|
|
42
|
+
startDate: {
|
|
43
|
+
type: DateConstructor;
|
|
44
|
+
};
|
|
45
|
+
endDate: {
|
|
46
|
+
type: DateConstructor;
|
|
47
|
+
};
|
|
48
|
+
data: typeof mongoose.Schema.Types.Mixed;
|
|
49
|
+
};
|
|
50
|
+
export declare const foldedSchema: {
|
|
51
|
+
name: StringConstructor;
|
|
52
|
+
description: StringConstructor;
|
|
53
|
+
locked: BooleanConstructor;
|
|
54
|
+
public: BooleanConstructor;
|
|
55
|
+
folder: BooleanConstructor;
|
|
56
|
+
folderId: typeof mongoose.Schema.Types.ObjectId;
|
|
57
|
+
ownerId: typeof mongoose.Schema.Types.ObjectId;
|
|
58
|
+
users: (typeof mongoose.Schema.Types.Mixed)[];
|
|
59
|
+
organizations: (typeof mongoose.Schema.Types.Mixed)[];
|
|
60
|
+
createdAt: DateConstructor;
|
|
61
|
+
updatedAt: DateConstructor;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
*
|
|
65
|
+
* @param model
|
|
66
|
+
*/
|
|
67
|
+
export declare function loadModel(model: BaseModel<any, any, any>): void;
|
|
68
|
+
/**
|
|
69
|
+
* name - name of the Model to be created
|
|
70
|
+
* classDef - (optional) class to be used to create model
|
|
71
|
+
* modelName - name of the Mongoose Model to be created
|
|
72
|
+
* modelParams - (optional) default model APP parameters (eg. default data sorting)
|
|
73
|
+
* schema - (optional) data schema used by mongoose
|
|
74
|
+
* (if not present it use the default schema "codedEntitySchema")
|
|
75
|
+
* collection - (optional) name of the collection for mongoose model
|
|
76
|
+
* load - (optional) if true add model in models array
|
|
77
|
+
* mergeSchema - (optional) if true "schema" object will be merged with default schema "codedEntitySchema"
|
|
78
|
+
* (property of "codedEntitySchema" with same key of "schema" will be overridden)
|
|
79
|
+
* @param { name, modelName, schema, collection, mergeSchema } param
|
|
80
|
+
* @param load
|
|
81
|
+
*/
|
|
82
|
+
export declare function createModel<TAttrs, TDoc extends BaseMongoDoc, TMongoModel extends BaseMongoModel<TAttrs, TDoc>, TNxModel>({ name, classDef, modelName, modelParams, collection, schema, mergeSchema }: ModelDef<TDoc, TMongoModel, TNxModel>, load: boolean, logger?: any): TNxModel;
|
|
83
|
+
/**
|
|
84
|
+
*
|
|
85
|
+
* @param {*} modelName
|
|
86
|
+
* @param {*} schema
|
|
87
|
+
* @param {*} collection
|
|
88
|
+
*/
|
|
89
|
+
export declare function loadMongoModel<TAttrs extends BaseDBItem, TDoc extends BaseMongoDoc, TMongoModel extends BaseMongoModel<TAttrs, TDoc>>(modelName: string, schema: any, collection: string, version?: boolean): TMongoModel;
|
|
90
|
+
/**
|
|
91
|
+
* transform string or string array to ObjectId or ObjectId array
|
|
92
|
+
*
|
|
93
|
+
* @param {*} value value or values to be converted
|
|
94
|
+
* @param {*} generate if true generate new id if value is not valid
|
|
95
|
+
*/
|
|
96
|
+
export declare function toObjectId(value: string | mongoose.Types.ObjectId, generate?: boolean): mongoose.Types.ObjectId;
|
|
97
|
+
/**
|
|
98
|
+
*
|
|
99
|
+
* @param values
|
|
100
|
+
* @param generate
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
export declare function toObjectIds(values: any[], generate?: boolean): mongoose.Types.ObjectId[];
|
|
104
|
+
/**
|
|
105
|
+
* return a new ObjectId value
|
|
106
|
+
*
|
|
107
|
+
* @param {*} value
|
|
108
|
+
*/
|
|
109
|
+
export declare function genObjectId(): mongoose.Types.ObjectId;
|
|
110
|
+
/**
|
|
111
|
+
* Check if string or string array is a valid ObjectId
|
|
112
|
+
*
|
|
113
|
+
* @param {*} value
|
|
114
|
+
*/
|
|
115
|
+
export declare function isValidObjectId(value: any): boolean | undefined;
|
|
116
|
+
/**
|
|
117
|
+
*
|
|
118
|
+
* @param {*} id1
|
|
119
|
+
* @param {*} id2
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
export declare function isSameId(id1: mongoose.Types.ObjectId | string, id2: mongoose.Types.ObjectId | string): boolean;
|
|
@@ -0,0 +1,242 @@
|
|
|
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.isSameId = exports.isValidObjectId = exports.genObjectId = exports.toObjectIds = exports.toObjectId = exports.loadMongoModel = exports.createModel = exports.loadModel = exports.foldedSchema = exports.codedEntitySchema = void 0;
|
|
7
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
8
|
+
const mongoose_update_if_current_1 = require("mongoose-update-if-current");
|
|
9
|
+
const constants_1 = require("../constants");
|
|
10
|
+
const base_model_1 = require("./base.model");
|
|
11
|
+
exports.codedEntitySchema = {
|
|
12
|
+
code: { type: String, required: true, unique: true },
|
|
13
|
+
name: { type: String, required: true },
|
|
14
|
+
description: String,
|
|
15
|
+
startDate: { type: Date },
|
|
16
|
+
endDate: { type: Date },
|
|
17
|
+
data: mongoose_1.default.SchemaTypes.Mixed
|
|
18
|
+
};
|
|
19
|
+
exports.foldedSchema = {
|
|
20
|
+
name: String,
|
|
21
|
+
description: String,
|
|
22
|
+
locked: Boolean,
|
|
23
|
+
public: Boolean,
|
|
24
|
+
folder: Boolean,
|
|
25
|
+
folderId: mongoose_1.default.SchemaTypes.ObjectId,
|
|
26
|
+
ownerId: mongoose_1.default.SchemaTypes.ObjectId,
|
|
27
|
+
users: [mongoose_1.default.SchemaTypes.Mixed],
|
|
28
|
+
organizations: [mongoose_1.default.SchemaTypes.Mixed],
|
|
29
|
+
createdAt: Date,
|
|
30
|
+
updatedAt: Date
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param model
|
|
35
|
+
*/
|
|
36
|
+
function loadModel(model) {
|
|
37
|
+
if (model.collectionName)
|
|
38
|
+
constants_1.APP.mongoModels[model.collectionName] = model;
|
|
39
|
+
}
|
|
40
|
+
exports.loadModel = loadModel;
|
|
41
|
+
/**
|
|
42
|
+
* name - name of the Model to be created
|
|
43
|
+
* classDef - (optional) class to be used to create model
|
|
44
|
+
* modelName - name of the Mongoose Model to be created
|
|
45
|
+
* modelParams - (optional) default model APP parameters (eg. default data sorting)
|
|
46
|
+
* schema - (optional) data schema used by mongoose
|
|
47
|
+
* (if not present it use the default schema "codedEntitySchema")
|
|
48
|
+
* collection - (optional) name of the collection for mongoose model
|
|
49
|
+
* load - (optional) if true add model in models array
|
|
50
|
+
* mergeSchema - (optional) if true "schema" object will be merged with default schema "codedEntitySchema"
|
|
51
|
+
* (property of "codedEntitySchema" with same key of "schema" will be overridden)
|
|
52
|
+
* @param { name, modelName, schema, collection, mergeSchema } param
|
|
53
|
+
* @param load
|
|
54
|
+
*/
|
|
55
|
+
function createModel({ name, classDef, modelName, modelParams, collection, schema, mergeSchema }, load, logger = constants_1.APP.logger) {
|
|
56
|
+
let entitySchema;
|
|
57
|
+
if (!name)
|
|
58
|
+
throw new Error('Missing param to create model');
|
|
59
|
+
if (!modelName)
|
|
60
|
+
modelName = name;
|
|
61
|
+
if (!schema)
|
|
62
|
+
entitySchema = exports.codedEntitySchema;
|
|
63
|
+
else
|
|
64
|
+
entitySchema = mergeSchema ? mergeDBSchema(exports.codedEntitySchema, schema) : schema;
|
|
65
|
+
const mgModel = loadMongoModel(modelName, entitySchema, collection);
|
|
66
|
+
const model = classDef ? new classDef({ name, model: mgModel, params: modelParams, logger }) : new base_model_1.BaseModel({ name, model: mgModel, params: modelParams, logger });
|
|
67
|
+
if (load)
|
|
68
|
+
loadModel(model);
|
|
69
|
+
logger.info(`created model ${modelName} (load: ${!!load})`);
|
|
70
|
+
//console.log(`created model ${modelName} (load: ${!!load})`);
|
|
71
|
+
return model;
|
|
72
|
+
}
|
|
73
|
+
exports.createModel = createModel;
|
|
74
|
+
/**
|
|
75
|
+
*
|
|
76
|
+
* @param {*} modelName
|
|
77
|
+
* @param {*} schema
|
|
78
|
+
* @param {*} collection
|
|
79
|
+
*/
|
|
80
|
+
function loadMongoModel(modelName, schema, collection, version = true) {
|
|
81
|
+
const mgSchema = new mongoose_1.default.Schema(schema, {
|
|
82
|
+
minimize: false,
|
|
83
|
+
toJSON: {
|
|
84
|
+
// convert _id to a standard id attribute
|
|
85
|
+
transform(doc, ret) {
|
|
86
|
+
ret.id = ret._id;
|
|
87
|
+
delete ret._id;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
// change documents' version key from __v to version
|
|
92
|
+
mgSchema.set('versionKey', 'version');
|
|
93
|
+
// apply version increment to every document update
|
|
94
|
+
if (version)
|
|
95
|
+
mgSchema.plugin(mongoose_update_if_current_1.updateIfCurrentPlugin);
|
|
96
|
+
mgSchema.statics.build = (attrs) => {
|
|
97
|
+
const item = { _id: attrs.id };
|
|
98
|
+
delete attrs.id;
|
|
99
|
+
Object.assign(item, attrs);
|
|
100
|
+
return new MGModel(item);
|
|
101
|
+
};
|
|
102
|
+
mgSchema.statics.findByEvent = (event) => {
|
|
103
|
+
return MGModel.findOne({
|
|
104
|
+
_id: toObjectId(event.id),
|
|
105
|
+
version: event.version - 1
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
// mgSchema.pre('save', async function(done) {
|
|
109
|
+
// console.log('CIAONE');
|
|
110
|
+
// done();
|
|
111
|
+
// });
|
|
112
|
+
const MGModel = (constants_1.APP.mongoose || mongoose_1.default).model(modelName, mgSchema, collection);
|
|
113
|
+
return MGModel;
|
|
114
|
+
}
|
|
115
|
+
exports.loadMongoModel = loadMongoModel;
|
|
116
|
+
/**
|
|
117
|
+
*
|
|
118
|
+
* @param {*} baseSchema
|
|
119
|
+
* @param {*} schemaToMerge
|
|
120
|
+
*/
|
|
121
|
+
function mergeDBSchema(baseSchema, schemaToMerge) {
|
|
122
|
+
let schema = Object.assign({}, baseSchema);
|
|
123
|
+
for (let property in schemaToMerge)
|
|
124
|
+
schema[property] = schemaToMerge[property];
|
|
125
|
+
return schema;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* transform string or string array to ObjectId or ObjectId array
|
|
129
|
+
*
|
|
130
|
+
* @param {*} value value or values to be converted
|
|
131
|
+
* @param {*} generate if true generate new id if value is not valid
|
|
132
|
+
*/
|
|
133
|
+
function toObjectId(value, generate = false) {
|
|
134
|
+
if (!value || !isValidObjectId(value))
|
|
135
|
+
return generate ? genObjectId() : value;
|
|
136
|
+
return (value instanceof mongoose_1.default.Types.ObjectId) ? value : mongoose_1.default.Types.ObjectId.createFromHexString(value);
|
|
137
|
+
}
|
|
138
|
+
exports.toObjectId = toObjectId;
|
|
139
|
+
/**
|
|
140
|
+
*
|
|
141
|
+
* @param values
|
|
142
|
+
* @param generate
|
|
143
|
+
* @returns
|
|
144
|
+
*/
|
|
145
|
+
function toObjectIds(values, generate = false) {
|
|
146
|
+
return values.map(v => toObjectId(v));
|
|
147
|
+
}
|
|
148
|
+
exports.toObjectIds = toObjectIds;
|
|
149
|
+
/**
|
|
150
|
+
* return a new ObjectId value
|
|
151
|
+
*
|
|
152
|
+
* @param {*} value
|
|
153
|
+
*/
|
|
154
|
+
function genObjectId() {
|
|
155
|
+
return new mongoose_1.default.Types.ObjectId();
|
|
156
|
+
}
|
|
157
|
+
exports.genObjectId = genObjectId;
|
|
158
|
+
// /**
|
|
159
|
+
// *
|
|
160
|
+
// * @param {*} period
|
|
161
|
+
// * @param {*} year
|
|
162
|
+
// */
|
|
163
|
+
// export function toFilterDatePeriod(period: DatePeriod, year: number) {
|
|
164
|
+
// let values = getDatePeriod(period, year);
|
|
165
|
+
// return values ? { $gte: values[0], $lt: values[1] } : {};
|
|
166
|
+
// }
|
|
167
|
+
// /**
|
|
168
|
+
// *
|
|
169
|
+
// * @param {*} idKey
|
|
170
|
+
// * @param {*} model
|
|
171
|
+
// * @param {*} item
|
|
172
|
+
// */
|
|
173
|
+
// export function checkDBItem<T>(idKey: string, model: T, item, upsert, logger = APP.logger) {
|
|
174
|
+
// if (!idKey || !model || !item || !item[idKey]) return (cb) => cb();
|
|
175
|
+
// let select = {};
|
|
176
|
+
// select[idKey] = item[idKey];
|
|
177
|
+
// return (cb) => {
|
|
178
|
+
// if (upsert) {
|
|
179
|
+
// return model.mgModel.updateOne(select, item, { upsert: true }).then(res => {
|
|
180
|
+
// logger.info(`Upserted ${model.name} (${item[idKey]})`, logClassCtx(this));
|
|
181
|
+
// cb();
|
|
182
|
+
// });
|
|
183
|
+
// }
|
|
184
|
+
// model.findOneNative(select).then(res => {
|
|
185
|
+
// if (res) return cb();
|
|
186
|
+
// model.mgModel.collection.insertOne(item).then(res => {
|
|
187
|
+
// logger.info(`Created ${model.name} (${item[idKey]})`, logClassCtx(this));
|
|
188
|
+
// cb();
|
|
189
|
+
// });
|
|
190
|
+
// });
|
|
191
|
+
// }
|
|
192
|
+
// }
|
|
193
|
+
// export function checkDBItemByList(id: mongoose.Types.ObjectId, model, items = [], logger = APP.logger) {
|
|
194
|
+
// let item = items.find(i => i._id.toString() === id);
|
|
195
|
+
// if (!item) {
|
|
196
|
+
// return (cb) => {
|
|
197
|
+
// logger.info(`${model.name} (${id}) not defined`, logClassCtx(this));
|
|
198
|
+
// cb();
|
|
199
|
+
// };
|
|
200
|
+
// }
|
|
201
|
+
// return checkDBItem('_id', model, item, logger = APP.logger);
|
|
202
|
+
// }
|
|
203
|
+
/**
|
|
204
|
+
* Check if string or string array is a valid ObjectId
|
|
205
|
+
*
|
|
206
|
+
* @param {*} value
|
|
207
|
+
*/
|
|
208
|
+
function isValidObjectId(value) {
|
|
209
|
+
if (!value)
|
|
210
|
+
return;
|
|
211
|
+
if (Array.isArray(value))
|
|
212
|
+
return value.every(v => mongoose_1.default.isValidObjectId(v));
|
|
213
|
+
return mongoose_1.default.isValidObjectId(value);
|
|
214
|
+
}
|
|
215
|
+
exports.isValidObjectId = isValidObjectId;
|
|
216
|
+
/**
|
|
217
|
+
*
|
|
218
|
+
* @param {*} id1
|
|
219
|
+
* @param {*} id2
|
|
220
|
+
* @returns
|
|
221
|
+
*/
|
|
222
|
+
function isSameId(id1, id2) {
|
|
223
|
+
return (id1 === null || id1 === void 0 ? void 0 : id1.toString()) === (id2 === null || id2 === void 0 ? void 0 : id2.toString());
|
|
224
|
+
}
|
|
225
|
+
exports.isSameId = isSameId;
|
|
226
|
+
// /**
|
|
227
|
+
// *
|
|
228
|
+
// * @param {*} cursor
|
|
229
|
+
// * @param {*} method
|
|
230
|
+
// * @param {*} resolve
|
|
231
|
+
// */
|
|
232
|
+
// export function manageCursor(cursor: any, method = (_item?: any) => {}, resolve: (res: boolean) => void) {
|
|
233
|
+
// if (!cursor)
|
|
234
|
+
// return resolve && resolve(true);
|
|
235
|
+
// cursor.next((_err: any, item: any) => {
|
|
236
|
+
// if (!item)
|
|
237
|
+
// return resolve && resolve(true);
|
|
238
|
+
// Promise.resolve(method(item)).then(() => {
|
|
239
|
+
// manageCursor(cursor, method, resolve);
|
|
240
|
+
// });
|
|
241
|
+
// });
|
|
242
|
+
// }
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { NxObject } from '../interfaces';
|
|
2
|
+
export type ModelDef<TDoc, TMongoModel, TNxModel> = {
|
|
3
|
+
classDef: new (params: ModelData<TDoc, TMongoModel>) => TNxModel;
|
|
4
|
+
name: string;
|
|
5
|
+
modelName: string;
|
|
6
|
+
modelParams: ModelDefParams;
|
|
7
|
+
collection: string;
|
|
8
|
+
schema: NxObject;
|
|
9
|
+
mergeSchema?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type ModelData<TDoc, TMongoModel> = {
|
|
12
|
+
name: string;
|
|
13
|
+
model: TMongoModel;
|
|
14
|
+
params: ModelDefParams;
|
|
15
|
+
logger: any;
|
|
16
|
+
};
|
|
17
|
+
export type ModelDefParams = {
|
|
18
|
+
sort?: NxObject;
|
|
19
|
+
CRUDEvents?: boolean;
|
|
20
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import sequelize from 'sequelize';
|
|
2
|
+
import { NxObject } from '../interfaces';
|
|
3
|
+
export declare const codedEntitySQLSchema: {
|
|
4
|
+
code: {
|
|
5
|
+
type: sequelize.StringDataTypeConstructor;
|
|
6
|
+
allowNull: boolean;
|
|
7
|
+
unique: boolean;
|
|
8
|
+
};
|
|
9
|
+
name: {
|
|
10
|
+
type: sequelize.StringDataTypeConstructor;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare const TABLE_SYNC_PROMISES: NxObject;
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param {*} modelName
|
|
17
|
+
* @param {*} schema
|
|
18
|
+
* @param {*} collection
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadSQLModel(modelName: string, modelDef: any, schema: any, tableName: string, schemaName: string, params?: any): any;
|