@vroskus/library-datahandler 1.0.13 → 1.0.15

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.
@@ -0,0 +1,3 @@
1
+ export type $Helpers = Record<string, never>;
2
+ declare const helpers: $Helpers;
3
+ export default helpers;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const helpers = {};
4
+ exports.default = helpers;
5
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;AAEA,MAAM,OAAO,GAAa,EACzB,CAAC;AAEF,kBAAe,OAAO,CAAC"}
@@ -0,0 +1,118 @@
1
+ import { Op, Sequelize } from 'sequelize';
2
+ import type { $Config, $Include, $QueryParams, $RequestContext, $Where } from './types';
3
+ import type { $Helpers } from './helpers';
4
+ type $RequestContextListener = (arg0: $RequestContext) => unknown;
5
+ type $SetRequestContextListenerParams = {
6
+ listener: $RequestContextListener;
7
+ };
8
+ type $SetRequestContextListenerResponse = void;
9
+ export * from './types';
10
+ declare class DatabaseService<C extends $Config, MC extends {
11
+ Classes: any;
12
+ Config: any;
13
+ Models: any;
14
+ Shapes: any;
15
+ }> {
16
+ #private;
17
+ models: MC['Classes'];
18
+ helpers: $Helpers;
19
+ stack: Sequelize;
20
+ Op: typeof Op;
21
+ requestContextListener: $RequestContextListener;
22
+ constructor({ database, dialect, host, logging, password, port, storage, username, }: C, modelShapes: MC['Shapes']);
23
+ setupTestEnvironment(): Promise<void>;
24
+ setRequestContextListener({ listener, }: $SetRequestContextListenerParams): $SetRequestContextListenerResponse;
25
+ getOne<MN extends keyof MC['Config']>(request: {
26
+ modelName: MN;
27
+ params: $QueryParams<MC['Config'], MC['Classes'], MN>;
28
+ }): Promise<MC['Models'][MN] | null>;
29
+ getFirst<MN extends keyof MC['Config']>(request: {
30
+ modelName: MN;
31
+ params: $QueryParams<MC['Config'], MC['Classes'], MN>;
32
+ }): Promise<MC['Models'][MN] | null>;
33
+ getLast<MN extends keyof MC['Config']>(request: {
34
+ modelName: MN;
35
+ params: $QueryParams<MC['Config'], MC['Classes'], MN>;
36
+ }): Promise<MC['Models'][MN] | null>;
37
+ getMany<MN extends keyof MC['Config']>(request: {
38
+ modelName: MN;
39
+ params: $QueryParams<MC['Config'], MC['Classes'], MN>;
40
+ }): Promise<Array<MC['Models'][MN]>>;
41
+ createOne<MN extends keyof MC['Config']>(request: {
42
+ modelName: MN;
43
+ params: MC['Config'][MN]['ModelCreateParams'];
44
+ }): Promise<MC['Models'][MN]>;
45
+ createBundle<MN extends keyof MC['Config']>(request: {
46
+ include: $Include<MC['Config'], MC['Classes'], MN>;
47
+ modelName: MN;
48
+ params: MC['Config'][MN]['ModelCreateParams'];
49
+ }): Promise<MC['Models'][MN]>;
50
+ createMany<MN extends keyof MC['Config']>(request: {
51
+ modelName: MN;
52
+ params: Array<MC['Config'][MN]['ModelCreateParams']>;
53
+ }): Promise<Array<MC['Models'][MN]>>;
54
+ upsertOne<MN extends keyof MC['Config']>(request: {
55
+ modelName: MN;
56
+ params: MC['Config'][MN]['ModelUpsertParams'] | MC['Models'][MN];
57
+ where: $Where<MC['Config'], MN>;
58
+ }): Promise<MC['Models'][MN]>;
59
+ updateOne<MN extends keyof MC['Config']>(request: {
60
+ modelName: MN;
61
+ params: MC['Config'][MN]['ModelUpdateParams'] | MC['Models'][MN];
62
+ }): Promise<MC['Models'][MN]>;
63
+ toggleOne<MN extends keyof MC['Config']>(request: {
64
+ modelName: MN;
65
+ where: $Where<MC['Config'], MN>;
66
+ }): Promise<MC['Models'][MN]>;
67
+ deleteOne<MN extends keyof MC['Config']>(request: {
68
+ modelName: MN;
69
+ params: $QueryParams<MC['Config'], MC['Classes'], MN>;
70
+ }): Promise<MC['Models'][MN]>;
71
+ deleteMany<MN extends keyof MC['Config']>(request: {
72
+ modelName: MN;
73
+ params: $QueryParams<MC['Config'], MC['Classes'], MN>;
74
+ }): Promise<Array<MC['Models'][MN]>>;
75
+ sync<MN extends keyof MC['Config'], AM extends keyof MC['Config']>(request: {
76
+ modelName: MN;
77
+ params: {
78
+ associationModelId: string;
79
+ associationModelName: AM;
80
+ items: Array<MC['Config'][MN]['ModelUpsertParams']>;
81
+ };
82
+ }): Promise<{
83
+ createdItemIds: Array<string>;
84
+ deletedItemIds: Array<string>;
85
+ syncedItems: Array<MC['Models'][MN]>;
86
+ updatedItemIds: Array<string>;
87
+ }>;
88
+ addAssociation<MN extends keyof MC['Config'], AM extends keyof MC['Config']>(request: {
89
+ modelName: MN;
90
+ params: {
91
+ associationModelId: string;
92
+ associationModelName: AM;
93
+ id: string;
94
+ pivot?: Record<string, unknown>;
95
+ };
96
+ }): Promise<MC['Models'][MN]>;
97
+ removeAssociation<MN extends keyof MC['Config'], AM extends keyof MC['Config']>(request: {
98
+ modelName: MN;
99
+ params: {
100
+ associationModelId: string;
101
+ associationModelName: AM;
102
+ id: string;
103
+ pivot?: Record<string, unknown>;
104
+ };
105
+ }): Promise<boolean>;
106
+ syncAssociations<MN extends keyof MC['Config'], AM extends keyof MC['Config']>(request: {
107
+ modelName: MN;
108
+ params: {
109
+ associationModelIds: Array<string>;
110
+ associationModelName: AM;
111
+ id: string;
112
+ };
113
+ }): Promise<{
114
+ addedAssociationModelIds: Array<string>;
115
+ removedAssociationModelIds: Array<string>;
116
+ }>;
117
+ }
118
+ export default DatabaseService;
package/dist/index.js ADDED
@@ -0,0 +1,610 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
26
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
27
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
28
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
29
+ };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
33
+ var _DatabaseService_instances, _DatabaseService_getModel, _DatabaseService_getModelInstance, _DatabaseService_mapQueryAssociations, _DatabaseService_prepareQueryParams, _DatabaseService_associate;
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const sequelize_1 = require("sequelize");
36
+ const lodash_1 = __importDefault(require("lodash"));
37
+ const pluralize_1 = __importDefault(require("pluralize"));
38
+ const library_error_1 = require("@vroskus/library-error");
39
+ const helpers_1 = __importDefault(require("./helpers"));
40
+ __exportStar(require("./types"), exports);
41
+ class DatabaseService {
42
+ constructor({ database, dialect, host, logging, password, port, storage, username, }, modelShapes) {
43
+ _DatabaseService_instances.add(this);
44
+ const dbConfig = {
45
+ database,
46
+ define: {
47
+ charset: 'utf8',
48
+ dialectOptions: {
49
+ collate: 'utf8_general_ci',
50
+ },
51
+ timestamps: true,
52
+ },
53
+ dialect: dialect || 'mysql',
54
+ dialectOptions: {},
55
+ host,
56
+ logging,
57
+ password,
58
+ port: Number(port),
59
+ storage,
60
+ username,
61
+ };
62
+ const stack = new sequelize_1.Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, dbConfig);
63
+ const models = lodash_1.default.mapValues(modelShapes, (f) => f(sequelize_1.Sequelize, stack, helpers_1.default));
64
+ Object.keys(models).forEach((modelName) => {
65
+ if (models[modelName].associate) {
66
+ models[modelName].associate(models);
67
+ }
68
+ if (models[modelName].hooks) {
69
+ models[modelName].hooks(models);
70
+ }
71
+ });
72
+ this.models = models;
73
+ this.helpers = helpers_1.default;
74
+ this.stack = stack;
75
+ this.Op = sequelize_1.Op;
76
+ this.requestContextListener = () => { };
77
+ }
78
+ setupTestEnvironment() {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ yield this.stack.sync({
81
+ force: true,
82
+ });
83
+ });
84
+ }
85
+ setRequestContextListener({ listener, }) {
86
+ this.requestContextListener = listener;
87
+ }
88
+ getOne(request) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ this.requestContextListener({
91
+ method: 'getOne',
92
+ request,
93
+ });
94
+ const { modelName, params, } = request;
95
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
96
+ modelName,
97
+ });
98
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
99
+ params,
100
+ });
101
+ const modelInstances = yield model.findAll(queryParams);
102
+ if (modelInstances.length > 1) {
103
+ throw new library_error_1.CustomError('Multiple records found', library_error_1.baseErrorKey.multipleRecordsFoundError, {
104
+ data: {
105
+ amount: modelInstances.length,
106
+ modelName,
107
+ request,
108
+ },
109
+ });
110
+ }
111
+ return lodash_1.default.head(modelInstances) || null;
112
+ });
113
+ }
114
+ getFirst(request) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ this.requestContextListener({
117
+ method: 'getFirst',
118
+ request,
119
+ });
120
+ const { modelName, params, } = request;
121
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
122
+ modelName,
123
+ });
124
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
125
+ params,
126
+ });
127
+ const modelInstances = yield model.findAll(queryParams);
128
+ return lodash_1.default.first(modelInstances) || null;
129
+ });
130
+ }
131
+ getLast(request) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ this.requestContextListener({
134
+ method: 'getLast',
135
+ request,
136
+ });
137
+ const { modelName, params, } = request;
138
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
139
+ modelName,
140
+ });
141
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
142
+ params,
143
+ });
144
+ const modelInstances = yield model.findAll(queryParams);
145
+ return lodash_1.default.last(modelInstances) || null;
146
+ });
147
+ }
148
+ getMany(request) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ this.requestContextListener({
151
+ method: 'getMany',
152
+ request,
153
+ });
154
+ const { modelName, params, } = request;
155
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
156
+ modelName,
157
+ });
158
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
159
+ params,
160
+ });
161
+ return model.findAll(queryParams);
162
+ });
163
+ }
164
+ createOne(request) {
165
+ return __awaiter(this, void 0, void 0, function* () {
166
+ this.requestContextListener({
167
+ method: 'createOne',
168
+ request,
169
+ });
170
+ const { modelName, params, } = request;
171
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
172
+ modelName,
173
+ });
174
+ const attributes = lodash_1.default.clone(params);
175
+ return model.create(attributes);
176
+ });
177
+ }
178
+ createBundle(request) {
179
+ return __awaiter(this, void 0, void 0, function* () {
180
+ this.requestContextListener({
181
+ method: 'createBundle',
182
+ request,
183
+ });
184
+ const { include, modelName, params, } = request;
185
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
186
+ modelName,
187
+ });
188
+ const attributes = lodash_1.default.clone(params);
189
+ return model.create(attributes, {
190
+ include,
191
+ });
192
+ });
193
+ }
194
+ createMany(request) {
195
+ return __awaiter(this, void 0, void 0, function* () {
196
+ this.requestContextListener({
197
+ method: 'createMany',
198
+ request,
199
+ });
200
+ const { modelName, params, } = request;
201
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
202
+ modelName,
203
+ });
204
+ const attributes = lodash_1.default.clone(params);
205
+ return model.bulkCreate(attributes);
206
+ });
207
+ }
208
+ upsertOne(request) {
209
+ return __awaiter(this, void 0, void 0, function* () {
210
+ this.requestContextListener({
211
+ method: 'upsertOne',
212
+ request,
213
+ });
214
+ const { modelName, params, where, } = request;
215
+ const attributes = lodash_1.default.omit(params, ['id', 'createdAt', 'updatedAt']);
216
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
217
+ modelName,
218
+ });
219
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
220
+ params: {
221
+ where,
222
+ },
223
+ });
224
+ const modelInstances = yield model.findAll(queryParams);
225
+ if (modelInstances.length > 1) {
226
+ throw new library_error_1.CustomError('Multiple records found', library_error_1.baseErrorKey.multipleRecordsFoundError, {
227
+ data: {
228
+ amount: modelInstances.length,
229
+ modelName,
230
+ request,
231
+ },
232
+ });
233
+ }
234
+ if (modelInstances.length === 0) {
235
+ return model.create(attributes);
236
+ }
237
+ if (typeof modelInstances[0].update === 'function') {
238
+ return modelInstances[0].update(attributes);
239
+ }
240
+ return modelInstances[0];
241
+ });
242
+ }
243
+ updateOne(request) {
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ this.requestContextListener({
246
+ method: 'updateOne',
247
+ request,
248
+ });
249
+ const { modelName, params, } = request;
250
+ const { id, } = params;
251
+ const modelInstance = yield __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModelInstance).call(this, {
252
+ id,
253
+ modelName,
254
+ });
255
+ const attributes = lodash_1.default.omit(params, ['id', 'createdAt', 'updatedAt']);
256
+ if (typeof modelInstance.update === 'function') {
257
+ yield modelInstance.update(attributes);
258
+ }
259
+ return modelInstance;
260
+ });
261
+ }
262
+ toggleOne(request) {
263
+ return __awaiter(this, void 0, void 0, function* () {
264
+ this.requestContextListener({
265
+ method: 'toggleOne',
266
+ request,
267
+ });
268
+ const { modelName, where, } = request;
269
+ const attributes = lodash_1.default.omit(where, ['id', 'createdAt', 'updatedAt']);
270
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
271
+ modelName,
272
+ });
273
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
274
+ params: {
275
+ where,
276
+ },
277
+ });
278
+ let modelInstance = yield model.findOne(queryParams);
279
+ if (modelInstance !== null) {
280
+ yield modelInstance.destroy();
281
+ }
282
+ else {
283
+ modelInstance = yield model.create(attributes);
284
+ }
285
+ return modelInstance;
286
+ });
287
+ }
288
+ deleteOne(request) {
289
+ return __awaiter(this, void 0, void 0, function* () {
290
+ this.requestContextListener({
291
+ method: 'deleteOne',
292
+ request,
293
+ });
294
+ const { modelName, params, } = request;
295
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
296
+ modelName,
297
+ });
298
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
299
+ params,
300
+ });
301
+ const modelInstances = yield model.findAll(queryParams);
302
+ if (modelInstances.length > 1) {
303
+ throw new library_error_1.CustomError('Multiple records found', library_error_1.baseErrorKey.multipleRecordsFoundError, {
304
+ data: {
305
+ amount: modelInstances.length,
306
+ modelName,
307
+ request,
308
+ },
309
+ });
310
+ }
311
+ if (modelInstances.length === 0) {
312
+ throw new library_error_1.CustomError('Record was not found', library_error_1.baseErrorKey.entityNotFoundError, {
313
+ data: {
314
+ modelName,
315
+ request,
316
+ },
317
+ });
318
+ }
319
+ yield modelInstances[0].destroy();
320
+ return modelInstances[0];
321
+ });
322
+ }
323
+ deleteMany(request) {
324
+ return __awaiter(this, void 0, void 0, function* () {
325
+ this.requestContextListener({
326
+ method: 'deleteMany',
327
+ request,
328
+ });
329
+ const { modelName, params, } = request;
330
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
331
+ modelName,
332
+ });
333
+ const queryParams = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_prepareQueryParams).call(this, {
334
+ params,
335
+ });
336
+ const modelInstances = yield model.findAll(queryParams);
337
+ for (let index = 0; index < modelInstances.length; index += 1) {
338
+ if (modelInstances[index].destroy) {
339
+ yield modelInstances[index].destroy();
340
+ }
341
+ }
342
+ return modelInstances;
343
+ });
344
+ }
345
+ sync(request) {
346
+ return __awaiter(this, void 0, void 0, function* () {
347
+ this.requestContextListener({
348
+ method: 'sync',
349
+ request,
350
+ });
351
+ const { modelName, params: { associationModelId, associationModelName, items, }, } = request;
352
+ const actions = [];
353
+ const where = {
354
+ [`${String(associationModelName)}Id`]: associationModelId,
355
+ };
356
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
357
+ modelName,
358
+ });
359
+ __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
360
+ modelName: associationModelName,
361
+ });
362
+ const presentItems = yield model.findAll({
363
+ where,
364
+ });
365
+ const presentItemIds = lodash_1.default.map(presentItems, 'id');
366
+ const updatedItemIds = lodash_1.default.map(lodash_1.default.filter(items, (item) => item.id), 'id');
367
+ const deletedItemIds = lodash_1.default.without(presentItemIds, ...updatedItemIds);
368
+ const createdItemIds = [];
369
+ const syncedItems = [];
370
+ items.forEach((item) => {
371
+ const updateRequest = {
372
+ modelName,
373
+ params: Object.assign(Object.assign({}, item), where),
374
+ where: {
375
+ id: item.id || null,
376
+ },
377
+ };
378
+ const action = (() => __awaiter(this, void 0, void 0, function* () {
379
+ const upsertedItem = yield this.upsertOne(updateRequest);
380
+ if (!item.id) {
381
+ createdItemIds.push(upsertedItem.id);
382
+ }
383
+ syncedItems.push(upsertedItem);
384
+ }))();
385
+ actions.push(action);
386
+ });
387
+ deletedItemIds.forEach((id) => {
388
+ const deleteRequest = {
389
+ modelName,
390
+ params: {
391
+ where: Object.assign({ id }, where),
392
+ },
393
+ };
394
+ const action = this.deleteOne(deleteRequest);
395
+ actions.push(action);
396
+ });
397
+ yield Promise.all(actions);
398
+ return {
399
+ createdItemIds,
400
+ deletedItemIds,
401
+ syncedItems,
402
+ updatedItemIds,
403
+ };
404
+ });
405
+ }
406
+ addAssociation(request) {
407
+ return __awaiter(this, void 0, void 0, function* () {
408
+ this.requestContextListener({
409
+ method: 'addAssociation',
410
+ request,
411
+ });
412
+ const { modelName, params: { associationModelId, associationModelName, id, pivot, }, } = request;
413
+ const result = yield __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_associate).call(this, {
414
+ action: 'add',
415
+ associationModelId,
416
+ associationModelName,
417
+ id,
418
+ modelName,
419
+ pivot,
420
+ });
421
+ if (!result || (result && result.length !== 1)) {
422
+ throw new library_error_1.CustomError('Already associatied records', library_error_1.baseErrorKey.alreadyAssociatedRecordsError, {
423
+ data: {
424
+ model1: modelName,
425
+ model1id: id,
426
+ model2: associationModelName,
427
+ model2id: associationModelId,
428
+ },
429
+ });
430
+ }
431
+ return result[0];
432
+ });
433
+ }
434
+ removeAssociation(request) {
435
+ return __awaiter(this, void 0, void 0, function* () {
436
+ this.requestContextListener({
437
+ method: 'removeAssociation',
438
+ request,
439
+ });
440
+ const { modelName, params: { associationModelId, associationModelName, id, }, } = request;
441
+ const result = yield __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_associate).call(this, {
442
+ action: 'remove',
443
+ associationModelId,
444
+ associationModelName,
445
+ id,
446
+ modelName,
447
+ });
448
+ if (!result || (result && result !== 1)) {
449
+ throw new library_error_1.CustomError('Not associatied records', library_error_1.baseErrorKey.notAssociatedRecordsError, {
450
+ data: {
451
+ model1: modelName,
452
+ model1id: id,
453
+ model2: associationModelName,
454
+ model2id: associationModelId,
455
+ },
456
+ });
457
+ }
458
+ return true;
459
+ });
460
+ }
461
+ syncAssociations(request) {
462
+ return __awaiter(this, void 0, void 0, function* () {
463
+ this.requestContextListener({
464
+ method: 'syncAssociations',
465
+ request,
466
+ });
467
+ const { modelName, params: { associationModelIds, associationModelName, id, }, } = request;
468
+ const actions = [];
469
+ const pluralAssociationModelName = pluralize_1.default.plural(associationModelName);
470
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
471
+ modelName,
472
+ });
473
+ const modelInstance = yield model.findOne({
474
+ include: [pluralAssociationModelName],
475
+ where: {
476
+ id,
477
+ },
478
+ });
479
+ const presentAssociationModelIds = lodash_1.default.map(lodash_1.default.get(modelInstance, pluralAssociationModelName, []), 'id');
480
+ const addAssociationModelIds = lodash_1.default.without(associationModelIds, ...presentAssociationModelIds);
481
+ addAssociationModelIds.forEach((associationModelId) => {
482
+ const addAssociationRequest = {
483
+ modelName,
484
+ params: {
485
+ associationModelId,
486
+ associationModelName,
487
+ id,
488
+ },
489
+ };
490
+ const action = this.addAssociation(addAssociationRequest);
491
+ actions.push(action);
492
+ });
493
+ const removeAssociationModelIds = lodash_1.default.without(presentAssociationModelIds, ...associationModelIds);
494
+ removeAssociationModelIds.forEach((associationModelId) => {
495
+ const addAssociationRequest = {
496
+ modelName,
497
+ params: {
498
+ associationModelId,
499
+ associationModelName,
500
+ id,
501
+ },
502
+ };
503
+ const action = this.removeAssociation(addAssociationRequest);
504
+ actions.push(action);
505
+ });
506
+ yield Promise.all(actions);
507
+ return {
508
+ addedAssociationModelIds: addAssociationModelIds,
509
+ removedAssociationModelIds: removeAssociationModelIds,
510
+ };
511
+ });
512
+ }
513
+ }
514
+ _DatabaseService_instances = new WeakSet(), _DatabaseService_getModel = function _DatabaseService_getModel({ modelName, }) {
515
+ if (!lodash_1.default.has(this.models, modelName)) {
516
+ throw new library_error_1.CustomError('Invalid model name', library_error_1.baseErrorKey.invalidModelNameError, {
517
+ data: {
518
+ modelName,
519
+ },
520
+ });
521
+ }
522
+ return lodash_1.default.get(this.models, modelName);
523
+ }, _DatabaseService_getModelInstance = function _DatabaseService_getModelInstance({ id, modelName, }) {
524
+ return __awaiter(this, void 0, void 0, function* () {
525
+ const model = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModel).call(this, {
526
+ modelName,
527
+ });
528
+ const modelInstance = yield model.findByPk(id);
529
+ if (modelInstance === null) {
530
+ throw new library_error_1.CustomError('Record was not found', library_error_1.baseErrorKey.entityNotFoundError, {
531
+ data: {
532
+ id,
533
+ modelName,
534
+ },
535
+ });
536
+ }
537
+ return modelInstance;
538
+ });
539
+ }, _DatabaseService_mapQueryAssociations = function _DatabaseService_mapQueryAssociations({ include: includeInput, where: whereInput, }) {
540
+ let where = lodash_1.default.clone(whereInput);
541
+ const include = lodash_1.default.clone(includeInput);
542
+ lodash_1.default.forOwn(where, (value, key) => {
543
+ if (key.includes('.')) {
544
+ const associationName = key.substr(0, key.indexOf('.'));
545
+ const param = key.substr(key.indexOf('.') + 1);
546
+ const model = this.models[pluralize_1.default.singular(associationName)];
547
+ if (model) {
548
+ include.push(Object.assign({ as: associationName, model }, __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_mapQueryAssociations).call(this, {
549
+ include: [],
550
+ where: {
551
+ [param]: value,
552
+ },
553
+ })));
554
+ }
555
+ else {
556
+ include.push(associationName);
557
+ }
558
+ where = lodash_1.default.omit(where, [key]);
559
+ }
560
+ });
561
+ return {
562
+ include,
563
+ where,
564
+ };
565
+ }, _DatabaseService_prepareQueryParams = function _DatabaseService_prepareQueryParams({ params, }) {
566
+ const preparedParams = {
567
+ attributes: params.attributes,
568
+ include: params.include,
569
+ where: undefined,
570
+ };
571
+ if (params.where) {
572
+ const { include, where, } = __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_mapQueryAssociations).call(this, {
573
+ include: lodash_1.default.get(preparedParams, 'include', []),
574
+ where: params.where,
575
+ });
576
+ preparedParams.where = where;
577
+ preparedParams.include = include;
578
+ }
579
+ return preparedParams;
580
+ }, _DatabaseService_associate = function _DatabaseService_associate({ action, associationModelId, associationModelName, id, modelName, pivot, }) {
581
+ return __awaiter(this, void 0, void 0, function* () {
582
+ const actionMethod = `${action}${String(associationModelName)}`;
583
+ const modelInstance = yield __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModelInstance).call(this, {
584
+ id,
585
+ modelName,
586
+ });
587
+ const associationModel = yield __classPrivateFieldGet(this, _DatabaseService_instances, "m", _DatabaseService_getModelInstance).call(this, {
588
+ id: associationModelId,
589
+ modelName: associationModelName,
590
+ });
591
+ const pivotData = pivot ? {
592
+ through: pivot,
593
+ } : {};
594
+ if (typeof modelInstance[actionMethod] !== 'function') {
595
+ throw new library_error_1.CustomError('Association action method not found', library_error_1.baseErrorKey.associationActionMethodNotFoundError, {
596
+ data: {
597
+ action,
598
+ associationModelId,
599
+ associationModelName,
600
+ id,
601
+ modelName,
602
+ pivot,
603
+ },
604
+ });
605
+ }
606
+ return modelInstance[actionMethod](associationModel, pivotData);
607
+ });
608
+ };
609
+ exports.default = DatabaseService;
610
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","s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database: string;
3
+ dialect?: 'mysql' | 'sqlite';
4
+ host: string;
5
+ logging?: boolean;
6
+ password: string;
7
+ port: string;
8
+ storage?: string;
9
+ username: string;
10
+ };
11
+ export type $Where<MCC extends Record<MN, any>, MN extends keyof MCC> = MCC[MN]['ModelQueryParams'] & {
12
+ [key: string]: Record<string, unknown> | boolean | number | string | void;
13
+ };
14
+ type $IncludeInside<MCC extends Record<MN, any>, MCS extends Record<MN, any>, MN extends keyof MCC> = {
15
+ as?: string;
16
+ model: MCS[MN];
17
+ where: $Where<MCC, MN>;
18
+ } | MN;
19
+ export type $Include<MCC extends Record<MN, any>, MCS extends Record<MN, any>, MN extends keyof MCC> = Array<$IncludeInside<MCC, MCS, MN> | string>;
20
+ export type $QueryParams<MCC extends Record<MN, any>, MCS extends Record<MN, any>, MN extends keyof MCC> = {
21
+ attributes?: Array<string>;
22
+ include?: $Include<MCC, MCS, any>;
23
+ where?: $Where<MCC, MN>;
24
+ };
25
+ export type $RequestContext = {
26
+ method: string;
27
+ request: Record<string, any>;
28
+ };
29
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vroskus/library-datahandler",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Data Handler",
5
5
  "author": "Vilius Roškus <vilius@regattas.eu>",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  "test:e2e": "echo 'No tests'"
21
21
  },
22
22
  "dependencies": {
23
- "@vroskus/library-error": "1.0.14",
23
+ "@vroskus/library-error": "1.0.15",
24
24
  "lodash": "4.17.21",
25
25
  "mysql2": "3.6.5",
26
26
  "pg-hstore": "2.3.4",
@@ -31,7 +31,7 @@
31
31
  "devDependencies": {
32
32
  "@types/jest": "29.5.11",
33
33
  "@types/lodash": "4.14.202",
34
- "@types/node": "20.10.6",
34
+ "@types/node": "20.10.7",
35
35
  "@types/sequelize": "4.28.19",
36
36
  "@typescript-eslint/eslint-plugin": "6.18.0",
37
37
  "@typescript-eslint/parser": "6.18.0",