on-zero 0.1.39 → 0.1.41

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 (84) hide show
  1. package/dist/cjs/createUseQuery.cjs +3 -2
  2. package/dist/cjs/createUseQuery.js +2 -2
  3. package/dist/cjs/createUseQuery.js.map +1 -1
  4. package/dist/cjs/createUseQuery.native.js +3 -2
  5. package/dist/cjs/createUseQuery.native.js.map +1 -1
  6. package/dist/cjs/createZeroClient.cjs +28 -5
  7. package/dist/cjs/createZeroClient.js +19 -4
  8. package/dist/cjs/createZeroClient.js.map +1 -1
  9. package/dist/cjs/createZeroClient.native.js +29 -5
  10. package/dist/cjs/createZeroClient.native.js.map +1 -1
  11. package/dist/cjs/createZeroServer.cjs +5 -2
  12. package/dist/cjs/createZeroServer.js +5 -2
  13. package/dist/cjs/createZeroServer.js.map +1 -1
  14. package/dist/cjs/createZeroServer.native.js +5 -2
  15. package/dist/cjs/createZeroServer.native.js.map +1 -1
  16. package/dist/cjs/generate.cjs +458 -39
  17. package/dist/cjs/generate.js +485 -31
  18. package/dist/cjs/generate.js.map +2 -2
  19. package/dist/cjs/generate.native.js +812 -51
  20. package/dist/cjs/generate.native.js.map +1 -1
  21. package/dist/cjs/generate.test.cjs +251 -0
  22. package/dist/cjs/generate.test.js +252 -0
  23. package/dist/cjs/generate.test.js.map +1 -1
  24. package/dist/cjs/generate.test.native.js +251 -0
  25. package/dist/cjs/generate.test.native.js.map +1 -1
  26. package/dist/cjs/helpers/createMutators.cjs +21 -8
  27. package/dist/cjs/helpers/createMutators.js +16 -6
  28. package/dist/cjs/helpers/createMutators.js.map +1 -1
  29. package/dist/cjs/helpers/createMutators.native.js +28 -10
  30. package/dist/cjs/helpers/createMutators.native.js.map +1 -1
  31. package/dist/esm/createUseQuery.js +3 -3
  32. package/dist/esm/createUseQuery.js.map +1 -1
  33. package/dist/esm/createUseQuery.mjs +4 -3
  34. package/dist/esm/createUseQuery.mjs.map +1 -1
  35. package/dist/esm/createUseQuery.native.js +4 -3
  36. package/dist/esm/createUseQuery.native.js.map +1 -1
  37. package/dist/esm/createZeroClient.js +19 -4
  38. package/dist/esm/createZeroClient.js.map +1 -1
  39. package/dist/esm/createZeroClient.mjs +28 -5
  40. package/dist/esm/createZeroClient.mjs.map +1 -1
  41. package/dist/esm/createZeroClient.native.js +29 -5
  42. package/dist/esm/createZeroClient.native.js.map +1 -1
  43. package/dist/esm/createZeroServer.js +5 -2
  44. package/dist/esm/createZeroServer.js.map +1 -1
  45. package/dist/esm/createZeroServer.mjs +5 -2
  46. package/dist/esm/createZeroServer.mjs.map +1 -1
  47. package/dist/esm/createZeroServer.native.js +5 -2
  48. package/dist/esm/createZeroServer.native.js.map +1 -1
  49. package/dist/esm/generate.js +486 -32
  50. package/dist/esm/generate.js.map +2 -2
  51. package/dist/esm/generate.mjs +459 -40
  52. package/dist/esm/generate.mjs.map +1 -1
  53. package/dist/esm/generate.native.js +813 -52
  54. package/dist/esm/generate.native.js.map +1 -1
  55. package/dist/esm/generate.test.js +252 -0
  56. package/dist/esm/generate.test.js.map +1 -1
  57. package/dist/esm/generate.test.mjs +251 -0
  58. package/dist/esm/generate.test.mjs.map +1 -1
  59. package/dist/esm/generate.test.native.js +251 -0
  60. package/dist/esm/generate.test.native.js.map +1 -1
  61. package/dist/esm/helpers/createMutators.js +6 -4
  62. package/dist/esm/helpers/createMutators.js.map +1 -1
  63. package/dist/esm/helpers/createMutators.mjs +6 -4
  64. package/dist/esm/helpers/createMutators.mjs.map +1 -1
  65. package/dist/esm/helpers/createMutators.native.js +13 -6
  66. package/dist/esm/helpers/createMutators.native.js.map +1 -1
  67. package/package.json +2 -2
  68. package/readme.md +110 -2
  69. package/src/createUseQuery.tsx +15 -6
  70. package/src/createZeroClient.tsx +42 -6
  71. package/src/createZeroServer.ts +9 -0
  72. package/src/generate.test.ts +340 -0
  73. package/src/generate.ts +863 -43
  74. package/src/helpers/createMutators.ts +22 -8
  75. package/types/createUseQuery.d.ts +2 -1
  76. package/types/createUseQuery.d.ts.map +1 -1
  77. package/types/createZeroClient.d.ts +10 -1
  78. package/types/createZeroClient.d.ts.map +1 -1
  79. package/types/createZeroServer.d.ts +7 -1
  80. package/types/createZeroServer.d.ts.map +1 -1
  81. package/types/generate.d.ts +1 -0
  82. package/types/generate.d.ts.map +1 -1
  83. package/types/helpers/createMutators.d.ts +3 -1
  84. package/types/helpers/createMutators.d.ts.map +1 -1
@@ -127,6 +127,7 @@ this folder is auto-generated by on-zero. do not edit files here directly.
127
127
  - \`tables.ts\` - exports table schemas for type inference
128
128
  - \`groupedQueries.ts\` - namespaced query re-exports for client setup
129
129
  - \`syncedQueries.ts\` - namespaced syncedQuery wrappers for server setup
130
+ - \`syncedMutations.ts\` - valibot validators for mutation args (server auto-validation)
130
131
 
131
132
  ## usage guidelines
132
133
 
@@ -198,22 +199,7 @@ import * as Queries from './groupedQueries'
198
199
  `,
199
200
  namespaceDefs = sortedFiles.map(file => {
200
201
  const queryDefs = queryByFile.get(file).sort((a, b) => a.name.localeCompare(b.name)).map(q => {
201
- const lines = q.valibotCode.split(`
202
- `).filter(l => l.trim()),
203
- schemaLineIndex = lines.findIndex(l => l.startsWith("export const QueryParams"));
204
- let validatorDef = "";
205
- if (schemaLineIndex !== -1) {
206
- const schemaLines = [];
207
- let openBraces = 0,
208
- started = !1;
209
- for (let i = schemaLineIndex; i < lines.length; i++) {
210
- const line = lines[i],
211
- cleaned = started ? line : line.replace("export const QueryParams = ", "");
212
- if (schemaLines.push(cleaned), started = !0, openBraces += (cleaned.match(/\{/g) || []).length, openBraces -= (cleaned.match(/\}/g) || []).length, openBraces += (cleaned.match(/\(/g) || []).length, openBraces -= (cleaned.match(/\)/g) || []).length, openBraces === 0 && schemaLines.length > 0) break;
213
- }
214
- validatorDef = schemaLines.join(`
215
- `);
216
- }
202
+ const validatorDef = q.valibotCode.trim();
217
203
  if (q.params === "void" || !validatorDef) return ` ${q.name}: defineQuery(() => Queries.${file}.${q.name}()),`;
218
204
  const indentedValidator = validatorDef.split(`
219
205
  `).map((line, i) => i === 0 ? line : ` ${line}`).join(`
@@ -240,6 +226,364 @@ ${queriesObject}
240
226
  })
241
227
  `;
242
228
  }
229
+ function createTypeResolver(ts, files, dir) {
230
+ const configPath = ts.findConfigFile(dir, ts.sys.fileExists, "tsconfig.json");
231
+ let compilerOptions = {
232
+ target: ts.ScriptTarget.Latest,
233
+ module: ts.ModuleKind.ESNext,
234
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
235
+ strict: !1,
236
+ skipLibCheck: !0,
237
+ noEmit: !0
238
+ };
239
+ if (configPath) {
240
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
241
+ if (configFile.config) {
242
+ const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, (0, import_node_path.dirname)(configPath));
243
+ compilerOptions = {
244
+ ...compilerOptions,
245
+ ...parsed.options
246
+ };
247
+ }
248
+ }
249
+ const fileMap = /* @__PURE__ */new Map();
250
+ for (const f of files) fileMap.set(f.path, f.content);
251
+ const host = ts.createCompilerHost(compilerOptions),
252
+ originalGetSourceFile = host.getSourceFile.bind(host);
253
+ host.getSourceFile = (fileName, languageVersion, onError) => {
254
+ const content = fileMap.get(fileName);
255
+ return content !== void 0 ? ts.createSourceFile(fileName, content, languageVersion, !0) : originalGetSourceFile(fileName, languageVersion, onError);
256
+ }, host.fileExists = fileName => fileMap.has(fileName) || ts.sys.fileExists(fileName), host.readFile = fileName => fileMap.get(fileName) ?? ts.sys.readFile(fileName);
257
+ const program = ts.createProgram(files.map(f => f.path), compilerOptions, host),
258
+ checker = program.getTypeChecker();
259
+ return {
260
+ program,
261
+ checker,
262
+ // resolve a type annotation node to a ts.Type
263
+ resolveType(node) {
264
+ try {
265
+ return checker.getTypeFromTypeNode(node);
266
+ } catch {
267
+ return null;
268
+ }
269
+ },
270
+ // convert a resolved type to valibot code
271
+ typeToValibot(type) {
272
+ return tsTypeToValibot(ts, checker, type);
273
+ }
274
+ };
275
+ }
276
+ function resolveParamType(ts, resolver, sourceFile, exportName, paramIndex) {
277
+ let result = null;
278
+ return ts.forEachChild(sourceFile, node => {
279
+ if (result || !ts.isVariableStatement(node) || !node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
280
+ const decl = node.declarationList.declarations[0];
281
+ if (!(!decl || !ts.isVariableDeclaration(decl)) && decl.name.getText(sourceFile) === exportName && decl.initializer && ts.isArrowFunction(decl.initializer)) {
282
+ const param = decl.initializer.parameters[paramIndex];
283
+ param?.type && (result = resolver.resolveType(param.type));
284
+ }
285
+ }), result;
286
+ }
287
+ function resolveMutationParamTypes(ts, resolver, sourceFile) {
288
+ const resolved = /* @__PURE__ */new Map();
289
+ return ts.forEachChild(sourceFile, node => {
290
+ if (!ts.isVariableStatement(node) || !node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
291
+ const decl = node.declarationList.declarations[0];
292
+ if (!decl || !ts.isVariableDeclaration(decl) || decl.name.getText(sourceFile) !== "mutate" || !decl.initializer || !ts.isCallExpression(decl.initializer)) return;
293
+ const args = decl.initializer.arguments;
294
+ let handlersArg = null;
295
+ for (let i = args.length - 1; i >= 0; i--) if (ts.isObjectLiteralExpression(args[i])) {
296
+ handlersArg = args[i];
297
+ break;
298
+ }
299
+ if (handlersArg) for (const prop of handlersArg.properties) {
300
+ if (!ts.isPropertyAssignment(prop) && !ts.isMethodDeclaration(prop)) continue;
301
+ const name = prop.name?.getText(sourceFile);
302
+ if (!name) continue;
303
+ let params = null;
304
+ if (ts.isPropertyAssignment(prop)) {
305
+ const init = prop.initializer;
306
+ (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) && (params = init.parameters);
307
+ } else ts.isMethodDeclaration(prop) && (params = prop.parameters);
308
+ if (!params || params.length < 2) continue;
309
+ const typeNode = params[1].type;
310
+ if (!typeNode) continue;
311
+ const expanded = resolver.resolveType(typeNode);
312
+ expanded && resolved.set(name, expanded);
313
+ }
314
+ }), resolved;
315
+ }
316
+ function extractMutationsFromModel(ts, sourceFile, content, _fileName, silent, typeToValibot, resolvedTypes, resolvedTypeToValibot) {
317
+ let mutateNode = null;
318
+ if (ts.forEachChild(sourceFile, node => {
319
+ if (!ts.isVariableStatement(node) || !node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
320
+ const decl = node.declarationList.declarations[0];
321
+ !decl || !ts.isVariableDeclaration(decl) || decl.name.getText(sourceFile) === "mutate" && decl.initializer && ts.isCallExpression(decl.initializer) && (mutateNode = decl.initializer);
322
+ }), !mutateNode) return null;
323
+ const args = mutateNode.arguments,
324
+ hasCRUD = args.length >= 2;
325
+ let handlersArg = null;
326
+ args.length === 1 && ts.isObjectLiteralExpression(args[0]) ? handlersArg = args[0] : args.length === 3 && ts.isObjectLiteralExpression(args[2]) && (handlersArg = args[2]);
327
+ const columns = {},
328
+ primaryKeys = [];
329
+ hasCRUD && extractSchemaColumns(ts, sourceFile, columns, primaryKeys);
330
+ const custom = [];
331
+ if (handlersArg) for (const prop of handlersArg.properties) {
332
+ if (!ts.isPropertyAssignment(prop) && !ts.isMethodDeclaration(prop)) continue;
333
+ const name = prop.name?.getText(sourceFile);
334
+ if (!name) continue;
335
+ let params = null;
336
+ if (ts.isPropertyAssignment(prop)) {
337
+ const init = prop.initializer;
338
+ (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) && (params = init.parameters);
339
+ } else ts.isMethodDeclaration(prop) && (params = prop.parameters);
340
+ if (!params) continue;
341
+ if (params.length < 2) {
342
+ custom.push({
343
+ name,
344
+ paramType: "void",
345
+ valibotCode: ""
346
+ });
347
+ continue;
348
+ }
349
+ const paramType = params[1].type?.getText(sourceFile) || "unknown";
350
+ if (paramType === "unknown") {
351
+ custom.push({
352
+ name,
353
+ paramType: "unknown",
354
+ valibotCode: ""
355
+ });
356
+ continue;
357
+ }
358
+ let valibotCode = typeToValibot(paramType);
359
+ if (!valibotCode && resolvedTypes && resolvedTypeToValibot) {
360
+ const resolvedType = resolvedTypes.get(name);
361
+ resolvedType && (valibotCode = resolvedTypeToValibot(resolvedType));
362
+ }
363
+ custom.push({
364
+ name,
365
+ paramType,
366
+ valibotCode: valibotCode || ""
367
+ });
368
+ }
369
+ return {
370
+ modelName: "",
371
+ hasCRUD,
372
+ columns,
373
+ primaryKeys,
374
+ custom
375
+ };
376
+ }
377
+ function extractSchemaColumns(ts, sourceFile, columns, primaryKeys) {
378
+ function visit(node) {
379
+ if (ts.isCallExpression(node)) {
380
+ const text = node.expression.getText(sourceFile);
381
+ if (text.endsWith(".primaryKey")) for (const arg of node.arguments) ts.isStringLiteral(arg) && primaryKeys.push(arg.text);
382
+ if (text.endsWith(".columns") && node.arguments.length === 1) {
383
+ const obj = node.arguments[0];
384
+ if (ts.isObjectLiteralExpression(obj)) for (const prop of obj.properties) {
385
+ if (!ts.isPropertyAssignment(prop)) continue;
386
+ const colName = prop.name?.getText(sourceFile);
387
+ if (!colName) continue;
388
+ const initText = prop.initializer.getText(sourceFile),
389
+ colType = parseColumnType(initText);
390
+ columns[colName] = colType;
391
+ }
392
+ }
393
+ }
394
+ ts.forEachChild(node, visit);
395
+ }
396
+ visit(sourceFile);
397
+ }
398
+ function parseColumnType(initText) {
399
+ const optional = initText.includes(".optional()");
400
+ let type = "string";
401
+ return initText.startsWith("number(") ? type = "number" : initText.startsWith("boolean(") ? type = "boolean" : initText.startsWith("json(") || initText.startsWith("json<") ? type = "json" : initText.startsWith("enumeration(") && (type = "enum"), {
402
+ type,
403
+ optional
404
+ };
405
+ }
406
+ function columnTypeToValibot(col) {
407
+ let base;
408
+ switch (col.type) {
409
+ case "string":
410
+ base = "v.string()";
411
+ break;
412
+ case "number":
413
+ base = "v.number()";
414
+ break;
415
+ case "boolean":
416
+ base = "v.boolean()";
417
+ break;
418
+ case "json":
419
+ base = "v.unknown()";
420
+ break;
421
+ case "enum":
422
+ base = "v.string()";
423
+ break;
424
+ default:
425
+ base = "v.unknown()";
426
+ }
427
+ return col.optional ? `v.optional(v.nullable(${base}))` : base;
428
+ }
429
+ function schemaColumnsToValibot(columns, primaryKeys, mode) {
430
+ const entries = [];
431
+ if (mode === "delete") for (const pk of primaryKeys) {
432
+ const col = columns[pk];
433
+ col && entries.push(`${pk}: ${columnTypeToValibot({
434
+ ...col,
435
+ optional: !1
436
+ })}`);
437
+ } else if (mode === "update") for (const [name, col] of Object.entries(columns)) primaryKeys.includes(name) ? entries.push(`${name}: ${columnTypeToValibot({
438
+ ...col,
439
+ optional: !1
440
+ })}`) : entries.push(`${name}: ${columnTypeToValibot({
441
+ ...col,
442
+ optional: !0
443
+ })}`);else for (const [name, col] of Object.entries(columns)) entries.push(`${name}: ${columnTypeToValibot(col)}`);
444
+ return `v.object({
445
+ ${entries.join(`,
446
+ `)},
447
+ })`;
448
+ }
449
+ function generateSyncedMutationsFile(modelMutations) {
450
+ return `// auto-generated by: on-zero generate
451
+ // mutation validators derived from model schemas and handler types
452
+ import * as v from 'valibot'
453
+
454
+ export const mutationValidators = {
455
+ ${[...modelMutations].sort((a, b) => a.modelName.localeCompare(b.modelName)).map(model => {
456
+ const entries = [];
457
+ if (model.hasCRUD && Object.keys(model.columns).length > 0) for (const mode of ["insert", "update", "delete"]) if (model.custom.some(m => m.name === mode)) {
458
+ const customMut = model.custom.find(m => m.name === mode);
459
+ customMut.valibotCode ? entries.push(` ${mode}: ${extractValibotExpression(customMut.valibotCode)},`) : entries.push(` ${mode}: ${schemaColumnsToValibot(model.columns, model.primaryKeys, mode)},`);
460
+ } else entries.push(` ${mode}: ${schemaColumnsToValibot(model.columns, model.primaryKeys, mode)},`);
461
+ for (const mut of model.custom) if (!(model.hasCRUD && ["insert", "update", "delete", "upsert"].includes(mut.name))) {
462
+ if (mut.paramType === "void" || !mut.valibotCode) {
463
+ entries.push(` ${mut.name}: v.void_(),`);
464
+ continue;
465
+ }
466
+ entries.push(` ${mut.name}: ${extractValibotExpression(mut.valibotCode)},`);
467
+ }
468
+ return ` ${model.modelName}: {
469
+ ${entries.join(`
470
+ `)}
471
+ },`;
472
+ }).join(`
473
+ `)}
474
+ }
475
+ `;
476
+ }
477
+ function extractValibotExpression(valibotCode) {
478
+ return valibotCode.trim() || "v.unknown()";
479
+ }
480
+ function parseTypeString(type) {
481
+ if (type = type.trim(), type === "string") return "v.string()";
482
+ if (type === "number") return "v.number()";
483
+ if (type === "boolean") return "v.boolean()";
484
+ if (type === "void" || type === "undefined") return "v.void_()";
485
+ if (type === "null") return "v.null_()";
486
+ if (type === "any" || type === "unknown") return "v.unknown()";
487
+ if (type.startsWith("{") && type.endsWith("}")) {
488
+ const inner = type.slice(1, -1).trim();
489
+ if (!inner) return "v.object({})";
490
+ const normalized = inner.replace(/\n/g, "; ").replace(/;\s*;/g, ";"),
491
+ entries = [];
492
+ for (const part of normalized.split(";")) {
493
+ const trimmed = part.trim().replace(/,\s*$/, "");
494
+ if (!trimmed) continue;
495
+ const match = trimmed.match(/^(?:readonly\s+)?(\w+)(\?)?:\s*(.+)$/);
496
+ if (!match) continue;
497
+ const [, name, opt, typeStr] = match,
498
+ parsed = parseTypeString(typeStr.trim());
499
+ if (!parsed) return null;
500
+ let val = parsed;
501
+ opt && (val = `v.optional(${val})`), entries.push(`${name}: ${val}`);
502
+ }
503
+ return entries.length === 0 ? "v.object({})" : `v.object({
504
+ ${entries.join(`,
505
+ `)},
506
+ })`;
507
+ }
508
+ if (type.endsWith("[]")) {
509
+ const inner = parseTypeString(type.slice(0, -2).trim());
510
+ return inner ? `v.array(${inner})` : null;
511
+ }
512
+ return null;
513
+ }
514
+ function tsTypeToValibot(ts, checker, type, seen) {
515
+ seen || (seen = /* @__PURE__ */new Set());
516
+ const flags = type.getFlags();
517
+ if (flags & (ts.TypeFlags.Object | ts.TypeFlags.Intersection)) {
518
+ if (seen.has(type)) return "v.unknown()";
519
+ seen.add(type);
520
+ }
521
+ const recurse = t => tsTypeToValibot(ts, checker, t, seen);
522
+ if (flags & ts.TypeFlags.String) return "v.string()";
523
+ if (flags & ts.TypeFlags.Number) return "v.number()";
524
+ if (flags & ts.TypeFlags.Boolean) return "v.boolean()";
525
+ if (flags & ts.TypeFlags.Void || flags & ts.TypeFlags.Undefined) return "v.void_()";
526
+ if (flags & ts.TypeFlags.Null) return "v.null_()";
527
+ if (flags & ts.TypeFlags.Any || flags & ts.TypeFlags.Unknown) return "v.unknown()";
528
+ if (flags & ts.TypeFlags.Never) return "v.never()";
529
+ if (flags & ts.TypeFlags.StringLiteral) return `v.literal(${JSON.stringify(type.value)})`;
530
+ if (flags & ts.TypeFlags.NumberLiteral) return `v.literal(${type.value})`;
531
+ if (flags & ts.TypeFlags.BooleanLiteral) return `v.literal(${type.intrinsicName === "true"})`;
532
+ if (type.isUnion()) {
533
+ const members = type.types,
534
+ hasNull = members.some(t => t.getFlags() & ts.TypeFlags.Null),
535
+ hasUndefined = members.some(t => t.getFlags() & (ts.TypeFlags.Undefined | ts.TypeFlags.Void)),
536
+ rest = members.filter(t => !(t.getFlags() & (ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.Void)));
537
+ if (rest.length === 2 && rest.every(t => t.getFlags() & ts.TypeFlags.BooleanLiteral)) {
538
+ let inner2 = "v.boolean()";
539
+ return hasNull && (inner2 = `v.nullable(${inner2})`), hasUndefined && (inner2 = `v.optional(${inner2})`), inner2;
540
+ }
541
+ if (rest.length === 0) return "v.unknown()";
542
+ let inner = rest.length === 1 ? recurse(rest[0]) : `v.union([${rest.map(t => recurse(t)).join(", ")}])`;
543
+ return hasNull && (inner = `v.nullable(${inner})`), hasUndefined && (inner = `v.optional(${inner})`), inner;
544
+ }
545
+ const resolveSymbolType = prop => prop.valueDeclaration ? checker.getTypeOfSymbolAtLocation(prop, prop.valueDeclaration) : prop.declarations?.[0] ? checker.getTypeOfSymbolAtLocation(prop, prop.declarations[0]) : checker.getDeclaredTypeOfSymbol(prop);
546
+ if (type.isIntersection()) {
547
+ const props2 = type.getProperties();
548
+ if (props2.length === 0) return "v.object({})";
549
+ const entries = [];
550
+ for (const prop of props2) {
551
+ const propType = resolveSymbolType(prop),
552
+ isOptional = !!(prop.getFlags() & ts.SymbolFlags.Optional);
553
+ let val = recurse(propType);
554
+ isOptional && !val.startsWith("v.optional(") && (val = `v.optional(${val})`), entries.push(`${prop.getName()}: ${val}`);
555
+ }
556
+ return `v.object({
557
+ ${entries.join(`,
558
+ `)},
559
+ })`;
560
+ }
561
+ const props = type.getProperties();
562
+ if (props.length > 0 && (type.getFlags() & ts.TypeFlags.Object || type.objectFlags)) {
563
+ const objectFlags = type.objectFlags ?? 0;
564
+ if (objectFlags & ts.ObjectFlags.Reference) {
565
+ const typeRef = type,
566
+ name = type.getSymbol()?.getName();
567
+ if ((name === "Array" || name === "ReadonlyArray") && typeRef.typeArguments?.length === 1) return `v.array(${recurse(typeRef.typeArguments[0])})`;
568
+ }
569
+ if (objectFlags & ts.ObjectFlags.Tuple) return `v.tuple([${(type.typeArguments || []).map(t => recurse(t)).join(", ")}])`;
570
+ const entries = [];
571
+ for (const prop of props) {
572
+ const propType = resolveSymbolType(prop),
573
+ isOptional = !!(prop.getFlags() & ts.SymbolFlags.Optional);
574
+ let val = recurse(propType);
575
+ isOptional && !val.startsWith("v.optional(") && (val = `v.optional(${val})`), entries.push(`${prop.getName()}: ${val}`);
576
+ }
577
+ return entries.length === 0 ? "v.object({})" : `v.object({
578
+ ${entries.join(`,
579
+ `)},
580
+ })`;
581
+ }
582
+ const stringIndex = type.getStringIndexType();
583
+ if (stringIndex) return `v.record(v.string(), ${recurse(stringIndex)})`;
584
+ const numberIndex = type.getNumberIndexType();
585
+ return numberIndex ? `v.record(v.number(), ${recurse(numberIndex)})` : "v.unknown()";
586
+ }
243
587
  async function generate(options) {
244
588
  const {
245
589
  dir,
@@ -256,17 +600,30 @@ async function generate(options) {
256
600
  const allModelFiles = (0, import_node_fs.readdirSync)(modelsDir).filter(f => f.endsWith(".ts")).sort(),
257
601
  filesWithSchema = allModelFiles.filter(f => (0, import_node_fs.readFileSync)((0, import_node_path.resolve)(modelsDir, f), "utf-8").includes("export const schema = table("));
258
602
  let filesChanged = [writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "models.ts"), generateModelsFile(allModelFiles)), writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "types.ts"), generateTypesFile(filesWithSchema)), writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "tables.ts"), generateTablesFile(filesWithSchema)), writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "README.md"), generateReadmeFile())].filter(Boolean).length,
259
- queryCount = 0;
603
+ queryCount = 0,
604
+ mutationCount = 0;
605
+ const ts = await import("typescript"),
606
+ typeToValibot = paramType => {
607
+ try {
608
+ return parseTypeString(paramType.trim());
609
+ } catch {
610
+ return null;
611
+ }
612
+ };
260
613
  if ((0, import_node_fs.existsSync)(queriesDir)) {
261
- const ts = await import("typescript"),
262
- {
263
- ModelToValibot
264
- } = await import("@sinclair/typebox-codegen/model/index.js"),
265
- {
266
- TypeScriptToModel
267
- } = await import("@sinclair/typebox-codegen/typescript/index.js"),
268
- queryFiles = (0, import_node_fs.readdirSync)(queriesDir).filter(f => f.endsWith(".ts")),
614
+ const queryFiles = (0, import_node_fs.readdirSync)(queriesDir).filter(f => f.endsWith(".ts")),
269
615
  allQueries = [];
616
+ let queryResolver = null;
617
+ const getQueryResolver = () => {
618
+ if (!queryResolver) {
619
+ const allFiles = (0, import_node_fs.readdirSync)(queriesDir).filter(f => f.endsWith(".ts")).map(f => ({
620
+ path: (0, import_node_path.resolve)(queriesDir, f),
621
+ content: (0, import_node_fs.readFileSync)((0, import_node_path.resolve)(queriesDir, f), "utf-8")
622
+ }));
623
+ queryResolver = createTypeResolver(ts, allFiles, queriesDir);
624
+ }
625
+ return queryResolver;
626
+ };
270
627
  for (const file of queryFiles) {
271
628
  const filePath = (0, import_node_path.resolve)(queriesDir, file),
272
629
  fileBaseName = (0, import_node_path.basename)(file, ".ts");
@@ -284,19 +641,21 @@ async function generate(options) {
284
641
  const params = declaration.initializer.parameters;
285
642
  let paramType = "void";
286
643
  params.length > 0 && (paramType = params[0].type?.getText(sourceFile) || "unknown");
287
- try {
288
- const typeString = `type QueryParams = ${paramType}`,
289
- model = TypeScriptToModel.Generate(typeString),
290
- valibotCode = ModelToValibot.Generate(model);
291
- allQueries.push({
292
- name,
293
- params: paramType,
294
- valibotCode,
295
- sourceFile: fileBaseName
296
- });
297
- } catch (err) {
298
- silent || console.error(`\u2717 ${name}: ${err}`);
644
+ let valibotCode = typeToValibot(paramType);
645
+ if (!valibotCode && params.length > 0 && params[0].type) {
646
+ const resolver = getQueryResolver(),
647
+ resolverSourceFile = resolver.program.getSourceFile(filePath);
648
+ if (resolverSourceFile) {
649
+ const resolvedType = resolveParamType(ts, resolver, resolverSourceFile, name, 0);
650
+ resolvedType && (valibotCode = resolver.typeToValibot(resolvedType));
651
+ }
299
652
  }
653
+ valibotCode ? allQueries.push({
654
+ name,
655
+ params: paramType,
656
+ valibotCode,
657
+ sourceFile: fileBaseName
658
+ }) : !silent && paramType !== "void" && console.error(`\u2717 ${name}: could not resolve type "${paramType}"`);
300
659
  }
301
660
  }
302
661
  });
@@ -309,7 +668,66 @@ async function generate(options) {
309
668
  syncedChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "syncedQueries.ts"), generateSyncedQueriesFile(allQueries));
310
669
  groupedChanged && filesChanged++, syncedChanged && filesChanged++;
311
670
  }
312
- if (filesChanged > 0 && !silent && console.info(`\u2713 ${allModelFiles.length} models (${filesWithSchema.length} schemas)${queryCount ? `, ${queryCount} queries` : ""}`), filesChanged > 0 && after) {
671
+ const allModelMutations = [],
672
+ mutationFiles = [],
673
+ unresolvedModels = [];
674
+ for (const file of allModelFiles) {
675
+ const filePath = (0, import_node_path.resolve)(modelsDir, file),
676
+ fileBaseName = (0, import_node_path.basename)(file, ".ts");
677
+ try {
678
+ const content = (0, import_node_fs.readFileSync)(filePath, "utf-8");
679
+ if (!content.includes("export const mutate")) continue;
680
+ mutationFiles.push({
681
+ path: filePath,
682
+ content,
683
+ baseName: fileBaseName
684
+ });
685
+ const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, !0),
686
+ result = extractMutationsFromModel(ts, sourceFile, content, filePath, !!silent, typeToValibot);
687
+ result && (result.modelName = fileBaseName, allModelMutations.push(result), result.custom.some(m => m.paramType !== "void" && m.paramType !== "unknown" && !m.valibotCode) && unresolvedModels.push({
688
+ baseName: fileBaseName,
689
+ filePath
690
+ }));
691
+ } catch (err) {
692
+ silent || console.error(`Error extracting mutations from ${file}:`, err);
693
+ }
694
+ }
695
+ if (unresolvedModels.length > 0) {
696
+ const collectTsFiles = dir2 => {
697
+ const results = [];
698
+ for (const entry of (0, import_node_fs.readdirSync)(dir2, {
699
+ withFileTypes: !0
700
+ })) {
701
+ const fullPath = (0, import_node_path.resolve)(dir2, entry.name);
702
+ entry.isDirectory() && entry.name !== "node_modules" ? results.push(...collectTsFiles(fullPath)) : entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".d.ts") && results.push({
703
+ path: fullPath,
704
+ content: (0, import_node_fs.readFileSync)(fullPath, "utf-8")
705
+ });
706
+ }
707
+ return results;
708
+ },
709
+ allFiles = collectTsFiles(baseDir),
710
+ modelResolver = createTypeResolver(ts, allFiles, baseDir);
711
+ for (const {
712
+ baseName,
713
+ filePath
714
+ } of unresolvedModels) {
715
+ const resolverSourceFile = modelResolver.program.getSourceFile(filePath);
716
+ if (!resolverSourceFile) continue;
717
+ const resolvedTypes = resolveMutationParamTypes(ts, modelResolver, resolverSourceFile);
718
+ if (resolvedTypes.size === 0) continue;
719
+ const content = (0, import_node_fs.readFileSync)(filePath, "utf-8"),
720
+ sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, !0),
721
+ result = extractMutationsFromModel(ts, sourceFile, content, filePath, !!silent, typeToValibot, resolvedTypes, modelResolver.typeToValibot);
722
+ if (result) {
723
+ result.modelName = baseName;
724
+ const idx = allModelMutations.findIndex(m => m.modelName === baseName);
725
+ idx >= 0 && (allModelMutations[idx] = result);
726
+ }
727
+ }
728
+ }
729
+ for (const model of allModelMutations) model.hasCRUD && (mutationCount += 3), mutationCount += model.custom.filter(m => !model.hasCRUD || !["insert", "update", "delete", "upsert"].includes(m.name)).length;
730
+ if (allModelMutations.length > 0 && writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "syncedMutations.ts"), generateSyncedMutationsFile(allModelMutations)) && filesChanged++, filesChanged > 0 && !silent && console.info(`\u2713 ${allModelFiles.length} models (${filesWithSchema.length} schemas)${queryCount ? `, ${queryCount} queries` : ""}${mutationCount ? `, ${mutationCount} mutations` : ""}`), filesChanged > 0 && after) {
313
731
  const {
314
732
  execSync
315
733
  } = await import("node:child_process");
@@ -329,7 +747,8 @@ async function generate(options) {
329
747
  filesChanged,
330
748
  modelCount: allModelFiles.length,
331
749
  schemaCount: filesWithSchema.length,
332
- queryCount
750
+ queryCount,
751
+ mutationCount
333
752
  };
334
753
  }
335
754
  async function watch(options) {