metal-orm 1.0.86 → 1.0.87
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/dist/index.cjs +35 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -16
- package/dist/index.d.ts +13 -16
- package/dist/index.js +35 -36
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/orm/execute.ts +12 -26
- package/src/query-builder/select/select-operations.ts +4 -12
- package/src/query-builder/select.ts +23 -25
package/package.json
CHANGED
package/src/orm/execute.ts
CHANGED
|
@@ -24,10 +24,6 @@ import {
|
|
|
24
24
|
|
|
25
25
|
type Row = Record<string, unknown>;
|
|
26
26
|
|
|
27
|
-
type ParamOperandOptions = {
|
|
28
|
-
allowParamOperands?: boolean;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
27
|
const flattenResults = (results: { columns: string[]; values: unknown[][] }[]): Row[] => {
|
|
32
28
|
const rows: Row[] = [];
|
|
33
29
|
for (const result of results) {
|
|
@@ -46,13 +42,10 @@ const flattenResults = (results: { columns: string[]; values: unknown[][] }[]):
|
|
|
46
42
|
const executeWithContexts = async <TTable extends TableDef>(
|
|
47
43
|
execCtx: ExecutionContext,
|
|
48
44
|
entityCtx: EntityContext,
|
|
49
|
-
qb: SelectQueryBuilder<unknown, TTable
|
|
50
|
-
options?: ParamOperandOptions
|
|
45
|
+
qb: SelectQueryBuilder<unknown, TTable>
|
|
51
46
|
): Promise<EntityInstance<TTable>[]> => {
|
|
52
47
|
const ast = qb.getAST();
|
|
53
|
-
const compiled =
|
|
54
|
-
? execCtx.dialect.compileSelectWithOptions(ast, { allowParams: true })
|
|
55
|
-
: execCtx.dialect.compileSelect(ast);
|
|
48
|
+
const compiled = execCtx.dialect.compileSelect(ast);
|
|
56
49
|
const executed = await execCtx.interceptors.run({ sql: compiled.sql, params: compiled.params }, execCtx.executor);
|
|
57
50
|
const rows = flattenResults(executed);
|
|
58
51
|
const lazyRelations = qb.getLazyRelations() as RelationKey<TTable>[];
|
|
@@ -75,13 +68,10 @@ const executeWithContexts = async <TTable extends TableDef>(
|
|
|
75
68
|
|
|
76
69
|
const executePlainWithContexts = async <TTable extends TableDef>(
|
|
77
70
|
execCtx: ExecutionContext,
|
|
78
|
-
qb: SelectQueryBuilder<unknown, TTable
|
|
79
|
-
options?: ParamOperandOptions
|
|
71
|
+
qb: SelectQueryBuilder<unknown, TTable>
|
|
80
72
|
): Promise<Record<string, unknown>[]> => {
|
|
81
73
|
const ast = qb.getAST();
|
|
82
|
-
const compiled =
|
|
83
|
-
? execCtx.dialect.compileSelectWithOptions(ast, { allowParams: true })
|
|
84
|
-
: execCtx.dialect.compileSelect(ast);
|
|
74
|
+
const compiled = execCtx.dialect.compileSelect(ast);
|
|
85
75
|
const executed = await execCtx.interceptors.run({ sql: compiled.sql, params: compiled.params }, execCtx.executor);
|
|
86
76
|
const rows = flattenResults(executed);
|
|
87
77
|
|
|
@@ -101,10 +91,9 @@ const executePlainWithContexts = async <TTable extends TableDef>(
|
|
|
101
91
|
*/
|
|
102
92
|
export async function executeHydrated<TTable extends TableDef>(
|
|
103
93
|
session: OrmSession,
|
|
104
|
-
qb: SelectQueryBuilder<unknown, TTable
|
|
105
|
-
options?: ParamOperandOptions
|
|
94
|
+
qb: SelectQueryBuilder<unknown, TTable>
|
|
106
95
|
): Promise<EntityInstance<TTable>[]> {
|
|
107
|
-
return executeWithContexts(session.getExecutionContext(), session, qb
|
|
96
|
+
return executeWithContexts(session.getExecutionContext(), session, qb);
|
|
108
97
|
}
|
|
109
98
|
|
|
110
99
|
/**
|
|
@@ -116,10 +105,9 @@ export async function executeHydrated<TTable extends TableDef>(
|
|
|
116
105
|
*/
|
|
117
106
|
export async function executeHydratedPlain<TTable extends TableDef>(
|
|
118
107
|
session: OrmSession,
|
|
119
|
-
qb: SelectQueryBuilder<unknown, TTable
|
|
120
|
-
options?: ParamOperandOptions
|
|
108
|
+
qb: SelectQueryBuilder<unknown, TTable>
|
|
121
109
|
): Promise<Record<string, unknown>[]> {
|
|
122
|
-
return executePlainWithContexts(session.getExecutionContext(), qb
|
|
110
|
+
return executePlainWithContexts(session.getExecutionContext(), qb);
|
|
123
111
|
}
|
|
124
112
|
|
|
125
113
|
/**
|
|
@@ -133,14 +121,13 @@ export async function executeHydratedPlain<TTable extends TableDef>(
|
|
|
133
121
|
export async function executeHydratedWithContexts<TTable extends TableDef>(
|
|
134
122
|
execCtx: ExecutionContext,
|
|
135
123
|
hydCtx: HydrationContext,
|
|
136
|
-
qb: SelectQueryBuilder<unknown, TTable
|
|
137
|
-
options?: ParamOperandOptions
|
|
124
|
+
qb: SelectQueryBuilder<unknown, TTable>
|
|
138
125
|
): Promise<EntityInstance<TTable>[]> {
|
|
139
126
|
const entityCtx = hydCtx.entityContext;
|
|
140
127
|
if (!entityCtx) {
|
|
141
128
|
throw new Error('Hydration context is missing an EntityContext');
|
|
142
129
|
}
|
|
143
|
-
return executeWithContexts(execCtx, entityCtx, qb
|
|
130
|
+
return executeWithContexts(execCtx, entityCtx, qb);
|
|
144
131
|
}
|
|
145
132
|
|
|
146
133
|
/**
|
|
@@ -152,10 +139,9 @@ export async function executeHydratedWithContexts<TTable extends TableDef>(
|
|
|
152
139
|
*/
|
|
153
140
|
export async function executeHydratedPlainWithContexts<TTable extends TableDef>(
|
|
154
141
|
execCtx: ExecutionContext,
|
|
155
|
-
qb: SelectQueryBuilder<unknown, TTable
|
|
156
|
-
options?: ParamOperandOptions
|
|
142
|
+
qb: SelectQueryBuilder<unknown, TTable>
|
|
157
143
|
): Promise<Record<string, unknown>[]> {
|
|
158
|
-
return executePlainWithContexts(execCtx, qb
|
|
144
|
+
return executePlainWithContexts(execCtx, qb);
|
|
159
145
|
}
|
|
160
146
|
|
|
161
147
|
const loadLazyRelationsForTable = async <TTable extends TableDef>(
|
|
@@ -15,10 +15,6 @@ export type WhereHasOptions = {
|
|
|
15
15
|
correlate?: ExpressionNode;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
type ParamOperandOptions = {
|
|
19
|
-
allowParamOperands?: boolean;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
18
|
export type RelationCallback = <TChildTable extends TableDef>(
|
|
23
19
|
qb: SelectQueryBuilder<unknown, TChildTable>
|
|
24
20
|
) => SelectQueryBuilder<unknown, TChildTable>;
|
|
@@ -46,8 +42,7 @@ export function applyOrderBy(
|
|
|
46
42
|
export async function executeCount(
|
|
47
43
|
context: SelectQueryBuilderContext,
|
|
48
44
|
env: SelectQueryBuilderEnvironment,
|
|
49
|
-
session: OrmSession
|
|
50
|
-
options?: ParamOperandOptions
|
|
45
|
+
session: OrmSession
|
|
51
46
|
): Promise<number> {
|
|
52
47
|
const unpagedAst: SelectQueryNode = {
|
|
53
48
|
...context.state.ast,
|
|
@@ -71,9 +66,7 @@ export async function executeCount(
|
|
|
71
66
|
};
|
|
72
67
|
|
|
73
68
|
const execCtx = session.getExecutionContext();
|
|
74
|
-
const compiled =
|
|
75
|
-
? execCtx.dialect.compileSelectWithOptions(countQuery, { allowParams: true })
|
|
76
|
-
: execCtx.dialect.compileSelect(countQuery);
|
|
69
|
+
const compiled = execCtx.dialect.compileSelect(countQuery);
|
|
77
70
|
const results = await execCtx.interceptors.run({ sql: compiled.sql, params: compiled.params }, execCtx.executor);
|
|
78
71
|
const value = results[0]?.values?.[0]?.[0];
|
|
79
72
|
|
|
@@ -97,8 +90,7 @@ export async function executePagedQuery<T, TTable extends TableDef>(
|
|
|
97
90
|
builder: SelectQueryBuilder<T, TTable>,
|
|
98
91
|
session: OrmSession,
|
|
99
92
|
options: { page: number; pageSize: number },
|
|
100
|
-
countCallback: (session: OrmSession) => Promise<number
|
|
101
|
-
paramOptions?: ParamOperandOptions
|
|
93
|
+
countCallback: (session: OrmSession) => Promise<number>
|
|
102
94
|
): Promise<PaginatedResult<T>> {
|
|
103
95
|
const { page, pageSize } = options;
|
|
104
96
|
|
|
@@ -112,7 +104,7 @@ export async function executePagedQuery<T, TTable extends TableDef>(
|
|
|
112
104
|
const offset = (page - 1) * pageSize;
|
|
113
105
|
|
|
114
106
|
const totalItems = await countCallback(session);
|
|
115
|
-
const items = await builder.limit(pageSize).offset(offset).execute(session
|
|
107
|
+
const items = await builder.limit(pageSize).offset(offset).execute(session);
|
|
116
108
|
|
|
117
109
|
return { items, totalItems, page, pageSize };
|
|
118
110
|
}
|
|
@@ -86,7 +86,7 @@ type SelectionResult<TSelection extends Record<string, ColumnSelectionValue>> =
|
|
|
86
86
|
[K in keyof TSelection]: SelectionValueType<TSelection[K]>;
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
-
type
|
|
89
|
+
type ParamOperandCompileOptions = {
|
|
90
90
|
allowParamOperands?: boolean;
|
|
91
91
|
};
|
|
92
92
|
|
|
@@ -734,8 +734,7 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
734
734
|
* Validates that the query does not contain Param operands.
|
|
735
735
|
* Param proxies are only for schema generation, not execution.
|
|
736
736
|
*/
|
|
737
|
-
private validateNoParamOperands(
|
|
738
|
-
if (options?.allowParamOperands) return;
|
|
737
|
+
private validateNoParamOperands(): void {
|
|
739
738
|
const ast = this.context.hydration.applyToAst(this.context.state.ast);
|
|
740
739
|
const paramName = findFirstParamOperandName(ast);
|
|
741
740
|
if (paramName) {
|
|
@@ -755,13 +754,13 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
755
754
|
* // users is User[]
|
|
756
755
|
* users[0] instanceof User; // true
|
|
757
756
|
*/
|
|
758
|
-
async execute(ctx: OrmSession
|
|
759
|
-
this.validateNoParamOperands(
|
|
757
|
+
async execute(ctx: OrmSession): Promise<T[]> {
|
|
758
|
+
this.validateNoParamOperands();
|
|
760
759
|
if (this.entityConstructor) {
|
|
761
|
-
return this.executeAs(this.entityConstructor, ctx
|
|
760
|
+
return this.executeAs(this.entityConstructor, ctx) as unknown as T[];
|
|
762
761
|
}
|
|
763
762
|
const builder = this.ensureDefaultSelection();
|
|
764
|
-
return executeHydrated(ctx, builder
|
|
763
|
+
return executeHydrated(ctx, builder) as unknown as T[];
|
|
765
764
|
}
|
|
766
765
|
|
|
767
766
|
/**
|
|
@@ -775,10 +774,10 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
775
774
|
* // rows is EntityInstance<UserTable>[] (plain objects)
|
|
776
775
|
* rows[0] instanceof User; // false
|
|
777
776
|
*/
|
|
778
|
-
async executePlain(ctx: OrmSession
|
|
779
|
-
this.validateNoParamOperands(
|
|
777
|
+
async executePlain(ctx: OrmSession): Promise<EntityInstance<TTable>[]> {
|
|
778
|
+
this.validateNoParamOperands();
|
|
780
779
|
const builder = this.ensureDefaultSelection();
|
|
781
|
-
const rows = await executeHydratedPlain(ctx, builder
|
|
780
|
+
const rows = await executeHydratedPlain(ctx, builder);
|
|
782
781
|
return rows as EntityInstance<TTable>[];
|
|
783
782
|
}
|
|
784
783
|
|
|
@@ -798,12 +797,11 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
798
797
|
*/
|
|
799
798
|
async executeAs<TEntity extends object>(
|
|
800
799
|
entityClass: EntityConstructor<TEntity>,
|
|
801
|
-
ctx: OrmSession
|
|
802
|
-
options?: ParamOperandOptions
|
|
800
|
+
ctx: OrmSession
|
|
803
801
|
): Promise<TEntity[]> {
|
|
804
|
-
this.validateNoParamOperands(
|
|
802
|
+
this.validateNoParamOperands();
|
|
805
803
|
const builder = this.ensureDefaultSelection();
|
|
806
|
-
const results = await executeHydrated(ctx, builder
|
|
804
|
+
const results = await executeHydrated(ctx, builder);
|
|
807
805
|
return materializeAs(entityClass, results as unknown as Record<string, unknown>[]);
|
|
808
806
|
}
|
|
809
807
|
|
|
@@ -813,9 +811,9 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
813
811
|
* @example
|
|
814
812
|
* const total = await qb.count(session);
|
|
815
813
|
*/
|
|
816
|
-
async count(session: OrmSession
|
|
817
|
-
this.validateNoParamOperands(
|
|
818
|
-
return executeCount(this.context, this.env, session
|
|
814
|
+
async count(session: OrmSession): Promise<number> {
|
|
815
|
+
this.validateNoParamOperands();
|
|
816
|
+
return executeCount(this.context, this.env, session);
|
|
819
817
|
}
|
|
820
818
|
|
|
821
819
|
/**
|
|
@@ -826,11 +824,11 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
826
824
|
*/
|
|
827
825
|
async executePaged(
|
|
828
826
|
session: OrmSession,
|
|
829
|
-
options: { page: number; pageSize: number }
|
|
827
|
+
options: { page: number; pageSize: number }
|
|
830
828
|
): Promise<PaginatedResult<T>> {
|
|
831
|
-
this.validateNoParamOperands(
|
|
829
|
+
this.validateNoParamOperands();
|
|
832
830
|
const builder = this.ensureDefaultSelection();
|
|
833
|
-
return executePagedQuery(builder, session, options, sess => builder.count(sess
|
|
831
|
+
return executePagedQuery(builder, session, options, sess => builder.count(sess));
|
|
834
832
|
}
|
|
835
833
|
|
|
836
834
|
/**
|
|
@@ -843,10 +841,10 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
843
841
|
* const hydCtx = new HydrationContext();
|
|
844
842
|
* const users = await qb.executeWithContexts(execCtx, hydCtx);
|
|
845
843
|
*/
|
|
846
|
-
async executeWithContexts(execCtx: ExecutionContext, hydCtx: HydrationContext
|
|
847
|
-
this.validateNoParamOperands(
|
|
844
|
+
async executeWithContexts(execCtx: ExecutionContext, hydCtx: HydrationContext): Promise<T[]> {
|
|
845
|
+
this.validateNoParamOperands();
|
|
848
846
|
const builder = this.ensureDefaultSelection();
|
|
849
|
-
const results = await executeHydratedWithContexts(execCtx, hydCtx, builder
|
|
847
|
+
const results = await executeHydratedWithContexts(execCtx, hydCtx, builder);
|
|
850
848
|
if (this.entityConstructor) {
|
|
851
849
|
return materializeAs(this.entityConstructor, results as unknown as Record<string, unknown>[]) as unknown as T[];
|
|
852
850
|
}
|
|
@@ -1115,7 +1113,7 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
1115
1113
|
* .compile('postgres');
|
|
1116
1114
|
* console.log(compiled.sql); // SELECT "id", "name" FROM "users" WHERE "active" = true
|
|
1117
1115
|
*/
|
|
1118
|
-
compile(dialect: SelectDialectInput, options?:
|
|
1116
|
+
compile(dialect: SelectDialectInput, options?: ParamOperandCompileOptions): CompiledQuery {
|
|
1119
1117
|
const resolved = resolveDialectInput(dialect);
|
|
1120
1118
|
const ast = this.getAST();
|
|
1121
1119
|
if (!options?.allowParamOperands) {
|
|
@@ -1138,7 +1136,7 @@ export class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Tab
|
|
|
1138
1136
|
* .toSql('postgres');
|
|
1139
1137
|
* console.log(sql); // SELECT "id", "name" FROM "users" WHERE "active" = true
|
|
1140
1138
|
*/
|
|
1141
|
-
toSql(dialect: SelectDialectInput, options?:
|
|
1139
|
+
toSql(dialect: SelectDialectInput, options?: ParamOperandCompileOptions): string {
|
|
1142
1140
|
return this.compile(dialect, options).sql;
|
|
1143
1141
|
}
|
|
1144
1142
|
|