@apisr/drizzle-model 2.0.0 → 2.0.1
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/CHANGELOG.md +22 -0
- package/README.md +433 -0
- package/TODO.md +5 -5
- package/package.json +4 -3
- package/src/core/query/joins.ts +70 -3
- package/src/core/result.ts +12 -6
- package/src/core/runtime.ts +39 -15
- package/src/model/result.ts +5 -2
- package/tests/base/relations.test.ts +593 -0
- package/tests/base/upsert.test.ts +3 -3
package/src/core/runtime.ts
CHANGED
|
@@ -105,6 +105,16 @@ export class ModelRuntime {
|
|
|
105
105
|
return this.config.options.format;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
/** The current where clause, exposed for relation descriptors. */
|
|
109
|
+
get $where(): unknown {
|
|
110
|
+
return this.currentWhere;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The table name this model is bound to, exposed for relation descriptors. */
|
|
114
|
+
get $tableName(): string {
|
|
115
|
+
return this.config.tableName;
|
|
116
|
+
}
|
|
117
|
+
|
|
108
118
|
// ---------------------------------------------------------------------------
|
|
109
119
|
// Public: filtering
|
|
110
120
|
// ---------------------------------------------------------------------------
|
|
@@ -127,16 +137,26 @@ export class ModelRuntime {
|
|
|
127
137
|
// ---------------------------------------------------------------------------
|
|
128
138
|
|
|
129
139
|
/**
|
|
130
|
-
* Returns the
|
|
140
|
+
* Returns a relation descriptor carrying the model's where clause
|
|
141
|
+
* and the nested relation includes.
|
|
131
142
|
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
143
|
+
* Used in `.with()` to filter a relation and load nested relations:
|
|
144
|
+
* ```ts
|
|
145
|
+
* userModel.findMany().with({
|
|
146
|
+
* posts: postModel.where({ ... }).include({ comments: true }),
|
|
147
|
+
* });
|
|
148
|
+
* ```
|
|
134
149
|
*
|
|
135
|
-
* @param value - The relation include descriptor.
|
|
136
|
-
* @returns
|
|
150
|
+
* @param value - The nested relation include descriptor.
|
|
151
|
+
* @returns A model relation descriptor consumed by the join executor.
|
|
137
152
|
*/
|
|
138
153
|
include(value: unknown): unknown {
|
|
139
|
-
return
|
|
154
|
+
return {
|
|
155
|
+
__modelRelation: true,
|
|
156
|
+
whereValue: this.currentWhere,
|
|
157
|
+
tableName: this.config.tableName,
|
|
158
|
+
with: value,
|
|
159
|
+
};
|
|
140
160
|
}
|
|
141
161
|
|
|
142
162
|
/**
|
|
@@ -182,7 +202,8 @@ export class ModelRuntime {
|
|
|
182
202
|
/**
|
|
183
203
|
* Returns a thenable that resolves to an array of matching rows.
|
|
184
204
|
*
|
|
185
|
-
* Supports chaining: `.select()`, `.exclude()
|
|
205
|
+
* Supports chaining: `.select()`, `.exclude()` (SQL SELECT),
|
|
206
|
+
* `.with()` (relations), `.raw()`, `.safe()`.
|
|
186
207
|
*
|
|
187
208
|
* @returns A {@link QueryResult} that can be awaited or further chained.
|
|
188
209
|
*/
|
|
@@ -202,6 +223,8 @@ export class ModelRuntime {
|
|
|
202
223
|
baseTable: table,
|
|
203
224
|
whereSql,
|
|
204
225
|
withValue: qState.with as AnyRecord,
|
|
226
|
+
select: qState.select,
|
|
227
|
+
exclude: qState.exclude,
|
|
205
228
|
limitOne: false,
|
|
206
229
|
});
|
|
207
230
|
} else {
|
|
@@ -234,7 +257,8 @@ export class ModelRuntime {
|
|
|
234
257
|
/**
|
|
235
258
|
* Returns a thenable that resolves to the first matching row (or `undefined`).
|
|
236
259
|
*
|
|
237
|
-
* Supports chaining: `.select()`, `.exclude()
|
|
260
|
+
* Supports chaining: `.select()`, `.exclude()` (SQL SELECT),
|
|
261
|
+
* `.with()` (relations), `.raw()`, `.safe()`.
|
|
238
262
|
*
|
|
239
263
|
* @returns A {@link QueryResult} that can be awaited or further chained.
|
|
240
264
|
*/
|
|
@@ -254,6 +278,8 @@ export class ModelRuntime {
|
|
|
254
278
|
baseTable: table,
|
|
255
279
|
whereSql,
|
|
256
280
|
withValue: qState.with as AnyRecord,
|
|
281
|
+
select: qState.select,
|
|
282
|
+
exclude: qState.exclude,
|
|
257
283
|
limitOne: true,
|
|
258
284
|
});
|
|
259
285
|
} else {
|
|
@@ -529,7 +555,11 @@ export class ModelRuntime {
|
|
|
529
555
|
}
|
|
530
556
|
|
|
531
557
|
/**
|
|
532
|
-
* Applies
|
|
558
|
+
* Applies post-query transforms to the query result.
|
|
559
|
+
*
|
|
560
|
+
* Note: `.select()` and `.exclude()` are handled at the SQL level
|
|
561
|
+
* (via {@link ProjectionBuilder}) and are NOT applied here.
|
|
562
|
+
* Only format transforms are applied post-query.
|
|
533
563
|
*/
|
|
534
564
|
private applyPostQueryTransforms(
|
|
535
565
|
result: unknown,
|
|
@@ -537,12 +567,6 @@ export class ModelRuntime {
|
|
|
537
567
|
): unknown {
|
|
538
568
|
let out = result;
|
|
539
569
|
|
|
540
|
-
if (qState.select) {
|
|
541
|
-
out = this.transformer.applySelect(out, qState.select);
|
|
542
|
-
}
|
|
543
|
-
if (qState.exclude) {
|
|
544
|
-
out = this.transformer.applyExclude(out, qState.exclude);
|
|
545
|
-
}
|
|
546
570
|
if (!qState.raw) {
|
|
547
571
|
out = this.transformer.applyFormat(
|
|
548
572
|
out,
|
package/src/model/result.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
TableRelationalConfig,
|
|
3
3
|
TablesRelationalConfig,
|
|
4
4
|
} from "drizzle-orm/relations";
|
|
5
|
+
import type { SimplifyDeep } from "type-fest";
|
|
5
6
|
import type {
|
|
6
7
|
ApplyArrayIfArray,
|
|
7
8
|
InferArrayItem,
|
|
@@ -49,7 +50,9 @@ export interface ModelQueryResult<
|
|
|
49
50
|
TWithSafe,
|
|
50
51
|
ApplyArrayIfArray<
|
|
51
52
|
TResult,
|
|
52
|
-
|
|
53
|
+
SimplifyDeep<
|
|
54
|
+
ModelFormatResult<InferArrayItem<TResult>, TFormat, TTable>
|
|
55
|
+
>
|
|
53
56
|
>
|
|
54
57
|
>
|
|
55
58
|
> {
|
|
@@ -148,7 +151,7 @@ export interface ModelInMutableResult<
|
|
|
148
151
|
// >(
|
|
149
152
|
// value?: TConfig["dialect"] extends ReturningIdDialects ? never : TValue
|
|
150
153
|
// ): Omit<ModelMutateResult<TResult, TConfig, TResultType>, "with">;
|
|
151
|
-
$return: MethodReturnResult<TConfig>;
|
|
154
|
+
// $return: MethodReturnResult<TConfig>;
|
|
152
155
|
|
|
153
156
|
omit<TValue extends MethodExcludeValue<TConfig["tableOutput"]>>(
|
|
154
157
|
value: TValue
|