@tstdl/base 0.92.68 → 0.92.70
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/orm/decorators.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { EntityType } from './entity.js';
|
|
|
4
4
|
type IndexMethod = LiteralUnion<'hash' | 'btree' | 'gist' | 'spgist' | 'gin' | 'brin' | 'hnsw' | 'ivfflat', string>;
|
|
5
5
|
export type OrmTableReflectionData = {
|
|
6
6
|
name?: string;
|
|
7
|
+
schema?: string;
|
|
7
8
|
unique?: UniqueReflectionData[];
|
|
8
9
|
index?: IndexReflectionData[];
|
|
9
10
|
};
|
|
@@ -47,9 +48,9 @@ export declare function PrimaryKey(): PropertyDecorator;
|
|
|
47
48
|
export declare function References(type: () => EntityType): PropertyDecorator;
|
|
48
49
|
export declare function Encrypted(): PropertyDecorator;
|
|
49
50
|
export declare function Embedded(type: AbstractConstructor, options?: TypedOmit<NonNullable<OrmColumnReflectionData['embedded']>, 'type'>): PropertyDecorator;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
type TableOptions = Partial<Pick<OrmTableReflectionData, 'name' | 'schema'>>;
|
|
52
|
+
export declare function Table(name?: string, options?: TypedOmit<TableOptions, 'schema'>): ClassDecorator;
|
|
53
|
+
export declare function Table(options?: TableOptions): ClassDecorator;
|
|
53
54
|
export declare function Unique(name?: string, options?: UniqueReflectionData['options']): PropertyDecorator;
|
|
54
55
|
export declare function Unique(name: string | undefined, columns: [string, ...string[]], options?: UniqueReflectionData['options']): ClassDecorator;
|
|
55
56
|
export declare function Unique(columns: [string, ...string[]], options?: UniqueReflectionData['options']): ClassDecorator;
|
package/orm/decorators.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { createClassDecorator, createDecorator, createPropertyDecorator } from '../reflection/utils.js';
|
|
2
2
|
import { Property } from '../schema/index.js';
|
|
3
|
-
import { filterUndefinedObjectProperties } from '../utils/object/object.js';
|
|
4
3
|
import { assertNotArrayPass, isArray, isString } from '../utils/type-guards.js';
|
|
5
4
|
export function createTableDecorator(data) {
|
|
6
5
|
return createClassDecorator({ data: { orm: data }, mergeData: true });
|
|
@@ -28,8 +27,10 @@ export function Embedded(type, options) {
|
|
|
28
27
|
include: [Property(type), createColumnDecorator({ embedded: { type, ...options } })]
|
|
29
28
|
});
|
|
30
29
|
}
|
|
31
|
-
export function Table(
|
|
32
|
-
|
|
30
|
+
export function Table(nameOrOptions, optionsOrNothing) {
|
|
31
|
+
const name = isString(nameOrOptions) ? nameOrOptions : nameOrOptions?.name;
|
|
32
|
+
const schema = isString(nameOrOptions) ? optionsOrNothing?.schema : nameOrOptions?.schema;
|
|
33
|
+
return createTableDecorator({ name, schema });
|
|
33
34
|
}
|
|
34
35
|
export function Unique(nameOrColumns, columnsOrOptions, options) {
|
|
35
36
|
if (isArray(nameOrColumns)) {
|
|
@@ -8,7 +8,7 @@ type ConverterContext = {
|
|
|
8
8
|
};
|
|
9
9
|
export declare const getDrizzleTableFromType: typeof _getDrizzleTableFromType;
|
|
10
10
|
export declare function getColumnDefinitions(table: PgTableWithColumns<any>): ColumnDefinition[];
|
|
11
|
-
export declare function _getDrizzleTableFromType<T extends EntityType, S extends string>(type: T, schemaName
|
|
11
|
+
export declare function _getDrizzleTableFromType<T extends EntityType, S extends string>(type: T, schemaName?: S): PgTableFromType<S, T>;
|
|
12
12
|
export declare function registerEnum(enumeration: Enumeration, name: string): void;
|
|
13
13
|
export declare function getPgEnum(schema: string | PgSchema, enumeration: Enumeration, context?: ConverterContext): PgEnum<[string, ...string[]]>;
|
|
14
14
|
export {};
|
|
@@ -28,9 +28,10 @@ export function getColumnDefinitions(table) {
|
|
|
28
28
|
export function _getDrizzleTableFromType(type, schemaName) {
|
|
29
29
|
const metadata = reflectionRegistry.getMetadata(type);
|
|
30
30
|
assertDefined(metadata, `Type ${type.name} does not have reflection metadata.`);
|
|
31
|
-
const dbSchema = getDbSchema(schemaName);
|
|
32
31
|
const tableReflectionData = metadata.data.tryGet('orm');
|
|
32
|
+
const schema = assertDefinedPass(schemaName ?? tableReflectionData?.schema, 'Table schema not provided');
|
|
33
33
|
const tableName = tableReflectionData?.name ?? getDefaultTableName(type);
|
|
34
|
+
const dbSchema = getDbSchema(schema);
|
|
34
35
|
const columnDefinitions = getPostgresColumnEntries(type, tableName, dbSchema);
|
|
35
36
|
function getColumn(table, propertyName) {
|
|
36
37
|
return assertDefinedPass(table[propertyName], `Property "${propertyName}" does not exist on ${type.name}`);
|
|
@@ -30,7 +30,6 @@ export declare class EntityRepositoryConfig {
|
|
|
30
30
|
schema: string;
|
|
31
31
|
}
|
|
32
32
|
export type TransactionHandler<T extends Entity, R> = (repository: EntityRepository<T>, transaction: Transaction) => Promise<R>;
|
|
33
|
-
export declare const ENTITY_TYPE: import("../../injector/token.js").InjectionToken<EntityType<any>, never>;
|
|
34
33
|
export declare class EntityRepository<T extends Entity = Entity> implements Resolvable<EntityType<T>> {
|
|
35
34
|
#private;
|
|
36
35
|
readonly type: EntityType<T>;
|
|
@@ -113,5 +112,5 @@ export declare class EntityRepository<T extends Entity = Entity> implements Reso
|
|
|
113
112
|
protected getTransformContext(): Promise<TransformContext>;
|
|
114
113
|
}
|
|
115
114
|
export declare function injectRepository<T extends Entity>(type: EntityType<T>): EntityRepository<T>;
|
|
116
|
-
export declare function getRepository<T extends Entity>(type: EntityType<T
|
|
115
|
+
export declare function getRepository<T extends Entity>(type: EntityType<T>): Type<EntityRepository<T>>;
|
|
117
116
|
export {};
|
package/orm/server/repository.js
CHANGED
|
@@ -15,7 +15,6 @@ import { NotFoundError } from '../../errors/not-found.error.js';
|
|
|
15
15
|
import { Singleton } from '../../injector/decorators.js';
|
|
16
16
|
import { inject, injectArgument } from '../../injector/inject.js';
|
|
17
17
|
import { resolveArgumentType } from '../../injector/interfaces.js';
|
|
18
|
-
import { injectionToken } from '../../injector/token.js';
|
|
19
18
|
import { Schema } from '../../schema/schema.js';
|
|
20
19
|
import { toArray } from '../../utils/array/array.js';
|
|
21
20
|
import { mapAsync } from '../../utils/async-iterable-helpers/map.js';
|
|
@@ -32,15 +31,13 @@ export const repositoryType = Symbol('repositoryType');
|
|
|
32
31
|
export class EntityRepositoryConfig {
|
|
33
32
|
schema;
|
|
34
33
|
}
|
|
35
|
-
export const ENTITY_TYPE = injectionToken('EntityType');
|
|
36
34
|
const entityTypeToken = Symbol('EntityType');
|
|
37
|
-
const entityRepositoryConfigToken = Symbol('EntityRepositoryConfig');
|
|
38
35
|
const TRANSACTION_TIMESTAMP = sql `transaction_timestamp()`;
|
|
39
|
-
const { getCurrentEntityRepositoryContext, runInEntityRepositoryContext } = createContextProvider('EntityRepository');
|
|
36
|
+
const { getCurrentEntityRepositoryContext, runInEntityRepositoryContext, isInEntityRepositoryContext } = createContextProvider('EntityRepository');
|
|
40
37
|
let EntityRepository = class EntityRepository {
|
|
41
38
|
#repositoryConstructor;
|
|
42
39
|
#withTransactionCache = new WeakMap();
|
|
43
|
-
#encryptionSecret = inject(ENCRYPTION_SECRET, undefined, { optional: true });
|
|
40
|
+
#encryptionSecret = isInEntityRepositoryContext() ? getCurrentEntityRepositoryContext()?.encryptionSecret : inject(ENCRYPTION_SECRET, undefined, { optional: true });
|
|
44
41
|
#transformContext;
|
|
45
42
|
type;
|
|
46
43
|
table;
|
|
@@ -50,14 +47,14 @@ let EntityRepository = class EntityRepository {
|
|
|
50
47
|
isInTransaction;
|
|
51
48
|
constructor() {
|
|
52
49
|
this.#repositoryConstructor = new.target;
|
|
53
|
-
const
|
|
54
|
-
const { type, table, columnDefinitions, columnDefinitionsMap, session } = getCurrentEntityRepositoryContext() ?? {};
|
|
50
|
+
const { type, table, columnDefinitions, columnDefinitionsMap, session, transformContext } = getCurrentEntityRepositoryContext() ?? {};
|
|
55
51
|
this.type = type ?? injectArgument(this, { optional: true }) ?? assertDefinedPass(new.target[entityTypeToken], 'Missing entity type.');
|
|
56
|
-
this.table = table ?? getDrizzleTableFromType(this.type,
|
|
52
|
+
this.table = table ?? getDrizzleTableFromType(this.type, inject(EntityRepositoryConfig).schema);
|
|
57
53
|
this.columnDefinitions = columnDefinitions ?? getColumnDefinitions(this.table);
|
|
58
54
|
this.columnDefinitionsMap = columnDefinitionsMap ?? new Map(this.columnDefinitions.map((column) => [column.objectPath.path, column]));
|
|
59
55
|
this.session = session ?? inject(Database);
|
|
60
56
|
this.isInTransaction = this.session instanceof DrizzlePgTransaction;
|
|
57
|
+
this.#transformContext = transformContext;
|
|
61
58
|
}
|
|
62
59
|
withOptionalTransaction(transaction) {
|
|
63
60
|
if (isUndefined(transaction)) {
|
|
@@ -74,7 +71,9 @@ let EntityRepository = class EntityRepository {
|
|
|
74
71
|
table: this.table,
|
|
75
72
|
columnDefinitions: this.columnDefinitions,
|
|
76
73
|
columnDefinitionsMap: this.columnDefinitionsMap,
|
|
77
|
-
session: transaction.transaction
|
|
74
|
+
session: transaction.transaction,
|
|
75
|
+
encryptionSecret: this.#encryptionSecret,
|
|
76
|
+
transformContext: this.#transformContext
|
|
78
77
|
};
|
|
79
78
|
const repositoryWithTransaction = runInEntityRepositoryContext(context, () => new this.#repositoryConstructor());
|
|
80
79
|
this.#withTransactionCache.set(transaction, repositoryWithTransaction);
|
|
@@ -469,12 +468,11 @@ export { EntityRepository };
|
|
|
469
468
|
export function injectRepository(type) {
|
|
470
469
|
return inject((EntityRepository), type);
|
|
471
470
|
}
|
|
472
|
-
export function getRepository(type
|
|
471
|
+
export function getRepository(type) {
|
|
473
472
|
const className = `${type.name}Service`;
|
|
474
473
|
const entityRepositoryClass = {
|
|
475
474
|
[className]: class extends EntityRepository {
|
|
476
475
|
static [entityTypeToken] = type;
|
|
477
|
-
static [entityRepositoryConfigToken] = config;
|
|
478
476
|
}
|
|
479
477
|
}[className];
|
|
480
478
|
Singleton()(entityRepositoryClass);
|