@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.
Files changed (119) hide show
  1. package/README.md +1 -0
  2. package/README.md.old +45 -0
  3. package/build/app.d.ts +0 -0
  4. package/build/app.js +1 -0
  5. package/build/aws/aws-cluster-manager.d.ts +28 -0
  6. package/build/aws/aws-cluster-manager.js +105 -0
  7. package/build/aws/aws-email-manager.d.ts +24 -0
  8. package/build/aws/aws-email-manager.js +57 -0
  9. package/build/aws/aws-queue-manager.d.ts +37 -0
  10. package/build/aws/aws-queue-manager.js +100 -0
  11. package/build/aws/aws.interfaces.d.ts +39 -0
  12. package/build/aws/aws.interfaces.js +2 -0
  13. package/build/aws/index.d.ts +4 -0
  14. package/build/aws/index.js +20 -0
  15. package/build/common/base/__test__/base.model.test.d.ts +1 -0
  16. package/build/common/base/__test__/base.model.test.js +283 -0
  17. package/build/common/base/base.controller.d.ts +124 -0
  18. package/build/common/base/base.controller.js +134 -0
  19. package/build/common/base/base.interfaces.d.ts +81 -0
  20. package/build/common/base/base.interfaces.js +2 -0
  21. package/build/common/base/base.model.d.ts +384 -0
  22. package/build/common/base/base.model.js +730 -0
  23. package/build/common/base/folded.model.d.ts +41 -0
  24. package/build/common/base/folded.model.js +215 -0
  25. package/build/common/base/mongo-utils.d.ts +122 -0
  26. package/build/common/base/mongo-utils.js +242 -0
  27. package/build/common/base/mongo.types.d.ts +20 -0
  28. package/build/common/base/mongo.types.js +2 -0
  29. package/build/common/base/sql-utils.d.ts +20 -0
  30. package/build/common/base/sql-utils.js +69 -0
  31. package/build/common/constants.d.ts +27 -0
  32. package/build/common/constants.js +70 -0
  33. package/build/common/crypto/crypto.classes.d.ts +100 -0
  34. package/build/common/crypto/crypto.classes.js +146 -0
  35. package/build/common/crypto/crypto.constants.d.ts +68 -0
  36. package/build/common/crypto/crypto.constants.js +57 -0
  37. package/build/common/crypto/crypto.error.d.ts +4 -0
  38. package/build/common/crypto/crypto.error.js +10 -0
  39. package/build/common/crypto/crypto.interfaces.d.ts +46 -0
  40. package/build/common/crypto/crypto.interfaces.js +2 -0
  41. package/build/common/crypto/crypto.utils.d.ts +139 -0
  42. package/build/common/crypto/crypto.utils.js +380 -0
  43. package/build/common/crypto/index.d.ts +3 -0
  44. package/build/common/crypto/index.js +19 -0
  45. package/build/common/enums.d.ts +61 -0
  46. package/build/common/enums.js +75 -0
  47. package/build/common/errors.d.ts +13 -0
  48. package/build/common/errors.js +24 -0
  49. package/build/common/express/express.interfaces.d.ts +20 -0
  50. package/build/common/express/express.interfaces.js +2 -0
  51. package/build/common/express/express.utils.d.ts +31 -0
  52. package/build/common/express/express.utils.js +85 -0
  53. package/build/common/express/index.d.ts +2 -0
  54. package/build/common/express/index.js +18 -0
  55. package/build/common/graphql/__test__/generator.schema.test.d.ts +1 -0
  56. package/build/common/graphql/__test__/generator.schema.test.js +391 -0
  57. package/build/common/graphql/directives.d.ts +18 -0
  58. package/build/common/graphql/directives.js +56 -0
  59. package/build/common/graphql/generator copy.d.ts +41 -0
  60. package/build/common/graphql/generator copy.js +385 -0
  61. package/build/common/graphql/generator.d.ts +42 -0
  62. package/build/common/graphql/generator.js +385 -0
  63. package/build/common/graphql/index.d.ts +4 -0
  64. package/build/common/graphql/index.js +21 -0
  65. package/build/common/graphql/interfaces.d.ts +67 -0
  66. package/build/common/graphql/interfaces.js +2 -0
  67. package/build/common/graphql/loader.d.ts +22 -0
  68. package/build/common/graphql/loader.js +186 -0
  69. package/build/common/graphql/scalars/index.d.ts +10 -0
  70. package/build/common/graphql/scalars/index.js +6 -0
  71. package/build/common/graphql/scalars/resolvers.d.ts +8 -0
  72. package/build/common/graphql/scalars/resolvers.js +71 -0
  73. package/build/common/graphql/scalars/schema.d.ts +1 -0
  74. package/build/common/graphql/scalars/schema.js +12 -0
  75. package/build/common/graphql/types/index.d.ts +3 -0
  76. package/build/common/graphql/types/index.js +6 -0
  77. package/build/common/graphql/types/schema.d.ts +1 -0
  78. package/build/common/graphql/types/schema.js +98 -0
  79. package/build/common/graphql/utils.d.ts +18 -0
  80. package/build/common/graphql/utils.js +40 -0
  81. package/build/common/grid-fs/gridfs-base.model.d.ts +53 -0
  82. package/build/common/grid-fs/gridfs-base.model.js +95 -0
  83. package/build/common/grid-fs/gridfs.interfaces.d.ts +41 -0
  84. package/build/common/grid-fs/gridfs.interfaces.js +2 -0
  85. package/build/common/index.d.ts +10 -0
  86. package/build/common/index.js +32 -0
  87. package/build/common/interfaces.d.ts +26 -0
  88. package/build/common/interfaces.js +10 -0
  89. package/build/common/manager/bulk-manager.d.ts +59 -0
  90. package/build/common/manager/bulk-manager.js +103 -0
  91. package/build/common/manager/crypto-manager.d.ts +88 -0
  92. package/build/common/manager/crypto-manager.js +200 -0
  93. package/build/common/manager/index.d.ts +5 -0
  94. package/build/common/manager/index.js +21 -0
  95. package/build/common/manager/pubsub-manager.d.ts +20 -0
  96. package/build/common/manager/pubsub-manager.js +38 -0
  97. package/build/common/manager/shutdown-manager.d.ts +27 -0
  98. package/build/common/manager/shutdown-manager.js +118 -0
  99. package/build/common/manager/socket-io-cli-manager.d.ts +53 -0
  100. package/build/common/manager/socket-io-cli-manager.js +91 -0
  101. package/build/common/manager/tunnel-manager.d.ts +28 -0
  102. package/build/common/manager/tunnel-manager.js +49 -0
  103. package/build/common/models/coded-entity.controller.d.ts +5 -0
  104. package/build/common/models/coded-entity.controller.js +9 -0
  105. package/build/common/models/coded-entity.model.d.ts +25 -0
  106. package/build/common/models/coded-entity.model.js +51 -0
  107. package/build/common/models/user.model.d.ts +56 -0
  108. package/build/common/models/user.model.js +72 -0
  109. package/build/common/types.d.ts +136 -0
  110. package/build/common/types.js +16 -0
  111. package/build/common/utils.d.ts +552 -0
  112. package/build/common/utils.js +1100 -0
  113. package/build/index.d.ts +2 -0
  114. package/build/index.js +18 -0
  115. package/build/test/env.d.ts +0 -0
  116. package/build/test/env.js +5 -0
  117. package/build/test/setup.d.ts +6 -0
  118. package/build/test/setup.js +59 -0
  119. package/package.json +90 -0
@@ -0,0 +1,385 @@
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.generateSchema = exports.publishSubData = exports.publishUpdateSubData = exports.publishEventSubData = exports.mainSubChannel = exports.foldedEntityFields = exports.codedEntityFields = void 0;
13
+ const utils_1 = require("../utils");
14
+ const loader_1 = require("./loader");
15
+ const utils_2 = require("./utils");
16
+ exports.codedEntityFields = {
17
+ type: `
18
+ id : ID!
19
+ code : String!
20
+ name : String!
21
+ description: String
22
+ startDate : Date
23
+ endDate : Date
24
+ `,
25
+ query: `
26
+ id : RegExp
27
+ code : RegExp
28
+ name : RegExp
29
+ description: RegExp
30
+ rangeDate : FilterDate
31
+ startDate : Date
32
+ endDate : Date
33
+ `,
34
+ input: `
35
+ code : String
36
+ name : String
37
+ description: String
38
+ startDate : Date
39
+ endDate : Date
40
+ `
41
+ };
42
+ exports.foldedEntityFields = {
43
+ type: `
44
+ id : ID!
45
+ title : String
46
+ name : String
47
+ description : String
48
+ locked : Boolean
49
+ public : Boolean
50
+ folder : Boolean
51
+ folderId : String
52
+ ownerId : String
53
+ users : [JSON]
54
+ organizations: [JSON]
55
+ `,
56
+ query: `
57
+ title : String
58
+ name : RegExp
59
+ description : String
60
+ public : Boolean
61
+ folder : Boolean
62
+ folderId : String
63
+ `,
64
+ input: `
65
+ title : String
66
+ name : String
67
+ description : String
68
+ locked : Boolean
69
+ public : Boolean
70
+ folder : Boolean
71
+ folderId : String
72
+ ownerId : String
73
+ users : [JSON]
74
+ organizations: [JSON]
75
+ `
76
+ };
77
+ exports.mainSubChannel = 'mainCH';
78
+ /**
79
+ *
80
+ * @param modelName
81
+ * @returns
82
+ */
83
+ function getMethodNames(modelName) {
84
+ const lowerModelName = modelName[0].toLowerCase() + modelName.slice(1);
85
+ return {
86
+ count: `${lowerModelName}Count`,
87
+ list: `${lowerModelName}List`,
88
+ get: `${lowerModelName}`,
89
+ findOne: `findOne${modelName}`,
90
+ create: `create${modelName}`,
91
+ update: `update${modelName}`,
92
+ remove: `remove${modelName}`,
93
+ copy: `copy${modelName}`,
94
+ addArrayItem: `add${modelName}ArrayItem`,
95
+ updateArrayItem: `update${modelName}ArrayItem`,
96
+ removeArrayItem: `remove${modelName}ArrayItem`,
97
+ bulkUpdate: `bulkUpdate${modelName}`,
98
+ bulkRemove: `bulkRemove${modelName}`,
99
+ };
100
+ }
101
+ /**
102
+ *
103
+ * @param modelName
104
+ * @returns
105
+ */
106
+ function getSubscriptionNames(modelName) {
107
+ const lowerModelName = modelName[0].toLowerCase() + modelName.slice(1);
108
+ return {
109
+ create: { id: `${lowerModelName}CreateEvent`, payload: `${lowerModelName}Added` },
110
+ update: { id: `${lowerModelName}UpdateEvent`, payload: `${lowerModelName}Changed` },
111
+ remove: { id: `${lowerModelName}RemoveEvent`, payload: `${lowerModelName}Removed` },
112
+ // addArrayItem : {},
113
+ // updateArrayItem: {},
114
+ // removeArrayItem: {}
115
+ };
116
+ }
117
+ /**
118
+ *
119
+ * @param extension
120
+ * @returns
121
+ */
122
+ function getAuth(extension = {}) {
123
+ const roles = { read: '', write: '' };
124
+ if (!extension.roles)
125
+ return roles;
126
+ if ((0, utils_1.isString)(extension.roles))
127
+ extension.roles = extension.roles.split(',');
128
+ if (Array.isArray(extension.roles))
129
+ extension.roles = { read: extension.roles, write: extension.roles };
130
+ const sr = extension.roles;
131
+ if ((sr.read))
132
+ roles.read = `(requires: "${Array.isArray(sr.read) ? sr.read.join(',') : sr.read}")`;
133
+ if (sr.write)
134
+ roles.write = `(requires: "${Array.isArray(sr.write) ? sr.write.join(',') : sr.write}")`;
135
+ return roles;
136
+ }
137
+ /**
138
+ *
139
+ * @param model
140
+ * @param schemaExtension
141
+ * @param mergeCodedEntity
142
+ * @param readOnly
143
+ * @returns
144
+ */
145
+ function generateTypeDefs(ctrl, schemaExtension = {}, mergeCodedEntity, readOnly) {
146
+ var _a, _b, _c, _d;
147
+ const modelName = getModelName(ctrl.model, schemaExtension);
148
+ const methods = getMethodNames(modelName);
149
+ const subscriptions = getSubscriptionNames(modelName);
150
+ const roles = getAuth(schemaExtension);
151
+ const typeDefs = schemaExtension.typeDefs || {};
152
+ let dataFields = schemaExtension.fields;
153
+ if (dataFields && mergeCodedEntity) {
154
+ dataFields.type = `${exports.codedEntityFields.type}${dataFields.type}`;
155
+ dataFields.input = `${exports.codedEntityFields.input}${dataFields.input}`;
156
+ dataFields.query = `${exports.codedEntityFields.query}${dataFields.query}`;
157
+ }
158
+ else if (!dataFields)
159
+ dataFields = exports.codedEntityFields;
160
+ const baseTypeDefs = {
161
+ query: `
162
+ ${methods.count}(params: ${modelName}Query) : Int! @auth${roles.read}
163
+ ${methods.list}(params: ${modelName}Query, pagination: OffsetPagination): [${modelName}]! @auth${roles.read}
164
+ ${methods.get}(id: ID!) : ${modelName}! @auth${roles.read}
165
+ ${methods.findOne}(params: ${modelName}Query) : ${modelName} @auth${roles.read}
166
+ `,
167
+ mutation: `
168
+ ${methods.create}(item: ${modelName}Input!) : ${modelName}! @auth${roles.write}
169
+ ${methods.update}(id: ID!, item: ${modelName}Input!) : ${modelName}! @auth${roles.write}
170
+ ${methods.remove}(id: ID!) : ${modelName}! @auth${roles.write}
171
+ ${methods.copy}(id: ID!, item: ${modelName}Input!) : ${modelName}! @auth${roles.write}
172
+
173
+ ${methods.addArrayItem}(id: ID!, item: AddArrayElemItem!) : ${modelName}! @auth${roles.write}
174
+ ${methods.updateArrayItem}(id: ID!, item: UpdateArrayElemItem!) : ${modelName}! @auth${roles.write}
175
+ ${methods.removeArrayItem}(id: ID!, item: RemoveArrayElemItem!) : ${modelName}! @auth${roles.write}
176
+
177
+ ${methods.bulkUpdate}(ids: [ID]!, items: [${modelName}Input]!) : [${modelName}]! @auth${roles.write}
178
+ ${methods.bulkRemove}(ids: [ID]!) : [${modelName}]! @auth${roles.write}
179
+ `,
180
+ subscription: `
181
+ ${subscriptions.create.id}(channelId: String!, filter: ${modelName}Query): ${modelName}! @auth${roles.read}
182
+ ${subscriptions.update.id}(channelId: String!, filter: ${modelName}Query): ${modelName}! @auth${roles.read}
183
+ ${subscriptions.remove.id}(channelId: String!, filter: ${modelName}Query): ${modelName}! @auth${roles.read}
184
+ `,
185
+ type: `
186
+ type ${modelName} {
187
+ ${dataFields.type}
188
+ }
189
+
190
+ input ${modelName}Query {
191
+ ${dataFields.query}
192
+ text : RegExp
193
+ pagination: OffsetPagination
194
+ }
195
+
196
+ input ${modelName}Input {
197
+ ${dataFields.input || 'noattr: String'}
198
+ }
199
+ `
200
+ };
201
+ return {
202
+ query: `
203
+ ${baseTypeDefs.query}
204
+ ${(_a = typeDefs.query) !== null && _a !== void 0 ? _a : ''}
205
+ `,
206
+ mutation: `
207
+ ${readOnly ? '' : baseTypeDefs.mutation}
208
+ ${(_b = typeDefs.mutation) !== null && _b !== void 0 ? _b : ''}
209
+ `,
210
+ subscription: `
211
+ ${readOnly ? '' : baseTypeDefs.subscription}
212
+ ${(_c = typeDefs.subscription) !== null && _c !== void 0 ? _c : ''}
213
+ `,
214
+ type: `
215
+ ${baseTypeDefs.type}
216
+ ${(_d = typeDefs.type) !== null && _d !== void 0 ? _d : ''}
217
+ `
218
+ };
219
+ }
220
+ /**
221
+ *
222
+ * @param model
223
+ * @param schemaExtension
224
+ * @param readOnly
225
+ * @returns
226
+ */
227
+ function generateResolvers(ctrl, schemaExtension = {}, readOnly) {
228
+ const modelName = getModelName(ctrl.model, schemaExtension);
229
+ const methods = getMethodNames(modelName);
230
+ const subscriptions = getSubscriptionNames(modelName);
231
+ const resolvers = { Query: {}, Mutation: {}, Subscription: {} };
232
+ resolvers.Query[methods.count] = (root, { params }, context) => ctrl.count(params, context);
233
+ resolvers.Query[methods.list] = (root, { params, fields, pagination }, context) => ctrl.list(params, fields, pagination, context);
234
+ resolvers.Query[methods.get] = (root, { id }, context) => ctrl.get(id, context);
235
+ resolvers.Query[methods.findOne] = (root, { params }, context) => ctrl.findOne(params, context);
236
+ if (!readOnly) {
237
+ resolvers.Mutation[methods.create] = (root, { item }, context) => appendSubscription(() => ctrl.create(item, context), subscriptions.create.id, subscriptions.create.payload, schemaExtension.subChannelAttr);
238
+ resolvers.Mutation[methods.update] = (root, { id, item }, context) => appendSubscription(() => ctrl.update(id, item, context), subscriptions.update.id, subscriptions.update.payload, schemaExtension.subChannelAttr);
239
+ resolvers.Mutation[methods.remove] = (root, { id }, context) => appendSubscription(() => ctrl.remove(id, context), subscriptions.remove.id, subscriptions.remove.payload, schemaExtension.subChannelAttr);
240
+ resolvers.Mutation[methods.copy] = (root, { id, item }, context) => appendSubscription(() => ctrl.copy(id, item, context), subscriptions.create.id, subscriptions.create.payload, schemaExtension.subChannelAttr);
241
+ // resolvers.Mutation[methods.create] = (root, { item }, context) => model.create(item, context);
242
+ // resolvers.Mutation[methods.update] = (root, { id, item }, context) => model.update(id, item, context);
243
+ // resolvers.Mutation[methods.remove] = (root, { id }, context) => model.remove(id, context);
244
+ resolvers.Mutation[methods.addArrayItem] = (root, { id, item }, context) => ctrl.addArrayItem(id, { arrayPath: item.path, item: item.data }, { new: true }, context);
245
+ resolvers.Mutation[methods.updateArrayItem] = (root, { id, item }, context) => ctrl.updateArrayItem(id, { arrayPath: item.path, arrayFilter: item.filter, updateAttr: item.updateAttr, partial: item.partial, item: item.data }, {}, context);
246
+ resolvers.Mutation[methods.removeArrayItem] = (root, { id, item }, context) => ctrl.removeArrayItem(id, { arrayPath: item.path, item: item.data }, { new: true }, context);
247
+ resolvers.Mutation[methods.bulkUpdate] = (root, { ids, items }, context) => ctrl.model.bulkUpdate(ids, items, context);
248
+ resolvers.Mutation[methods.bulkRemove] = (root, { ids }, context) => ctrl.model.bulkRemove(ids, context);
249
+ resolvers.Subscription[subscriptions.create.id] = generateSubscription(ctrl.model, subscriptions.create);
250
+ resolvers.Subscription[subscriptions.update.id] = generateSubscription(ctrl.model, subscriptions.update);
251
+ resolvers.Subscription[subscriptions.remove.id] = generateSubscription(ctrl.model, subscriptions.remove);
252
+ }
253
+ if (!schemaExtension.resolvers)
254
+ return resolvers;
255
+ if (schemaExtension.resolvers.Query)
256
+ Object.assign(resolvers.Query, schemaExtension.resolvers.Query);
257
+ if (schemaExtension.resolvers.Mutation)
258
+ Object.assign(resolvers.Mutation, schemaExtension.resolvers.Mutation);
259
+ if (schemaExtension.resolvers.Subscription)
260
+ Object.assign(resolvers.Subscription, schemaExtension.resolvers.Subscription);
261
+ if (schemaExtension.resolvers.Other)
262
+ Object.assign(resolvers, schemaExtension.resolvers.Other);
263
+ return resolvers;
264
+ }
265
+ /**
266
+ *
267
+ * @param {*} model
268
+ * @param {*} extension
269
+ */
270
+ function getModelName(model, extension = {}) {
271
+ const modelName = extension.modelName || model.modelName || model.name;
272
+ return modelName[0].toUpperCase() + modelName.slice(1);
273
+ }
274
+ /**
275
+ *
276
+ * @param model
277
+ * @param subscription
278
+ * @returns
279
+ */
280
+ function generateSubscription(model, subscription) {
281
+ if (!(subscription === null || subscription === void 0 ? void 0 : subscription.id))
282
+ throw new Error('Missing subscription id');
283
+ let payloadKeys = [];
284
+ if (subscription.payload)
285
+ payloadKeys = (0, utils_1.isObject)(subscription.payload) ? Object.values(subscription.payload) : [subscription.payload];
286
+ payloadKeys.push(subscription.id);
287
+ return {
288
+ // include resolve to manipulate payload
289
+ resolve: (payload_1, _a, context_1) => __awaiter(this, [payload_1, _a, context_1], void 0, function* (payload, { channelId, filter }, context) {
290
+ if (!payload)
291
+ return false;
292
+ for (let pk of payloadKeys) {
293
+ // if (payload[pk] && model.checkContextPermission(payload[pk], context))
294
+ // return payload[pk];//filterObject(payload[pk], filter) || false;
295
+ if (payload[pk])
296
+ return yield model.checkContextPermission(payload[pk], context);
297
+ return null;
298
+ //return payload[pk];//filterObject(payload[pk], filter) || false;
299
+ }
300
+ }),
301
+ subscribe: (0, utils_2.getChannelSubscriber)(subscription.id, subscription.payload, model)
302
+ };
303
+ }
304
+ /**
305
+ *
306
+ * @param method
307
+ * @param subId
308
+ * @param payload
309
+ * @param subChannelAttr
310
+ * @returns
311
+ */
312
+ function appendSubscription() {
313
+ return __awaiter(this, arguments, void 0, function* (method = () => Promise.resolve({}), subId, payload, subChannelAttr) {
314
+ if (!subId || !process.env.GRAPHQL_WS)
315
+ return method();
316
+ if (!payload)
317
+ payload = subId;
318
+ let promise = method();
319
+ if (!promise)
320
+ return Promise.resolve();
321
+ if (!promise.then)
322
+ promise = Promise.resolve(promise);
323
+ const item = yield promise;
324
+ publishSubData(item, subId, payload, subChannelAttr);
325
+ return item;
326
+ });
327
+ }
328
+ /**
329
+ *
330
+ * @param item
331
+ * @param model
332
+ * @param schemaExtension
333
+ * @param eventType
334
+ */
335
+ function publishEventSubData(item, model, schemaExtension = {}, eventType) {
336
+ const modelName = getModelName(model, schemaExtension);
337
+ const subscriptions = getSubscriptionNames(modelName);
338
+ publishSubData(item, subscriptions[eventType].id, subscriptions[eventType].payload, schemaExtension.subChannelAttr);
339
+ }
340
+ exports.publishEventSubData = publishEventSubData;
341
+ /**
342
+ *
343
+ * @param item
344
+ * @param model
345
+ * @param schemaExtension
346
+ */
347
+ function publishUpdateSubData(item, model, schemaExtension = {}) {
348
+ const modelName = getModelName(model, schemaExtension);
349
+ const subscriptions = getSubscriptionNames(modelName);
350
+ publishSubData(item, subscriptions.update.id, subscriptions.update.payload, schemaExtension.subChannelAttr);
351
+ }
352
+ exports.publishUpdateSubData = publishUpdateSubData;
353
+ /**
354
+ *
355
+ * @param item
356
+ * @param subId
357
+ * @param payload
358
+ * @param subChannelAttr
359
+ */
360
+ function publishSubData(item, subId, payload, subChannelAttr) {
361
+ payload !== null && payload !== void 0 ? payload : (payload = subId);
362
+ const data = {};
363
+ data[payload] = item;
364
+ data[payload].nxChannelId = [exports.mainSubChannel];
365
+ if (item.id)
366
+ data[payload].nxChannelId.push(item.id.toString());
367
+ if (subChannelAttr && (subChannelAttr = (0, utils_1.getAttr)(item, subChannelAttr)))
368
+ data[payload].nxChannelId.push(subChannelAttr.toString());
369
+ console.log('CHANNEL IDS', data[payload].nxChannelId);
370
+ utils_2.pubsub.publish(subId, data);
371
+ }
372
+ exports.publishSubData = publishSubData;
373
+ /**
374
+ *
375
+ * @param model
376
+ * @param schemaExtension
377
+ * @param mergeCodedEntity
378
+ * @param readOnly
379
+ */
380
+ function generateSchema(ctrl, schemaExtension = {}, mergeCodedEntity = false, readOnly = false) {
381
+ const typeDefs = generateTypeDefs(ctrl, schemaExtension, mergeCodedEntity, readOnly);
382
+ const resolvers = generateResolvers(ctrl, schemaExtension, readOnly);
383
+ (0, loader_1.loadSchema)({ typeDefs, resolvers });
384
+ }
385
+ exports.generateSchema = generateSchema;
@@ -0,0 +1,4 @@
1
+ export * from './directives';
2
+ export * from './loader';
3
+ export * from './generator';
4
+ export * from './utils';
@@ -0,0 +1,21 @@
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("./directives"), exports);
18
+ __exportStar(require("./loader"), exports);
19
+ __exportStar(require("./generator"), exports);
20
+ __exportStar(require("./utils"), exports);
21
+ //export * from './scalars';
@@ -0,0 +1,67 @@
1
+ import { GraphQLFieldResolver } from 'graphql';
2
+ import { IResolvers } from '@graphql-tools/utils';
3
+ import { NxContext, NxObject, NxTypeObject } from '../interfaces';
4
+ export interface NxGraphQLSchemaTypeDefs {
5
+ type?: string;
6
+ query?: string;
7
+ mutation?: string;
8
+ subscription?: string;
9
+ }
10
+ export interface NxGraphQLSchemaResolvers extends NxObject {
11
+ Query?: NxTypeObject<GraphQLFieldResolver<any, NxContext, any, unknown>>;
12
+ Mutation?: NxTypeObject<GraphQLFieldResolver<any, NxContext, any, unknown>>;
13
+ Other?: NxTypeObject<any>;
14
+ Subscription?: NxTypeObject<IResolvers<any, NxContext>>;
15
+ }
16
+ export interface NxGraphQLSchemaDataAttrs {
17
+ type: string;
18
+ query: string;
19
+ input: string;
20
+ }
21
+ export interface NxGraphQLSchemaData {
22
+ typeDefs?: NxGraphQLSchemaTypeDefs;
23
+ resolvers?: NxGraphQLSchemaResolvers;
24
+ fields?: NxGraphQLSchemaDataAttrs;
25
+ }
26
+ export interface NxGraphQLBaseMethodNames {
27
+ count: string;
28
+ list: string;
29
+ get: string;
30
+ findOne: string;
31
+ create: string;
32
+ update: string;
33
+ remove: string;
34
+ copy: string;
35
+ addArrayItem: string;
36
+ updateArrayItem: string;
37
+ removeArrayItem: string;
38
+ bulkUpdate: string;
39
+ bulkRemove: string;
40
+ }
41
+ export interface NxGraphQLBaseSubscriptionNames {
42
+ create: {
43
+ id: string;
44
+ payload: string;
45
+ };
46
+ update: {
47
+ id: string;
48
+ payload: string;
49
+ };
50
+ remove: {
51
+ id: string;
52
+ payload: string;
53
+ };
54
+ }
55
+ export interface NxGraphQLBaseSubscriptionDef {
56
+ id: string;
57
+ payload: string;
58
+ }
59
+ export interface NxGraphQLSchemaExtension extends NxGraphQLSchemaData {
60
+ modelName?: string;
61
+ roles?: string | string[] | NxGraphQLSchemaRole;
62
+ subChannelAttr?: string;
63
+ }
64
+ export interface NxGraphQLSchemaRole {
65
+ read?: string | string[];
66
+ write?: string | string[];
67
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,22 @@
1
+ import { GraphQLSchema } from 'graphql';
2
+ import { BaseContext } from '@apollo/server';
3
+ import { NxGraphQLSchemaData } from './interfaces';
4
+ export declare const schemas: never[];
5
+ export declare var mainSchema: NxGraphQLSchemaData;
6
+ /**
7
+ *
8
+ * @param data
9
+ */
10
+ export declare function loadSchema(data: NxGraphQLSchemaData): void;
11
+ /**
12
+ *
13
+ * @param {*} directives
14
+ * @returns
15
+ */
16
+ export declare function makeSchema(directives?: any[], federation?: boolean): GraphQLSchema;
17
+ /**
18
+ *
19
+ * @param {*} directives
20
+ * @returns
21
+ */
22
+ export declare function makeApolloConfig(directives?: any[], federation?: boolean): import("@apollo/server").ApolloServerOptionsWithSchema<BaseContext>;
@@ -0,0 +1,186 @@
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.makeApolloConfig = exports.makeSchema = exports.loadSchema = exports.mainSchema = exports.schemas = void 0;
7
+ const merge_1 = __importDefault(require("lodash/merge"));
8
+ const subgraph_1 = require("@apollo/subgraph");
9
+ const schema_1 = require("@graphql-tools/schema");
10
+ const constants_1 = require("../constants");
11
+ const utils_1 = require("../utils");
12
+ const scalars_1 = require("./scalars");
13
+ const types_1 = require("./types");
14
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
15
+ /**
16
+ *
17
+ * @param err
18
+ * @returns
19
+ */
20
+ let getOriginalError = function (err) {
21
+ if (!err.originalError)
22
+ return new Error();
23
+ if (err.originalError.extensions && Object.keys(err.originalError.extensions).length)
24
+ return err.originalError;
25
+ // if (!err.originalError.errors || !err.originalError.errors[0] || !err.originalError.errors[0].originalError)
26
+ // return {};
27
+ // let originalError = err.originalError.errors[0].originalError;
28
+ // if (!originalError.extensions)
29
+ // originalError.extensions = {};
30
+ return new Error();
31
+ };
32
+ exports.schemas = [];
33
+ exports.mainSchema = {
34
+ typeDefs: { query: '', mutation: '', subscription: '', type: `${scalars_1.scalarsSchema.typeDefs} ${types_1.typesSchema.typeDefs}` },
35
+ resolvers: scalars_1.scalarsSchema.resolvers
36
+ };
37
+ /**
38
+ *
39
+ * @param data
40
+ */
41
+ function loadSchema(data) {
42
+ var _a, _b, _c, _d;
43
+ if ((_a = data.typeDefs) === null || _a === void 0 ? void 0 : _a.query)
44
+ exports.mainSchema.typeDefs.query += data.typeDefs.query;
45
+ if ((_b = data.typeDefs) === null || _b === void 0 ? void 0 : _b.mutation)
46
+ exports.mainSchema.typeDefs.mutation += data.typeDefs.mutation;
47
+ if ((_c = data.typeDefs) === null || _c === void 0 ? void 0 : _c.subscription)
48
+ exports.mainSchema.typeDefs.subscription += data.typeDefs.subscription;
49
+ if ((_d = data.typeDefs) === null || _d === void 0 ? void 0 : _d.type)
50
+ exports.mainSchema.typeDefs.type += data.typeDefs.type;
51
+ exports.mainSchema.resolvers = (0, merge_1.default)(exports.mainSchema.resolvers, data.resolvers);
52
+ }
53
+ exports.loadSchema = loadSchema;
54
+ /**
55
+ *
56
+ * @param {*} directives
57
+ * @returns
58
+ */
59
+ function makeSchema(directives = [], federation = false) {
60
+ var _a, _b, _c;
61
+ if (!exports.mainSchema.typeDefs || !exports.mainSchema.resolvers)
62
+ throw new Error('GraphQL schema addDirectives: missing schema typeDefs or resolvers');
63
+ let subscription = '';
64
+ let query = '';
65
+ let mutation = '';
66
+ exports.mainSchema.typeDefs.query = ((_a = exports.mainSchema.typeDefs.query) === null || _a === void 0 ? void 0 : _a.trim()) || '';
67
+ exports.mainSchema.typeDefs.mutation = ((_b = exports.mainSchema.typeDefs.mutation) === null || _b === void 0 ? void 0 : _b.trim()) || '';
68
+ exports.mainSchema.typeDefs.subscription = ((_c = exports.mainSchema.typeDefs.subscription) === null || _c === void 0 ? void 0 : _c.trim()) || '';
69
+ if (exports.mainSchema.typeDefs.query)
70
+ query = `type Query { ${exports.mainSchema.typeDefs.query} }`;
71
+ else {
72
+ query = `type Query { helloWorld: String }`;
73
+ exports.mainSchema.resolvers.Query = { helloWorld: () => 'Hello NxFrame' };
74
+ }
75
+ if (exports.mainSchema.typeDefs.mutation)
76
+ mutation = `type Mutation { ${exports.mainSchema.typeDefs.mutation} }`;
77
+ else
78
+ delete exports.mainSchema.resolvers.Mutation;
79
+ if (exports.mainSchema.typeDefs.subscription)
80
+ subscription = `type Subscription { ${exports.mainSchema.typeDefs.subscription} }`;
81
+ else
82
+ delete exports.mainSchema.resolvers.Subscription;
83
+ let schema;
84
+ if (!federation) {
85
+ schema = (0, schema_1.makeExecutableSchema)({
86
+ typeDefs: [
87
+ ...directives.map(d => d.typeDefs),
88
+ `
89
+ ${query}
90
+ ${mutation}
91
+ ${subscription}
92
+ ${exports.mainSchema.typeDefs.type}
93
+ `
94
+ ],
95
+ resolvers: exports.mainSchema.resolvers
96
+ });
97
+ }
98
+ else {
99
+ let schemaString = `
100
+ extend schema @link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key", "@shareable"])
101
+ ${query}
102
+ ${mutation}
103
+ ${subscription}
104
+ ${exports.mainSchema.typeDefs.type}
105
+ `;
106
+ schema = (0, subgraph_1.buildSubgraphSchema)({
107
+ typeDefs: [
108
+ ...directives.map(d => (0, graphql_tag_1.default)(d.typeDefs)),
109
+ (0, graphql_tag_1.default)(schemaString)
110
+ ],
111
+ resolvers: exports.mainSchema.resolvers
112
+ });
113
+ }
114
+ // apply directives transform method to actual schema
115
+ directives.forEach(d => {
116
+ constants_1.APP.logger.info(`Adding ${d.name || ''} directive`);
117
+ if (!d.transformer) {
118
+ constants_1.APP.logger.warn(`Missing ${d.name || ''} directive transformer`);
119
+ return;
120
+ }
121
+ schema = d.transformer(schema);
122
+ });
123
+ return schema;
124
+ }
125
+ exports.makeSchema = makeSchema;
126
+ /**
127
+ *
128
+ * @param {*} directives
129
+ * @returns
130
+ */
131
+ function makeApolloConfig(directives = [], federation = false) {
132
+ const config = {
133
+ schema: makeSchema(directives, federation),
134
+ // context: ({ req, connection, payload }: any) => {
135
+ // // manage graphql api request
136
+ // if (req) {
137
+ // return {
138
+ // user : req.auth && req.auth.user,
139
+ // userType : req.auth && req.auth.iss,
140
+ // access : req.auth && req.auth.jti,
141
+ // remoteAddress: req.connection.remoteAddress,
142
+ // userAgent : req.headers['user-agent']
143
+ // };
144
+ // }
145
+ // // manage graphql ws request
146
+ // return connection && connection.context || {};
147
+ // },
148
+ formatError: (err) => {
149
+ const errExtensions = err.extensions && !!Object.keys(err.extensions).length;
150
+ const error = {
151
+ message: err.message,
152
+ extensions: errExtensions ? err.extensions : getOriginalError(err).extensions,
153
+ locations: err.locations,
154
+ path: err.path
155
+ };
156
+ if ((0, utils_1.prodMode)()) {
157
+ const exceptionName = (0, utils_1.getAttr)(error, 'extensions.exception.name');
158
+ const extensionCode = (0, utils_1.getAttr)(error, 'extensions.code');
159
+ if (exceptionName === 'MongoError') {
160
+ error.message = 'errors.internalError';
161
+ delete error.extensions.exception;
162
+ }
163
+ switch (extensionCode) {
164
+ case 'INTERNAL_SERVER_ERROR':
165
+ const message = error.message.toLowerCase();
166
+ if (message.includes('duplicate key'))
167
+ error.message = 'errors.duplicateKey';
168
+ else
169
+ error.message = 'errors.internalError';
170
+ break;
171
+ case 'BAD_USER_INPUT':
172
+ error.message = 'errors.badUserInput';
173
+ break;
174
+ default:
175
+ break;
176
+ }
177
+ delete error.extensions.stacktrace;
178
+ error.message = error.message.replace(/(Cannot query field).*$/i, "$1")
179
+ .replace(/(Unknown operation).*$/i, "$1");
180
+ }
181
+ return error;
182
+ }
183
+ };
184
+ return config;
185
+ }
186
+ exports.makeApolloConfig = makeApolloConfig;