@forklaunch/implementation-worker-database 0.1.0 → 0.1.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.
@@ -1,2 +1,2 @@
1
1
  export {};
2
- //# sourceMappingURL=schemaEquality.test.d.ts.map
2
+ //# sourceMappingURL=schemaEquality.test.d.ts.map
@@ -3,10 +3,18 @@ import { testSchemaEquality } from '@forklaunch/core/test';
3
3
  import { DatabaseWorkerOptionsSchema as TypeboxDatabaseWorkerOptionsSchema } from '../schemas/typebox/databaseWorker.schema';
4
4
  import { DatabaseWorkerOptionsSchema as ZodDatabaseWorkerOptionsSchema } from '../schemas/zod/databaseWorker.schema';
5
5
  describe('schema equality', () => {
6
- it('should be equal for bullmq worker', () => {
7
- expect(isTrue(testSchemaEquality(ZodDatabaseWorkerOptionsSchema, TypeboxDatabaseWorkerOptionsSchema, {
6
+ it('should be equal for bullmq worker', () => {
7
+ expect(
8
+ isTrue(
9
+ testSchemaEquality(
10
+ ZodDatabaseWorkerOptionsSchema,
11
+ TypeboxDatabaseWorkerOptionsSchema,
12
+ {
8
13
  retries: 1,
9
14
  interval: 1000
10
- }))).toBeTruthy();
11
- });
15
+ }
16
+ )
17
+ )
18
+ ).toBeTruthy();
19
+ });
12
20
  });
@@ -1,18 +1,32 @@
1
1
  import { BaseEntity } from '@forklaunch/core/persistence';
2
2
  import { WorkerConsumer } from '@forklaunch/interfaces-worker/interfaces';
3
- import { WorkerEventEntity, WorkerFailureHandler, WorkerProcessFunction } from '@forklaunch/interfaces-worker/types';
4
- import { EntityManager } from '@mikro-orm/core';
3
+ import {
4
+ WorkerEventEntity,
5
+ WorkerFailureHandler,
6
+ WorkerProcessFunction
7
+ } from '@forklaunch/interfaces-worker/types';
8
+ import { EntityManager, EntityName } from '@mikro-orm/core';
5
9
  import { DatabaseWorkerOptions } from '../types/databaseWorker.types';
6
- export declare class DatabaseWorkerConsumer<EventEntity extends WorkerEventEntity & BaseEntity> implements WorkerConsumer<EventEntity> {
7
- protected readonly entityName: string;
8
- protected readonly em: EntityManager;
9
- protected readonly options: DatabaseWorkerOptions;
10
- protected readonly processEvents: WorkerProcessFunction<EventEntity>;
11
- protected readonly failureHandler: WorkerFailureHandler<EventEntity>;
12
- constructor(entityName: string, em: EntityManager, options: DatabaseWorkerOptions, processEvents: WorkerProcessFunction<EventEntity>, failureHandler: WorkerFailureHandler<EventEntity>);
13
- private retrieveEvents;
14
- private updateEvents;
15
- peekEvents(): Promise<EventEntity[]>;
16
- start(): Promise<void>;
10
+ export declare class DatabaseWorkerConsumer<
11
+ EventEntity extends WorkerEventEntity & BaseEntity,
12
+ Options extends DatabaseWorkerOptions
13
+ > implements WorkerConsumer<EventEntity>
14
+ {
15
+ protected readonly entityName: EntityName<EventEntity>;
16
+ protected readonly em: EntityManager;
17
+ protected readonly options: Options;
18
+ protected readonly processEvents: WorkerProcessFunction<EventEntity>;
19
+ protected readonly failureHandler: WorkerFailureHandler<EventEntity>;
20
+ constructor(
21
+ entityName: EntityName<EventEntity>,
22
+ em: EntityManager,
23
+ options: Options,
24
+ processEvents: WorkerProcessFunction<EventEntity>,
25
+ failureHandler: WorkerFailureHandler<EventEntity>
26
+ );
27
+ private retrieveEvents;
28
+ private updateEvents;
29
+ peekEvents(): Promise<EventEntity[]>;
30
+ start(): Promise<void>;
17
31
  }
18
- //# sourceMappingURL=databaseWorker.consumer.d.ts.map
32
+ //# sourceMappingURL=databaseWorker.consumer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"databaseWorker.consumer.d.ts","sourceRoot":"","sources":["../../consumers/databaseWorker.consumer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,0CAA0C,CAAC;AAC1E,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACtB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE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AqB,CACrC,YAAW,cAAc,CAAC,WAAW,CAAC;IAGpC,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,WAAW,CAAC;IACtD,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,aAAa;IACpC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO;IACnC,SAAS,CAAC,QAAQ,CAAC,aAAa,EAAE,qBAAqB,CAAC,WAAW,CAAC;IACpE,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,oBAAoB,CAAC,WAAW,CAAC;gBAJjD,UAAU,EAAE,UAAU,CAAC,WAAW,CAAC,EACnC,EAAE,EAAE,aAAa,EACjB,OAAO,EAAE,OAAO,EAChB,aAAa,EAAE,qBAAqB,CAAC,WAAW,CAAC,EACjD,cAAc,EAAE,oBAAoB,CAAC,WAAW,CAAC;YAGxD,cAAc;YAUd,YAAY;IASpB,UAAU,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAIpC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAS7B"}
@@ -1,41 +1,43 @@
1
1
  export class DatabaseWorkerConsumer {
2
- entityName;
3
- em;
4
- options;
5
- processEvents;
6
- failureHandler;
7
- constructor(entityName, em, options, processEvents, failureHandler) {
8
- this.entityName = entityName;
9
- this.em = em;
10
- this.options = options;
11
- this.processEvents = processEvents;
12
- this.failureHandler = failureHandler;
13
- }
14
- async retrieveEvents() {
15
- return await this.em.getRepository(this.entityName).find({
16
- where: {
17
- processed: false,
18
- retryCount: { $lt: this.options.retries }
19
- },
20
- orderBy: { createdAt: 'ASC' }
21
- });
22
- }
23
- async updateEvents(events) {
24
- await this.em.getRepository(this.entityName).upsertMany(events.map((event) => ({
25
- ...event,
26
- retryCount: event.retryCount + 1
27
- })));
28
- }
29
- async peekEvents() {
30
- return await this.retrieveEvents();
31
- }
32
- async start() {
33
- setInterval(async () => {
34
- const events = await this.retrieveEvents();
35
- const failedEvents = await this.processEvents(events);
36
- await this.failureHandler(failedEvents);
37
- await this.updateEvents(failedEvents.map((event) => event.value));
38
- await this.em.flush();
39
- }, this.options.interval);
40
- }
2
+ entityName;
3
+ em;
4
+ options;
5
+ processEvents;
6
+ failureHandler;
7
+ constructor(entityName, em, options, processEvents, failureHandler) {
8
+ this.entityName = entityName;
9
+ this.em = em;
10
+ this.options = options;
11
+ this.processEvents = processEvents;
12
+ this.failureHandler = failureHandler;
13
+ }
14
+ async retrieveEvents() {
15
+ return await this.em.getRepository(this.entityName).find({
16
+ where: {
17
+ processed: false,
18
+ retryCount: { $lt: this.options.retries }
19
+ },
20
+ orderBy: { createdAt: 'ASC' }
21
+ });
22
+ }
23
+ async updateEvents(events) {
24
+ await this.em.getRepository(this.entityName).upsertMany(
25
+ events.map((event) => ({
26
+ ...event,
27
+ retryCount: event.retryCount + 1
28
+ }))
29
+ );
30
+ }
31
+ async peekEvents() {
32
+ return await this.retrieveEvents();
33
+ }
34
+ async start() {
35
+ setInterval(async () => {
36
+ const events = await this.retrieveEvents();
37
+ const failedEvents = await this.processEvents(events);
38
+ await this.failureHandler(failedEvents);
39
+ await this.updateEvents(failedEvents.map((event) => event.value));
40
+ await this.em.flush();
41
+ }, this.options.interval);
42
+ }
41
43
  }
@@ -1,2 +1,2 @@
1
1
  export * from './databaseWorker.consumer';
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=index.d.ts.map
@@ -5,23 +5,24 @@ import {
5
5
  WorkerFailureHandler,
6
6
  WorkerProcessFunction
7
7
  } from '@forklaunch/interfaces-worker/types';
8
- import { EntityManager } from '@mikro-orm/core';
8
+ import { EntityManager, EntityName } from '@mikro-orm/core';
9
9
  import { DatabaseWorkerOptions } from '../types/databaseWorker.types';
10
10
 
11
11
  export class DatabaseWorkerConsumer<
12
- EventEntity extends WorkerEventEntity & BaseEntity
12
+ EventEntity extends WorkerEventEntity & BaseEntity,
13
+ Options extends DatabaseWorkerOptions
13
14
  > implements WorkerConsumer<EventEntity>
14
15
  {
15
16
  constructor(
16
- protected readonly entityName: string,
17
+ protected readonly entityName: EntityName<EventEntity>,
17
18
  protected readonly em: EntityManager,
18
- protected readonly options: DatabaseWorkerOptions,
19
+ protected readonly options: Options,
19
20
  protected readonly processEvents: WorkerProcessFunction<EventEntity>,
20
21
  protected readonly failureHandler: WorkerFailureHandler<EventEntity>
21
22
  ) {}
22
23
 
23
24
  private async retrieveEvents(): Promise<EventEntity[]> {
24
- return await this.em.getRepository<EventEntity>(this.entityName).find({
25
+ return await this.em.getRepository(this.entityName).find({
25
26
  where: {
26
27
  processed: false,
27
28
  retryCount: { $lt: this.options.retries }
@@ -1,4 +1,4 @@
1
1
  import type { JestConfigWithTsJest } from 'ts-jest';
2
2
  declare const jestConfig: JestConfigWithTsJest;
3
3
  export default jestConfig;
4
- //# sourceMappingURL=jest.config.d.ts.map
4
+ //# sourceMappingURL=jest.config.d.ts.map
@@ -1,19 +1,19 @@
1
1
  const jestConfig = {
2
- preset: 'ts-jest/presets/default-esm', // or other ESM presets
3
- moduleNameMapper: {
4
- '^(\\.{1,2}/.*)\\.js$': '$1'
5
- },
6
- transform: {
7
- // '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
8
- // '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
9
- '^.+\\.[tj]sx?$': [
10
- 'ts-jest',
11
- {
12
- useESM: true
13
- }
14
- ],
15
- '^.+\\.js$': 'babel-jest'
16
- },
17
- testPathIgnorePatterns: ['.*dist/', '.*node_modules/']
2
+ preset: 'ts-jest/presets/default-esm', // or other ESM presets
3
+ moduleNameMapper: {
4
+ '^(\\.{1,2}/.*)\\.js$': '$1'
5
+ },
6
+ transform: {
7
+ // '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
8
+ // '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
9
+ '^.+\\.[tj]sx?$': [
10
+ 'ts-jest',
11
+ {
12
+ useESM: true
13
+ }
14
+ ],
15
+ '^.+\\.js$': 'babel-jest'
16
+ },
17
+ testPathIgnorePatterns: ['.*dist/', '.*node_modules/']
18
18
  };
19
19
  export default jestConfig;
@@ -1,10 +1,12 @@
1
1
  import { BaseEntity } from '@forklaunch/core/persistence';
2
2
  import { WorkerEventEntity } from '@forklaunch/interfaces-worker/types';
3
3
  import { EntityManager } from '@mikro-orm/core';
4
- export declare class DatabaseWorkerProducer<EventEntity extends WorkerEventEntity & BaseEntity> {
5
- private readonly em;
6
- constructor(em: EntityManager);
7
- enqueueJob(event: EventEntity): Promise<void>;
8
- enqueueBatchJobs(events: EventEntity[]): Promise<void>;
4
+ export declare class DatabaseWorkerProducer<
5
+ EventEntity extends WorkerEventEntity & BaseEntity
6
+ > {
7
+ private readonly em;
8
+ constructor(em: EntityManager);
9
+ enqueueJob(event: EventEntity): Promise<void>;
10
+ enqueueBatchJobs(events: EventEntity[]): Promise<void>;
9
11
  }
10
- //# sourceMappingURL=databaseWorker.producer.d.ts.map
12
+ //# sourceMappingURL=databaseWorker.producer.d.ts.map
@@ -1,12 +1,12 @@
1
1
  export class DatabaseWorkerProducer {
2
- em;
3
- constructor(em) {
4
- this.em = em;
5
- }
6
- async enqueueJob(event) {
7
- await this.em.persistAndFlush(event);
8
- }
9
- async enqueueBatchJobs(events) {
10
- await this.em.persistAndFlush(events);
11
- }
2
+ em;
3
+ constructor(em) {
4
+ this.em = em;
5
+ }
6
+ async enqueueJob(event) {
7
+ await this.em.persistAndFlush(event);
8
+ }
9
+ async enqueueBatchJobs(events) {
10
+ await this.em.persistAndFlush(events);
11
+ }
12
12
  }
@@ -1,2 +1,2 @@
1
1
  export * from './databaseWorker.producer';
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=index.d.ts.map
@@ -1,10 +1,42 @@
1
- export declare const DatabaseWorkerSchemas: <SchemaValidator extends import("@forklaunch/validator").AnySchemaValidator>(options: Record<string, unknown> & {
1
+ export declare const DatabaseWorkerSchemas: <
2
+ SchemaValidator extends import('@forklaunch/validator').AnySchemaValidator
3
+ >(
4
+ options: Record<string, unknown> & {
2
5
  validator: SchemaValidator;
3
- }) => import("@forklaunch/core/mappers").SchemasByValidator<SchemaValidator, (options: Record<string, unknown>) => {
4
- retries: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TString, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull, import("@sinclair/typebox").TDate, import("@sinclair/typebox").TBigInt]>, number>;
5
- interval: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TString, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull, import("@sinclair/typebox").TDate, import("@sinclair/typebox").TBigInt]>, number>;
6
- }, (options: Record<string, unknown>) => {
7
- retries: import("zod").ZodNumber;
8
- interval: import("zod").ZodNumber;
9
- }>;
10
- //# sourceMappingURL=databaseWorker.schema.d.ts.map
6
+ }
7
+ ) => import('@forklaunch/core/mappers').SchemasByValidator<
8
+ SchemaValidator,
9
+ (options: Record<string, unknown>) => {
10
+ retries: import('@sinclair/typebox').TTransform<
11
+ import('@sinclair/typebox').TUnion<
12
+ [
13
+ import('@sinclair/typebox').TNumber,
14
+ import('@sinclair/typebox').TString,
15
+ import('@sinclair/typebox').TBoolean,
16
+ import('@sinclair/typebox').TNull,
17
+ import('@sinclair/typebox').TDate,
18
+ import('@sinclair/typebox').TBigInt
19
+ ]
20
+ >,
21
+ number
22
+ >;
23
+ interval: import('@sinclair/typebox').TTransform<
24
+ import('@sinclair/typebox').TUnion<
25
+ [
26
+ import('@sinclair/typebox').TNumber,
27
+ import('@sinclair/typebox').TString,
28
+ import('@sinclair/typebox').TBoolean,
29
+ import('@sinclair/typebox').TNull,
30
+ import('@sinclair/typebox').TDate,
31
+ import('@sinclair/typebox').TBigInt
32
+ ]
33
+ >,
34
+ number
35
+ >;
36
+ },
37
+ (options: Record<string, unknown>) => {
38
+ retries: import('zod').ZodNumber;
39
+ interval: import('zod').ZodNumber;
40
+ }
41
+ >;
42
+ //# sourceMappingURL=databaseWorker.schema.d.ts.map
@@ -1,4 +1,7 @@
1
1
  import { serviceSchemaResolver } from '@forklaunch/core/mappers';
2
2
  import { DatabaseWorkerOptionsSchema as TypeBoxSchemas } from './typebox/databaseWorker.schema';
3
3
  import { DatabaseWorkerOptionsSchema as ZodSchemas } from './zod/databaseWorker.schema';
4
- export const DatabaseWorkerSchemas = serviceSchemaResolver(() => TypeBoxSchemas, () => ZodSchemas);
4
+ export const DatabaseWorkerSchemas = serviceSchemaResolver(
5
+ () => TypeBoxSchemas,
6
+ () => ZodSchemas
7
+ );
@@ -1,2 +1,2 @@
1
1
  export * from './databaseWorker.schema';
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=index.d.ts.map
@@ -1,5 +1,29 @@
1
1
  export declare const DatabaseWorkerOptionsSchema: {
2
- retries: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TString, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull, import("@sinclair/typebox").TDate, import("@sinclair/typebox").TBigInt]>, number>;
3
- interval: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TString, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull, import("@sinclair/typebox").TDate, import("@sinclair/typebox").TBigInt]>, number>;
2
+ retries: import('@sinclair/typebox').TTransform<
3
+ import('@sinclair/typebox').TUnion<
4
+ [
5
+ import('@sinclair/typebox').TNumber,
6
+ import('@sinclair/typebox').TString,
7
+ import('@sinclair/typebox').TBoolean,
8
+ import('@sinclair/typebox').TNull,
9
+ import('@sinclair/typebox').TDate,
10
+ import('@sinclair/typebox').TBigInt
11
+ ]
12
+ >,
13
+ number
14
+ >;
15
+ interval: import('@sinclair/typebox').TTransform<
16
+ import('@sinclair/typebox').TUnion<
17
+ [
18
+ import('@sinclair/typebox').TNumber,
19
+ import('@sinclair/typebox').TString,
20
+ import('@sinclair/typebox').TBoolean,
21
+ import('@sinclair/typebox').TNull,
22
+ import('@sinclair/typebox').TDate,
23
+ import('@sinclair/typebox').TBigInt
24
+ ]
25
+ >,
26
+ number
27
+ >;
4
28
  };
5
- //# sourceMappingURL=databaseWorker.schema.d.ts.map
29
+ //# sourceMappingURL=databaseWorker.schema.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { number } from '@forklaunch/validator/typebox';
2
2
  export const DatabaseWorkerOptionsSchema = {
3
- retries: number,
4
- interval: number
3
+ retries: number,
4
+ interval: number
5
5
  };
@@ -1,5 +1,5 @@
1
1
  export declare const DatabaseWorkerOptionsSchema: {
2
- retries: import("zod").ZodNumber;
3
- interval: import("zod").ZodNumber;
2
+ retries: import('zod').ZodNumber;
3
+ interval: import('zod').ZodNumber;
4
4
  };
5
- //# sourceMappingURL=databaseWorker.schema.d.ts.map
5
+ //# sourceMappingURL=databaseWorker.schema.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { number } from '@forklaunch/validator/zod';
2
2
  export const DatabaseWorkerOptionsSchema = {
3
- retries: number,
4
- interval: number
3
+ retries: number,
4
+ interval: number
5
5
  };