@tstdl/base 0.92.85 → 0.92.87

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 (152) hide show
  1. package/ai/ai.service.d.ts +4 -4
  2. package/ai/ai.service.js +27 -14
  3. package/ai/types.d.ts +5 -4
  4. package/api/server/gateway.js +1 -1
  5. package/authentication/authentication.api.d.ts +9 -9
  6. package/authentication/models/schemas.d.ts +2 -2
  7. package/authentication/server/authentication-ancillary.service.d.ts +6 -4
  8. package/authentication/server/authentication-ancillary.service.js +5 -5
  9. package/cancellation/token.d.ts +1 -1
  10. package/context/context.d.ts +1 -9
  11. package/context/context.js +8 -5
  12. package/document-management/api/document-management.api.d.ts +142 -110
  13. package/document-management/models/document-category.model.d.ts +1 -0
  14. package/document-management/models/document-category.model.js +2 -0
  15. package/document-management/models/document-collection-document.model.js +7 -3
  16. package/document-management/models/document-property-value.model.d.ts +13 -14
  17. package/document-management/models/document-property-value.model.js +60 -27
  18. package/document-management/models/document-property.model.d.ts +2 -0
  19. package/document-management/models/document-property.model.js +4 -1
  20. package/document-management/models/document-request-assignment-task-collection.model.d.ts +7 -0
  21. package/document-management/models/document-request-assignment-task-collection.model.js +32 -0
  22. package/document-management/models/document-request-assignment-task.model.d.ts +14 -0
  23. package/document-management/models/document-request-assignment-task.model.js +72 -0
  24. package/document-management/models/document-request-collection.model.d.ts +1 -0
  25. package/document-management/models/document-request-collection.model.js +7 -3
  26. package/document-management/models/document-request-file.model.d.ts +7 -2
  27. package/document-management/models/document-request-file.model.js +29 -4
  28. package/document-management/models/document-request.model.d.ts +1 -0
  29. package/document-management/models/document-requests-template.js +2 -0
  30. package/document-management/models/document-type-property.model.js +7 -3
  31. package/document-management/models/document-type.model.d.ts +1 -0
  32. package/document-management/models/document-type.model.js +7 -3
  33. package/document-management/models/document.model.d.ts +5 -2
  34. package/document-management/models/document.model.js +21 -6
  35. package/document-management/models/index.d.ts +2 -0
  36. package/document-management/models/index.js +2 -0
  37. package/document-management/models/service-models/document.service-model.d.ts +84 -65
  38. package/document-management/models/service-models/document.service-model.js +11 -6
  39. package/document-management/models/service-models/document.view-model.d.ts +1 -1
  40. package/document-management/models/service-models/document.view-model.js +2 -2
  41. package/document-management/server/drizzle/{0000_sloppy_fenris.sql → 0000_cool_victor_mancha.sql} +99 -43
  42. package/document-management/server/drizzle/meta/0000_snapshot.json +518 -130
  43. package/document-management/server/drizzle/meta/_journal.json +2 -2
  44. package/document-management/server/drizzle.config.js +1 -1
  45. package/document-management/server/module.d.ts +3 -2
  46. package/document-management/server/module.js +4 -2
  47. package/document-management/server/schemas.d.ts +36 -0
  48. package/document-management/server/schemas.js +37 -0
  49. package/document-management/server/services/document-management-ancillary.service.d.ts +4 -0
  50. package/document-management/server/services/document-management-ancillary.service.js +13 -0
  51. package/document-management/server/services/document-management.service.d.ts +71 -22
  52. package/document-management/server/services/document-management.service.js +528 -81
  53. package/document-management/server/services/index.d.ts +1 -0
  54. package/document-management/server/services/index.js +1 -0
  55. package/eslint.config.js +1 -0
  56. package/examples/document-management/main.d.ts +5 -0
  57. package/examples/document-management/main.js +20 -2
  58. package/examples/orm/schemas.d.ts +1 -1
  59. package/file/index.d.ts +1 -0
  60. package/file/index.js +1 -0
  61. package/file/temporary-file.d.ts +17 -0
  62. package/file/temporary-file.js +49 -0
  63. package/http/server/http-server-response.d.ts +2 -0
  64. package/http/server/http-server-response.js +13 -0
  65. package/injector/index.d.ts +1 -0
  66. package/injector/index.js +1 -0
  67. package/injector/injector.js +19 -7
  68. package/injector/interfaces.d.ts +1 -1
  69. package/injector/interfaces.js +1 -1
  70. package/injector/resolution.d.ts +15 -0
  71. package/injector/resolution.js +6 -0
  72. package/logger/console/logger.d.ts +1 -1
  73. package/logger/logger.d.ts +1 -1
  74. package/mail/drizzle.config.js +1 -1
  75. package/mail/models/schemas.d.ts +1 -1
  76. package/object-storage/object-storage.d.ts +5 -7
  77. package/object-storage/s3/s3.object-storage.d.ts +0 -1
  78. package/object-storage/s3/s3.object-storage.js +0 -3
  79. package/orm/{server/data-types → data-types}/numeric-date.js +2 -3
  80. package/orm/decorators.d.ts +17 -8
  81. package/orm/decorators.js +13 -7
  82. package/orm/entity.d.ts +5 -7
  83. package/orm/entity.js +11 -7
  84. package/orm/index.d.ts +2 -0
  85. package/orm/index.js +2 -0
  86. package/orm/query.d.ts +1 -3
  87. package/orm/query.js +0 -1
  88. package/orm/repository.types.d.ts +32 -0
  89. package/orm/repository.types.js +1 -0
  90. package/orm/server/database-schema.d.ts +4 -4
  91. package/orm/server/drizzle/schema-converter.d.ts +1 -1
  92. package/orm/server/drizzle/schema-converter.js +48 -19
  93. package/orm/server/index.d.ts +1 -0
  94. package/orm/server/index.js +1 -0
  95. package/orm/server/query-converter.d.ts +1 -2
  96. package/orm/server/query-converter.js +66 -61
  97. package/orm/server/repository.d.ts +80 -43
  98. package/orm/server/repository.js +219 -112
  99. package/orm/server/sqls.d.ts +15 -0
  100. package/orm/server/sqls.js +19 -0
  101. package/orm/server/types.d.ts +3 -3
  102. package/orm/types.d.ts +4 -4
  103. package/orm/utils.d.ts +3 -0
  104. package/orm/utils.js +6 -0
  105. package/package.json +23 -19
  106. package/pdf/pdf.service.d.ts +0 -1
  107. package/pdf/pdf.service.js +1 -95
  108. package/pdf/utils.d.ts +3 -1
  109. package/pdf/utils.js +129 -4
  110. package/promise/lazy-promise.d.ts +3 -3
  111. package/queue/enqueue-batch.d.ts +1 -0
  112. package/queue/enqueue-batch.js +1 -1
  113. package/queue/mongo/queue.d.ts +9 -4
  114. package/queue/mongo/queue.js +5 -6
  115. package/queue/postgres/drizzle/0000_zippy_moondragon.sql +11 -0
  116. package/queue/postgres/drizzle/meta/0000_snapshot.json +90 -0
  117. package/queue/postgres/drizzle/meta/_journal.json +13 -0
  118. package/queue/postgres/drizzle.config.d.ts +2 -0
  119. package/queue/postgres/drizzle.config.js +11 -0
  120. package/queue/postgres/index.d.ts +4 -0
  121. package/queue/postgres/index.js +4 -0
  122. package/queue/postgres/job.model.d.ts +13 -0
  123. package/queue/postgres/job.model.js +55 -0
  124. package/queue/postgres/module.d.ts +9 -0
  125. package/queue/postgres/module.js +29 -0
  126. package/queue/postgres/queue.d.ts +28 -0
  127. package/queue/postgres/queue.js +147 -0
  128. package/queue/postgres/queue.provider.d.ts +7 -0
  129. package/queue/postgres/queue.provider.js +21 -0
  130. package/queue/postgres/schemas.d.ts +3 -0
  131. package/queue/postgres/schemas.js +4 -0
  132. package/queue/provider.d.ts +2 -1
  133. package/queue/queue.d.ts +32 -6
  134. package/queue/queue.js +43 -0
  135. package/schema/schemas/object.d.ts +1 -1
  136. package/utils/date-time.d.ts +4 -2
  137. package/utils/date-time.js +10 -3
  138. package/utils/format-error.js +0 -1
  139. package/utils/object/lazy-property.js +0 -1
  140. package/utils/timing.d.ts +4 -3
  141. package/utils/timing.js +3 -1
  142. package/utils/try-ignore.d.ts +9 -2
  143. package/utils/try-ignore.js +30 -6
  144. package/document-management/models/schemas.d.ts +0 -33
  145. package/document-management/models/schemas.js +0 -34
  146. /package/orm/{server/data-types → data-types}/bytea.d.ts +0 -0
  147. /package/orm/{server/data-types → data-types}/bytea.js +0 -0
  148. /package/orm/{server/data-types → data-types}/index.d.ts +0 -0
  149. /package/orm/{server/data-types → data-types}/index.js +0 -0
  150. /package/orm/{server/data-types → data-types}/numeric-date.d.ts +0 -0
  151. /package/orm/{server/data-types → data-types}/timestamp.d.ts +0 -0
  152. /package/orm/{server/data-types → data-types}/timestamp.js +0 -0
@@ -0,0 +1,13 @@
1
+ {
2
+ "version": "7",
3
+ "dialect": "postgresql",
4
+ "entries": [
5
+ {
6
+ "idx": 0,
7
+ "version": "7",
8
+ "when": 1740684120614,
9
+ "tag": "0000_zippy_moondragon",
10
+ "breakpoints": true
11
+ }
12
+ ]
13
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: import("drizzle-kit").Config;
2
+ export default _default;
@@ -0,0 +1,11 @@
1
+ import { relative, resolve } from 'node:path';
2
+ import { defineConfig } from 'drizzle-kit';
3
+ export default defineConfig({
4
+ dialect: 'postgresql',
5
+ out: relative('./', resolve(__dirname, './drizzle/').replace('dist', 'source')),
6
+ schema: resolve(__dirname, './schemas.js'),
7
+ migrations: {
8
+ schema: 'queue',
9
+ table: '_migrations'
10
+ }
11
+ });
@@ -0,0 +1,4 @@
1
+ export * from './job.model.js';
2
+ export * from './module.js';
3
+ export * from './queue.js';
4
+ export * from './queue.provider.js';
@@ -0,0 +1,4 @@
1
+ export * from './job.model.js';
2
+ export * from './module.js';
3
+ export * from './queue.js';
4
+ export * from './queue.provider.js';
@@ -0,0 +1,13 @@
1
+ import { EntityWithoutMetadata } from '../../orm/entity.js';
2
+ import { Integer, Json, Timestamp } from '../../orm/index.js';
3
+ import type { ObjectLiteral } from '../../types.js';
4
+ import type { Job } from '../queue.js';
5
+ export declare class PostgresJob<T extends ObjectLiteral = ObjectLiteral> extends EntityWithoutMetadata implements Job<T> {
6
+ queue: string;
7
+ tag: string | null;
8
+ priority: Integer;
9
+ enqueueTimestamp: Timestamp;
10
+ tries: Integer;
11
+ lastDequeueTimestamp: Timestamp | null;
12
+ data: Json<T>;
13
+ }
@@ -0,0 +1,55 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { Table } from '../../orm/decorators.js';
11
+ import { EntityWithoutMetadata } from '../../orm/entity.js';
12
+ import { Integer, Json, Timestamp, Unique } from '../../orm/index.js';
13
+ import { StringProperty } from '../../schema/index.js';
14
+ let PostgresJob = class PostgresJob extends EntityWithoutMetadata {
15
+ queue;
16
+ tag;
17
+ priority;
18
+ enqueueTimestamp;
19
+ tries;
20
+ lastDequeueTimestamp;
21
+ data;
22
+ };
23
+ __decorate([
24
+ StringProperty(),
25
+ __metadata("design:type", String)
26
+ ], PostgresJob.prototype, "queue", void 0);
27
+ __decorate([
28
+ StringProperty({ nullable: true }),
29
+ __metadata("design:type", Object)
30
+ ], PostgresJob.prototype, "tag", void 0);
31
+ __decorate([
32
+ Integer(),
33
+ __metadata("design:type", Number)
34
+ ], PostgresJob.prototype, "priority", void 0);
35
+ __decorate([
36
+ Timestamp(),
37
+ __metadata("design:type", Number)
38
+ ], PostgresJob.prototype, "enqueueTimestamp", void 0);
39
+ __decorate([
40
+ Integer(),
41
+ __metadata("design:type", Number)
42
+ ], PostgresJob.prototype, "tries", void 0);
43
+ __decorate([
44
+ Timestamp({ nullable: true }),
45
+ __metadata("design:type", Object)
46
+ ], PostgresJob.prototype, "lastDequeueTimestamp", void 0);
47
+ __decorate([
48
+ Json(),
49
+ __metadata("design:type", Object)
50
+ ], PostgresJob.prototype, "data", void 0);
51
+ PostgresJob = __decorate([
52
+ Table('job'),
53
+ Unique(['queue', 'tag'])
54
+ ], PostgresJob);
55
+ export { PostgresJob };
@@ -0,0 +1,9 @@
1
+ import { type DatabaseConfig } from '../../orm/server/index.js';
2
+ export declare class PostgresQueueModuleConfig {
3
+ database?: DatabaseConfig;
4
+ }
5
+ /**
6
+ * configure mail module
7
+ */
8
+ export declare function configurePostgresQueue(config?: PostgresQueueModuleConfig, register?: boolean): void;
9
+ export declare function migratePostgresQueueSchema(): Promise<void>;
@@ -0,0 +1,29 @@
1
+ import { inject } from '../../injector/index.js';
2
+ import { Injector } from '../../injector/injector.js';
3
+ import { Database, migrate } from '../../orm/server/index.js';
4
+ import { QueueProvider } from '../provider.js';
5
+ import { Queue } from '../queue.js';
6
+ import { PostgresQueue } from './queue.js';
7
+ import { PostgresQueueProvider } from './queue.provider.js';
8
+ export class PostgresQueueModuleConfig {
9
+ database;
10
+ }
11
+ /**
12
+ * configure mail module
13
+ */
14
+ export function configurePostgresQueue(config, register = true) {
15
+ Injector.register(PostgresQueueModuleConfig, { useValue: config });
16
+ if (register) {
17
+ Injector.registerSingleton(QueueProvider, { useToken: PostgresQueueProvider });
18
+ Injector.registerSingleton(Queue, { useToken: PostgresQueue });
19
+ }
20
+ }
21
+ export async function migratePostgresQueueSchema() {
22
+ const connection = inject(PostgresQueueModuleConfig, undefined, { optional: true })?.database?.connection;
23
+ const database = inject(Database, connection);
24
+ await migrate(database, {
25
+ migrationsSchema: 'queue',
26
+ migrationsTable: '_migrations',
27
+ migrationsFolder: import.meta.resolve('./drizzle').replace('file://', '')
28
+ });
29
+ }
@@ -0,0 +1,28 @@
1
+ import { CancellationSignal } from '../../cancellation/index.js';
2
+ import type { ObjectLiteral } from '../../types.js';
3
+ import { Queue, type EnqueueManyItem, type EnqueueManyOptions, type EnqueueOneOptions, type Job, type JobTag } from '../queue.js';
4
+ export declare class PostgresQueue<T extends ObjectLiteral> extends Queue<T> {
5
+ #private;
6
+ enqueue(data: T, options?: EnqueueOneOptions): Promise<Job<T>>;
7
+ enqueueMany(items: EnqueueManyItem<T>[], options?: EnqueueManyOptions & {
8
+ returnJobs?: false;
9
+ }): Promise<void>;
10
+ enqueueMany(items: EnqueueManyItem<T>[], options: EnqueueManyOptions & {
11
+ returnJobs: true;
12
+ }): Promise<Job<T>[]>;
13
+ enqueueMany(items: EnqueueManyItem<T>[], options?: EnqueueManyOptions): Promise<Job<T>[] | undefined>;
14
+ has(id: string): Promise<boolean>;
15
+ countByTag(tag: JobTag): Promise<number>;
16
+ get(id: string): Promise<Job<T> | undefined>;
17
+ getByTag(tag: JobTag): Promise<Job<T>[]>;
18
+ cancel(id: string): Promise<void>;
19
+ cancelMany(ids: string[]): Promise<void>;
20
+ cancelByTag(tag: JobTag): Promise<void>;
21
+ cancelByTags(tags: JobTag[]): Promise<void>;
22
+ dequeue(): Promise<Job<T> | undefined>;
23
+ dequeueMany(count: number): Promise<Job<T>[]>;
24
+ acknowledge(job: Job<T>): Promise<void>;
25
+ acknowledgeMany(jobs: Job<T>[]): Promise<void>;
26
+ getConsumer(cancellationSignal: CancellationSignal): AsyncIterableIterator<Job<T>>;
27
+ getBatchConsumer(size: number, cancellationSignal: CancellationSignal): AsyncIterableIterator<Job<T>[]>;
28
+ }
@@ -0,0 +1,147 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { and, asc, eq, inArray, isNull as isSqlNull, lt, lte, or, sql } from 'drizzle-orm';
8
+ import { merge } from 'rxjs';
9
+ import { CancellationSignal } from '../../cancellation/index.js';
10
+ import { inject, injectArgument, provide, Singleton } from '../../injector/index.js';
11
+ import { MessageBus } from '../../message-bus/index.js';
12
+ import { DatabaseConfig, EntityRepositoryConfig, injectRepository, interval, RANDOM_UUID, TRANSACTION_TIMESTAMP } from '../../orm/server/index.js';
13
+ import { cancelableTimeout } from '../../utils/timing.js';
14
+ import { isDefined, isString } from '../../utils/type-guards.js';
15
+ import { millisecondsPerSecond } from '../../utils/units.js';
16
+ import { defaultQueueConfig, Queue, UniqueTagStrategy } from '../queue.js';
17
+ import { PostgresJob } from './job.model.js';
18
+ import { PostgresQueueModuleConfig } from './module.js';
19
+ import { job } from './schemas.js';
20
+ let PostgresQueue = class PostgresQueue extends Queue {
21
+ #repository = injectRepository(PostgresJob);
22
+ #config = injectArgument(this);
23
+ #processTimeout = (isString(this.#config) ? undefined : this.#config.processTimeout) ?? defaultQueueConfig.processTimeout;
24
+ #maxTries = (isString(this.#config) ? undefined : this.#config.maxTries) ?? defaultQueueConfig.maxTries;
25
+ #queueName = isString(this.#config) ? this.#config : this.#config.name;
26
+ #messageBus = inject((MessageBus), `PostgresQueue:${this.#queueName}`);
27
+ #keepOldUpdate = { id: sql `${job.id}` };
28
+ #takeNewUpdate = {
29
+ id: RANDOM_UUID,
30
+ queue: this.#queueName,
31
+ priority: sql `excluded.priority`,
32
+ tag: sql `excluded.tag`,
33
+ tries: 0,
34
+ enqueueTimestamp: TRANSACTION_TIMESTAMP,
35
+ lastDequeueTimestamp: null,
36
+ data: sql `excluded.data`
37
+ };
38
+ #dequeueQuery = and(eq(job.queue, this.#queueName), lt(job.tries, this.#maxTries), or(isSqlNull(job.lastDequeueTimestamp), lte(sql `${job.lastDequeueTimestamp} + ${interval(this.#processTimeout, 'milliseconds')}`, TRANSACTION_TIMESTAMP)));
39
+ #dequeueUpdate = {
40
+ tries: sql `${job.tries} + 1`,
41
+ lastDequeueTimestamp: TRANSACTION_TIMESTAMP
42
+ };
43
+ async enqueue(data, options) {
44
+ const jobs = await this.enqueueMany([{ data, tag: options?.tag, priority: options?.priority }], { uniqueTag: options?.uniqueTag, returnJobs: true });
45
+ return jobs[0];
46
+ }
47
+ async enqueueMany(items, options) {
48
+ const newEntities = items.map((item) => ({
49
+ queue: this.#queueName,
50
+ priority: item.priority ?? 1000,
51
+ tag: item.tag ?? null,
52
+ tries: 0,
53
+ enqueueTimestamp: TRANSACTION_TIMESTAMP,
54
+ lastDequeueTimestamp: null,
55
+ data: item.data
56
+ }));
57
+ const update = (options?.uniqueTag == UniqueTagStrategy.TakeNew)
58
+ ? this.#takeNewUpdate
59
+ : this.#keepOldUpdate;
60
+ const jobs = await this.#repository.upsertMany(['queue', 'tag'], newEntities, update);
61
+ this.#messageBus.publishAndForget();
62
+ return jobs;
63
+ }
64
+ async has(id) {
65
+ return this.#repository.hasByQuery({ queue: this.#queueName, id });
66
+ }
67
+ async countByTag(tag) {
68
+ return this.#repository.countByQuery({ queue: this.#queueName, tag });
69
+ }
70
+ async get(id) {
71
+ return this.#repository.tryLoadByQuery({ queue: this.#queueName, id });
72
+ }
73
+ async getByTag(tag) {
74
+ return this.#repository.loadManyByQuery({ queue: this.#queueName, tag });
75
+ }
76
+ async cancel(id) {
77
+ await this.#repository.hardDeleteByQuery({ queue: this.#queueName, id });
78
+ }
79
+ async cancelMany(ids) {
80
+ await this.#repository.hardDeleteManyByQuery({ queue: this.#queueName, id: { $in: ids } });
81
+ }
82
+ async cancelByTag(tag) {
83
+ await this.#repository.hardDeleteByQuery({ queue: this.#queueName, tag });
84
+ }
85
+ async cancelByTags(tags) {
86
+ await this.#repository.hardDeleteManyByQuery({ queue: this.#queueName, tag: { $in: tags } });
87
+ }
88
+ async dequeue() {
89
+ const jobs = await this.dequeueMany(1);
90
+ if (jobs.length == 0) {
91
+ return undefined;
92
+ }
93
+ return jobs[0];
94
+ }
95
+ async dequeueMany(count) {
96
+ const rows = await this.#repository.session
97
+ .update(job)
98
+ .set(this.#dequeueUpdate)
99
+ .where(inArray(job.id, this.#repository.session
100
+ .select({ id: job.id })
101
+ .from(job)
102
+ .where(this.#dequeueQuery)
103
+ .orderBy(asc(job.priority), asc(job.enqueueTimestamp), asc(job.lastDequeueTimestamp), asc(job.tries))
104
+ .limit(count)
105
+ .for('update')))
106
+ .returning();
107
+ return this.#repository.$mapManyToEntity(rows); // eslint-disable-line @typescript-eslint/no-unsafe-argument
108
+ }
109
+ async acknowledge(job) {
110
+ return this.cancel(job.id);
111
+ }
112
+ async acknowledgeMany(jobs) {
113
+ const ids = jobs.map((job) => job.id);
114
+ return this.cancelMany(ids);
115
+ }
116
+ async *getConsumer(cancellationSignal) {
117
+ const continue$ = merge(this.#messageBus.allMessages$, cancellationSignal);
118
+ while (cancellationSignal.isUnset) {
119
+ const job = await this.dequeue();
120
+ if (isDefined(job)) {
121
+ yield job;
122
+ continue;
123
+ }
124
+ await cancelableTimeout(5 * millisecondsPerSecond, continue$);
125
+ }
126
+ }
127
+ async *getBatchConsumer(size, cancellationSignal) {
128
+ const continue$ = merge(this.#messageBus.allMessages$, cancellationSignal);
129
+ while (cancellationSignal.isUnset) {
130
+ const jobs = await this.dequeueMany(size);
131
+ if (jobs.length > 0) {
132
+ yield jobs;
133
+ continue;
134
+ }
135
+ await cancelableTimeout(5 * millisecondsPerSecond, continue$);
136
+ }
137
+ }
138
+ };
139
+ PostgresQueue = __decorate([
140
+ Singleton({
141
+ providers: [
142
+ provide(EntityRepositoryConfig, { useValue: { schema: 'queue' } }),
143
+ provide(DatabaseConfig, { useFactory: (_, context) => context.resolve(PostgresQueueModuleConfig).database ?? context.resolve(DatabaseConfig, undefined, { skipSelf: true }) })
144
+ ]
145
+ })
146
+ ], PostgresQueue);
147
+ export { PostgresQueue };
@@ -0,0 +1,7 @@
1
+ import { QueueProvider, type QueueConfig } from '../../queue/index.js';
2
+ import type { ObjectLiteral } from '../../types.js';
3
+ import { PostgresQueue } from './queue.js';
4
+ export declare class PostgresQueueProvider extends QueueProvider {
5
+ #private;
6
+ get<T extends ObjectLiteral>(name: string, config?: QueueConfig): PostgresQueue<T>;
7
+ }
@@ -0,0 +1,21 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Singleton } from '../../injector/decorators.js';
8
+ import { inject } from '../../injector/inject.js';
9
+ import { Injector } from '../../injector/injector.js';
10
+ import { QueueProvider } from '../../queue/index.js';
11
+ import { PostgresQueue } from './queue.js';
12
+ let PostgresQueueProvider = class PostgresQueueProvider extends QueueProvider {
13
+ #injector = inject(Injector);
14
+ get(name, config) {
15
+ return this.#injector.resolve((PostgresQueue), { ...config, name: name });
16
+ }
17
+ };
18
+ PostgresQueueProvider = __decorate([
19
+ Singleton()
20
+ ], PostgresQueueProvider);
21
+ export { PostgresQueueProvider };
@@ -0,0 +1,3 @@
1
+ import { PostgresJob } from './job.model.js';
2
+ export declare const queueSchema: import("../../orm/server/database-schema.js").DatabaseSchema<"queue">;
3
+ export declare const job: import("../../orm/server/types.js").PgTableFromType<typeof PostgresJob, "queue">;
@@ -0,0 +1,4 @@
1
+ import { databaseSchema } from '../../orm/server/database-schema.js';
2
+ import { PostgresJob } from './job.model.js';
3
+ export const queueSchema = databaseSchema('queue');
4
+ export const job = queueSchema.getTable(PostgresJob);
@@ -1,4 +1,5 @@
1
+ import type { ObjectLiteral } from '../types.js';
1
2
  import type { Queue, QueueConfig } from './queue.js';
2
3
  export declare abstract class QueueProvider {
3
- abstract get<T>(key: string, config?: QueueConfig): Queue<T>;
4
+ abstract get<T extends ObjectLiteral>(key: string, config?: QueueConfig): Queue<T>;
4
5
  }
package/queue/queue.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import type { CancellationSignal } from '../cancellation/token.js';
2
2
  import type { Resolvable, resolveArgumentType } from '../injector/interfaces.js';
3
+ import type { Logger } from '../logger/logger.js';
3
4
  import { QueueEnqueueBatch } from './enqueue-batch.js';
4
- export type JobTag = string | number | null;
5
+ export type ProcessWorker<T> = (job: Job<T>) => void | Promise<void>;
6
+ export type ProcessBatchWorker<T> = (jobs: Job<T>[]) => void | Promise<void>;
7
+ export type JobTag = string | null;
5
8
  export type Job<T> = {
6
9
  id: string;
7
10
  /**
@@ -18,6 +21,10 @@ export declare enum UniqueTagStrategy {
18
21
  TakeNew = 1
19
22
  }
20
23
  export type EnqueueOptions = {
24
+ tag?: JobTag;
25
+ priority?: number;
26
+ };
27
+ export type EnqueueOneOptions = {
21
28
  tag?: JobTag;
22
29
  uniqueTag?: UniqueTagStrategy;
23
30
  priority?: number;
@@ -25,21 +32,29 @@ export type EnqueueOptions = {
25
32
  export type EnqueueManyItem<T> = EnqueueOptions & {
26
33
  data: T;
27
34
  };
35
+ export type EnqueueManyOptions = {
36
+ uniqueTag?: UniqueTagStrategy;
37
+ returnJobs?: boolean;
38
+ };
28
39
  export type QueueConfig = {
29
40
  processTimeout?: number;
30
41
  maxTries?: number;
31
42
  };
32
43
  export type QueueArgument = string | (QueueConfig & {
33
- key: string;
44
+ name: string;
34
45
  });
35
46
  export declare const defaultQueueConfig: Required<QueueConfig>;
36
47
  export declare abstract class Queue<T> implements Resolvable<QueueArgument> {
37
48
  readonly [resolveArgumentType]: QueueArgument;
38
49
  batch(): QueueEnqueueBatch<T>;
39
- abstract enqueue(data: T, options?: EnqueueOptions): Promise<Job<T>>;
40
- abstract enqueueMany(items: EnqueueManyItem<T>[], returnJobs?: false): Promise<void>;
41
- abstract enqueueMany(items: EnqueueManyItem<T>[], returnJobs: true): Promise<Job<T>[]>;
42
- abstract enqueueMany(items: EnqueueManyItem<T>[], returnJobs?: boolean): Promise<void | Job<T>[]>;
50
+ abstract enqueue(data: T, options?: EnqueueOneOptions): Promise<Job<T>>;
51
+ abstract enqueueMany(items: EnqueueManyItem<T>[], options?: EnqueueManyOptions & {
52
+ returnJobs?: false;
53
+ }): Promise<void>;
54
+ abstract enqueueMany(items: EnqueueManyItem<T>[], options: EnqueueManyOptions & {
55
+ returnJobs: true;
56
+ }): Promise<Job<T>[]>;
57
+ abstract enqueueMany(items: EnqueueManyItem<T>[], options?: EnqueueManyOptions): Promise<Job<T>[] | undefined>;
43
58
  abstract has(id: string): Promise<boolean>;
44
59
  abstract countByTag(tag: JobTag): Promise<number>;
45
60
  abstract get(id: string): Promise<Job<T> | undefined>;
@@ -54,4 +69,15 @@ export declare abstract class Queue<T> implements Resolvable<QueueArgument> {
54
69
  abstract acknowledgeMany(jobs: Job<T>[]): Promise<void>;
55
70
  abstract getConsumer(cancellationSignal: CancellationSignal): AsyncIterableIterator<Job<T>>;
56
71
  abstract getBatchConsumer(size: number, cancellationSignal: CancellationSignal): AsyncIterableIterator<Job<T>[]>;
72
+ process({ concurrency, cancellationSignal }: {
73
+ concurrency?: number;
74
+ cancellationSignal: CancellationSignal;
75
+ }, handler: ProcessWorker<T>, errorHandler?: Logger | ((error: unknown) => void | Promise<void>)): void;
76
+ processBatch({ batchSize, concurrency, cancellationSignal }: {
77
+ batchSize?: number;
78
+ concurrency?: number;
79
+ cancellationSignal: CancellationSignal;
80
+ }, handler: ProcessBatchWorker<T>, errorHandler?: Logger | ((error: unknown) => void | Promise<void>)): void;
81
+ private processWorker;
82
+ private processBatchWorker;
57
83
  }
package/queue/queue.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { isDefined, isFunction } from '../utils/type-guards.js';
1
2
  import { millisecondsPerMinute } from '../utils/units.js';
2
3
  import { QueueEnqueueBatch } from './enqueue-batch.js';
3
4
  export const defaultJobPriority = 1000;
@@ -14,4 +15,46 @@ export class Queue {
14
15
  batch() {
15
16
  return new QueueEnqueueBatch(this);
16
17
  }
18
+ process({ concurrency = 1, cancellationSignal }, handler, errorHandler) {
19
+ const handleError = isFunction(errorHandler)
20
+ ? errorHandler
21
+ : isDefined(errorHandler)
22
+ ? (error) => errorHandler.error(error)
23
+ : undefined;
24
+ for (let i = 0; i < concurrency; i++) {
25
+ void this.processWorker(cancellationSignal, handler, handleError);
26
+ }
27
+ }
28
+ processBatch({ batchSize = 10, concurrency = 1, cancellationSignal }, handler, errorHandler) {
29
+ const handleError = isFunction(errorHandler)
30
+ ? errorHandler
31
+ : isDefined(errorHandler)
32
+ ? (error) => errorHandler.error(error)
33
+ : undefined;
34
+ for (let i = 0; i < concurrency; i++) {
35
+ void this.processBatchWorker(batchSize, cancellationSignal, handler, handleError);
36
+ }
37
+ }
38
+ async processWorker(cancellationSignal, handler, errorHandler) {
39
+ for await (const job of this.getConsumer(cancellationSignal)) {
40
+ try {
41
+ await handler(job);
42
+ await this.acknowledge(job);
43
+ }
44
+ catch (error) {
45
+ await errorHandler?.(error);
46
+ }
47
+ }
48
+ }
49
+ async processBatchWorker(size, cancellationSignal, handler, errorHandler) {
50
+ for await (const jobs of this.getBatchConsumer(size, cancellationSignal)) {
51
+ try {
52
+ await handler(jobs);
53
+ await this.acknowledgeMany(jobs);
54
+ }
55
+ catch (error) {
56
+ await errorHandler?.(error);
57
+ }
58
+ }
59
+ }
17
60
  }
@@ -29,7 +29,7 @@ export type OptionalProperties<T> = {
29
29
  } ? P : never;
30
30
  }[keyof T];
31
31
  export type ObjectSchemaPropertiesType<TP extends ObjectSchemaProperties> = SimplifyObject<{
32
- [P in keyof PartialProperty<TP, OptionalProperties<TP>>]: SchemaOutput<TP[P]>;
32
+ -readonly [P in keyof PartialProperty<TP, OptionalProperties<TP>>]: SchemaOutput<TP[P]>;
33
33
  }>;
34
34
  export declare const tryGetSchemaFromReflection: typeof _tryGetSchemaFromReflection;
35
35
  export declare class ObjectSchema<T extends Record = Record> extends Schema<T> {
@@ -42,13 +42,15 @@ export declare function timeObjectToNumericTime(time: Partial<TimeObject>): numb
42
42
  export declare function numericTimeToTimeObject(time: number): TimeObject;
43
43
  export declare function timestampToNumericDateAndTime(timestamp: number): NumericDateTime;
44
44
  export declare function numericDateToTimestamp(numericDate: number): number;
45
- export declare function numericDateToDate(numericDate: number): {
45
+ export declare function numericDateToDate(numericDate: number): Date;
46
+ export declare function numericDateToDateObject(numericDate: number): {
46
47
  year: number;
47
48
  month: number;
48
49
  day: number;
49
50
  };
50
51
  export declare function numericDateTimeToTimestamp({ date, time }: NumericDateTime): number;
51
- export declare function zonedDateObjectToDateTime(zonedDate: ZonedDateObject, units?: DateObjectUnits, options?: DateTimeJSOptions): DateTime;
52
+ export declare function dateObjectToDateTime(dateObject: ZonedDateObject, units?: DateObjectUnits, options?: DateTimeJSOptions): DateTime;
53
+ export declare function dateObjectToNumericDate(dateObject: DateObject): number;
52
54
  export declare function dateTimeToNumericDate(dateTime: DateTime): number;
53
55
  export declare function numericDateToDateTime(numericDate: number, units?: DateObjectUnits, options?: DateTimeJSOptions): DateTime;
54
56
  export declare function dateTimeToTime(dateTime: DateTime): number;
@@ -74,7 +74,10 @@ export function numericDateToTimestamp(numericDate) {
74
74
  }
75
75
  export function numericDateToDate(numericDate) {
76
76
  const timestamp = numericDateToTimestamp(numericDate);
77
- const date = new Date(timestamp);
77
+ return new Date(timestamp);
78
+ }
79
+ export function numericDateToDateObject(numericDate) {
80
+ const date = numericDateToDate(numericDate);
78
81
  return {
79
82
  year: date.getUTCFullYear(),
80
83
  month: date.getUTCMonth() + 1,
@@ -84,8 +87,12 @@ export function numericDateToDate(numericDate) {
84
87
  export function numericDateTimeToTimestamp({ date, time }) {
85
88
  return numericDateToTimestamp(date) + time;
86
89
  }
87
- export function zonedDateObjectToDateTime(zonedDate, units, options) {
88
- return DateTime.fromObject({ ...zonedDate, ...units }, options);
90
+ export function dateObjectToDateTime(dateObject, units, options) {
91
+ return DateTime.fromObject({ ...dateObject, ...units }, options);
92
+ }
93
+ export function dateObjectToNumericDate(dateObject) {
94
+ const dateTime = DateTime.fromObject({ ...dateObject }, { zone: 'UTC' });
95
+ return dateTimeToNumericDate(dateTime);
89
96
  }
90
97
  export function dateTimeToNumericDate(dateTime) {
91
98
  const timestamp = dateTime.toUTC(undefined, { keepLocalTime: true }).toMillis();
@@ -2,7 +2,6 @@ import { unwrapError } from '../errors/utils.js';
2
2
  import { decycle } from './object/decycle.js';
3
3
  import { objectKeys } from './object/object.js';
4
4
  import { isDefined, isFunction, isUndefined } from './type-guards.js';
5
- // eslint-disable-next-line max-statements, complexity
6
5
  export function formatError(error, options = {}) {
7
6
  const { includeRest = 'if-no-extra-info', includeExtraInfo = true, includeStack = true } = options;
8
7
  let name;
@@ -36,7 +36,6 @@ export function lazyProperty(object, propertyKey, initializer, descriptor = {})
36
36
  configurable
37
37
  });
38
38
  }
39
- // eslint-disable-next-line max-statements, max-lines-per-function
40
39
  export function lazyObject(initializers) {
41
40
  const object = {};
42
41
  for (const [key, value] of objectEntries(initializers)) {
package/utils/timing.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { CancellationSignal } from '../cancellation/token.js';
1
+ import { type Observable } from 'rxjs';
2
+ import { CancellationSignal } from '../cancellation/token.js';
2
3
  import { type ValueOrProvider } from './value-or-provider.js';
3
4
  /** Timeout for specified duration */
4
5
  export declare function timeout(milliseconds?: number, options?: {
@@ -7,9 +8,9 @@ export declare function timeout(milliseconds?: number, options?: {
7
8
  /** Timeout until specified time */
8
9
  export declare function timeoutUntil(timestamp: number | Date): Promise<void>;
9
10
  /** Timeout for specified duration */
10
- export declare function cancelableTimeout(milliseconds: number, cancelSignal: CancellationSignal): Promise<boolean>;
11
+ export declare function cancelableTimeout(milliseconds: number, cancelSignal: Observable<void> | CancellationSignal): Promise<boolean>;
11
12
  /** Timeout until specified time */
12
- export declare function cancelableTimeoutUntil(timestamp: number | Date, cancelSignal: CancellationSignal): Promise<boolean>;
13
+ export declare function cancelableTimeoutUntil(timestamp: number | Date, cancelSignal: Observable<void> | CancellationSignal): Promise<boolean>;
13
14
  export declare function withTimeout<T>(milliseconds: number, promiseOrProvider: ValueOrProvider<Promise<T>>, options?: {
14
15
  errorMessage?: string;
15
16
  }): Promise<T>;
package/utils/timing.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { firstValueFrom, map, race, timer } from 'rxjs';
2
+ import { CancellationSignal } from '../cancellation/token.js';
2
3
  import { TimeoutError } from '../errors/timeout.error.js';
3
4
  import { _throw } from './throw.js';
4
5
  import { resolveValueOrProvider } from './value-or-provider.js';
@@ -20,9 +21,10 @@ export async function timeoutUntil(timestamp) {
20
21
  }
21
22
  /** Timeout for specified duration */
22
23
  export async function cancelableTimeout(milliseconds, cancelSignal) {
24
+ const observable = (cancelSignal instanceof CancellationSignal) ? cancelSignal.set$ : cancelSignal;
23
25
  return firstValueFrom(race([
24
26
  timer(milliseconds).pipe(map(() => false)),
25
- cancelSignal.set$.pipe(map(() => true))
27
+ observable.pipe(map(() => true))
26
28
  ]));
27
29
  }
28
30
  /** Timeout until specified time */
@@ -1,2 +1,9 @@
1
- export declare function tryIgnore(fn: () => any): void;
2
- export declare function tryIgnoreAsync(fn: () => Promise<any>): Promise<void>;
1
+ import type { Logger } from '../logger/logger.js';
2
+ export declare function tryIgnore<R>(fn: () => R): R;
3
+ export declare function tryIgnore<R, F>(fn: () => R, fallback: F): R | F;
4
+ export declare function tryIgnoreAsync<R>(fn: () => Promise<R>): Promise<R>;
5
+ export declare function tryIgnoreAsync<R, F>(fn: () => Promise<R>, fallback: F): Promise<F>;
6
+ export declare function tryIgnoreLog<R>(fn: () => R, logger: Logger): R;
7
+ export declare function tryIgnoreLog<R, F>(fn: () => R, logger: Logger, fallback: F): R | F;
8
+ export declare function tryIgnoreLogAsync<R>(fn: () => Promise<R>, logger: Logger): Promise<R>;
9
+ export declare function tryIgnoreLogAsync<R, F>(fn: () => Promise<R>, logger: Logger, fallback: F): Promise<F>;