neoorm 0.8.2 → 0.8.4

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 (147) hide show
  1. package/dist/bin/neoorm.js +22 -50
  2. package/dist/bin/neoorm.js.map +1 -1
  3. package/dist/codegen/column-ts.d.ts +41 -0
  4. package/dist/codegen/column-ts.d.ts.map +1 -0
  5. package/dist/codegen/column-ts.js +269 -0
  6. package/dist/codegen/column-ts.js.map +1 -0
  7. package/dist/codegen/emit-includes.d.ts.map +1 -1
  8. package/dist/codegen/emit-includes.js +13 -4
  9. package/dist/codegen/emit-includes.js.map +1 -1
  10. package/dist/codegen/emit-models.d.ts +2 -1
  11. package/dist/codegen/emit-models.d.ts.map +1 -1
  12. package/dist/codegen/emit-models.js +100 -38
  13. package/dist/codegen/emit-models.js.map +1 -1
  14. package/dist/codegen/emit-query-types.d.ts +3 -0
  15. package/dist/codegen/emit-query-types.d.ts.map +1 -0
  16. package/dist/codegen/emit-query-types.js +638 -0
  17. package/dist/codegen/emit-query-types.js.map +1 -0
  18. package/dist/codegen/generate.d.ts +2 -2
  19. package/dist/codegen/generate.d.ts.map +1 -1
  20. package/dist/codegen/generate.js +12 -32
  21. package/dist/codegen/generate.js.map +1 -1
  22. package/dist/dialect/mariadb.test.js +31 -6
  23. package/dist/dialect/mariadb.test.js.map +1 -1
  24. package/dist/dialect/mysql-family.d.ts +7 -0
  25. package/dist/dialect/mysql-family.d.ts.map +1 -1
  26. package/dist/dialect/mysql-family.js +25 -3
  27. package/dist/dialect/mysql-family.js.map +1 -1
  28. package/dist/dialect/mysql.test.js +37 -5
  29. package/dist/dialect/mysql.test.js.map +1 -1
  30. package/dist/dialect/postgres.d.ts.map +1 -1
  31. package/dist/dialect/postgres.js +1 -0
  32. package/dist/dialect/postgres.js.map +1 -1
  33. package/dist/dialect/sqlite.d.ts.map +1 -1
  34. package/dist/dialect/sqlite.js +2 -1
  35. package/dist/dialect/sqlite.js.map +1 -1
  36. package/dist/dialect/types.d.ts +2 -0
  37. package/dist/dialect/types.d.ts.map +1 -1
  38. package/dist/init/prompt.d.ts +21 -0
  39. package/dist/init/prompt.d.ts.map +1 -0
  40. package/dist/init/prompt.js +127 -0
  41. package/dist/init/prompt.js.map +1 -0
  42. package/dist/init/scaffold.d.ts +9 -0
  43. package/dist/init/scaffold.d.ts.map +1 -1
  44. package/dist/init/scaffold.js +22 -13
  45. package/dist/init/scaffold.js.map +1 -1
  46. package/dist/init/templates.d.ts +1 -0
  47. package/dist/init/templates.d.ts.map +1 -1
  48. package/dist/init/templates.js +1 -1
  49. package/dist/init/templates.js.map +1 -1
  50. package/dist/init/validate.d.ts +12 -0
  51. package/dist/init/validate.d.ts.map +1 -0
  52. package/dist/init/validate.js +51 -0
  53. package/dist/init/validate.js.map +1 -0
  54. package/dist/runtime/client.d.ts +13 -7
  55. package/dist/runtime/client.d.ts.map +1 -1
  56. package/dist/runtime/client.js +3 -1
  57. package/dist/runtime/client.js.map +1 -1
  58. package/dist/runtime/client.test.js +1 -0
  59. package/dist/runtime/client.test.js.map +1 -1
  60. package/dist/runtime/driver-sqlite.test.js +12 -0
  61. package/dist/runtime/driver-sqlite.test.js.map +1 -1
  62. package/dist/runtime/driver.d.ts.map +1 -1
  63. package/dist/runtime/driver.js +16 -8
  64. package/dist/runtime/driver.js.map +1 -1
  65. package/dist/runtime/executor.d.ts +1 -0
  66. package/dist/runtime/executor.d.ts.map +1 -1
  67. package/dist/runtime/executor.js +12 -5
  68. package/dist/runtime/executor.js.map +1 -1
  69. package/dist/runtime/mariadb-driver.d.ts.map +1 -1
  70. package/dist/runtime/mariadb-driver.js +50 -10
  71. package/dist/runtime/mariadb-driver.js.map +1 -1
  72. package/dist/runtime/mysql-driver.d.ts.map +1 -1
  73. package/dist/runtime/mysql-driver.js +8 -4
  74. package/dist/runtime/mysql-driver.js.map +1 -1
  75. package/dist/runtime/mysql-family-pool.test.js +178 -6
  76. package/dist/runtime/mysql-family-pool.test.js.map +1 -1
  77. package/dist/runtime/query/aggregate.d.ts.map +1 -1
  78. package/dist/runtime/query/aggregate.js +33 -2
  79. package/dist/runtime/query/aggregate.js.map +1 -1
  80. package/dist/runtime/query/compile-aggregate.d.ts +1 -0
  81. package/dist/runtime/query/compile-aggregate.d.ts.map +1 -1
  82. package/dist/runtime/query/compile-aggregate.js +12 -3
  83. package/dist/runtime/query/compile-aggregate.js.map +1 -1
  84. package/dist/runtime/query/compile-where.d.ts.map +1 -1
  85. package/dist/runtime/query/compile-where.js +6 -1
  86. package/dist/runtime/query/compile-where.js.map +1 -1
  87. package/dist/runtime/query/compile-write.d.ts +8 -2
  88. package/dist/runtime/query/compile-write.d.ts.map +1 -1
  89. package/dist/runtime/query/compile-write.js +100 -8
  90. package/dist/runtime/query/compile-write.js.map +1 -1
  91. package/dist/runtime/query/compile.d.ts +2 -2
  92. package/dist/runtime/query/compile.d.ts.map +1 -1
  93. package/dist/runtime/query/compile.js +2 -2
  94. package/dist/runtime/query/compile.js.map +1 -1
  95. package/dist/runtime/query/create.d.ts.map +1 -1
  96. package/dist/runtime/query/create.js +19 -6
  97. package/dist/runtime/query/create.js.map +1 -1
  98. package/dist/runtime/query/delete.d.ts.map +1 -1
  99. package/dist/runtime/query/delete.js +45 -3
  100. package/dist/runtime/query/delete.js.map +1 -1
  101. package/dist/runtime/query/find.d.ts.map +1 -1
  102. package/dist/runtime/query/find.js +43 -15
  103. package/dist/runtime/query/find.js.map +1 -1
  104. package/dist/runtime/query/map-row.d.ts.map +1 -1
  105. package/dist/runtime/query/map-row.js +11 -2
  106. package/dist/runtime/query/map-row.js.map +1 -1
  107. package/dist/runtime/query/mutation-returning.d.ts +3 -1
  108. package/dist/runtime/query/mutation-returning.d.ts.map +1 -1
  109. package/dist/runtime/query/mutation-returning.js +22 -0
  110. package/dist/runtime/query/mutation-returning.js.map +1 -1
  111. package/dist/runtime/query/relation-planner.d.ts +2 -0
  112. package/dist/runtime/query/relation-planner.d.ts.map +1 -1
  113. package/dist/runtime/query/relation-planner.js +8 -2
  114. package/dist/runtime/query/relation-planner.js.map +1 -1
  115. package/dist/runtime/query/relation-writes.d.ts.map +1 -1
  116. package/dist/runtime/query/relation-writes.js +7 -2
  117. package/dist/runtime/query/relation-writes.js.map +1 -1
  118. package/dist/runtime/query/strip.d.ts.map +1 -1
  119. package/dist/runtime/query/strip.js +23 -3
  120. package/dist/runtime/query/strip.js.map +1 -1
  121. package/dist/runtime/query/table-index.d.ts +4 -0
  122. package/dist/runtime/query/table-index.d.ts.map +1 -1
  123. package/dist/runtime/query/table-index.js +13 -1
  124. package/dist/runtime/query/table-index.js.map +1 -1
  125. package/dist/runtime/query/unique.d.ts +6 -0
  126. package/dist/runtime/query/unique.d.ts.map +1 -1
  127. package/dist/runtime/query/unique.js +40 -4
  128. package/dist/runtime/query/unique.js.map +1 -1
  129. package/dist/runtime/query/unique.test.js +15 -1
  130. package/dist/runtime/query/unique.test.js.map +1 -1
  131. package/dist/runtime/query/update.d.ts.map +1 -1
  132. package/dist/runtime/query/update.js +100 -29
  133. package/dist/runtime/query/update.js.map +1 -1
  134. package/dist/runtime/transaction.test.js +22 -1
  135. package/dist/runtime/transaction.test.js.map +1 -1
  136. package/dist/runtime/types.d.ts +6 -6
  137. package/dist/runtime/types.d.ts.map +1 -1
  138. package/dist/schema/relation-types.d.ts +5 -5
  139. package/dist/schema/relation-types.d.ts.map +1 -1
  140. package/dist/schema/types.d.ts +3 -3
  141. package/dist/schema/types.d.ts.map +1 -1
  142. package/docs/cli.md +6 -4
  143. package/docs/getting-started.md +2 -0
  144. package/docs/mariadb.md +10 -6
  145. package/docs/mysql.md +4 -2
  146. package/docs/queries.md +15 -1
  147. package/package.json +2 -1
@@ -0,0 +1,638 @@
1
+ import { findFkReferencedColumn } from "../dialect/fk.js";
2
+ import { buildEnumRegistry, columnNonNullTsType, columnTsType, isCreateRequired, isJunctionTable, isOmittedOnCreate, isOmittedOnUpdate, pkTsNames, uniqueKeyCandidates, updateScalarTsType, } from "./column-ts.js";
3
+ import { effectiveRelations, modelTypeName } from "./manifest-relations.js";
4
+ const STRING_FILTER_KINDS = new Set([
5
+ "id",
6
+ "text",
7
+ "citext",
8
+ "uuid",
9
+ "enum",
10
+ "date",
11
+ "time",
12
+ "interval",
13
+ "inet",
14
+ "cidr",
15
+ "xml",
16
+ "money",
17
+ "int4Range",
18
+ "int8Range",
19
+ "numRange",
20
+ "tsRange",
21
+ "tstzRange",
22
+ "dateRange",
23
+ ]);
24
+ const COMPARABLE_FILTER_KINDS = new Set([
25
+ "int",
26
+ "serial",
27
+ "real",
28
+ "double",
29
+ "decimal",
30
+ "timestamp",
31
+ "bool",
32
+ "bigint",
33
+ ]);
34
+ function effectiveKind(col, manifest) {
35
+ if (col.kind !== "fk")
36
+ return col.kind;
37
+ const referenced = findFkReferencedColumn(col, manifest);
38
+ if (referenced && referenced.kind !== "fk")
39
+ return referenced.kind;
40
+ return "text";
41
+ }
42
+ function filterForColumn(col, manifest, nonNullTs) {
43
+ const kind = effectiveKind(col, manifest);
44
+ if (kind === "json" || kind === "jsonb") {
45
+ return `JsonFilter<${nonNullTs}>`;
46
+ }
47
+ if (kind === "geometry" || kind === "geography" || kind === "point") {
48
+ return "SpatialFilter";
49
+ }
50
+ if (STRING_FILTER_KINDS.has(kind)) {
51
+ return `StringFilter<${nonNullTs}>`;
52
+ }
53
+ if (COMPARABLE_FILTER_KINDS.has(kind)) {
54
+ return `ComparableFilter<${nonNullTs}>`;
55
+ }
56
+ return `EqualsFilter<${nonNullTs}>`;
57
+ }
58
+ function whereFieldType(col, manifest, enums, accessor) {
59
+ const full = columnTsType(col, manifest, enums, accessor);
60
+ const nonNull = columnNonNullTsType(col, manifest, enums, accessor);
61
+ const filter = filterForColumn(col, manifest, nonNull);
62
+ return `${full} | ${filter}`;
63
+ }
64
+ function emitWhereType(manifest, table, enums) {
65
+ const name = `${modelTypeName(table.accessor)}Where`;
66
+ const lines = [];
67
+ lines.push(`export interface ${name} {`);
68
+ lines.push(` AND?: ${name}[];`);
69
+ lines.push(` OR?: ${name}[];`);
70
+ lines.push(` NOT?: ${name};`);
71
+ for (const col of table.columns) {
72
+ lines.push(` ${col.tsName}?: ${whereFieldType(col, manifest, enums, table.accessor)};`);
73
+ }
74
+ for (const rel of effectiveRelations(manifest, table)) {
75
+ const targetWhere = `${modelTypeName(rel.targetAccessor)}Where`;
76
+ if (rel.cardinality === "many") {
77
+ lines.push(` ${rel.name}?: { some?: ${targetWhere}; every?: ${targetWhere}; none?: ${targetWhere} };`);
78
+ }
79
+ else {
80
+ lines.push(` ${rel.name}?: ${targetWhere};`);
81
+ }
82
+ }
83
+ lines.push(`}`);
84
+ return lines.join("\n");
85
+ }
86
+ function emitWhereUniqueType(manifest, table, enums) {
87
+ const name = `${modelTypeName(table.accessor)}WhereUnique`;
88
+ const candidates = uniqueKeyCandidates(table);
89
+ if (candidates.length === 0) {
90
+ return `export type ${name} = Record<string, unknown>;`;
91
+ }
92
+ const bySql = new Map(table.columns.map((c) => [c.sqlName, c]));
93
+ const byTs = new Map(table.columns.map((c) => [c.tsName, c]));
94
+ const members = candidates.map((keys) => {
95
+ const fields = keys.map((tsName) => {
96
+ const col = byTs.get(tsName) ??
97
+ (bySql.get(tsName)
98
+ ? table.columns.find((c) => c.sqlName === tsName)
99
+ : undefined);
100
+ const ts = col
101
+ ? columnTsType(col, manifest, enums, table.accessor)
102
+ : "string";
103
+ return `${tsName}: UniqueEq<${ts}>`;
104
+ });
105
+ return `{ ${fields.join("; ")} }`;
106
+ });
107
+ return `export type ${name} = ${members.join(" | ")};`;
108
+ }
109
+ function emitOrderByType(table) {
110
+ const name = `${modelTypeName(table.accessor)}OrderBy`;
111
+ const fields = table.columns.map((c) => ` ${c.tsName}?: OrderDirection;`);
112
+ return `export interface ${name} {\n${fields.join("\n")}\n}`;
113
+ }
114
+ function columnKeyUnion(table) {
115
+ if (table.columns.length === 0)
116
+ return "never";
117
+ return table.columns.map((c) => JSON.stringify(c.tsName)).join(" | ");
118
+ }
119
+ function emitSelectOmitTypes(table) {
120
+ const model = modelTypeName(table.accessor);
121
+ const union = columnKeyUnion(table);
122
+ const objectFields = table.columns
123
+ .map((c) => `${c.tsName}?: true`)
124
+ .join("; ");
125
+ return [
126
+ `export type ${model}Select = readonly (${union})[] | { ${objectFields} };`,
127
+ `export type ${model}Omit = ${model}Select;`,
128
+ `export type ${model}DistinctKey = ${union};`,
129
+ `export type ${model}CountSelect = { _all?: true; ${objectFields} };`,
130
+ `export type ${model}AggregateSelect = { ${objectFields} };`,
131
+ ].join("\n");
132
+ }
133
+ function emitConnectType(table, manifest) {
134
+ const model = modelTypeName(table.accessor);
135
+ const pk = pkTsNames(table);
136
+ if (pk.length === 0) {
137
+ return `export type ${model}Connect = Record<string, unknown>;`;
138
+ }
139
+ const fields = pk.map((tsName) => {
140
+ const col = table.columns.find((c) => c.tsName === tsName);
141
+ const ts = col
142
+ ? columnNonNullTsType(col, manifest, null, table.accessor)
143
+ : "string";
144
+ return `${tsName}: ${ts}`;
145
+ });
146
+ return `export type ${model}Connect = { ${fields.join("; ")} };`;
147
+ }
148
+ function relationWriteField(manifest, relation) {
149
+ const targetModel = modelTypeName(relation.targetAccessor);
150
+ const targetTable = manifest.tables[relation.targetAccessor];
151
+ const uniqueName = targetTable
152
+ ? `${targetModel}WhereUnique`
153
+ : "Record<string, unknown>";
154
+ if (relation.cardinality === "many") {
155
+ return ` ${relation.name}?: {
156
+ connect?: ${targetModel}Connect | ${targetModel}Connect[];
157
+ create?: Create${targetModel}Input | Create${targetModel}Input[];
158
+ connectOrCreate?: { where: ${uniqueName}; create: Create${targetModel}Input } | { where: ${uniqueName}; create: Create${targetModel}Input }[];
159
+ disconnect?: true | Record<string, unknown> | Record<string, unknown>[];
160
+ delete?: true | Record<string, unknown> | Record<string, unknown>[];
161
+ set?: Record<string, unknown>[];
162
+ };`;
163
+ }
164
+ return ` ${relation.name}?: {
165
+ connect?: ${targetModel}Connect;
166
+ create?: Create${targetModel}Input;
167
+ connectOrCreate?: { where: ${uniqueName}; create: Create${targetModel}Input };
168
+ disconnect?: true;
169
+ delete?: true | Record<string, unknown>;
170
+ };`;
171
+ }
172
+ function emitCreateInputType(manifest, table, enums) {
173
+ const model = modelTypeName(table.accessor);
174
+ const isJunction = isJunctionTable(manifest, table.accessor);
175
+ const lines = [];
176
+ lines.push(`export interface Create${model}Input {`);
177
+ for (const col of table.columns) {
178
+ if (isOmittedOnCreate(col, table, isJunction))
179
+ continue;
180
+ const required = isCreateRequired(col, table, isJunction);
181
+ const optional = required ? "" : "?";
182
+ lines.push(` ${col.tsName}${optional}: ${columnTsType(col, manifest, enums, table.accessor)};`);
183
+ }
184
+ for (const rel of effectiveRelations(manifest, table)) {
185
+ lines.push(relationWriteField(manifest, rel));
186
+ }
187
+ lines.push(`}`);
188
+ return lines.join("\n");
189
+ }
190
+ function emitUpdateInputType(manifest, table, enums) {
191
+ const model = modelTypeName(table.accessor);
192
+ const isJunction = isJunctionTable(manifest, table.accessor);
193
+ const lines = [];
194
+ lines.push(`export interface Update${model}Input {`);
195
+ for (const col of table.columns) {
196
+ if (isOmittedOnUpdate(col, table, isJunction))
197
+ continue;
198
+ lines.push(` ${col.tsName}?: ${updateScalarTsType(col, manifest, enums, table.accessor)};`);
199
+ }
200
+ for (const rel of effectiveRelations(manifest, table)) {
201
+ lines.push(relationWriteField(manifest, rel));
202
+ }
203
+ lines.push(`}`);
204
+ return lines.join("\n");
205
+ }
206
+ function emitArgsTypes(table) {
207
+ const model = modelTypeName(table.accessor);
208
+ const withName = `${model}With`;
209
+ return [
210
+ `export interface FindMany${model}Args {`,
211
+ ` where?: ${model}Where;`,
212
+ ` orderBy?: ${model}OrderBy;`,
213
+ ` take?: number;`,
214
+ ` skip?: number;`,
215
+ ` distinct?: ${model}Select;`,
216
+ ` select?: ${model}Select;`,
217
+ ` omit?: ${model}Omit;`,
218
+ ` with?: ${withName};`,
219
+ ` includeHidden?: boolean;`,
220
+ `}`,
221
+ `export interface FindUnique${model}Args {`,
222
+ ` where: ${model}WhereUnique;`,
223
+ ` select?: ${model}Select;`,
224
+ ` omit?: ${model}Omit;`,
225
+ ` with?: ${withName};`,
226
+ ` includeHidden?: boolean;`,
227
+ `}`,
228
+ `export interface Create${model}Args {`,
229
+ ` data: Create${model}Input;`,
230
+ ` with?: ${withName};`,
231
+ ` returnCreated?: boolean;`,
232
+ `}`,
233
+ `export interface CreateMany${model}Args {`,
234
+ ` data: Create${model}Input[];`,
235
+ ` skipDuplicates?: boolean;`,
236
+ `}`,
237
+ `export interface Upsert${model}Args {`,
238
+ ` where: ${model}WhereUnique;`,
239
+ ` create: Create${model}Input;`,
240
+ ` update: Update${model}Input;`,
241
+ ` with?: ${withName};`,
242
+ `}`,
243
+ `export interface FindOrCreate${model}Args {`,
244
+ ` where: ${model}WhereUnique;`,
245
+ ` create: Create${model}Input;`,
246
+ ` select?: ${model}Select;`,
247
+ ` omit?: ${model}Omit;`,
248
+ ` with?: ${withName};`,
249
+ ` includeHidden?: boolean;`,
250
+ `}`,
251
+ `export interface Update${model}Args {`,
252
+ ` where: ${model}WhereUnique;`,
253
+ ` data: Update${model}Input;`,
254
+ ` with?: ${withName};`,
255
+ ` returnUpdated?: boolean;`,
256
+ `}`,
257
+ `export interface UpdateMany${model}Args {`,
258
+ ` where?: ${model}Where;`,
259
+ ` data: Update${model}Input;`,
260
+ `}`,
261
+ `export interface Delete${model}Args {`,
262
+ ` where: ${model}WhereUnique;`,
263
+ ` with?: ${withName};`,
264
+ ` returnDeleted?: boolean;`,
265
+ `}`,
266
+ `export interface DeleteMany${model}Args {`,
267
+ ` where?: ${model}Where;`,
268
+ `}`,
269
+ `export interface Count${model}Args {`,
270
+ ` where?: ${model}Where;`,
271
+ ` distinct?: ${model}DistinctKey;`,
272
+ ` select?: ${model}CountSelect;`,
273
+ `}`,
274
+ `export interface Exists${model}Args {`,
275
+ ` where?: ${model}Where;`,
276
+ `}`,
277
+ `export interface Aggregate${model}Args {`,
278
+ ` where?: ${model}Where;`,
279
+ ` _count?: true | ${model}CountSelect;`,
280
+ ` _avg?: ${model}AggregateSelect;`,
281
+ ` _sum?: ${model}AggregateSelect;`,
282
+ ` _min?: ${model}AggregateSelect;`,
283
+ ` _max?: ${model}AggregateSelect;`,
284
+ `}`,
285
+ `export interface ${model}GroupByCountHaving {`,
286
+ ` _all?: number | NumericHaving;`,
287
+ ...table.columns.map((c) => ` ${c.tsName}?: number | NumericHaving;`),
288
+ `}`,
289
+ `export interface ${model}NumericHavingFields {`,
290
+ ...table.columns.map((c) => ` ${c.tsName}?: number | NumericHaving;`),
291
+ `}`,
292
+ `export interface GroupBy${model}Having {`,
293
+ ` _count?: number | NumericHaving | ${model}GroupByCountHaving;`,
294
+ ` _avg?: ${model}NumericHavingFields;`,
295
+ ` _sum?: ${model}NumericHavingFields;`,
296
+ ` _min?: ${model}NumericHavingFields;`,
297
+ ` _max?: ${model}NumericHavingFields;`,
298
+ `}`,
299
+ `export interface ${model}GroupByCountOrderBy {`,
300
+ ` _all?: OrderDirection;`,
301
+ ...table.columns.map((c) => ` ${c.tsName}?: OrderDirection;`),
302
+ `}`,
303
+ `export interface ${model}FieldOrderBy {`,
304
+ ...table.columns.map((c) => ` ${c.tsName}?: OrderDirection;`),
305
+ `}`,
306
+ `export interface GroupBy${model}OrderBy extends ${model}OrderBy {`,
307
+ ` _count?: OrderDirection | ${model}GroupByCountOrderBy;`,
308
+ ` _avg?: ${model}FieldOrderBy;`,
309
+ ` _sum?: ${model}FieldOrderBy;`,
310
+ ` _min?: ${model}FieldOrderBy;`,
311
+ ` _max?: ${model}FieldOrderBy;`,
312
+ `}`,
313
+ `export interface GroupBy${model}Args {`,
314
+ ` by: ${model}Select;`,
315
+ ` where?: ${model}Where;`,
316
+ ` having?: GroupBy${model}Having;`,
317
+ ` orderBy?: GroupBy${model}OrderBy;`,
318
+ ` take?: number;`,
319
+ ` skip?: number;`,
320
+ ` _count?: true | ${model}CountSelect;`,
321
+ ` _avg?: ${model}AggregateSelect;`,
322
+ ` _sum?: ${model}AggregateSelect;`,
323
+ ` _min?: ${model}AggregateSelect;`,
324
+ ` _max?: ${model}AggregateSelect;`,
325
+ `}`,
326
+ `export interface Paginate${model}Args {`,
327
+ ` where?: ${model}Where;`,
328
+ ` orderBy: ${model}OrderBy;`,
329
+ ` take: number;`,
330
+ ` after?: Partial<${model}>;`,
331
+ ` before?: Partial<${model}>;`,
332
+ ` select?: ${model}Select;`,
333
+ ` omit?: ${model}Omit;`,
334
+ ` with?: ${withName};`,
335
+ ` includeHidden?: boolean;`,
336
+ `}`,
337
+ `export type Count${model}Result<TArgs> = TArgs extends { select: infer S }`,
338
+ ` ? S extends Record<string, unknown>`,
339
+ ` ? { [K in keyof S as S[K] extends true ? K : never]: number }`,
340
+ ` : number`,
341
+ ` : number;`,
342
+ `export type Aggregate${model}Result<TArgs> = (TArgs extends { _count: true }`,
343
+ ` ? { _count: number }`,
344
+ ` : TArgs extends { _count: infer C extends Record<string, true | undefined> }`,
345
+ ` ? { _count: { [K in keyof C as C[K] extends true ? K : never]: number } }`,
346
+ ` : Record<never, never>) & (TArgs extends { _avg: infer S extends Record<string, true> }`,
347
+ ` ? { _avg: { [K in keyof S & string]: number | null } }`,
348
+ ` : Record<never, never>) & (TArgs extends { _sum: infer S extends Record<string, true> }`,
349
+ ` ? { _sum: { [K in keyof S & string]: number | null } }`,
350
+ ` : Record<never, never>) & (TArgs extends { _min: infer S extends Record<string, true> }`,
351
+ ` ? { _min: { [K in keyof S & string]: number | null } }`,
352
+ ` : Record<never, never>) & (TArgs extends { _max: infer S extends Record<string, true> }`,
353
+ ` ? { _max: { [K in keyof S & string]: number | null } }`,
354
+ ` : Record<never, never>);`,
355
+ `export type GroupBy${model}Result<TArgs> = Pick<${model}, QSelectKeys<TArgs extends { by: infer B } ? B : never> & keyof ${model}> & Aggregate${model}Result<TArgs>;`,
356
+ ].join("\n");
357
+ }
358
+ function visibleRowFor(table, model) {
359
+ const hasHidden = table.columns.some((col) => col.hidden === true);
360
+ return hasHidden ? `Omit<${model}, ${model}HiddenKeys>` : model;
361
+ }
362
+ function emitRepositoryType(table) {
363
+ const model = modelTypeName(table.accessor);
364
+ const withName = `${model}With`;
365
+ const visibleRow = visibleRowFor(table, model);
366
+ const pk = pkTsNames(table);
367
+ const createMinimal = pk.length === 0
368
+ ? "Record<never, never>"
369
+ : `Pick<${model}, ${pk.map((name) => JSON.stringify(name)).join(" | ")}>`;
370
+ const narrowOmit = `"with" | "select" | "omit" | "includeHidden"`;
371
+ const findCommon = (base) => `Omit<${base}, ${narrowOmit}> & { includeHidden?: false }`;
372
+ const findWith = (base) => `Omit<${base}, ${narrowOmit}> & { with: W; select?: S; omit?: O; includeHidden?: IH }`;
373
+ const findSelect = (base) => `Omit<${base}, ${narrowOmit}> & { select: S; omit?: O; includeHidden?: IH }`;
374
+ const findOmit = (base) => `Omit<${base}, ${narrowOmit}> & { omit: O; includeHidden?: IH }`;
375
+ const findHidden = (base) => `Omit<${base}, ${narrowOmit}> & { includeHidden: true }`;
376
+ const withGenerics = `const W extends ${withName}, const S extends ${model}Select | undefined = undefined, const O extends ${model}Omit | undefined = undefined, const IH extends boolean | undefined = undefined`;
377
+ const selectGenerics = `const S extends ${model}Select, const O extends ${model}Omit | undefined = undefined, const IH extends boolean | undefined = undefined`;
378
+ const omitGenerics = `const O extends ${model}Omit, const IH extends boolean | undefined = undefined`;
379
+ return [
380
+ `export interface ${model}Repository {`,
381
+ ` findMany(args?: ${findCommon(`FindMany${model}Args`)}): Promise<${visibleRow}[]>;`,
382
+ ` findMany<${withGenerics}>(args: ${findWith(`FindMany${model}Args`)}): Promise<${model}FindResult<W, S, O, IH>[]>;`,
383
+ ` findMany<${selectGenerics}>(args: ${findSelect(`FindMany${model}Args`)}): Promise<${model}FindResult<undefined, S, O, IH>[]>;`,
384
+ ` findMany<${omitGenerics}>(args: ${findOmit(`FindMany${model}Args`)}): Promise<${model}FindResult<undefined, undefined, O, IH>[]>;`,
385
+ ` findMany(args: ${findHidden(`FindMany${model}Args`)}): Promise<${model}[]>;`,
386
+ ` findFirst(args?: ${findCommon(`FindMany${model}Args`)}): Promise<${visibleRow} | null>;`,
387
+ ` findFirst<${withGenerics}>(args: ${findWith(`FindMany${model}Args`)}): Promise<${model}FindResult<W, S, O, IH> | null>;`,
388
+ ` findFirst<${selectGenerics}>(args: ${findSelect(`FindMany${model}Args`)}): Promise<${model}FindResult<undefined, S, O, IH> | null>;`,
389
+ ` findFirst<${omitGenerics}>(args: ${findOmit(`FindMany${model}Args`)}): Promise<${model}FindResult<undefined, undefined, O, IH> | null>;`,
390
+ ` findFirst(args: ${findHidden(`FindMany${model}Args`)}): Promise<${model} | null>;`,
391
+ ` findUnique(args: ${findCommon(`FindUnique${model}Args`)}): Promise<${visibleRow} | null>;`,
392
+ ` findUnique<${withGenerics}>(args: ${findWith(`FindUnique${model}Args`)}): Promise<${model}FindResult<W, S, O, IH> | null>;`,
393
+ ` findUnique<${selectGenerics}>(args: ${findSelect(`FindUnique${model}Args`)}): Promise<${model}FindResult<undefined, S, O, IH> | null>;`,
394
+ ` findUnique<${omitGenerics}>(args: ${findOmit(`FindUnique${model}Args`)}): Promise<${model}FindResult<undefined, undefined, O, IH> | null>;`,
395
+ ` findUnique(args: ${findHidden(`FindUnique${model}Args`)}): Promise<${model} | null>;`,
396
+ ` findById(id: string | Record<string, unknown>, args?: { includeHidden?: false }): Promise<${visibleRow} | null>;`,
397
+ ` findById<${withGenerics}>(id: string | Record<string, unknown>, args: { with: W; select?: S; omit?: O; includeHidden?: IH }): Promise<${model}FindResult<W, S, O, IH> | null>;`,
398
+ ` findById<${selectGenerics}>(id: string | Record<string, unknown>, args: { select: S; omit?: O; includeHidden?: IH }): Promise<${model}FindResult<undefined, S, O, IH> | null>;`,
399
+ ` findById<${omitGenerics}>(id: string | Record<string, unknown>, args: { omit: O; includeHidden?: IH }): Promise<${model}FindResult<undefined, undefined, O, IH> | null>;`,
400
+ ` findById(id: string | Record<string, unknown>, args: { includeHidden: true }): Promise<${model} | null>;`,
401
+ ` create(args: Omit<Create${model}Args, "with" | "returnCreated">): Promise<${createMinimal}>;`,
402
+ ` create<const W extends ${withName}>(args: Omit<Create${model}Args, "with" | "returnCreated"> & { with: W }): Promise<${model}CreateResult<W>>;`,
403
+ ` create(args: Omit<Create${model}Args, "with" | "returnCreated"> & { returnCreated: true }): Promise<${model}>;`,
404
+ ` createMany(args: CreateMany${model}Args): Promise<number>;`,
405
+ ` createManyAndReturn(args: CreateMany${model}Args): Promise<${model}[]>;`,
406
+ ` upsert(args: Omit<Upsert${model}Args, "with">): Promise<${model}>;`,
407
+ ` upsert<const W extends ${withName}>(args: Omit<Upsert${model}Args, "with"> & { with: W }): Promise<${model}WithIncludes<W>>;`,
408
+ ` findOrCreate(args: ${findCommon(`FindOrCreate${model}Args`)}): Promise<{ record: ${visibleRow}; created: boolean }>;`,
409
+ ` findOrCreate<${withGenerics}>(args: ${findWith(`FindOrCreate${model}Args`)}): Promise<{ record: ${model}FindResult<W, S, O, IH>; created: boolean }>;`,
410
+ ` findOrCreate<${selectGenerics}>(args: ${findSelect(`FindOrCreate${model}Args`)}): Promise<{ record: ${model}FindResult<undefined, S, O, IH>; created: boolean }>;`,
411
+ ` findOrCreate<${omitGenerics}>(args: ${findOmit(`FindOrCreate${model}Args`)}): Promise<{ record: ${model}FindResult<undefined, undefined, O, IH>; created: boolean }>;`,
412
+ ` findOrCreate(args: ${findHidden(`FindOrCreate${model}Args`)}): Promise<{ record: ${model}; created: boolean }>;`,
413
+ ` update(args: Omit<Update${model}Args, "with" | "returnUpdated">): Promise<Record<never, never> | null>;`,
414
+ ` update<const W extends ${withName}>(args: Omit<Update${model}Args, "with" | "returnUpdated"> & { with: W }): Promise<${model}MutationResult<W> | null>;`,
415
+ ` update(args: Omit<Update${model}Args, "with" | "returnUpdated"> & { returnUpdated: true }): Promise<${model} | null>;`,
416
+ ` updateMany(args: UpdateMany${model}Args): Promise<number>;`,
417
+ ` updateManyAndReturn(args: UpdateMany${model}Args): Promise<${model}[]>;`,
418
+ ` updateById(id: string | Record<string, unknown>, args: { data: Update${model}Input }): Promise<Record<never, never> | null>;`,
419
+ ` updateById<const W extends ${withName}>(id: string | Record<string, unknown>, args: { data: Update${model}Input; with: W }): Promise<${model}MutationResult<W> | null>;`,
420
+ ` updateById(id: string | Record<string, unknown>, args: { data: Update${model}Input; returnUpdated: true }): Promise<${model} | null>;`,
421
+ ` delete(args: Omit<Delete${model}Args, "with" | "returnDeleted">): Promise<Record<never, never> | null>;`,
422
+ ` delete<const W extends ${withName}>(args: Omit<Delete${model}Args, "with" | "returnDeleted"> & { with: W }): Promise<${model}MutationResult<W> | null>;`,
423
+ ` delete(args: Omit<Delete${model}Args, "with" | "returnDeleted"> & { returnDeleted: true }): Promise<${model} | null>;`,
424
+ ` deleteMany(args?: DeleteMany${model}Args): Promise<number>;`,
425
+ ` deleteManyAndReturn(args?: DeleteMany${model}Args): Promise<${model}[]>;`,
426
+ ` deleteById(id: string | Record<string, unknown>): Promise<Record<never, never> | null>;`,
427
+ ` count(args?: Omit<Count${model}Args, "select">): Promise<number>;`,
428
+ ` count<const TArgs extends Count${model}Args = Count${model}Args>(args?: TArgs): Promise<Count${model}Result<TArgs>>;`,
429
+ ` exists(args?: Exists${model}Args): Promise<boolean>;`,
430
+ ` aggregate<TArgs extends Aggregate${model}Args>(args: TArgs): Promise<Aggregate${model}Result<TArgs>>;`,
431
+ ` groupBy<const TArgs extends GroupBy${model}Args>(args: TArgs): Promise<GroupBy${model}Result<TArgs>[]>;`,
432
+ ` paginate(args: ${findCommon(`Paginate${model}Args`)}): Promise<PaginateResult<${visibleRow}, Partial<${model}> | null>>;`,
433
+ ` paginate<const TOrderBy extends ${model}OrderBy, ${withGenerics}>(args: Omit<Paginate${model}Args, "orderBy" | ${narrowOmit}> & { orderBy: TOrderBy; with: W; select?: S; omit?: O; includeHidden?: IH }): Promise<PaginateResult<${model}FindResult<W, S, O, IH>, Partial<${model}> | null>>;`,
434
+ ` paginate<const TOrderBy extends ${model}OrderBy, ${selectGenerics}>(args: Omit<Paginate${model}Args, "orderBy" | ${narrowOmit}> & { orderBy: TOrderBy; select: S; omit?: O; includeHidden?: IH }): Promise<PaginateResult<${model}FindResult<undefined, S, O, IH>, Partial<${model}> | null>>;`,
435
+ ` paginate<const TOrderBy extends ${model}OrderBy, ${omitGenerics}>(args: Omit<Paginate${model}Args, "orderBy" | ${narrowOmit}> & { orderBy: TOrderBy; omit: O; includeHidden?: IH }): Promise<PaginateResult<${model}FindResult<undefined, undefined, O, IH>, Partial<${model}> | null>>;`,
436
+ ` paginate(args: ${findHidden(`Paginate${model}Args`)}): Promise<PaginateResult<${model}, Partial<${model}> | null>>;`,
437
+ `}`,
438
+ ].join("\n");
439
+ }
440
+ export function emitQueryTypesTs(manifest) {
441
+ const tables = Object.values(manifest.tables).sort((a, b) => a.accessor.localeCompare(b.accessor));
442
+ const enums = buildEnumRegistry(manifest);
443
+ const hasPostgis = (manifest.extensions ?? []).includes("postgis");
444
+ const modelNames = tables.map((t) => modelTypeName(t.accessor));
445
+ const withNames = tables.map((t) => `${modelTypeName(t.accessor)}With`);
446
+ const enumNames = [...enums.byName.keys()].sort();
447
+ const resultNames = tables.flatMap((t) => {
448
+ const m = modelTypeName(t.accessor);
449
+ return [
450
+ m,
451
+ `${m}HiddenKeys`,
452
+ `${m}FindResult`,
453
+ `${m}CreateResult`,
454
+ `${m}MutationResult`,
455
+ `${m}WithIncludes`,
456
+ ];
457
+ });
458
+ const lines = [
459
+ "// Auto-generated by neoorm generate — do not edit",
460
+ `import type { ${[...new Set([...resultNames, ...enumNames])].join(", ")} } from "./models.js";`,
461
+ `import type { ${[...new Set(withNames)].join(", ") || "NeoOrmIncludes"} } from "./includes.js";`,
462
+ ...(hasPostgis
463
+ ? [`import type { GeoJsonGeometry } from "./models.js";`]
464
+ : []),
465
+ "",
466
+ 'type OrderDirection = "asc" | "desc";',
467
+ "",
468
+ 'type QueryMode = "default" | "insensitive";',
469
+ "",
470
+ "export interface StringFilter<T extends string = string> {",
471
+ " equals?: T;",
472
+ " contains?: T;",
473
+ " startsWith?: T;",
474
+ " endsWith?: T;",
475
+ " search?: T;",
476
+ " mode?: QueryMode;",
477
+ " in?: readonly T[];",
478
+ " notIn?: readonly T[];",
479
+ " isNull?: true;",
480
+ " isNotNull?: true;",
481
+ "}",
482
+ "",
483
+ "export interface ComparableFilter<T> {",
484
+ " equals?: T;",
485
+ " gt?: T;",
486
+ " gte?: T;",
487
+ " lt?: T;",
488
+ " lte?: T;",
489
+ " in?: readonly T[];",
490
+ " notIn?: readonly T[];",
491
+ " isNull?: true;",
492
+ " isNotNull?: true;",
493
+ "}",
494
+ "",
495
+ "export interface JsonFilter<T> {",
496
+ " equals?: T;",
497
+ " jsonContains?: Partial<T> | T;",
498
+ " hasKey?: string;",
499
+ " hasAnyKeys?: readonly string[];",
500
+ " hasAllKeys?: readonly string[];",
501
+ " path?: { segments: readonly string[]; equals?: unknown; jsonContains?: unknown };",
502
+ " isNull?: true;",
503
+ " isNotNull?: true;",
504
+ "}",
505
+ "",
506
+ "export interface EqualsFilter<T> {",
507
+ " equals?: T;",
508
+ " isNull?: true;",
509
+ " isNotNull?: true;",
510
+ "}",
511
+ "",
512
+ "export type UniqueEq<T> = T | { equals: T };",
513
+ "",
514
+ "export interface NumericHaving {",
515
+ " equals?: number;",
516
+ " gt?: number;",
517
+ " gte?: number;",
518
+ " lt?: number;",
519
+ " lte?: number;",
520
+ " in?: readonly number[];",
521
+ " notIn?: readonly number[];",
522
+ "}",
523
+ "",
524
+ "export interface PaginateResult<TItem, TCursor> {",
525
+ " items: TItem[];",
526
+ " nextCursor: TCursor | null;",
527
+ " prevCursor: TCursor | null;",
528
+ " hasMore: boolean;",
529
+ " hasPrevious: boolean;",
530
+ "}",
531
+ "",
532
+ "type QSelectKeys<S> = S extends readonly (infer K extends PropertyKey)[]",
533
+ " ? K",
534
+ " : S extends Record<string, unknown>",
535
+ " ? { [K in keyof S]: S[K] extends true ? K : never }[keyof S]",
536
+ " : never;",
537
+ "",
538
+ ];
539
+ if (hasPostgis) {
540
+ lines.push("export interface SpatialFilter {", " equals?: GeoJsonGeometry;", " intersects?: GeoJsonGeometry;", " within?: GeoJsonGeometry;", " dWithin?: { geometry: GeoJsonGeometry; distance: number };", " isNull?: true;", " isNotNull?: true;", "}", "");
541
+ }
542
+ for (const table of tables) {
543
+ lines.push(emitWhereType(manifest, table, enums));
544
+ lines.push("");
545
+ lines.push(emitWhereUniqueType(manifest, table, enums));
546
+ lines.push("");
547
+ lines.push(emitOrderByType(table));
548
+ lines.push("");
549
+ lines.push(emitSelectOmitTypes(table));
550
+ lines.push("");
551
+ lines.push(emitConnectType(table, manifest));
552
+ lines.push("");
553
+ lines.push(emitCreateInputType(manifest, table, enums));
554
+ lines.push("");
555
+ lines.push(emitUpdateInputType(manifest, table, enums));
556
+ lines.push("");
557
+ lines.push(emitArgsTypes(table));
558
+ lines.push("");
559
+ }
560
+ for (const table of tables) {
561
+ lines.push(emitRepositoryType(table));
562
+ lines.push("");
563
+ const model = modelTypeName(table.accessor);
564
+ lines.push(`export type ${model}Include = ${model}With;`);
565
+ lines.push("");
566
+ }
567
+ const repoEntries = tables
568
+ .map((t) => ` ${t.accessor}: ${modelTypeName(t.accessor)}Repository;`)
569
+ .join("\n");
570
+ lines.push(`export type TransactionIsolationLevel =`);
571
+ lines.push(` | "ReadUncommitted"`);
572
+ lines.push(` | "ReadCommitted"`);
573
+ lines.push(` | "RepeatableRead"`);
574
+ lines.push(` | "Serializable";`);
575
+ lines.push(``);
576
+ lines.push(`export interface TransactionOptions {`);
577
+ lines.push(` isolationLevel?: TransactionIsolationLevel;`);
578
+ lines.push(` readOnly?: boolean;`);
579
+ lines.push(`}`);
580
+ lines.push(``);
581
+ lines.push(`/** Transaction-scoped client (savepoints; no isolation/readOnly on nesting). */`);
582
+ lines.push(`export interface TransactionNeoOrmClient {`);
583
+ lines.push(repoEntries);
584
+ lines.push(` sql<T = Record<string, unknown>>(`);
585
+ lines.push(` strings: TemplateStringsArray,`);
586
+ lines.push(` ...values: unknown[]`);
587
+ lines.push(` ): Promise<T[]>;`);
588
+ lines.push(` sqlId(name: string): {`);
589
+ lines.push(` readonly _kind: "fragment";`);
590
+ lines.push(` readonly text: string;`);
591
+ lines.push(` readonly params: readonly unknown[];`);
592
+ lines.push(` };`);
593
+ lines.push(` execute(query: {`);
594
+ lines.push(` text: string;`);
595
+ lines.push(` params: unknown[];`);
596
+ lines.push(` }): Promise<Record<string, unknown>[]>;`);
597
+ lines.push(` $connect(): Promise<void>;`);
598
+ lines.push(` $disconnect(): Promise<void>;`);
599
+ lines.push(` $transaction<T>(`);
600
+ lines.push(` fn: (tx: TransactionNeoOrmClient) => Promise<T>,`);
601
+ lines.push(` ): Promise<T>;`);
602
+ lines.push(` $transaction<T extends readonly unknown[]>(`);
603
+ lines.push(` steps: { [K in keyof T]: (tx: TransactionNeoOrmClient) => Promise<T[K]> } & readonly unknown[],`);
604
+ lines.push(` ): Promise<T>;`);
605
+ lines.push(`}`);
606
+ lines.push(``);
607
+ lines.push(`/** Generated flat client: one repository per table accessor plus database helpers. */`);
608
+ lines.push(`export interface NeoOrmClient {`);
609
+ lines.push(repoEntries);
610
+ lines.push(` sql<T = Record<string, unknown>>(`);
611
+ lines.push(` strings: TemplateStringsArray,`);
612
+ lines.push(` ...values: unknown[]`);
613
+ lines.push(` ): Promise<T[]>;`);
614
+ lines.push(` sqlId(name: string): {`);
615
+ lines.push(` readonly _kind: "fragment";`);
616
+ lines.push(` readonly text: string;`);
617
+ lines.push(` readonly params: readonly unknown[];`);
618
+ lines.push(` };`);
619
+ lines.push(` execute(query: {`);
620
+ lines.push(` text: string;`);
621
+ lines.push(` params: unknown[];`);
622
+ lines.push(` }): Promise<Record<string, unknown>[]>;`);
623
+ lines.push(` $connect(): Promise<void>;`);
624
+ lines.push(` $disconnect(): Promise<void>;`);
625
+ lines.push(` $transaction<T>(`);
626
+ lines.push(` fn: (tx: TransactionNeoOrmClient) => Promise<T>,`);
627
+ lines.push(` options?: TransactionOptions,`);
628
+ lines.push(` ): Promise<T>;`);
629
+ lines.push(` $transaction<T extends readonly unknown[]>(`);
630
+ lines.push(` steps: { [K in keyof T]: (tx: TransactionNeoOrmClient) => Promise<T[K]> } & readonly unknown[],`);
631
+ lines.push(` options?: TransactionOptions,`);
632
+ lines.push(` ): Promise<T>;`);
633
+ lines.push(`}`);
634
+ lines.push(``);
635
+ void modelNames;
636
+ return lines.join("\n");
637
+ }
638
+ //# sourceMappingURL=emit-query-types.js.map