@tstdl/base 0.92.89 → 0.92.91
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/query.d.ts +4 -4
- package/orm/server/repository.d.ts +7 -7
- package/orm/server/repository.js +9 -9
- package/package.json +1 -1
package/orm/query.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { SQLWrapper } from 'drizzle-orm';
|
|
2
2
|
import type { Flatten, Record } from '../types.js';
|
|
3
3
|
import type { Geometry } from '../types/geo-json.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { UntaggedDeep } from '../types/tagged.js';
|
|
5
5
|
export type LogicalQuery<T = any> = LogicalAndQuery<T> | LogicalOrQuery<T> | LogicalNorQuery<T>;
|
|
6
6
|
export type LogicalQueryTypes = keyof (LogicalAndQuery & LogicalOrQuery & LogicalNorQuery);
|
|
7
7
|
export declare const allLogicalQueryTypes: LogicalQueryTypes[];
|
|
8
8
|
export type ComparisonQueryBody<T = any> = {
|
|
9
|
-
[P in keyof T]?: ComparisonQueryOrValue<T[P]>;
|
|
9
|
+
[P in keyof T]?: T[P] extends Record ? ComparisonQueryBody<T[P]> : ComparisonQueryOrValue<T[P]>;
|
|
10
10
|
} & Record<ComparisonQueryOrValue>;
|
|
11
11
|
export type ComparisonQueryOrValue<T = any> = ComparisonQuery<T> | ComparisonValue<T>;
|
|
12
12
|
export type ComparisonQuery<T = any> = Partial<ComparisonNotQuery<T> & ComparisonEqualsQuery<T> & ComparisonNotEqualsQuery<T> & ComparisonExistsQuery & ComparisonItemQuery<T> & ComparisonInQuery<T> & ComparisonNotInQuery<T> & ComparisonAllQuery<T> & ComparisonGreaterThanQuery<T> & ComparisonGreaterThanOrEqualsQuery<T> & ComparisonLessThanQuery<T> & ComparisonLessThanOrEqualsQuery<T> & ComparisonRegexQuery & ComparisonTextQuery & ComparisonGeoShapeQuery & ComparisonGeoDistanceQuery>;
|
|
@@ -15,7 +15,7 @@ export declare const allComparisonQueryTypes: ComparisonQueryTypes[];
|
|
|
15
15
|
export type SpecialQuery<T = any> = Partial<TextSpanQuery<T>>;
|
|
16
16
|
export type SpecialQueryTypes = keyof SpecialQuery;
|
|
17
17
|
export declare const allSpecialQueryTypes: SpecialQueryTypes[];
|
|
18
|
-
export type Query<T = any> = SQLWrapper | QueryObject<T
|
|
18
|
+
export type Query<T = any> = SQLWrapper | QueryObject<UntaggedDeep<T>>;
|
|
19
19
|
export type QueryObject<T> = LogicalQuery<T> | (ComparisonQueryBody<T> & SpecialQuery<T>);
|
|
20
20
|
export type QueryTypes = LogicalQueryTypes | ComparisonQueryTypes | SpecialQueryTypes;
|
|
21
21
|
export declare const allQueryTypes: ("$and" | "$or" | "$nor" | "$not" | "$eq" | "$neq" | "$exists" | "$item" | "$in" | "$nin" | "$all" | "$gt" | "$gte" | "$lt" | "$lte" | "$regex" | "$text" | "$geoShape" | "$geoDistance" | "$textSpan")[];
|
|
@@ -30,7 +30,7 @@ export type LogicalOrQuery<T = any> = {
|
|
|
30
30
|
export type LogicalNorQuery<T = any> = {
|
|
31
31
|
$nor: readonly Query<T>[];
|
|
32
32
|
};
|
|
33
|
-
export type ComparisonValue<T> =
|
|
33
|
+
export type ComparisonValue<T> = T | Flatten<T> | SQLWrapper;
|
|
34
34
|
export type ComparisonValueWithRegex<T> = T extends string ? ComparisonValue<T | RegExp> : T extends readonly string[] ? ComparisonValue<readonly (Flatten<T> | RegExp)[]> : (T | Flatten<T>);
|
|
35
35
|
export type ComparisonNotQuery<T = any> = {
|
|
36
36
|
$not: ComparisonQuery<T>;
|
|
@@ -14,7 +14,7 @@ export declare const repositoryType: unique symbol;
|
|
|
14
14
|
export declare class EntityRepositoryConfig {
|
|
15
15
|
schema: string;
|
|
16
16
|
}
|
|
17
|
-
export type TransactionHandler<T extends
|
|
17
|
+
export type TransactionHandler<T extends EntityRepository<any>, R> = (repository: T, transaction: Transaction) => Promise<R>;
|
|
18
18
|
type InferSelect<T extends Entity | EntityWithoutMetadata = Entity | EntityWithoutMetadata> = PgTableFromType<EntityType<T>>['$inferSelect'];
|
|
19
19
|
export declare class EntityRepository<T extends Entity | EntityWithoutMetadata = EntityWithoutMetadata> implements Resolvable<EntityType<T>> {
|
|
20
20
|
#private;
|
|
@@ -27,11 +27,11 @@ export declare class EntityRepository<T extends Entity | EntityWithoutMetadata =
|
|
|
27
27
|
protected get isFork(): boolean;
|
|
28
28
|
readonly [resolveArgumentType]: EntityType<T>;
|
|
29
29
|
constructor();
|
|
30
|
-
withOptionalTransaction(transaction: Transaction | undefined):
|
|
31
|
-
withTransaction(transaction: Transaction):
|
|
30
|
+
withOptionalTransaction(transaction: Transaction | undefined): this;
|
|
31
|
+
withTransaction(transaction: Transaction): this;
|
|
32
32
|
startTransaction(config?: TransactionConfig): Promise<Transaction>;
|
|
33
|
-
useTransaction<R>(transaction: Transaction | undefined, handler: TransactionHandler<
|
|
34
|
-
transaction<R>(handler: TransactionHandler<
|
|
33
|
+
useTransaction<R>(transaction: Transaction | undefined, handler: TransactionHandler<this, R>): Promise<R>;
|
|
34
|
+
transaction<R>(handler: TransactionHandler<this, R>, config?: TransactionConfig): Promise<R>;
|
|
35
35
|
load(id: string): Promise<T>;
|
|
36
36
|
tryLoad(id: string): Promise<T | undefined>;
|
|
37
37
|
loadByQuery(query: Query<T>, options?: LoadOptions<T>): Promise<T>;
|
|
@@ -75,8 +75,8 @@ export declare class EntityRepository<T extends Entity | EntityWithoutMetadata =
|
|
|
75
75
|
tryHardDeleteByQuery(query: Query<T>): Promise<T | undefined>;
|
|
76
76
|
hardDeleteMany(ids: string[]): Promise<T[]>;
|
|
77
77
|
hardDeleteManyByQuery(query: Query<T>): Promise<T[]>;
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
getColumn(pathOrColumn: Paths<UntaggedDeep<T>> | ColumnDefinition): PgColumn;
|
|
79
|
+
convertOrderBy(orderBy: Order<T>): SQL<unknown>[];
|
|
80
80
|
convertQuery(query: Query<T>): SQL;
|
|
81
81
|
mapManyToEntity(columns: InferSelect[]): Promise<T[]>;
|
|
82
82
|
mapToEntity(columns: InferSelect): Promise<T>;
|
package/orm/server/repository.js
CHANGED
|
@@ -125,7 +125,7 @@ let EntityRepository = class EntityRepository {
|
|
|
125
125
|
.offset(options?.offset)
|
|
126
126
|
.$dynamic();
|
|
127
127
|
if (isDefined(options?.order)) {
|
|
128
|
-
dbQuery = dbQuery.orderBy(...this
|
|
128
|
+
dbQuery = dbQuery.orderBy(...this.convertOrderBy(options.order));
|
|
129
129
|
}
|
|
130
130
|
const [row] = await dbQuery;
|
|
131
131
|
if (isUndefined(row)) {
|
|
@@ -150,7 +150,7 @@ let EntityRepository = class EntityRepository {
|
|
|
150
150
|
.limit(options?.limit)
|
|
151
151
|
.$dynamic();
|
|
152
152
|
if (isDefined(options?.order)) {
|
|
153
|
-
dbQuery = dbQuery.orderBy(...this
|
|
153
|
+
dbQuery = dbQuery.orderBy(...this.convertOrderBy(options.order));
|
|
154
154
|
}
|
|
155
155
|
const rows = await dbQuery;
|
|
156
156
|
return this.mapManyToEntity(rows);
|
|
@@ -230,7 +230,7 @@ let EntityRepository = class EntityRepository {
|
|
|
230
230
|
return this.mapManyToEntity(rows);
|
|
231
231
|
}
|
|
232
232
|
async upsert(target, entity, update) {
|
|
233
|
-
const targetColumns = toArray(target).map((path) => this
|
|
233
|
+
const targetColumns = toArray(target).map((path) => this.getColumn(path));
|
|
234
234
|
const columns = await this.mapToInsertColumns(entity);
|
|
235
235
|
const mappedUpdate = await this.mapUpdate(update ?? entity);
|
|
236
236
|
const [row] = await this.session
|
|
@@ -244,12 +244,12 @@ let EntityRepository = class EntityRepository {
|
|
|
244
244
|
return this.mapToEntity(row);
|
|
245
245
|
}
|
|
246
246
|
async upsertMany(target, entities, update) {
|
|
247
|
-
const targetColumns = toArray(target).map((path) => this
|
|
247
|
+
const targetColumns = toArray(target).map((path) => this.getColumn(path));
|
|
248
248
|
const columns = await this.mapManyToInsertColumns(entities);
|
|
249
249
|
const mappedUpdate = isDefined(update)
|
|
250
250
|
? await this.mapUpdate(update)
|
|
251
251
|
: {
|
|
252
|
-
...fromEntries(this.#columnDefinitions.map((column) => [column.name, sql `excluded.${sql.identifier(this
|
|
252
|
+
...fromEntries(this.#columnDefinitions.map((column) => [column.name, sql `excluded.${sql.identifier(this.getColumn(column).name)}`])),
|
|
253
253
|
...this._getMetadataUpdate(update)
|
|
254
254
|
};
|
|
255
255
|
const rows = await this.session
|
|
@@ -427,26 +427,26 @@ let EntityRepository = class EntityRepository {
|
|
|
427
427
|
.returning();
|
|
428
428
|
return this.mapManyToEntity(rows);
|
|
429
429
|
}
|
|
430
|
-
|
|
430
|
+
getColumn(pathOrColumn) {
|
|
431
431
|
if (isString(pathOrColumn)) {
|
|
432
432
|
const columnName = assertDefinedPass(this.#columnDefinitionsMap.get(pathOrColumn), `Could not map ${pathOrColumn} to column.`).name;
|
|
433
433
|
return this.#table[columnName];
|
|
434
434
|
}
|
|
435
435
|
return this.#table[pathOrColumn.name];
|
|
436
436
|
}
|
|
437
|
-
|
|
437
|
+
convertOrderBy(orderBy) {
|
|
438
438
|
if (isArray(orderBy)) {
|
|
439
439
|
return orderBy.map((item) => {
|
|
440
440
|
const itemIsArray = isArray(item);
|
|
441
441
|
const target = itemIsArray ? item[0] : item;
|
|
442
|
-
const column = isSQLWrapper(target) ? target : this
|
|
442
|
+
const column = isSQLWrapper(target) ? target : this.getColumn(target);
|
|
443
443
|
const direction = itemIsArray ? item[1] : 'asc';
|
|
444
444
|
return direction == 'asc' ? asc(column) : desc(column);
|
|
445
445
|
});
|
|
446
446
|
}
|
|
447
447
|
return objectEntries(orderBy)
|
|
448
448
|
.map(([path, direction]) => {
|
|
449
|
-
const column = this
|
|
449
|
+
const column = this.getColumn(path);
|
|
450
450
|
return direction == 'asc' ? asc(column) : desc(column);
|
|
451
451
|
});
|
|
452
452
|
}
|