@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.
Files changed (70) hide show
  1. package/dist/index.d.mts +3 -0
  2. package/dist/index.mjs +4 -0
  3. package/dist/model/builder.d.mts +20 -0
  4. package/dist/model/builder.mjs +18 -0
  5. package/dist/model/config.d.mts +23 -0
  6. package/dist/model/core/joins.mjs +184 -0
  7. package/dist/model/core/projection.mjs +28 -0
  8. package/dist/model/core/runtime.mjs +198 -0
  9. package/dist/model/core/thenable.mjs +64 -0
  10. package/dist/model/core/transform.mjs +39 -0
  11. package/dist/model/core/where.mjs +130 -0
  12. package/dist/model/core/with.mjs +19 -0
  13. package/dist/model/dialect.d.mts +12 -0
  14. package/dist/model/format.d.mts +4 -0
  15. package/dist/model/index.d.mts +1 -0
  16. package/dist/model/index.mjs +3 -0
  17. package/dist/model/methods/exclude.d.mts +8 -0
  18. package/dist/model/methods/include.d.mts +6 -0
  19. package/dist/model/methods/insert.d.mts +7 -0
  20. package/dist/model/methods/query/where.d.mts +14 -0
  21. package/dist/model/methods/return.d.mts +12 -0
  22. package/dist/model/methods/select.d.mts +8 -0
  23. package/dist/model/methods/update.d.mts +7 -0
  24. package/dist/model/methods/upsert.d.mts +26 -0
  25. package/dist/model/methods/with.d.mts +16 -0
  26. package/dist/model/model.d.mts +105 -0
  27. package/dist/model/options.d.mts +28 -0
  28. package/dist/model/query/operations.d.mts +66 -0
  29. package/dist/model/relation.d.mts +68 -0
  30. package/dist/model/result.d.mts +34 -0
  31. package/dist/model/table.d.mts +69 -0
  32. package/dist/types.d.mts +10 -0
  33. package/drizzle.config.ts +6 -6
  34. package/package.json +1 -1
  35. package/src/model/builder.ts +37 -37
  36. package/src/model/config.ts +28 -25
  37. package/src/model/core/joins.ts +337 -252
  38. package/src/model/core/projection.ts +49 -35
  39. package/src/model/core/runtime.ts +302 -221
  40. package/src/model/core/thenable.ts +70 -61
  41. package/src/model/core/transform.ts +53 -33
  42. package/src/model/core/where.ts +228 -162
  43. package/src/model/core/with.ts +21 -21
  44. package/src/model/dialect.ts +12 -2
  45. package/src/model/foreigns.ts +6 -10
  46. package/src/model/format.ts +8 -8
  47. package/src/model/methods/include.ts +1 -1
  48. package/src/model/methods/insert.ts +13 -10
  49. package/src/model/methods/levels.ts +7 -1
  50. package/src/model/methods/query/where.ts +49 -36
  51. package/src/model/methods/return.ts +13 -12
  52. package/src/model/methods/select.ts +29 -29
  53. package/src/model/methods/update.ts +3 -1
  54. package/src/model/methods/upsert.ts +35 -36
  55. package/src/model/model.ts +115 -107
  56. package/src/model/options.ts +44 -37
  57. package/src/model/query/operations.ts +73 -72
  58. package/src/model/relation.ts +47 -46
  59. package/src/model/result.ts +79 -63
  60. package/src/model/shape.ts +5 -4
  61. package/src/model/table.ts +34 -33
  62. package/src/types.ts +3 -3
  63. package/tests/builder-v2-mysql.type-test.ts +31 -20
  64. package/tests/builder-v2.type-test.ts +246 -253
  65. package/tests/builder.test.ts +1 -1
  66. package/tests/db.ts +3 -3
  67. package/tests/find.test.ts +149 -138
  68. package/tests/insert.test.ts +217 -203
  69. package/tests/relations.ts +34 -34
  70. package/tests/schema.ts +34 -35
@@ -1,10 +1,10 @@
1
1
  import type {
2
- TableRelationalConfig,
3
- TablesRelationalConfig,
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
- Kind extends RelationKind,
23
- T,
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
- TTable extends TableRelationalConfig,
34
- Key extends string,
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
- TSchema extends TablesRelationalConfig,
45
- Meta extends { targetTableName: string; },
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
- Key extends string,
57
- TSchema extends TablesRelationalConfig,
58
- TTable extends TableRelationalConfig,
56
+ Key extends string,
57
+ TSchema extends TablesRelationalConfig,
58
+ TTable extends TableRelationalConfig,
59
59
  > = TableOutput<
60
- IsTable<TargetTable<TSchema, RelationMeta<TTable, Key>>["table"]>
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
- Key extends string,
77
- Value,
78
- TSchema extends TablesRelationalConfig,
79
- TTable extends TableRelationalConfig,
80
- > = Value extends Record<string, any>
81
- ? ApplyRelationCardinality<
82
- RelationMeta<TTable, Key>["relationType"],
83
- ResolveRelationSelection<
84
- Value,
85
- TSchema,
86
- TargetTable<TSchema, RelationMeta<TTable, Key>>
87
- > &
88
- RelationTargetRow<Key, TSchema, TTable>
89
- >
90
- : ApplyRelationCardinality<
91
- RelationMeta<TTable, Key>["relationType"],
92
- RelationTargetRow<Key, TSchema, TTable>
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
- TSelection extends Record<string, any>,
107
- TSchema extends TablesRelationalConfig,
108
- TTable extends TableRelationalConfig,
107
+ TSelection extends Record<string, any>,
108
+ TSchema extends TablesRelationalConfig,
109
+ TTable extends TableRelationalConfig,
109
110
  > = {
110
- [Key in keyof TSelection as TSelection[Key] extends
111
- | true
112
- | RecursiveBooleanRecord
113
- | ModelIdentifier<any>
114
- ? Key & string
115
- : never]: ResolveSingleRelation<
116
- Key & string,
117
- TSelection[Key],
118
- TSchema,
119
- TTable
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
+ };
@@ -1,19 +1,22 @@
1
1
  import type {
2
- TableRelationalConfig,
3
- TablesRelationalConfig,
2
+ TableRelationalConfig,
3
+ TablesRelationalConfig,
4
4
  } from "drizzle-orm/relations";
5
- import type { MethodWithResult, MethodWithValue } from "./methods/with.ts";
6
- import type { MethodSelectResult, MethodSelectValue } from "./methods/select.ts";
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
- MethodExcludeResult,
9
- MethodExcludeValue,
9
+ MethodExcludeResult,
10
+ MethodExcludeValue,
10
11
  } from "./methods/exclude.ts";
11
12
  import type { MethodReturnResult } from "./methods/return.ts";
12
- import type { ReturningIdDialects } from "./dialect.ts";
13
- import type { TableOutput } from "./table.ts";
14
- import type { ModelConfig } from "./config.ts";
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 { ModelFormatValue } from "./format.ts";
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
- TResult extends Record<string, any>,
30
- TConfig extends ModelConfig,
31
- TExcludedKeys extends string = string,
32
- TSchema extends TablesRelationalConfig = TConfig["schema"],
33
- TTable extends TableRelationalConfig = TConfig["table"],
34
- TFormat extends Record<string, any> | undefined = ResolveOptionsFormat<TConfig["options"]["format"]>
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
- with<TValue extends MethodWithValue<TSchema, TTable["relations"]>, TExcludeKeys extends string = TExcludedKeys | "with">(
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
- exclude<TValue extends MethodExcludeValue<TResult>, TExcludeKeys extends string = TExcludedKeys | "exclude">(
53
- value: TValue,
54
- ): ModelQueryResult<
55
- MethodExcludeResult<TValue, TResult>,
56
- TConfig,
57
- TExcludeKeys
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
- raw<TExcludeKeys extends string = TExcludedKeys | "raw">(): ModelQueryResult<TResult, TConfig, TExcludeKeys, TSchema, TTable, undefined>;
54
+ raw<TExcludeKeys extends string = TExcludedKeys | "raw">(): ModelQueryResult<
55
+ TResult,
56
+ TConfig,
57
+ TExcludeKeys,
58
+ TSchema,
59
+ TTable,
60
+ undefined
61
+ >;
61
62
 
62
- debug(): any;
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
- TBaseResult extends Record<string, any> | void,
67
- TConfig extends ModelConfig,
68
- TResultType extends string = "one"
86
+ TBaseResult extends Record<string, any> | void,
87
+ TConfig extends ModelConfig,
88
+ TResultType extends string = "one",
69
89
  > extends Promise<TBaseResult> {
70
- // TODO: Planned for future
71
- // with<TValue extends MethodWithInsertValue<TSchema, TTable["relations"]>>(
72
- // value: TValue,
73
- // ): void;
90
+ // TODO: Planned for future
91
+ // with<TValue extends MethodWithInsertValue<TSchema, TTable["relations"]>>(
92
+ // value: TValue,
93
+ // ): void;
74
94
 
75
- return<
76
- TValue extends MethodSelectValue<TableOutput<TConfig["table"]>> | undefined,
77
- TReturnResult extends MethodReturnResult<TResultType, TConfig> = MethodReturnResult<TResultType, TConfig>,
78
- TResult extends Record<string, any> = TValue extends undefined ? TReturnResult : MethodSelectResult<Exclude<TValue, undefined>, TReturnResult>,
79
- >(
80
- value?: TConfig["dialect"] extends ReturningIdDialects
81
- ? never
82
- : TValue,
83
- ): Omit<
84
- ModelMutateResult<
85
- TResult,
86
- TConfig,
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
  }
@@ -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
- ModelIdentifier<TConfig["table"]["name"]> &
7
- ModelBase<TConfig> &
8
- ResolveOptionsMethods<TConfig["options"]["methods"]>;
5
+ export type ModelShape<TConfig extends ModelConfig> = ModelIdentifier<
6
+ TConfig["table"]["name"]
7
+ > &
8
+ ModelBase<TConfig> &
9
+ ResolveOptionsMethods<TConfig["options"]["methods"]>;
@@ -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<TTable["table"]>["_"]["columns"];
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
- TColumnName extends string,
33
- TTable extends TableRelationalConfig,
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<TTable extends TableRelationalConfig | SchemaEntry | Table> =
42
- NormalizeTable<TTable> extends Table ? InferSelectModel<NormalizeTable<TTable>> : never;
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<TTable extends TableRelationalConfig | SchemaEntry | Table> =
60
- // Is relation?
61
- (TTable extends TableRelationalConfig
62
- ? IsTable<TTable["table"]>
63
- : (TTable extends SchemaEntry
64
- // Is schema entry (view/table)?
65
- ? IsTable<TTable>
66
- : (TTable extends Table
67
- // Is Table?
68
- ? TTable
69
- : never)));
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
- TTable extends TableRelationalConfig
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
- [K in keyof TTable["relations"]as
90
- TTable["relations"][K]["relationType"] extends "many"
91
- ? never
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
- TTable extends TableRelationalConfig
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
- | InferInsertModel<TTable>
127
- | InferInsertModel<TTable>[];
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
- [K in keyof T]: T[K] & A
7
+ [K in keyof T]: T[K] & A;
8
8
  };
9
9
 
10
10
  export type AddUnionToValues<T extends Record<PropertyKey, any>, A> = {
11
- [K in keyof T]: T[K] | A
11
+ [K in keyof T]: T[K] | A;
12
12
  };
13
13
 
14
14
  export interface RecursiveBooleanRecord {
15
- [key: string]: boolean | RecursiveBooleanRecord;
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('users', {
8
- id: int('id').primaryKey(),
9
- name: text('name').notNull(),
10
- verified: boolean('verified').notNull().default(false),
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('users_default_fn', {
14
- customId: varchar('id', { length: 256 }).primaryKey().$defaultFn(() => "123"),
15
- name: text('name').notNull(),
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
- usersTable,
20
- usersTableDefFn
27
+ usersTable,
28
+ usersTableDefFn,
21
29
  };
22
30
 
23
31
  const relations = defineRelations(schema, (r) => ({
24
- usersTable: {}
32
+ usersTable: {},
25
33
  }));
26
34
 
27
35
  const model = modelBuilder({
28
- schema,
29
- db: mysqlDb,
30
- relations,
31
- dialect: "MySQL"
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.where({
37
- customId: esc("123")
38
- }).delete().return();
44
+ const testRaw1 = await userModel
45
+ .where({
46
+ customId: esc("123"),
47
+ })
48
+ .delete()
49
+ .return();
39
50
 
40
51
  // testRaw1[0].