@tstdl/base 0.92.63 → 0.92.65
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/context/context.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { SimplifyObject } from '../types.js';
|
|
1
|
+
import type { Function, SimplifyObject } from '../types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Creates a new context provider
|
|
4
4
|
* @param name name of of the context used for function names
|
|
5
5
|
*/
|
|
6
|
-
export declare function createContextProvider<Context, const Name extends string>(name: Name): SimplifyObject<
|
|
6
|
+
export declare function createContextProvider<Context, const Name extends string>(name: Name): SimplifyObject<Record<`getCurrent${Name}Context`, {
|
|
7
7
|
(required: true, debugFn: Function): Context;
|
|
8
|
-
(required?: false
|
|
8
|
+
(required?: false, debugFn?: Function): Context | null;
|
|
9
9
|
(required: boolean, debugFn: Function): Context | null;
|
|
10
|
-
}
|
|
10
|
+
}> & Record<`setCurrent${Name}Context`, (context: Context | null) => Context | null> & Record<`runIn${Name}Context`, <ReturnT>(context: Context, fn: () => ReturnT) => ReturnT> & Record<`isIn${Name}Context`, {
|
|
11
11
|
(required: true, debugFn: Function): Context;
|
|
12
|
-
(required?: false
|
|
12
|
+
(required?: false, debugFn?: Function): Context | null;
|
|
13
13
|
(required: boolean, debugFn: Function): Context | null;
|
|
14
|
-
}
|
|
14
|
+
}> & Record<`assertIn${Name}Context`, {
|
|
15
15
|
(required: true, debugFn: Function): Context;
|
|
16
|
-
(required?: false
|
|
16
|
+
(required?: false, debugFn?: Function): Context | null;
|
|
17
17
|
(required: boolean, debugFn: Function): Context | null;
|
|
18
|
-
}
|
|
18
|
+
}>>;
|
|
@@ -40,7 +40,7 @@ export declare class EntityRepository<T extends Entity = Entity> implements Reso
|
|
|
40
40
|
readonly session: Database | PgTransaction;
|
|
41
41
|
readonly isInTransaction: boolean;
|
|
42
42
|
readonly [resolveArgumentType]: EntityType<T>;
|
|
43
|
-
constructor(
|
|
43
|
+
constructor();
|
|
44
44
|
withOptionalTransaction(transaction: Transaction | undefined): EntityRepository<T>;
|
|
45
45
|
withTransaction(transaction: Transaction): EntityRepository<T>;
|
|
46
46
|
startTransaction(config?: TransactionConfig): Promise<Transaction>;
|
package/orm/server/repository.js
CHANGED
|
@@ -10,6 +10,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
import { count, eq, inArray, sql } from 'drizzle-orm';
|
|
12
12
|
import { PgTransaction as DrizzlePgTransaction } from 'drizzle-orm/pg-core';
|
|
13
|
+
import { createContextProvider } from '../../context/context.js';
|
|
13
14
|
import { NotFoundError } from '../../errors/not-found.error.js';
|
|
14
15
|
import { Singleton } from '../../injector/decorators.js';
|
|
15
16
|
import { inject, injectArgument } from '../../injector/inject.js';
|
|
@@ -35,6 +36,7 @@ export const ENTITY_TYPE = injectionToken('EntityType');
|
|
|
35
36
|
const entityTypeToken = Symbol('EntityType');
|
|
36
37
|
const entityRepositoryConfigToken = Symbol('EntityRepositoryConfig');
|
|
37
38
|
const TRANSACTION_TIMESTAMP = sql `transaction_timestamp()`;
|
|
39
|
+
const { getCurrentEntityRepositoryContext, runInEntityRepositoryContext } = createContextProvider('EntityRepository');
|
|
38
40
|
let EntityRepository = class EntityRepository {
|
|
39
41
|
#repositoryConstructor;
|
|
40
42
|
#withTransactionCache = new WeakMap();
|
|
@@ -46,9 +48,10 @@ let EntityRepository = class EntityRepository {
|
|
|
46
48
|
columnDefinitionsMap;
|
|
47
49
|
session;
|
|
48
50
|
isInTransaction;
|
|
49
|
-
constructor(
|
|
51
|
+
constructor() {
|
|
50
52
|
this.#repositoryConstructor = new.target;
|
|
51
53
|
const entityRepositoryConfig = new.target[entityRepositoryConfigToken] ?? inject(EntityRepositoryConfig);
|
|
54
|
+
const { type, table, columnDefinitions, columnDefinitionsMap, session } = getCurrentEntityRepositoryContext() ?? {};
|
|
52
55
|
this.type = type ?? injectArgument(this, { optional: true }) ?? assertDefinedPass(new.target[entityTypeToken], 'Missing entity type.');
|
|
53
56
|
this.table = table ?? getDrizzleTableFromType(this.type, entityRepositoryConfig.schema);
|
|
54
57
|
this.columnDefinitions = columnDefinitions ?? getColumnDefinitions(this.table);
|
|
@@ -66,7 +69,14 @@ let EntityRepository = class EntityRepository {
|
|
|
66
69
|
if (this.#withTransactionCache.has(transaction)) {
|
|
67
70
|
return this.#withTransactionCache.get(transaction);
|
|
68
71
|
}
|
|
69
|
-
const
|
|
72
|
+
const context = {
|
|
73
|
+
type: this.type,
|
|
74
|
+
table: this.table,
|
|
75
|
+
columnDefinitions: this.columnDefinitions,
|
|
76
|
+
columnDefinitionsMap: this.columnDefinitionsMap,
|
|
77
|
+
session: transaction.transaction
|
|
78
|
+
};
|
|
79
|
+
const repositoryWithTransaction = runInEntityRepositoryContext(context, () => new this.#repositoryConstructor());
|
|
70
80
|
this.#withTransactionCache.set(transaction, repositoryWithTransaction);
|
|
71
81
|
return repositoryWithTransaction;
|
|
72
82
|
}
|
|
@@ -452,12 +462,8 @@ let EntityRepository = class EntityRepository {
|
|
|
452
462
|
}
|
|
453
463
|
};
|
|
454
464
|
EntityRepository = __decorate([
|
|
455
|
-
Singleton(
|
|
456
|
-
|
|
457
|
-
useFactory: () => new EntityRepository()
|
|
458
|
-
}
|
|
459
|
-
}),
|
|
460
|
-
__metadata("design:paramtypes", [Object, Object, Array, Map, Object])
|
|
465
|
+
Singleton(),
|
|
466
|
+
__metadata("design:paramtypes", [])
|
|
461
467
|
], EntityRepository);
|
|
462
468
|
export { EntityRepository };
|
|
463
469
|
export function injectRepository(type) {
|