@koalarx/nest 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/backgroud-services/cron-service/cron-job.d.ts +14 -0
- package/{common → core}/backgroud-services/cron-service/cron-job.js +18 -16
- package/core/backgroud-services/cron-service/cron-job.spec.d.ts +8 -0
- package/core/backgroud-services/cron-service/cron-job.spec.js +33 -0
- package/core/backgroud-services/event-service/event-class.d.ts +5 -0
- package/{common → core}/backgroud-services/event-service/event-class.js +3 -3
- package/core/backgroud-services/event-service/event-handler.d.ts +5 -0
- package/core/backgroud-services/event-service/event-job.d.ts +13 -0
- package/{common → core}/backgroud-services/event-service/event-job.js +4 -2
- package/{common → core}/backgroud-services/event-service/event-queue.js +7 -4
- package/core/backgroud-services/event-service/event-queue.spec.d.ts +1 -0
- package/core/backgroud-services/event-service/event-queue.spec.js +34 -0
- package/core/controllers/created-registre-response.base.d.ts +4 -0
- package/core/controllers/schemas/boolean.schema.d.ts +2 -0
- package/core/controllers/schemas/boolean.schema.js +15 -0
- package/core/database/entity.base.d.ts +9 -0
- package/core/database/entity.base.js +39 -0
- package/core/database/repository.base.d.ts +6 -11
- package/core/database/repository.base.js +23 -21
- package/core/koala-app.d.ts +10 -8
- package/core/koala-app.js +41 -8
- package/core/koala-global-vars.d.ts +7 -0
- package/core/koala-global-vars.js +9 -0
- package/core/koala-nest-database.module.js +2 -2
- package/core/koala-nest-http.module.d.ts +3 -3
- package/core/koala-nest-http.module.js +5 -9
- package/core/koala-nest.module.js +4 -3
- package/core/mapping/auto-mapping-class-context.d.ts +15 -0
- package/core/mapping/auto-mapping-class-context.js +18 -0
- package/core/mapping/auto-mapping-context.d.ts +11 -0
- package/core/mapping/auto-mapping-context.js +24 -0
- package/core/mapping/auto-mapping-list.d.ts +18 -0
- package/core/mapping/auto-mapping-list.js +46 -0
- package/core/mapping/auto-mapping-profile.d.ts +3 -0
- package/core/mapping/auto-mapping-profile.js +6 -0
- package/core/mapping/auto-mapping.decorator.d.ts +9 -0
- package/core/mapping/auto-mapping.decorator.js +27 -0
- package/core/mapping/auto-mapping.module.d.ts +5 -0
- package/core/mapping/auto-mapping.module.js +28 -0
- package/core/mapping/auto-mapping.service.d.ts +10 -0
- package/core/mapping/auto-mapping.service.js +93 -0
- package/core/mapping/create-map.d.ts +3 -0
- package/core/mapping/create-map.js +8 -0
- package/core/mapping/for-member.d.ts +5 -0
- package/core/mapping/for-member.js +9 -0
- package/{common → core}/models/pagination-params.d.ts +3 -0
- package/{common/mediator → core/request-overflow}/request-handler.base.d.ts +1 -1
- package/core/request-overflow/request-result.spec.d.ts +1 -0
- package/core/request-overflow/request-result.spec.js +21 -0
- package/{common/mediator → core/request-overflow}/request-validator.base.d.ts +1 -1
- package/core/utils/assing-object.d.ts +2 -0
- package/core/utils/assing-object.js +7 -0
- package/core/utils/list.d.ts +10 -7
- package/core/utils/list.js +31 -9
- package/core/utils/list.spec.d.ts +1 -0
- package/core/utils/list.spec.js +42 -0
- package/{services/env → env}/env.d.ts +6 -6
- package/{services/env → env}/env.js +2 -2
- package/{services/env → env}/env.service.d.ts +2 -2
- package/filters/domain-errors.filter.d.ts +7 -7
- package/filters/domain-errors.filter.js +7 -7
- package/package.json +10 -27
- package/services/redis/redis.service.d.ts +1 -1
- package/services/redis/redis.service.js +1 -1
- package/test/koala-app-test-dependencies.d.ts +10 -0
- package/test/koala-app-test-dependencies.js +13 -0
- package/test/koala-app-test.d.ts +20 -0
- package/test/koala-app-test.js +66 -0
- package/test/repositories/in-memory-base.repository.d.ts +16 -0
- package/test/repositories/in-memory-base.repository.js +55 -0
- package/test/services/fake-logging.service.d.ts +4 -0
- package/test/services/fake-logging.service.js +9 -0
- package/test/services/fake-red-lock.service.d.ts +5 -0
- package/test/services/fake-red-lock.service.js +11 -0
- package/test/utils/create-e2e-database.d.ts +2 -0
- package/test/utils/create-e2e-database.js +27 -0
- package/test/utils/drop-e2e-database.d.ts +1 -0
- package/test/utils/drop-e2e-database.js +8 -0
- package/test/utils/wait-for.d.ts +1 -0
- package/test/utils/wait-for.js +22 -0
- package/tsconfig.lib.tsbuildinfo +1 -1
- package/common/backgroud-services/cron-service/cron-job.d.ts +0 -15
- package/common/backgroud-services/event-service/event-class.d.ts +0 -5
- package/common/backgroud-services/event-service/event-handler.d.ts +0 -5
- package/common/backgroud-services/event-service/event-job.d.ts +0 -10
- package/common/controllers/created-registre-response.base.d.ts +0 -4
- package/core/database/entity.d.ts +0 -8
- package/core/database/entity.js +0 -28
- package/decorators/is-cnpj.decorator.d.ts +0 -2
- package/decorators/is-cnpj.decorator.js +0 -71
- /package/{common → core}/backgroud-services/event-service/event-handler.js +0 -0
- /package/{common → core}/backgroud-services/event-service/event-is-trigger.d.ts +0 -0
- /package/{common → core}/backgroud-services/event-service/event-is-trigger.js +0 -0
- /package/{common → core}/backgroud-services/event-service/event-queue.d.ts +0 -0
- /package/{common → core}/controllers/base.controller.d.ts +0 -0
- /package/{common → core}/controllers/base.controller.js +0 -0
- /package/{common → core}/controllers/created-registre-response.base.js +0 -0
- /package/{common → core}/controllers/list-response.base.d.ts +0 -0
- /package/{common → core}/controllers/list-response.base.js +0 -0
- /package/{common → core}/controllers/router-config.base.d.ts +0 -0
- /package/{common → core}/controllers/router-config.base.js +0 -0
- /package/{common → core}/controllers/schemas/document-number.schema.d.ts +0 -0
- /package/{common → core}/controllers/schemas/document-number.schema.js +0 -0
- /package/{common → core}/controllers/schemas/email.schema.d.ts +0 -0
- /package/{common → core}/controllers/schemas/email.schema.js +0 -0
- /package/{common → core}/controllers/schemas/list-query.schema.d.ts +0 -0
- /package/{common → core}/controllers/schemas/list-query.schema.js +0 -0
- /package/{common → core}/controllers/schemas/native-enum.schema.d.ts +0 -0
- /package/{common → core}/controllers/schemas/native-enum.schema.js +0 -0
- /package/{services/prisma → core/database}/prisma-client-with-custom-transaction.interface.d.ts +0 -0
- /package/{services/prisma → core/database}/prisma-client-with-custom-transaction.interface.js +0 -0
- /package/{services/prisma → core/database}/prisma-transactional-client.d.ts +0 -0
- /package/{services/prisma → core/database}/prisma-transactional-client.js +0 -0
- /package/{services/prisma → core/database}/prisma.service.d.ts +0 -0
- /package/{services/prisma → core/database}/prisma.service.js +0 -0
- /package/{common → core}/errors/bad-request.error.d.ts +0 -0
- /package/{common → core}/errors/bad-request.error.js +0 -0
- /package/{common → core}/errors/conflict.error.d.ts +0 -0
- /package/{common → core}/errors/conflict.error.js +0 -0
- /package/{common → core}/errors/no-content.error.d.ts +0 -0
- /package/{common → core}/errors/no-content.error.js +0 -0
- /package/{common → core}/errors/not-allowed.error.d.ts +0 -0
- /package/{common → core}/errors/not-allowed.error.js +0 -0
- /package/{common → core}/errors/resource-not-found.error.d.ts +0 -0
- /package/{common → core}/errors/resource-not-found.error.js +0 -0
- /package/{common → core}/errors/use-case-error.d.ts +0 -0
- /package/{common → core}/errors/use-case-error.js +0 -0
- /package/{common → core}/errors/user-already-exist.error.d.ts +0 -0
- /package/{common → core}/errors/user-already-exist.error.js +0 -0
- /package/{common → core}/errors/wrong-credentials.error.d.ts +0 -0
- /package/{common → core}/errors/wrong-credentials.error.js +0 -0
- /package/{common → core}/models/pagination-params.js +0 -0
- /package/{common/mediator → core/request-overflow}/request-handler.base.js +0 -0
- /package/{common/mediator → core/request-overflow}/request-result.d.ts +0 -0
- /package/{common/mediator → core/request-overflow}/request-result.js +0 -0
- /package/{common/mediator → core/request-overflow}/request-validator.base.js +0 -0
- /package/{services/env → env}/env.module.d.ts +0 -0
- /package/{services/env → env}/env.module.js +0 -0
- /package/{services/env → env}/env.service.js +0 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ILoggingService } from '../../../services/logging/ilogging.service';
|
|
2
|
+
import { IRedLockService } from '../../../services/redlock/ired-lock.service';
|
|
3
|
+
import { RequestResult } from '../../request-overflow/request-result';
|
|
4
|
+
export type CronJobResponse = RequestResult<Error, null>;
|
|
5
|
+
export declare abstract class CronJob {
|
|
6
|
+
private readonly redlockService;
|
|
7
|
+
private readonly loggingService;
|
|
8
|
+
private readonly _timeout;
|
|
9
|
+
constructor(redlockService: IRedLockService, loggingService: ILoggingService);
|
|
10
|
+
protected abstract run(): Promise<CronJobResponse>;
|
|
11
|
+
protected abstract isActive(): Promise<boolean>;
|
|
12
|
+
protected abstract defineTimeInMinutes(): number;
|
|
13
|
+
start(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -2,27 +2,24 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CronJob = void 0;
|
|
4
4
|
const delay_1 = require("@koalarx/utils/operators/delay");
|
|
5
|
+
const koala_global_vars_1 = require("../../koala-global-vars");
|
|
5
6
|
class CronJob {
|
|
6
7
|
redlockService;
|
|
7
8
|
loggingService;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
timeout;
|
|
11
|
-
constructor(redlockService, loggingService, loggedUsername, appName, timeInMinutes) {
|
|
9
|
+
_timeout;
|
|
10
|
+
constructor(redlockService, loggingService) {
|
|
12
11
|
this.redlockService = redlockService;
|
|
13
12
|
this.loggingService = loggingService;
|
|
14
|
-
this.
|
|
15
|
-
this.appName = appName;
|
|
16
|
-
this.timeout = timeInMinutes * 60 * 1000;
|
|
13
|
+
this._timeout = this.defineTimeInMinutes() * 60 * 1000;
|
|
17
14
|
}
|
|
18
|
-
async start(
|
|
15
|
+
async start() {
|
|
19
16
|
const name = this.constructor.name;
|
|
20
17
|
while (true) {
|
|
21
18
|
if (await this.isActive()) {
|
|
22
|
-
const ttlSecondsLock = this.
|
|
19
|
+
const ttlSecondsLock = this._timeout / 1000;
|
|
23
20
|
const acquiredLock = await this.redlockService.acquiredLock(name, ttlSecondsLock);
|
|
24
21
|
if (acquiredLock) {
|
|
25
|
-
const error = await
|
|
22
|
+
const error = await this.run()
|
|
26
23
|
.then((result) => {
|
|
27
24
|
if (result.isFailure()) {
|
|
28
25
|
return result.value;
|
|
@@ -31,15 +28,20 @@ class CronJob {
|
|
|
31
28
|
})
|
|
32
29
|
.catch((error) => error);
|
|
33
30
|
if (error) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
try {
|
|
32
|
+
await this.loggingService.report({
|
|
33
|
+
error,
|
|
34
|
+
packageName: koala_global_vars_1.KoalaGlobalVars.appName,
|
|
35
|
+
loggedUsername: koala_global_vars_1.KoalaGlobalVars.internalUserName,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
console.error(error);
|
|
40
|
+
}
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
|
-
await (0, delay_1.klDelay)(this.
|
|
44
|
+
await (0, delay_1.klDelay)(this._timeout);
|
|
43
45
|
await this.redlockService.releaseLock(name);
|
|
44
46
|
}
|
|
45
47
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CronJob, CronJobResponse } from './cron-job';
|
|
2
|
+
export declare class CronJobTest extends CronJob {
|
|
3
|
+
constructor();
|
|
4
|
+
static isCalled(): Promise<CronJobResponse>;
|
|
5
|
+
protected run(): Promise<CronJobResponse>;
|
|
6
|
+
protected isActive(): Promise<boolean>;
|
|
7
|
+
protected defineTimeInMinutes(): number;
|
|
8
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CronJobTest = void 0;
|
|
4
|
+
const delay_1 = require("@koalarx/utils/operators/delay");
|
|
5
|
+
const vitest_1 = require("vitest");
|
|
6
|
+
const fake_logging_service_1 = require("../../../test/services/fake-logging.service");
|
|
7
|
+
const fake_red_lock_service_1 = require("../../../test/services/fake-red-lock.service");
|
|
8
|
+
const request_result_1 = require("../../request-overflow/request-result");
|
|
9
|
+
const cron_job_1 = require("./cron-job");
|
|
10
|
+
class CronJobTest extends cron_job_1.CronJob {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(new fake_red_lock_service_1.FakeRedLockService(), new fake_logging_service_1.FakeLoggingService());
|
|
13
|
+
}
|
|
14
|
+
static async isCalled() {
|
|
15
|
+
return (0, request_result_1.ok)(null);
|
|
16
|
+
}
|
|
17
|
+
run() {
|
|
18
|
+
return CronJobTest.isCalled();
|
|
19
|
+
}
|
|
20
|
+
async isActive() {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
defineTimeInMinutes() {
|
|
24
|
+
return 0.01;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.CronJobTest = CronJobTest;
|
|
28
|
+
test('cron job', async () => {
|
|
29
|
+
const callbackSpy = vitest_1.vi.spyOn(CronJobTest, 'isCalled');
|
|
30
|
+
new CronJobTest().start();
|
|
31
|
+
await (0, delay_1.klDelay)(100);
|
|
32
|
+
expect(callbackSpy).toHaveBeenCalled();
|
|
33
|
+
});
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventClass = void 0;
|
|
4
4
|
class EventClass {
|
|
5
|
-
|
|
5
|
+
data;
|
|
6
6
|
ocurredAt = new Date();
|
|
7
|
-
constructor(
|
|
8
|
-
this.
|
|
7
|
+
constructor(data) {
|
|
8
|
+
this.data = data;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
exports.EventClass = EventClass;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { EventClass } from './event-class';
|
|
3
|
+
import { EventHandler } from './event-handler';
|
|
4
|
+
import { EventQueue } from './event-queue';
|
|
5
|
+
export declare abstract class EventJob<TEntity> {
|
|
6
|
+
_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
7
|
+
private _eventQueue;
|
|
8
|
+
constructor();
|
|
9
|
+
abstract defineHandlers(): Array<Type<EventHandler<any>>>;
|
|
10
|
+
get eventQueue(): EventQueue[];
|
|
11
|
+
clearQueue(): void;
|
|
12
|
+
addEvent(event: EventClass<TEntity>): void;
|
|
13
|
+
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventJob = void 0;
|
|
4
|
-
const entity_1 = require("../../../core/database/entity");
|
|
5
4
|
const event_queue_1 = require("./event-queue");
|
|
6
|
-
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
class EventJob {
|
|
7
|
+
_id = (0, node_crypto_1.randomUUID)();
|
|
7
8
|
_eventQueue = [];
|
|
9
|
+
constructor() { }
|
|
8
10
|
get eventQueue() {
|
|
9
11
|
return this._eventQueue;
|
|
10
12
|
}
|
|
@@ -35,17 +35,20 @@ class EventQueue {
|
|
|
35
35
|
aggregate.eventQueue.forEach((event) => this.dispatch(event));
|
|
36
36
|
}
|
|
37
37
|
static removeAggregateFromMarkedDispatchList(aggregate) {
|
|
38
|
-
const index = this.markedAggregates.findIndex((a) => a.
|
|
38
|
+
const index = this.markedAggregates.findIndex((a) => a._id === aggregate._id);
|
|
39
39
|
this.markedAggregates.splice(index, 1);
|
|
40
40
|
}
|
|
41
41
|
static findMarkedAggregateByID(id) {
|
|
42
42
|
return this.markedAggregates.find((aggregate) => aggregate._id === id);
|
|
43
43
|
}
|
|
44
44
|
static dispatch(event) {
|
|
45
|
-
const
|
|
46
|
-
const
|
|
45
|
+
const eventJobHandlers = this.markedAggregates.find((a) => a.eventQueue.find((e) => e === event));
|
|
46
|
+
const eventHandler = Object.keys(this.handlersMap).find((handlerName) => eventJobHandlers
|
|
47
|
+
?.defineHandlers()
|
|
48
|
+
.find((handler) => handler.name === handlerName));
|
|
49
|
+
const isEventRegistered = !!eventHandler;
|
|
47
50
|
if (isEventRegistered) {
|
|
48
|
-
const handlers = this.handlersMap[
|
|
51
|
+
const handlers = this.handlersMap[eventHandler];
|
|
49
52
|
for (const handler of handlers) {
|
|
50
53
|
handler(event);
|
|
51
54
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const event_class_1 = require("./event-class");
|
|
5
|
+
const event_handler_1 = require("./event-handler");
|
|
6
|
+
const event_job_1 = require("./event-job");
|
|
7
|
+
const event_queue_1 = require("./event-queue");
|
|
8
|
+
class CustomEvent extends event_class_1.EventClass {
|
|
9
|
+
}
|
|
10
|
+
class CustomEventHandler extends event_handler_1.EventHandler {
|
|
11
|
+
static async isCalled() {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
async handleEvent(event) {
|
|
15
|
+
await CustomEventHandler.isCalled();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class CustomEventJob extends event_job_1.EventJob {
|
|
19
|
+
defineHandlers() {
|
|
20
|
+
return [CustomEventHandler];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
describe('event queue', () => {
|
|
24
|
+
it('should be able to dispatch and listen to events', () => {
|
|
25
|
+
const callbackSpy = vitest_1.vi.spyOn(CustomEventHandler, 'isCalled');
|
|
26
|
+
new CustomEventHandler().setupSubscriptions();
|
|
27
|
+
const jobs = new CustomEventJob();
|
|
28
|
+
jobs.addEvent(new CustomEvent());
|
|
29
|
+
expect(jobs.eventQueue).toHaveLength(1);
|
|
30
|
+
event_queue_1.EventQueue.dispatchEventsForAggregate(jobs._id);
|
|
31
|
+
expect(callbackSpy).toHaveBeenCalled();
|
|
32
|
+
expect(jobs.eventQueue).toHaveLength(0);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.booleanSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
function booleanSchema() {
|
|
6
|
+
return zod_1.z.coerce
|
|
7
|
+
.string()
|
|
8
|
+
.transform((value) => {
|
|
9
|
+
if (value !== undefined) {
|
|
10
|
+
return value === 'true';
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
exports.booleanSchema = booleanSchema;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IComparable, IComparableId } from '../utils/interfaces/icomparable';
|
|
2
|
+
export type EntityProps<T extends IComparable<T>> = Omit<{
|
|
3
|
+
[K in keyof T as T[K] extends Function ? never : K]: T[K];
|
|
4
|
+
}, '_id'>;
|
|
5
|
+
export declare abstract class EntityBase<T extends IComparable<T>> implements IComparable<T> {
|
|
6
|
+
_id: IComparableId;
|
|
7
|
+
automap(props?: EntityProps<T>): void;
|
|
8
|
+
equals(obj: EntityBase<T>): boolean;
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityBase = void 0;
|
|
4
|
+
const list_1 = require("../utils/list");
|
|
5
|
+
class EntityBase {
|
|
6
|
+
_id;
|
|
7
|
+
automap(props) {
|
|
8
|
+
if (props) {
|
|
9
|
+
for (const key of Object.keys(props)) {
|
|
10
|
+
if (Array.isArray(props[key]) && this[key] instanceof list_1.List) {
|
|
11
|
+
let value = props[key];
|
|
12
|
+
if (this[key].entityType) {
|
|
13
|
+
value = value.map((item) => {
|
|
14
|
+
const entity = new this[key].entityType();
|
|
15
|
+
entity.automap(item);
|
|
16
|
+
return entity;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
this[key].setList(value);
|
|
20
|
+
}
|
|
21
|
+
else if (this[key] instanceof EntityBase) {
|
|
22
|
+
const entity = new this[key].constructor();
|
|
23
|
+
entity.automap(props[key]);
|
|
24
|
+
this[key] = entity;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
if (key === 'id') {
|
|
28
|
+
this._id = props[key];
|
|
29
|
+
}
|
|
30
|
+
this[key] = props[key];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
equals(obj) {
|
|
36
|
+
return obj._id === this._id;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.EntityBase = EntityBase;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import { PaginationParams } from '../../common/models/pagination-params';
|
|
3
|
-
import { PrismaTransactionalClient } from '../../services/prisma/prisma-transactional-client';
|
|
4
2
|
import { ListResponse } from '../@types';
|
|
3
|
+
import { PaginationParams } from '../models/pagination-params';
|
|
5
4
|
import { IComparableId } from '../utils/interfaces/icomparable';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
where: TWhere;
|
|
9
|
-
paginateAndOrderingProps?: TPaginateAndOrdering;
|
|
10
|
-
include?: TInclude;
|
|
11
|
-
}
|
|
5
|
+
import { EntityBase } from './entity.base';
|
|
6
|
+
import { PrismaTransactionalClient } from './prisma-transactional-client';
|
|
12
7
|
type RepositoryInclude<TEntity> = {
|
|
13
8
|
[key in keyof TEntity]?: boolean | RepositoryInclude<TEntity[keyof TEntity]>;
|
|
14
9
|
};
|
|
@@ -18,12 +13,11 @@ interface RepositoryInitProps<TEntity> {
|
|
|
18
13
|
transactionContext?: Type<PrismaTransactionalClient>;
|
|
19
14
|
include?: RepositoryInclude<TEntity>;
|
|
20
15
|
}
|
|
21
|
-
export declare abstract class RepositoryBase<TEntity extends
|
|
16
|
+
export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>> {
|
|
22
17
|
protected _context: PrismaTransactionalClient;
|
|
23
|
-
private _transactionContext?;
|
|
24
18
|
private readonly _modelName;
|
|
25
19
|
private readonly _include?;
|
|
26
|
-
constructor({ context,
|
|
20
|
+
constructor({ context, modelName, include, }: RepositoryInitProps<TEntity>);
|
|
27
21
|
withTransaction(fn: (prisma: PrismaTransactionalClient) => Promise<any>): Promise<any>;
|
|
28
22
|
protected findById(id: IComparableId): Promise<TEntity | null>;
|
|
29
23
|
protected findMany<T>(where: T, pagination?: PaginationParams): Promise<any>;
|
|
@@ -34,5 +28,6 @@ export declare abstract class RepositoryBase<TEntity extends Entity<TEntity>> {
|
|
|
34
28
|
private entityToPrisma;
|
|
35
29
|
private context;
|
|
36
30
|
private findManySchema;
|
|
31
|
+
private createEntity;
|
|
37
32
|
}
|
|
38
33
|
export {};
|
|
@@ -2,23 +2,22 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RepositoryBase = void 0;
|
|
4
4
|
const string_1 = require("@koalarx/utils/operators/string");
|
|
5
|
-
const
|
|
5
|
+
const koala_global_vars_1 = require("../koala-global-vars");
|
|
6
|
+
const pagination_params_1 = require("../models/pagination-params");
|
|
6
7
|
const list_1 = require("../utils/list");
|
|
7
|
-
const
|
|
8
|
+
const entity_base_1 = require("./entity.base");
|
|
8
9
|
class RepositoryBase {
|
|
9
10
|
_context;
|
|
10
|
-
_transactionContext;
|
|
11
11
|
_modelName;
|
|
12
12
|
_include;
|
|
13
|
-
constructor({ context,
|
|
13
|
+
constructor({ context, modelName, include, }) {
|
|
14
14
|
this._context = context;
|
|
15
|
-
this._transactionContext = transactionContext;
|
|
16
15
|
this._modelName = modelName;
|
|
17
16
|
this._include = include;
|
|
18
17
|
}
|
|
19
18
|
withTransaction(fn) {
|
|
20
19
|
return this._context.withTransaction(async (client) => {
|
|
21
|
-
return fn(new
|
|
20
|
+
return fn(new koala_global_vars_1.KoalaGlobalVars.dbTransactionContext(client));
|
|
22
21
|
});
|
|
23
22
|
}
|
|
24
23
|
async findById(id) {
|
|
@@ -29,7 +28,7 @@ class RepositoryBase {
|
|
|
29
28
|
})
|
|
30
29
|
.then((response) => {
|
|
31
30
|
if (response) {
|
|
32
|
-
return
|
|
31
|
+
return this.createEntity(response);
|
|
33
32
|
}
|
|
34
33
|
return null;
|
|
35
34
|
});
|
|
@@ -37,11 +36,7 @@ class RepositoryBase {
|
|
|
37
36
|
async findMany(where, pagination) {
|
|
38
37
|
return this.context()
|
|
39
38
|
.findMany(this.findManySchema(where, pagination))
|
|
40
|
-
.then((result) =>
|
|
41
|
-
return result.map((response) => {
|
|
42
|
-
return new this._modelName(response);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
39
|
+
.then((result) => result.map((response) => this.createEntity(response)));
|
|
45
40
|
}
|
|
46
41
|
async findManyAndCount(where, pagination) {
|
|
47
42
|
const count = await this.context().count({ where });
|
|
@@ -113,15 +108,17 @@ class RepositoryBase {
|
|
|
113
108
|
.filter((key) => key !== 'id' && key !== '_id')
|
|
114
109
|
.forEach((key) => {
|
|
115
110
|
if (entity[key] instanceof list_1.List) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
111
|
+
if (entity[key].toArray('added').length > 0) {
|
|
112
|
+
prismaSchema[key] = {
|
|
113
|
+
createMany: {
|
|
114
|
+
data: entity[key].toArray('added').map((item) => {
|
|
115
|
+
return this.entityToPrisma(item);
|
|
116
|
+
}),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
123
120
|
}
|
|
124
|
-
else if (entity[key] instanceof
|
|
121
|
+
else if (entity[key] instanceof entity_base_1.EntityBase) {
|
|
125
122
|
prismaSchema[key] = this.entityToPrisma(entity[key]);
|
|
126
123
|
}
|
|
127
124
|
else {
|
|
@@ -131,7 +128,7 @@ class RepositoryBase {
|
|
|
131
128
|
return prismaSchema;
|
|
132
129
|
}
|
|
133
130
|
context(transactionalClient) {
|
|
134
|
-
const modelName = this._modelName
|
|
131
|
+
const modelName = this._modelName.name;
|
|
135
132
|
if (!modelName)
|
|
136
133
|
throw new Error('modelName não informado no contrutor do repositorio');
|
|
137
134
|
if (transactionalClient) {
|
|
@@ -148,5 +145,10 @@ class RepositoryBase {
|
|
|
148
145
|
take: (pagination?.limit ?? 0) > 0 ? pagination?.limit : undefined,
|
|
149
146
|
};
|
|
150
147
|
}
|
|
148
|
+
createEntity(data) {
|
|
149
|
+
const entity = new this._modelName();
|
|
150
|
+
entity.automap(data);
|
|
151
|
+
return entity;
|
|
152
|
+
}
|
|
151
153
|
}
|
|
152
154
|
exports.RepositoryBase = RepositoryBase;
|
package/core/koala-app.d.ts
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
import { INestApplication, Type } from '@nestjs/common';
|
|
2
2
|
import { BaseExceptionFilter } from '@nestjs/core';
|
|
3
|
-
import { CronJob } from '../
|
|
4
|
-
import { EventHandler } from '../
|
|
3
|
+
import { CronJob } from '../core/backgroud-services/cron-service/cron-job';
|
|
4
|
+
import { EventHandler } from '../core/backgroud-services/event-service/event-handler';
|
|
5
|
+
import { PrismaTransactionalClient } from './database/prisma-transactional-client';
|
|
5
6
|
interface ApiDocConfig {
|
|
6
7
|
endpoint: string;
|
|
7
8
|
title: string;
|
|
9
|
+
ui?: 'swagger' | 'scalar';
|
|
8
10
|
description?: string;
|
|
9
11
|
externalDoc?: {
|
|
10
12
|
message: string;
|
|
11
13
|
url: string;
|
|
12
14
|
};
|
|
13
15
|
version: string;
|
|
14
|
-
accessDocWithCredentials?: {
|
|
15
|
-
username: string;
|
|
16
|
-
password: string;
|
|
17
|
-
};
|
|
18
16
|
withAuthorization?: boolean;
|
|
19
17
|
}
|
|
20
18
|
type CronJobClass = string | symbol | Function | Type<CronJob>;
|
|
19
|
+
type EventJobClass = string | symbol | Function | Type<EventHandler<any>>;
|
|
21
20
|
export declare class KoalaApp {
|
|
22
21
|
private readonly app;
|
|
23
22
|
private _globalExceptionFilter;
|
|
@@ -28,13 +27,16 @@ export declare class KoalaApp {
|
|
|
28
27
|
private _eventJobs;
|
|
29
28
|
constructor(app: INestApplication<any>);
|
|
30
29
|
addCronJob(job: CronJobClass): this;
|
|
31
|
-
addEventJob(eventJob:
|
|
30
|
+
addEventJob(eventJob: EventJobClass): this;
|
|
32
31
|
addCustomGlobalExceptionFilter(filter: BaseExceptionFilter): this;
|
|
33
32
|
addCustomPrismaValidationExceptionFilter(filter: BaseExceptionFilter): this;
|
|
34
33
|
addCustomDomainExceptionFilter(filter: BaseExceptionFilter): this;
|
|
35
34
|
addCustomZodExceptionFilter(filter: BaseExceptionFilter): this;
|
|
36
|
-
|
|
35
|
+
useDoc(config: ApiDocConfig): this;
|
|
37
36
|
enableCors(): this;
|
|
37
|
+
setAppName(name: string): this;
|
|
38
|
+
setInternalUserName(name: string): this;
|
|
39
|
+
setDbTransactionContext(transactionContext: Type<PrismaTransactionalClient>): this;
|
|
38
40
|
build(): Promise<INestApplication<any>>;
|
|
39
41
|
}
|
|
40
42
|
export {};
|
package/core/koala-app.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.KoalaApp = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
4
5
|
const core_1 = require("@nestjs/core");
|
|
5
6
|
const swagger_1 = require("@nestjs/swagger");
|
|
6
|
-
const
|
|
7
|
+
const nestjs_api_reference_1 = require("@scalar/nestjs-api-reference");
|
|
8
|
+
const expressBasicAuth = require("express-basic-auth");
|
|
7
9
|
const domain_errors_filter_1 = require("../filters/domain-errors.filter");
|
|
8
10
|
const global_exception_filter_1 = require("../filters/global-exception.filter");
|
|
9
11
|
const prisma_validation_exception_filter_1 = require("../filters/prisma-validation-exception.filter");
|
|
10
12
|
const zod_errors_filter_1 = require("../filters/zod-errors.filter");
|
|
13
|
+
const koala_global_vars_1 = require("./koala-global-vars");
|
|
11
14
|
const env_config_1 = require("./utils/env.config");
|
|
12
15
|
class KoalaApp {
|
|
13
16
|
app;
|
|
@@ -50,12 +53,16 @@ class KoalaApp {
|
|
|
50
53
|
this._zodExceptionFilter = filter;
|
|
51
54
|
return this;
|
|
52
55
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
useDoc(config) {
|
|
57
|
+
const credentials = {
|
|
58
|
+
username: process.env.SWAGGER_USERNAME ?? '',
|
|
59
|
+
password: process.env.SWAGGER_PASSWORD ?? ''
|
|
60
|
+
};
|
|
61
|
+
if (env_config_1.EnvConfig.isEnvDevelop && credentials.username && credentials.password) {
|
|
62
|
+
this.app.use([config.endpoint], expressBasicAuth({
|
|
56
63
|
challenge: true,
|
|
57
64
|
users: {
|
|
58
|
-
[
|
|
65
|
+
[credentials.username]: credentials.password
|
|
59
66
|
},
|
|
60
67
|
}));
|
|
61
68
|
}
|
|
@@ -73,7 +80,15 @@ class KoalaApp {
|
|
|
73
80
|
}
|
|
74
81
|
const document = swagger_1.SwaggerModule.createDocument(this.app, documentBuilder.build());
|
|
75
82
|
const swaggerEndpoint = config.endpoint;
|
|
76
|
-
|
|
83
|
+
if (config.ui === 'scalar' && swaggerEndpoint === '/') {
|
|
84
|
+
throw new common_1.InternalServerErrorException("O endpoint de documentação não pode ser '/' para UI Scalar.");
|
|
85
|
+
}
|
|
86
|
+
swagger_1.SwaggerModule.setup(swaggerEndpoint, this.app, document, {
|
|
87
|
+
swaggerUiEnabled: config.ui !== 'scalar',
|
|
88
|
+
});
|
|
89
|
+
if (config.ui === 'scalar') {
|
|
90
|
+
this.app.use(swaggerEndpoint, (0, nestjs_api_reference_1.apiReference)({ spec: { content: document } }));
|
|
91
|
+
}
|
|
77
92
|
return this;
|
|
78
93
|
}
|
|
79
94
|
enableCors() {
|
|
@@ -84,10 +99,28 @@ class KoalaApp {
|
|
|
84
99
|
});
|
|
85
100
|
return this;
|
|
86
101
|
}
|
|
102
|
+
setAppName(name) {
|
|
103
|
+
koala_global_vars_1.KoalaGlobalVars.appName = name;
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
setInternalUserName(name) {
|
|
107
|
+
koala_global_vars_1.KoalaGlobalVars.internalUserName = name;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
setDbTransactionContext(transactionContext) {
|
|
111
|
+
koala_global_vars_1.KoalaGlobalVars.dbTransactionContext = transactionContext;
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
87
114
|
async build() {
|
|
88
115
|
this.app.useGlobalFilters(this._globalExceptionFilter, this._prismaValidationExceptionFilter, this._domainExceptionFilter, this._zodExceptionFilter);
|
|
89
|
-
Promise.all(this._cronJobs.map((job) => this.app.resolve(job)))
|
|
90
|
-
|
|
116
|
+
const cronJobs = await Promise.all(this._cronJobs.map((job) => this.app.resolve(job)));
|
|
117
|
+
for (const cronJob of cronJobs) {
|
|
118
|
+
cronJob.start();
|
|
119
|
+
}
|
|
120
|
+
const eventJobs = await Promise.all(this._eventJobs.map((job) => this.app.resolve(job)));
|
|
121
|
+
for (const eventJob of eventJobs) {
|
|
122
|
+
eventJob.setupSubscriptions();
|
|
123
|
+
}
|
|
91
124
|
return this.app;
|
|
92
125
|
}
|
|
93
126
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Type } from "@nestjs/common";
|
|
2
|
+
import { PrismaTransactionalClient } from "./database/prisma-transactional-client";
|
|
3
|
+
export declare class KoalaGlobalVars {
|
|
4
|
+
static appName: string;
|
|
5
|
+
static internalUserName: string;
|
|
6
|
+
static dbTransactionContext?: Type<PrismaTransactionalClient>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KoalaGlobalVars = void 0;
|
|
4
|
+
class KoalaGlobalVars {
|
|
5
|
+
static appName = 'koala-nest';
|
|
6
|
+
static internalUserName = 'internal';
|
|
7
|
+
static dbTransactionContext;
|
|
8
|
+
}
|
|
9
|
+
exports.KoalaGlobalVars = KoalaGlobalVars;
|
|
@@ -9,8 +9,8 @@ var KoalaNestDatabaseModule_1;
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.KoalaNestDatabaseModule = exports.PRISMA_TOKEN = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
|
-
const env_service_1 = require("../
|
|
13
|
-
const prisma_service_1 = require("
|
|
12
|
+
const env_service_1 = require("../env/env.service");
|
|
13
|
+
const prisma_service_1 = require("./database/prisma.service");
|
|
14
14
|
exports.PRISMA_TOKEN = 'PRISMA_SERVICE_TOKEN';
|
|
15
15
|
let KoalaNestDatabaseModule = exports.KoalaNestDatabaseModule = KoalaNestDatabaseModule_1 = class KoalaNestDatabaseModule {
|
|
16
16
|
static register(config) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DynamicModule, MiddlewareConsumer, NestMiddleware, NestModule,
|
|
2
|
-
import {
|
|
1
|
+
import { DynamicModule, MiddlewareConsumer, NestMiddleware, NestModule, Type } from '@nestjs/common';
|
|
2
|
+
import { AutoMappingProfile } from './mapping/auto-mapping-profile';
|
|
3
3
|
interface KoalaNestHttpModuleConfig {
|
|
4
|
-
automapperProfile:
|
|
4
|
+
automapperProfile: Type<AutoMappingProfile>;
|
|
5
5
|
middlewares?: Type<NestMiddleware>[];
|
|
6
6
|
}
|
|
7
7
|
export declare class KoalaNestHttpModule implements NestModule {
|