@tstdl/base 0.92.64 → 0.92.66

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
+ }>>;
@@ -1 +1,3 @@
1
1
  export * from './bytea.js';
2
+ export * from './numeric-date.js';
3
+ export * from './timestamp.js';
@@ -1 +1,3 @@
1
1
  export * from './bytea.js';
2
+ export * from './numeric-date.js';
3
+ export * from './timestamp.js';
@@ -0,0 +1,10 @@
1
+ import { type ConvertCustomConfig, type PgCustomColumnBuilder } from 'drizzle-orm/pg-core';
2
+ type Config = {
3
+ data: number;
4
+ driverData: string;
5
+ };
6
+ export declare const numericDate: {
7
+ (): PgCustomColumnBuilder<ConvertCustomConfig<"", Config>>;
8
+ <TName extends string>(dbName: TName): PgCustomColumnBuilder<ConvertCustomConfig<TName, Config>>;
9
+ };
10
+ export {};
@@ -0,0 +1,15 @@
1
+ import { customType } from 'drizzle-orm/pg-core';
2
+ import { dateToNumericDate, numericDateToDate } from '../../../utils/date-time.js';
3
+ export const numericDate = customType({
4
+ dataType() {
5
+ return 'date';
6
+ },
7
+ toDriver(value) {
8
+ const { year, month, day } = numericDateToDate(value);
9
+ return new Date(year, month - 1, day).toISOString();
10
+ },
11
+ fromDriver(value) {
12
+ const date = new Date(value);
13
+ return dateToNumericDate(date);
14
+ }
15
+ });
@@ -0,0 +1,10 @@
1
+ import { type ConvertCustomConfig, type PgCustomColumnBuilder } from 'drizzle-orm/pg-core';
2
+ type Config = {
3
+ data: number;
4
+ driverData: string;
5
+ };
6
+ export declare const timestamp: {
7
+ (): PgCustomColumnBuilder<ConvertCustomConfig<"", Config>>;
8
+ <TName extends string>(dbName: TName): PgCustomColumnBuilder<ConvertCustomConfig<TName, Config>>;
9
+ };
10
+ export {};
@@ -0,0 +1,12 @@
1
+ import { customType } from 'drizzle-orm/pg-core';
2
+ export const timestamp = customType({
3
+ dataType() {
4
+ return 'timestamp with time zone';
5
+ },
6
+ toDriver(value) {
7
+ return new Date(value).toISOString();
8
+ },
9
+ fromDriver(value) {
10
+ return new Date(value).getTime();
11
+ }
12
+ });
@@ -1,5 +1,5 @@
1
1
  import { toCamelCase, toSnakeCase } from 'drizzle-orm/casing';
2
- import { boolean, date, doublePrecision, index, integer, jsonb, pgSchema, text, timestamp, unique, uniqueIndex, uuid } from 'drizzle-orm/pg-core';
2
+ import { boolean, doublePrecision, index, integer, jsonb, pgSchema, text, unique, uniqueIndex, uuid } from 'drizzle-orm/pg-core';
3
3
  import { MultiKeyMap } from '../../../data-structures/multi-key-map.js';
4
4
  import { tryGetEnumName } from '../../../enumeration/enumeration.js';
5
5
  import { NotSupportedError } from '../../../errors/not-supported.error.js';
@@ -17,7 +17,7 @@ import { JsonSchema } from '../../schemas/json.js';
17
17
  import { NumericDateSchema } from '../../schemas/numeric-date.js';
18
18
  import { TimestampSchema } from '../../schemas/timestamp.js';
19
19
  import { UuidSchema } from '../../schemas/uuid.js';
20
- import { bytea } from '../data-types/bytea.js';
20
+ import { bytea, numericDate, timestamp } from '../data-types/index.js';
21
21
  import { decryptBytes, encryptBytes } from '../encryption.js';
22
22
  const getDbSchema = memoizeSingle(pgSchema);
23
23
  export const getDrizzleTableFromType = memoize(_getDrizzleTableFromType);
@@ -166,10 +166,10 @@ function getPostgresBaseColumn(columnName, dbSchema, schema, context) {
166
166
  return column;
167
167
  }
168
168
  if (schema instanceof TimestampSchema) {
169
- return timestamp(columnName, { withTimezone: true });
169
+ return timestamp(columnName);
170
170
  }
171
171
  if (schema instanceof NumericDateSchema) {
172
- return date(columnName);
172
+ return numericDate(columnName);
173
173
  }
174
174
  if (schema instanceof NumberSchema) {
175
175
  return schema.integer
@@ -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.66",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"