@apisr/drizzle-model 0.0.1 → 0.0.2
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.d.mts +3 -0
- package/dist/index.mjs +4 -0
- package/dist/model/builder.d.mts +20 -0
- package/dist/model/builder.mjs +18 -0
- package/dist/model/config.d.mts +23 -0
- package/dist/model/core/joins.mjs +184 -0
- package/dist/model/core/projection.mjs +28 -0
- package/dist/model/core/runtime.mjs +198 -0
- package/dist/model/core/thenable.mjs +64 -0
- package/dist/model/core/transform.mjs +39 -0
- package/dist/model/core/where.mjs +130 -0
- package/dist/model/core/with.mjs +19 -0
- package/dist/model/dialect.d.mts +12 -0
- package/dist/model/format.d.mts +4 -0
- package/dist/model/index.d.mts +1 -0
- package/dist/model/index.mjs +3 -0
- package/dist/model/methods/exclude.d.mts +8 -0
- package/dist/model/methods/include.d.mts +6 -0
- package/dist/model/methods/insert.d.mts +7 -0
- package/dist/model/methods/query/where.d.mts +14 -0
- package/dist/model/methods/return.d.mts +12 -0
- package/dist/model/methods/select.d.mts +8 -0
- package/dist/model/methods/update.d.mts +7 -0
- package/dist/model/methods/upsert.d.mts +26 -0
- package/dist/model/methods/with.d.mts +16 -0
- package/dist/model/model.d.mts +105 -0
- package/dist/model/options.d.mts +28 -0
- package/dist/model/query/operations.d.mts +66 -0
- package/dist/model/relation.d.mts +68 -0
- package/dist/model/result.d.mts +34 -0
- package/dist/model/table.d.mts +69 -0
- package/dist/types.d.mts +10 -0
- package/drizzle.config.ts +6 -6
- package/package.json +1 -1
- package/src/model/builder.ts +37 -37
- package/src/model/config.ts +28 -25
- package/src/model/core/joins.ts +337 -252
- package/src/model/core/projection.ts +49 -35
- package/src/model/core/runtime.ts +302 -221
- package/src/model/core/thenable.ts +70 -61
- package/src/model/core/transform.ts +53 -33
- package/src/model/core/where.ts +228 -162
- package/src/model/core/with.ts +21 -21
- package/src/model/dialect.ts +12 -2
- package/src/model/foreigns.ts +6 -10
- package/src/model/format.ts +8 -8
- package/src/model/methods/include.ts +1 -1
- package/src/model/methods/insert.ts +13 -10
- package/src/model/methods/levels.ts +7 -1
- package/src/model/methods/query/where.ts +49 -36
- package/src/model/methods/return.ts +13 -12
- package/src/model/methods/select.ts +29 -29
- package/src/model/methods/update.ts +3 -1
- package/src/model/methods/upsert.ts +35 -36
- package/src/model/model.ts +115 -107
- package/src/model/options.ts +44 -37
- package/src/model/query/operations.ts +73 -72
- package/src/model/relation.ts +47 -46
- package/src/model/result.ts +79 -63
- package/src/model/shape.ts +5 -4
- package/src/model/table.ts +34 -33
- package/src/types.ts +3 -3
- package/tests/builder-v2-mysql.type-test.ts +31 -20
- package/tests/builder-v2.type-test.ts +246 -253
- package/tests/builder.test.ts +1 -1
- package/tests/db.ts +3 -3
- package/tests/find.test.ts +149 -138
- package/tests/insert.test.ts +217 -203
- package/tests/relations.ts +34 -34
- package/tests/schema.ts +34 -35
package/src/model/relation.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
TableRelationalConfig,
|
|
3
|
+
TablesRelationalConfig,
|
|
4
4
|
} from "drizzle-orm/relations";
|
|
5
|
+
import type { RecursiveBooleanRecord } from "../types.ts";
|
|
5
6
|
import type { ModelIdentifier } from "./model.ts";
|
|
6
7
|
import type { IsTable, TableOutput } from "./table.ts";
|
|
7
|
-
import type { RecursiveBooleanRecord } from "../types.ts";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Defines the cardinality of a relation: "one" (hasOne) or "many" (hasMany).
|
|
@@ -19,8 +19,8 @@ export type RelationKind = "one" | "many";
|
|
|
19
19
|
* @typeParam T - The type to potentially wrap
|
|
20
20
|
*/
|
|
21
21
|
export type ApplyRelationCardinality<
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
Kind extends RelationKind,
|
|
23
|
+
T,
|
|
24
24
|
> = Kind extends "many" ? T[] : T;
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -30,8 +30,8 @@ export type ApplyRelationCardinality<
|
|
|
30
30
|
* @typeParam Key - Name of the relation field
|
|
31
31
|
*/
|
|
32
32
|
export type RelationMeta<
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
TTable extends TableRelationalConfig,
|
|
34
|
+
Key extends string,
|
|
35
35
|
> = TTable["relations"][Key];
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -41,8 +41,8 @@ export type RelationMeta<
|
|
|
41
41
|
* @typeParam Meta - Metadata object containing the target table name
|
|
42
42
|
*/
|
|
43
43
|
export type TargetTable<
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
TSchema extends TablesRelationalConfig,
|
|
45
|
+
Meta extends { targetTableName: string },
|
|
46
46
|
> = TSchema[Meta["targetTableName"]];
|
|
47
47
|
|
|
48
48
|
/**
|
|
@@ -53,11 +53,11 @@ export type TargetTable<
|
|
|
53
53
|
* @typeParam TTable - Relational configuration for the source table
|
|
54
54
|
*/
|
|
55
55
|
export type RelationTargetRow<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
Key extends string,
|
|
57
|
+
TSchema extends TablesRelationalConfig,
|
|
58
|
+
TTable extends TableRelationalConfig,
|
|
59
59
|
> = TableOutput<
|
|
60
|
-
|
|
60
|
+
IsTable<TargetTable<TSchema, RelationMeta<TTable, Key>>["table"]>
|
|
61
61
|
>;
|
|
62
62
|
|
|
63
63
|
/**
|
|
@@ -73,24 +73,25 @@ export type RelationTargetRow<
|
|
|
73
73
|
* @typeParam TTable - Relational configuration for the source table
|
|
74
74
|
*/
|
|
75
75
|
export type ResolveSingleRelation<
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
> =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
76
|
+
Key extends string,
|
|
77
|
+
Value,
|
|
78
|
+
TSchema extends TablesRelationalConfig,
|
|
79
|
+
TTable extends TableRelationalConfig,
|
|
80
|
+
> =
|
|
81
|
+
Value extends Record<string, any>
|
|
82
|
+
? ApplyRelationCardinality<
|
|
83
|
+
RelationMeta<TTable, Key>["relationType"],
|
|
84
|
+
ResolveRelationSelection<
|
|
85
|
+
Value,
|
|
86
|
+
TSchema,
|
|
87
|
+
TargetTable<TSchema, RelationMeta<TTable, Key>>
|
|
88
|
+
> &
|
|
89
|
+
RelationTargetRow<Key, TSchema, TTable>
|
|
90
|
+
>
|
|
91
|
+
: ApplyRelationCardinality<
|
|
92
|
+
RelationMeta<TTable, Key>["relationType"],
|
|
93
|
+
RelationTargetRow<Key, TSchema, TTable>
|
|
94
|
+
>;
|
|
94
95
|
|
|
95
96
|
/**
|
|
96
97
|
* Recursively resolves a selection object into a typed result object.
|
|
@@ -103,19 +104,19 @@ export type ResolveSingleRelation<
|
|
|
103
104
|
* @typeParam TTable - Relational configuration for the current table
|
|
104
105
|
*/
|
|
105
106
|
export type ResolveRelationSelection<
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
TSelection extends Record<string, any>,
|
|
108
|
+
TSchema extends TablesRelationalConfig,
|
|
109
|
+
TTable extends TableRelationalConfig,
|
|
109
110
|
> = {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
111
|
+
[Key in keyof TSelection as TSelection[Key] extends
|
|
112
|
+
| true
|
|
113
|
+
| RecursiveBooleanRecord
|
|
114
|
+
| ModelIdentifier<any>
|
|
115
|
+
? Key & string
|
|
116
|
+
: never]: ResolveSingleRelation<
|
|
117
|
+
Key & string,
|
|
118
|
+
TSelection[Key],
|
|
119
|
+
TSchema,
|
|
120
|
+
TTable
|
|
121
|
+
>;
|
|
122
|
+
};
|
package/src/model/result.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
TableRelationalConfig,
|
|
3
|
+
TablesRelationalConfig,
|
|
4
4
|
} from "drizzle-orm/relations";
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
5
|
+
import type { ModelConfig } from "./config.ts";
|
|
6
|
+
import type { ReturningIdDialects } from "./dialect.ts";
|
|
7
|
+
import type { ModelFormatValue } from "./format.ts";
|
|
7
8
|
import type {
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
MethodExcludeResult,
|
|
10
|
+
MethodExcludeValue,
|
|
10
11
|
} from "./methods/exclude.ts";
|
|
11
12
|
import type { MethodReturnResult } from "./methods/return.ts";
|
|
12
|
-
import type {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
import type {
|
|
14
|
+
MethodSelectResult,
|
|
15
|
+
MethodSelectValue,
|
|
16
|
+
} from "./methods/select.ts";
|
|
17
|
+
import type { MethodWithResult, MethodWithValue } from "./methods/with.ts";
|
|
15
18
|
import type { ResolveOptionsFormat } from "./options.ts";
|
|
16
|
-
import type {
|
|
19
|
+
import type { TableOutput } from "./table.ts";
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* Represents the result of a model operation (like findMany or findFirst).
|
|
@@ -26,66 +29,79 @@ import type { ModelFormatValue } from "./format.ts";
|
|
|
26
29
|
* @typeParam TTable - Relational configuration for the current table
|
|
27
30
|
*/
|
|
28
31
|
export interface ModelQueryResult<
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
TResult extends Record<string, any>,
|
|
33
|
+
TConfig extends ModelConfig,
|
|
34
|
+
TExcludedKeys extends string = string,
|
|
35
|
+
TSchema extends TablesRelationalConfig = TConfig["schema"],
|
|
36
|
+
TTable extends TableRelationalConfig = TConfig["table"],
|
|
37
|
+
TFormat extends Record<string, any> | undefined = ResolveOptionsFormat<
|
|
38
|
+
TConfig["options"]["format"]
|
|
39
|
+
>,
|
|
35
40
|
> extends Promise<ModelFormatValue<TResult, TFormat>> {
|
|
36
|
-
|
|
37
|
-
value: TValue,
|
|
38
|
-
): ModelQueryResult<
|
|
39
|
-
MethodWithResult<TValue, TResult, TSchema, TTable>,
|
|
40
|
-
TConfig,
|
|
41
|
-
TExcludeKeys
|
|
42
|
-
>,
|
|
43
|
-
|
|
44
|
-
select<TValue extends MethodSelectValue<TResult>, TExcludeKeys extends string = TExcludedKeys | "select">(
|
|
45
|
-
value: TValue,
|
|
46
|
-
): ModelQueryResult<
|
|
47
|
-
MethodSelectResult<TValue, TResult>,
|
|
48
|
-
TConfig,
|
|
49
|
-
TExcludeKeys
|
|
50
|
-
>,
|
|
41
|
+
debug(): any;
|
|
51
42
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
43
|
+
exclude<
|
|
44
|
+
TValue extends MethodExcludeValue<TResult>,
|
|
45
|
+
TExcludeKeys extends string = TExcludedKeys | "exclude",
|
|
46
|
+
>(
|
|
47
|
+
value: TValue
|
|
48
|
+
): ModelQueryResult<
|
|
49
|
+
MethodExcludeResult<TValue, TResult>,
|
|
50
|
+
TConfig,
|
|
51
|
+
TExcludeKeys
|
|
52
|
+
>;
|
|
59
53
|
|
|
60
|
-
|
|
54
|
+
raw<TExcludeKeys extends string = TExcludedKeys | "raw">(): ModelQueryResult<
|
|
55
|
+
TResult,
|
|
56
|
+
TConfig,
|
|
57
|
+
TExcludeKeys,
|
|
58
|
+
TSchema,
|
|
59
|
+
TTable,
|
|
60
|
+
undefined
|
|
61
|
+
>;
|
|
61
62
|
|
|
62
|
-
|
|
63
|
+
select<
|
|
64
|
+
TValue extends MethodSelectValue<TResult>,
|
|
65
|
+
TExcludeKeys extends string = TExcludedKeys | "select",
|
|
66
|
+
>(
|
|
67
|
+
value: TValue
|
|
68
|
+
): ModelQueryResult<
|
|
69
|
+
MethodSelectResult<TValue, TResult>,
|
|
70
|
+
TConfig,
|
|
71
|
+
TExcludeKeys
|
|
72
|
+
>;
|
|
73
|
+
with<
|
|
74
|
+
TValue extends MethodWithValue<TSchema, TTable["relations"]>,
|
|
75
|
+
TExcludeKeys extends string = TExcludedKeys | "with",
|
|
76
|
+
>(
|
|
77
|
+
value: TValue
|
|
78
|
+
): ModelQueryResult<
|
|
79
|
+
MethodWithResult<TValue, TResult, TSchema, TTable>,
|
|
80
|
+
TConfig,
|
|
81
|
+
TExcludeKeys
|
|
82
|
+
>;
|
|
63
83
|
}
|
|
64
84
|
|
|
65
85
|
export interface ModelMutateResult<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
86
|
+
TBaseResult extends Record<string, any> | void,
|
|
87
|
+
TConfig extends ModelConfig,
|
|
88
|
+
TResultType extends string = "one",
|
|
69
89
|
> extends Promise<TBaseResult> {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
90
|
+
// TODO: Planned for future
|
|
91
|
+
// with<TValue extends MethodWithInsertValue<TSchema, TTable["relations"]>>(
|
|
92
|
+
// value: TValue,
|
|
93
|
+
// ): void;
|
|
74
94
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
TResultType
|
|
88
|
-
>,
|
|
89
|
-
"with"
|
|
90
|
-
>;
|
|
95
|
+
return<
|
|
96
|
+
TValue extends MethodSelectValue<TableOutput<TConfig["table"]>> | undefined,
|
|
97
|
+
TReturnResult extends MethodReturnResult<
|
|
98
|
+
TResultType,
|
|
99
|
+
TConfig
|
|
100
|
+
> = MethodReturnResult<TResultType, TConfig>,
|
|
101
|
+
TResult extends Record<string, any> = TValue extends undefined
|
|
102
|
+
? TReturnResult
|
|
103
|
+
: MethodSelectResult<Exclude<TValue, undefined>, TReturnResult>,
|
|
104
|
+
>(
|
|
105
|
+
value?: TConfig["dialect"] extends ReturningIdDialects ? never : TValue
|
|
106
|
+
): Omit<ModelMutateResult<TResult, TConfig, TResultType>, "with">;
|
|
91
107
|
}
|
package/src/model/shape.ts
CHANGED
|
@@ -2,7 +2,8 @@ import type { ModelConfig } from "./config.ts";
|
|
|
2
2
|
import type { ModelBase, ModelIdentifier } from "./model.ts";
|
|
3
3
|
import type { ResolveOptionsMethods } from "./options.ts";
|
|
4
4
|
|
|
5
|
-
export type ModelShape<TConfig extends ModelConfig> =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
export type ModelShape<TConfig extends ModelConfig> = ModelIdentifier<
|
|
6
|
+
TConfig["table"]["name"]
|
|
7
|
+
> &
|
|
8
|
+
ModelBase<TConfig> &
|
|
9
|
+
ResolveOptionsMethods<TConfig["options"]["methods"]>;
|
package/src/model/table.ts
CHANGED
|
@@ -14,7 +14,9 @@ import type { SchemaEntry, TableRelationalConfig } from "drizzle-orm/relations";
|
|
|
14
14
|
*
|
|
15
15
|
* @typeParam TTable - Relational configuration for the table
|
|
16
16
|
*/
|
|
17
|
-
export type TableColumns<TTable extends TableRelationalConfig> = IsTable<
|
|
17
|
+
export type TableColumns<TTable extends TableRelationalConfig> = IsTable<
|
|
18
|
+
TTable["table"]
|
|
19
|
+
>["_"]["columns"];
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* Resolves a single column definition from a table by column name.
|
|
@@ -29,8 +31,8 @@ export type TableColumns<TTable extends TableRelationalConfig> = IsTable<TTable[
|
|
|
29
31
|
* @typeParam TTable - Relational configuration for the table
|
|
30
32
|
*/
|
|
31
33
|
export type TableColumn<
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
TColumnName extends string,
|
|
35
|
+
TTable extends TableRelationalConfig,
|
|
34
36
|
> = TableColumns<TTable>[TColumnName];
|
|
35
37
|
|
|
36
38
|
/**
|
|
@@ -38,8 +40,12 @@ export type TableColumn<
|
|
|
38
40
|
*
|
|
39
41
|
* @typeParam TTable - Relation config, schema entry, or table
|
|
40
42
|
*/
|
|
41
|
-
export type TableOutput<
|
|
42
|
-
|
|
43
|
+
export type TableOutput<
|
|
44
|
+
TTable extends TableRelationalConfig | SchemaEntry | Table,
|
|
45
|
+
> =
|
|
46
|
+
NormalizeTable<TTable> extends Table
|
|
47
|
+
? InferSelectModel<NormalizeTable<TTable>>
|
|
48
|
+
: never;
|
|
43
49
|
// Is relation?
|
|
44
50
|
// (TTable extends TableRelationalConfig
|
|
45
51
|
// ? InferSelectModel<IsTable<TTable["table"]>>
|
|
@@ -56,28 +62,27 @@ export type TableOutput<TTable extends TableRelationalConfig | SchemaEntry | Tab
|
|
|
56
62
|
*
|
|
57
63
|
* @typeParam TTable - Relation config, schema entry, or table
|
|
58
64
|
*/
|
|
59
|
-
export type NormalizeTable<
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
export type NormalizeTable<
|
|
66
|
+
TTable extends TableRelationalConfig | SchemaEntry | Table,
|
|
67
|
+
> =
|
|
68
|
+
// Is relation?
|
|
69
|
+
TTable extends TableRelationalConfig
|
|
70
|
+
? IsTable<TTable["table"]>
|
|
71
|
+
: TTable extends SchemaEntry
|
|
72
|
+
? // Is schema entry (view/table)?
|
|
73
|
+
IsTable<TTable>
|
|
74
|
+
: TTable extends Table
|
|
75
|
+
? // Is Table?
|
|
76
|
+
TTable
|
|
77
|
+
: never;
|
|
70
78
|
|
|
71
79
|
/**
|
|
72
80
|
* Collects target table names from all relations.
|
|
73
81
|
*
|
|
74
82
|
* @typeParam TTable - Relational configuration for the table
|
|
75
83
|
*/
|
|
76
|
-
export type TableRelationsTableName<
|
|
77
|
-
|
|
78
|
-
> = {
|
|
79
|
-
[K in keyof TTable["relations"]]:
|
|
80
|
-
TTable["relations"][K]["targetTableName"]
|
|
84
|
+
export type TableRelationsTableName<TTable extends TableRelationalConfig> = {
|
|
85
|
+
[K in keyof TTable["relations"]]: TTable["relations"][K]["targetTableName"];
|
|
81
86
|
}[keyof TTable["relations"]];
|
|
82
87
|
|
|
83
88
|
/**
|
|
@@ -86,11 +91,9 @@ export type TableRelationsTableName<
|
|
|
86
91
|
* @typeParam TTable - Relational configuration for the table
|
|
87
92
|
*/
|
|
88
93
|
type TableOneRelationsMap<TTable extends TableRelationalConfig> = {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
: K
|
|
93
|
-
]: TTable["relations"][K]["targetTableName"]
|
|
94
|
+
[K in keyof TTable["relations"] as TTable["relations"][K]["relationType"] extends "many"
|
|
95
|
+
? never
|
|
96
|
+
: K]: TTable["relations"][K]["targetTableName"];
|
|
94
97
|
};
|
|
95
98
|
|
|
96
99
|
/**
|
|
@@ -98,9 +101,8 @@ type TableOneRelationsMap<TTable extends TableRelationalConfig> = {
|
|
|
98
101
|
*
|
|
99
102
|
* @typeParam TTable - Relational configuration for the table
|
|
100
103
|
*/
|
|
101
|
-
export type TableOneRelationsTableName<
|
|
102
|
-
|
|
103
|
-
> = TableOneRelationsMap<TTable>[keyof TableOneRelationsMap<TTable>];
|
|
104
|
+
export type TableOneRelationsTableName<TTable extends TableRelationalConfig> =
|
|
105
|
+
TableOneRelationsMap<TTable>[keyof TableOneRelationsMap<TTable>];
|
|
104
106
|
|
|
105
107
|
/**
|
|
106
108
|
* Narrows a type to a Drizzle table when possible.
|
|
@@ -114,8 +116,7 @@ export type IsTable<T> = T extends Table ? T : never;
|
|
|
114
116
|
*
|
|
115
117
|
* @typeParam TTable - Drizzle table type
|
|
116
118
|
*/
|
|
117
|
-
export type TableInsertModel<TTable extends Table> =
|
|
118
|
-
InferInsertModel<TTable>;
|
|
119
|
+
export type TableInsertModel<TTable extends Table> = InferInsertModel<TTable>;
|
|
119
120
|
|
|
120
121
|
/**
|
|
121
122
|
* Accepts a single insert payload or a batch of insert payloads.
|
|
@@ -123,5 +124,5 @@ export type TableInsertModel<TTable extends Table> =
|
|
|
123
124
|
* @typeParam TTable - Drizzle table type
|
|
124
125
|
*/
|
|
125
126
|
export type TableInsertValues<TTable extends Table> =
|
|
126
|
-
|
|
127
|
-
|
|
127
|
+
| InferInsertModel<TTable>
|
|
128
|
+
| InferInsertModel<TTable>[];
|
package/src/types.ts
CHANGED
|
@@ -4,13 +4,13 @@ export type Replace<T, R> = Omit<T, keyof R> & R;
|
|
|
4
4
|
export type UnwrapArray<T> = T extends (infer R)[] ? R : T;
|
|
5
5
|
|
|
6
6
|
export type AddToValues<T extends Record<PropertyKey, any>, A> = {
|
|
7
|
-
|
|
7
|
+
[K in keyof T]: T[K] & A;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
export type AddUnionToValues<T extends Record<PropertyKey, any>, A> = {
|
|
11
|
-
|
|
11
|
+
[K in keyof T]: T[K] | A;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export interface RecursiveBooleanRecord {
|
|
15
|
-
|
|
15
|
+
[key: string]: boolean | RecursiveBooleanRecord;
|
|
16
16
|
}
|
|
@@ -1,40 +1,51 @@
|
|
|
1
|
-
import { mysqlDb } from "./db";
|
|
2
|
-
import { modelBuilder } from "src/model";
|
|
3
|
-
import { boolean, int, text, mysqlTable, varchar } from 'drizzle-orm/mysql-core';
|
|
4
1
|
import { defineRelations } from "drizzle-orm";
|
|
2
|
+
import {
|
|
3
|
+
boolean,
|
|
4
|
+
int,
|
|
5
|
+
mysqlTable,
|
|
6
|
+
text,
|
|
7
|
+
varchar,
|
|
8
|
+
} from "drizzle-orm/mysql-core";
|
|
9
|
+
import { modelBuilder } from "src/model";
|
|
5
10
|
import { esc } from "@/model/query/operations";
|
|
11
|
+
import { mysqlDb } from "./db";
|
|
6
12
|
|
|
7
|
-
const usersTable = mysqlTable(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
const usersTable = mysqlTable("users", {
|
|
14
|
+
id: int("id").primaryKey(),
|
|
15
|
+
name: text("name").notNull(),
|
|
16
|
+
verified: boolean("verified").notNull().default(false),
|
|
11
17
|
});
|
|
12
18
|
|
|
13
|
-
const usersTableDefFn = mysqlTable(
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
const usersTableDefFn = mysqlTable("users_default_fn", {
|
|
20
|
+
customId: varchar("id", { length: 256 })
|
|
21
|
+
.primaryKey()
|
|
22
|
+
.$defaultFn(() => "123"),
|
|
23
|
+
name: text("name").notNull(),
|
|
16
24
|
});
|
|
17
25
|
|
|
18
26
|
const schema = {
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
usersTable,
|
|
28
|
+
usersTableDefFn,
|
|
21
29
|
};
|
|
22
30
|
|
|
23
31
|
const relations = defineRelations(schema, (r) => ({
|
|
24
|
-
|
|
32
|
+
usersTable: {},
|
|
25
33
|
}));
|
|
26
34
|
|
|
27
35
|
const model = modelBuilder({
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
schema,
|
|
37
|
+
db: mysqlDb,
|
|
38
|
+
relations,
|
|
39
|
+
dialect: "MySQL",
|
|
32
40
|
});
|
|
33
41
|
|
|
34
42
|
const userModel = model("usersTableDefFn", {});
|
|
35
43
|
|
|
36
|
-
const testRaw1 = await userModel
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
const testRaw1 = await userModel
|
|
45
|
+
.where({
|
|
46
|
+
customId: esc("123"),
|
|
47
|
+
})
|
|
48
|
+
.delete()
|
|
49
|
+
.return();
|
|
39
50
|
|
|
40
51
|
// testRaw1[0].
|