@tstdl/base 0.92.64 → 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.
@@ -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<{ [P in `getCurrent${Name}Context`]: {
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 | undefined, debugFn?: Function): Context | null;
8
+ (required?: false, debugFn?: Function): Context | null;
9
9
  (required: boolean, debugFn: Function): Context | null;
10
- }; } & { [P in `setCurrent${Name}Context`]: (context: Context | null) => Context | null; } & { [P in `runIn${Name}Context`]: <ReturnT>(context: Context, fn: () => ReturnT) => ReturnT; } & { [P in `isIn${Name}Context`]: {
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 | undefined, debugFn?: Function): Context | null;
12
+ (required?: false, debugFn?: Function): Context | null;
13
13
  (required: boolean, debugFn: Function): Context | null;
14
- }; } & { [P in `assertIn${Name}Context`]: {
14
+ }> & Record<`assertIn${Name}Context`, {
15
15
  (required: true, debugFn: Function): Context;
16
- (required?: false | undefined, debugFn?: Function): Context | null;
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(type?: EntityType<T>, table?: PgTableFromType<string, EntityType>, columnDefinitions?: ColumnDefinition[], columnDefinitionsMap?: Map<string, ColumnDefinition>, session?: Database | PgTransaction);
43
+ constructor();
44
44
  withOptionalTransaction(transaction: Transaction | undefined): EntityRepository<T>;
45
45
  withTransaction(transaction: Transaction): EntityRepository<T>;
46
46
  startTransaction(config?: TransactionConfig): Promise<Transaction>;
@@ -8,13 +8,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
11
  import { count, eq, inArray, sql } from 'drizzle-orm';
15
12
  import { PgTransaction as DrizzlePgTransaction } from 'drizzle-orm/pg-core';
13
+ import { createContextProvider } from '../../context/context.js';
16
14
  import { NotFoundError } from '../../errors/not-found.error.js';
17
- import { Optional, Singleton } from '../../injector/decorators.js';
15
+ import { Singleton } from '../../injector/decorators.js';
18
16
  import { inject, injectArgument } from '../../injector/inject.js';
19
17
  import { resolveArgumentType } from '../../injector/interfaces.js';
20
18
  import { injectionToken } from '../../injector/token.js';
@@ -38,6 +36,7 @@ export const ENTITY_TYPE = injectionToken('EntityType');
38
36
  const entityTypeToken = Symbol('EntityType');
39
37
  const entityRepositoryConfigToken = Symbol('EntityRepositoryConfig');
40
38
  const TRANSACTION_TIMESTAMP = sql `transaction_timestamp()`;
39
+ const { getCurrentEntityRepositoryContext, runInEntityRepositoryContext } = createContextProvider('EntityRepository');
41
40
  let EntityRepository = class EntityRepository {
42
41
  #repositoryConstructor;
43
42
  #withTransactionCache = new WeakMap();
@@ -49,9 +48,10 @@ let EntityRepository = class EntityRepository {
49
48
  columnDefinitionsMap;
50
49
  session;
51
50
  isInTransaction;
52
- constructor(type, table, columnDefinitions, columnDefinitionsMap, session) {
51
+ constructor() {
53
52
  this.#repositoryConstructor = new.target;
54
53
  const entityRepositoryConfig = new.target[entityRepositoryConfigToken] ?? inject(EntityRepositoryConfig);
54
+ const { type, table, columnDefinitions, columnDefinitionsMap, session } = getCurrentEntityRepositoryContext() ?? {};
55
55
  this.type = type ?? injectArgument(this, { optional: true }) ?? assertDefinedPass(new.target[entityTypeToken], 'Missing entity type.');
56
56
  this.table = table ?? getDrizzleTableFromType(this.type, entityRepositoryConfig.schema);
57
57
  this.columnDefinitions = columnDefinitions ?? getColumnDefinitions(this.table);
@@ -69,7 +69,14 @@ let EntityRepository = class EntityRepository {
69
69
  if (this.#withTransactionCache.has(transaction)) {
70
70
  return this.#withTransactionCache.get(transaction);
71
71
  }
72
- const repositoryWithTransaction = new this.#repositoryConstructor(this.type, this.table, this.columnDefinitions, this.columnDefinitionsMap, transaction.transaction);
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());
73
80
  this.#withTransactionCache.set(transaction, repositoryWithTransaction);
74
81
  return repositoryWithTransaction;
75
82
  }
@@ -456,12 +463,7 @@ let EntityRepository = class EntityRepository {
456
463
  };
457
464
  EntityRepository = __decorate([
458
465
  Singleton(),
459
- __param(0, Optional()),
460
- __param(1, Optional()),
461
- __param(2, Optional()),
462
- __param(3, Optional()),
463
- __param(4, Optional()),
464
- __metadata("design:paramtypes", [Object, Object, Array, Map, Object])
466
+ __metadata("design:paramtypes", [])
465
467
  ], EntityRepository);
466
468
  export { EntityRepository };
467
469
  export function injectRepository(type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.92.64",
3
+ "version": "0.92.65",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"