@vibeorm/generator 1.1.4 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vibeorm/generator",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "TypeScript client generator for VibeORM — produces typed delegates, inputs, and Zod schemas from a Prisma schema",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -37,6 +37,6 @@
37
37
  "bun": ">=1.1.0"
38
38
  },
39
39
  "dependencies": {
40
- "@vibeorm/parser": "1.1.3"
40
+ "@vibeorm/parser": "1.1.5"
41
41
  }
42
42
  }
package/src/generate.ts CHANGED
@@ -11,6 +11,7 @@ import { generateResult } from "./generators/generate-result.ts";
11
11
  import { generateDelegates } from "./generators/generate-delegates.ts";
12
12
  import { generateClient } from "./generators/generate-client.ts";
13
13
  import { generateSchemas } from "./generators/generate-schemas.ts";
14
+ import { generateViewTypes } from "./generators/generate-view-types.ts";
14
15
 
15
16
  export type GeneratedFile = {
16
17
  filename: string;
@@ -59,6 +60,10 @@ export function generate(params: { schema: Schema }): GeneratedFile[] {
59
60
  filename: "schemas.ts",
60
61
  content: generateSchemas({ schema }),
61
62
  },
63
+ {
64
+ filename: "view-types.ts",
65
+ content: generateViewTypes({ schema }),
66
+ },
62
67
  ];
63
68
 
64
69
  return files;
@@ -18,6 +18,7 @@ export function generateClient(params: { schema: Schema }): string {
18
18
  parts.push(`export * from "./result.ts";`);
19
19
  parts.push(`export * from "./delegates.ts";`);
20
20
  parts.push(`export * from "./schemas.ts";`);
21
+ parts.push(`export * from "./view-types.ts";`);
21
22
  parts.push(``);
22
23
 
23
24
  // Import delegate types
@@ -26,6 +27,9 @@ export function generateClient(params: { schema: Schema }): string {
26
27
  .join(", ");
27
28
  parts.push(`import type { ${delegateImports} } from "./delegates.ts";`);
28
29
 
30
+ // Import view types
31
+ parts.push(`import type { ViewDefinitionInput, ViewClient } from "./view-types.ts";`);
32
+
29
33
  // Import runtime
30
34
  parts.push(`import { createClient } from "@vibeorm/runtime";`);
31
35
  parts.push(`import type { VibeClientOptions, TransactionOptions } from "@vibeorm/runtime";`);
@@ -62,6 +66,35 @@ ${clientProperties}
62
66
  $executeRawUnsafe(query: string, ...values: unknown[]): Promise<number>;
63
67
  $connect(): Promise<void>;
64
68
  $disconnect(): Promise<void>;
69
+
70
+ /**
71
+ * Create a read-only view that restricts which fields are returned per model.
72
+ *
73
+ * @example
74
+ * const publicApi = db.defineView({
75
+ * name: "public-api",
76
+ * definition: {
77
+ * user: { id: true, name: true, createdAt: true },
78
+ * post: { id: true, title: true },
79
+ * },
80
+ * });
81
+ *
82
+ * // Only returns { id, name, createdAt }
83
+ * const users = await publicApi.user.findMany();
84
+ *
85
+ * // Reveal extra fields (accessible but excluded from JSON.stringify)
86
+ * const user = await publicApi.user.findUniqueOrThrow({
87
+ * where: { id: 1 },
88
+ * reveal: { email: true },
89
+ * });
90
+ * user.email; // accessible
91
+ * JSON.stringify(user); // { id, name, createdAt } — email excluded
92
+ * user.$unsafe(); // { id, name, createdAt, email } — escape hatch
93
+ */
94
+ defineView<D extends ViewDefinitionInput>(params: {
95
+ name: string;
96
+ definition: D;
97
+ }): ViewClient<D>;
65
98
  };
66
99
  `);
67
100
 
@@ -0,0 +1,446 @@
1
+ import type { Model, Schema, RelationField } from "@vibeorm/parser";
2
+ import { fileHeader, toCamelCase } from "../utils.ts";
3
+
4
+ /**
5
+ * Generates view-related types:
6
+ *
7
+ * - ViewDefinitionInput: restricts what fields can go into defineView()
8
+ * - ViewClient<D>: the typed return type of defineView()
9
+ * - Per-model ViewDelegate types with only read operations
10
+ * - Per-model ViewFindManyArgs etc. with reveal support
11
+ * - ViewResultWrapper type for toJSON/$unsafe
12
+ */
13
+ export function generateViewTypes(params: { schema: Schema }): string {
14
+ const { schema } = params;
15
+ const parts: string[] = [fileHeader()];
16
+
17
+ // ─── Imports ──────────────────────────────────────────────────────
18
+
19
+ // Import model types
20
+ const modelImports = schema.models.map((m) => m.name).join(", ");
21
+ parts.push(`import type { ${modelImports} } from "./models.ts";`);
22
+
23
+ // Import payload types
24
+ const payloadImports = schema.models.map((m) => `$${m.name}Payload`).join(", ");
25
+ parts.push(`import type { ${payloadImports}, OperationPayload } from "./models.ts";`);
26
+
27
+ // Import args types (for where, orderBy, whereUnique)
28
+ const argsImports = schema.models.flatMap((m) => [
29
+ `${m.name}CountArgs`,
30
+ `${m.name}AggregateArgs`,
31
+ `${m.name}GroupByArgs`,
32
+ ]).join(", ");
33
+ parts.push(`import type { ${argsImports} } from "./args.ts";`);
34
+
35
+ // Import input types
36
+ const inputImports = schema.models.flatMap((m) => [
37
+ `${m.name}WhereInput`,
38
+ `${m.name}WhereUniqueInput`,
39
+ `${m.name}OrderByInput`,
40
+ ]).join(", ");
41
+ parts.push(`import type { ${inputImports} } from "./inputs.ts";`);
42
+
43
+ // Import delegate types for aggregate/groupBy result types
44
+ const delegateResultImports = schema.models.flatMap((m) => {
45
+ const hasRelations = m.fields.some((f) => f.kind === "relation");
46
+ return [
47
+ `Aggregate${m.name}Result`,
48
+ `${m.name}GroupByResult`,
49
+ ];
50
+ }).join(", ");
51
+ parts.push(`import type { ${delegateResultImports} } from "./delegates.ts";`);
52
+
53
+ // Import Exact from result
54
+ parts.push(`import type { Exact } from "./result.ts";`);
55
+
56
+ // Import ViewResult from runtime
57
+ parts.push(`import { ViewResult } from "@vibeorm/runtime";`);
58
+
59
+ parts.push(``);
60
+
61
+ // ─── ViewResultWrapper ────────────────────────────────────────────
62
+
63
+ parts.push(`
64
+ // ─── View Result Wrapper Types ──────────────────────────────────
65
+
66
+ /**
67
+ * Wraps a query result with view serialization safety.
68
+ * - Direct property access for all fields (view + revealed)
69
+ * - toJSON() excludes revealed fields (called by JSON.stringify)
70
+ * - $unsafe() returns a plain object with all fields
71
+ */
72
+ export type ViewResultWrapper<T> = T & {
73
+ toJSON(): Partial<T>;
74
+ $unsafe(): T;
75
+ };
76
+
77
+ /** Flatten intersection types for cleaner hover tooltips and type resolution */
78
+ type Compute<T> = T extends Function ? T : { [K in keyof T]: T[K] } & unknown;
79
+ `);
80
+
81
+ // ─── ViewDefinitionInput ──────────────────────────────────────────
82
+
83
+ parts.push(`// ─── View Definition ──────────────────────────────────────────────`);
84
+ parts.push(``);
85
+ parts.push(`/**`);
86
+ parts.push(` * Restricts what can be passed to defineView({ definition }).`);
87
+ parts.push(` * Maps model camelCase keys to boolean field maps.`);
88
+ parts.push(` */`);
89
+
90
+ const defEntries = schema.models.map((m) => {
91
+ const modelVar = toCamelCase({ str: m.name });
92
+ const scalarFields = m.fields
93
+ .filter((f) => f.kind === "scalar" || f.kind === "enum")
94
+ .map((f) => `"${f.name}"`)
95
+ .join(" | ");
96
+ return ` ${modelVar}?: Partial<Record<${scalarFields}, true>>;`;
97
+ });
98
+
99
+ parts.push(`export type ViewDefinitionInput = {`);
100
+ parts.push(defEntries.join("\n"));
101
+ parts.push(`};`);
102
+ parts.push(``);
103
+
104
+ // ─── Per-model scalar field keys type ─────────────────────────────
105
+
106
+ for (const model of schema.models) {
107
+ const scalarFieldNames = model.fields
108
+ .filter((f) => f.kind === "scalar" || f.kind === "enum")
109
+ .map((f) => `"${f.name}"`)
110
+ .join(" | ");
111
+ parts.push(`type ${model.name}ScalarFieldKeys = ${scalarFieldNames};`);
112
+ }
113
+ parts.push(``);
114
+
115
+ // ─── Per-model ViewIncludeResult ──────────────────────────────────
116
+
117
+ for (const model of schema.models) {
118
+ parts.push(generateViewIncludeResult({ model, schema }));
119
+ }
120
+
121
+ // ─── Per-model GetViewResult ────────────────────────────────────
122
+
123
+ for (const model of schema.models) {
124
+ parts.push(generateGetViewResult({ model, schema }));
125
+ }
126
+
127
+ // ─── Per-model ViewFindArgs ───────────────────────────────────────
128
+
129
+ for (const model of schema.models) {
130
+ parts.push(generateViewFindArgs({ model, schema }));
131
+ }
132
+
133
+ // ─── Per-model ViewDelegate ───────────────────────────────────────
134
+
135
+ for (const model of schema.models) {
136
+ parts.push(generateViewDelegate({ model, schema }));
137
+ }
138
+
139
+ // ─── ViewClient mapped type ───────────────────────────────────────
140
+
141
+ parts.push(generateViewClientType({ schema }));
142
+
143
+ return parts.join("\n");
144
+ }
145
+
146
+ // ─── ViewIncludeResult (per model) ────────────────────────────────
147
+
148
+ /**
149
+ * Generates a mapped type that computes the result types for included
150
+ * relations on a view query. For each relation field:
151
+ *
152
+ * - If the related model is in VD: result is ViewResultWrapper<Pick<Model, viewFields>>
153
+ * - If the related model is NOT in VD: result is the full model type
154
+ * - Array relations get [], nullable singles get | null
155
+ */
156
+ function generateViewIncludeResult(params: { model: Model; schema: Schema }): string {
157
+ const { model } = params;
158
+ const n = model.name;
159
+
160
+ const relationFields = model.fields.filter(
161
+ (f): f is RelationField => f.kind === "relation"
162
+ );
163
+
164
+ if (relationFields.length === 0) {
165
+ // No relations — include result is always empty
166
+ return `
167
+ // ─── ${n} View Include Result ────────────────────────────────
168
+
169
+ type ${n}ViewIncludeResult<
170
+ I extends Record<string, unknown>,
171
+ VD extends ViewDefinitionInput,
172
+ > = {};
173
+ `;
174
+ }
175
+
176
+ // Build nested conditional branches: K extends "relName" ? ... : K extends ... : never
177
+ const branches = relationFields.map((f) => {
178
+ const relModelVar = toCamelCase({ str: f.relatedModel });
179
+ const relModel = f.relatedModel;
180
+
181
+ // Type when the related model IS in the view definition
182
+ const viewRestricted = `ViewResultWrapper<Pick<${relModel}, keyof RF & keyof ${relModel}>>`;
183
+ // Type when the related model is NOT in the view definition
184
+ const fullType = relModel;
185
+
186
+ let inViewType: string;
187
+ let notInViewType: string;
188
+
189
+ if (f.isList) {
190
+ inViewType = `${viewRestricted}[]`;
191
+ notInViewType = `${fullType}[]`;
192
+ } else if (!f.isRequired) {
193
+ inViewType = `${viewRestricted} | null`;
194
+ notInViewType = `${fullType} | null`;
195
+ } else {
196
+ inViewType = viewRestricted;
197
+ notInViewType = fullType;
198
+ }
199
+
200
+ return ` K extends "${f.name}"
201
+ ? (VD extends { ${relModelVar}: infer RF extends Record<string, true> }
202
+ ? ${inViewType}
203
+ : ${notInViewType})`;
204
+ });
205
+
206
+ return `
207
+ // ─── ${n} View Include Result ────────────────────────────────
208
+
209
+ type ${n}ViewIncludeResult<
210
+ I extends Record<string, unknown>,
211
+ VD extends ViewDefinitionInput,
212
+ > = {
213
+ [K in keyof I as I[K] extends false | undefined | null ? never : K]:
214
+ ${branches.join("\n : ")}
215
+ : never;
216
+ };
217
+ `;
218
+ }
219
+
220
+ // ─── GetViewResult (per model) ────────────────────────────────────
221
+
222
+ /**
223
+ * Generates a top-level conditional type that resolves the complete result
224
+ * type for a view query, including both scalar fields and included relations.
225
+ *
226
+ * The conditional is at the outermost position (matching how GetFindResult
227
+ * works in the regular client) so TypeScript evaluates it eagerly at call
228
+ * sites instead of deferring it inside ViewResultWrapper's type parameter.
229
+ */
230
+ function generateGetViewResult(params: { model: Model; schema: Schema }): string {
231
+ const { model } = params;
232
+ const n = model.name;
233
+ const hasRelations = model.fields.some((f) => f.kind === "relation");
234
+
235
+ if (!hasRelations) return "";
236
+
237
+ return `
238
+ // ─── ${n} Get View Result ───────────────────────────────────
239
+
240
+ type ${n}GetViewResult<
241
+ VF extends Record<string, true>,
242
+ T,
243
+ VD extends ViewDefinitionInput,
244
+ > =
245
+ T extends { include: infer I extends Record<string, unknown> }
246
+ ? ViewResultWrapper<Compute<
247
+ ViewSelectResult<${n}, VF, T>
248
+ & ${n}ViewIncludeResult<I, VD>
249
+ >>
250
+ : ViewResultWrapper<ViewSelectResult<${n}, VF, T>>;
251
+ `;
252
+ }
253
+
254
+ // ─── ViewFindArgs (per model) ─────────────────────────────────────
255
+
256
+ function generateViewFindArgs(params: { model: Model; schema: Schema }): string {
257
+ const { model, schema } = params;
258
+ const n = model.name;
259
+
260
+ const relationFields = model.fields.filter(
261
+ (f): f is RelationField => f.kind === "relation"
262
+ );
263
+ const hasRelations = relationFields.length > 0;
264
+
265
+ // Build the include type for views (maps relation names to view-aware nested args)
266
+ let viewIncludeType = "Record<string, never>";
267
+ if (hasRelations) {
268
+ const includeEntries = relationFields.map((f) => {
269
+ const relModelVar = toCamelCase({ str: f.relatedModel });
270
+ if (f.isList) {
271
+ return ` ${f.name}?: boolean | ${f.relatedModel}ViewFindManyArgs<VD extends { ${relModelVar}: infer RF } ? RF extends Record<string, true> ? RF : Record<string, true> : Record<string, true>, VD>;`;
272
+ }
273
+ return ` ${f.name}?: boolean | ${f.relatedModel}ViewFindFirstArgs<VD extends { ${relModelVar}: infer RF } ? RF extends Record<string, true> ? RF : Record<string, true> : Record<string, true>, VD>;`;
274
+ });
275
+ viewIncludeType = `{\n${includeEntries.join("\n")}\n }`;
276
+ }
277
+
278
+ return `
279
+ // ─── ${n} View Args ─────────────────────────────────────────
280
+
281
+ export type ${n}ViewFindManyArgs<
282
+ VF extends Record<string, true> = Record<string, true>,
283
+ VD extends ViewDefinitionInput = ViewDefinitionInput,
284
+ > = {
285
+ select?: Partial<Record<keyof VF & ${n}ScalarFieldKeys, boolean>>;
286
+ reveal?: Partial<Record<Exclude<${n}ScalarFieldKeys, keyof VF>, boolean>>;
287
+ include?: ${hasRelations ? viewIncludeType : "Record<string, never>"};
288
+ where?: ${n}WhereInput;
289
+ orderBy?: ${n}OrderByInput | ${n}OrderByInput[];
290
+ take?: number;
291
+ skip?: number;
292
+ cursor?: ${n}WhereUniqueInput;
293
+ distinct?: (keyof ${n})[];
294
+ relationStrategy?: "query" | "join";
295
+ };
296
+
297
+ export type ${n}ViewFindFirstArgs<
298
+ VF extends Record<string, true> = Record<string, true>,
299
+ VD extends ViewDefinitionInput = ViewDefinitionInput,
300
+ > = ${n}ViewFindManyArgs<VF, VD>;
301
+
302
+ export type ${n}ViewFindUniqueArgs<
303
+ VF extends Record<string, true> = Record<string, true>,
304
+ VD extends ViewDefinitionInput = ViewDefinitionInput,
305
+ > = {
306
+ select?: Partial<Record<keyof VF & ${n}ScalarFieldKeys, boolean>>;
307
+ reveal?: Partial<Record<Exclude<${n}ScalarFieldKeys, keyof VF>, boolean>>;
308
+ include?: ${hasRelations ? viewIncludeType : "Record<string, never>"};
309
+ where: ${n}WhereUniqueInput;
310
+ relationStrategy?: "query" | "join";
311
+ };
312
+ `;
313
+ }
314
+
315
+ // ─── ViewDelegate (per model) ─────────────────────────────────────
316
+
317
+ function generateViewDelegate(params: { model: Model; schema: Schema }): string {
318
+ const { model } = params;
319
+ const n = model.name;
320
+
321
+ const hasRelations = model.fields.some((f) => f.kind === "relation");
322
+
323
+ // Helper: builds the full result type including the include intersection.
324
+ // Models with relations use a dedicated GetViewResult type that places the
325
+ // conditional at the outermost position (matching GetFindResult in the regular
326
+ // client) so TypeScript evaluates it eagerly instead of deferring it inside
327
+ // ViewResultWrapper's type parameter.
328
+ const resultType = (argsType: string) => {
329
+ if (!hasRelations) {
330
+ return `ViewResultWrapper<ViewSelectResult<${n}, VF, ${argsType}>>`;
331
+ }
332
+ return `${n}GetViewResult<VF, ${argsType}, VD>`;
333
+ };
334
+
335
+ return `
336
+ // ─── ${n} View Delegate ─────────────────────────────────────
337
+
338
+ export type ${n}ViewDelegate<
339
+ VF extends Record<string, true> = Record<string, true>,
340
+ VD extends ViewDefinitionInput = ViewDefinitionInput,
341
+ > = {
342
+ /**
343
+ * Find zero or more ${n} records, restricted to view fields.
344
+ */
345
+ findMany<T extends ${n}ViewFindManyArgs<VF, VD>>(
346
+ args?: Exact<T, ${n}ViewFindManyArgs<VF, VD>>
347
+ ): Promise<${resultType("T")}[]>;
348
+
349
+ /**
350
+ * Find the first ${n} matching the filter, or null.
351
+ */
352
+ findFirst<T extends ${n}ViewFindFirstArgs<VF, VD>>(
353
+ args?: Exact<T, ${n}ViewFindFirstArgs<VF, VD>>
354
+ ): Promise<${resultType("T")} | null>;
355
+
356
+ /**
357
+ * Find a unique ${n} by its primary key or unique field, or null.
358
+ */
359
+ findUnique<T extends ${n}ViewFindUniqueArgs<VF, VD>>(
360
+ args: Exact<T, ${n}ViewFindUniqueArgs<VF, VD>>
361
+ ): Promise<${resultType("T")} | null>;
362
+
363
+ /**
364
+ * Find a unique ${n} or throw if not found.
365
+ */
366
+ findUniqueOrThrow<T extends ${n}ViewFindUniqueArgs<VF, VD>>(
367
+ args: Exact<T, ${n}ViewFindUniqueArgs<VF, VD>>
368
+ ): Promise<${resultType("T")}>;
369
+
370
+ /**
371
+ * Find the first ${n} matching the filter, or throw.
372
+ */
373
+ findFirstOrThrow<T extends ${n}ViewFindFirstArgs<VF, VD>>(
374
+ args?: Exact<T, ${n}ViewFindFirstArgs<VF, VD>>
375
+ ): Promise<${resultType("T")}>;
376
+
377
+ /**
378
+ * Count matching ${n} records.
379
+ */
380
+ count(args?: ${n}CountArgs): Promise<number>;
381
+
382
+ /**
383
+ * Aggregate ${n} records.
384
+ */
385
+ aggregate(args: ${n}AggregateArgs): Promise<Aggregate${n}Result>;
386
+
387
+ /**
388
+ * Group ${n} records.
389
+ */
390
+ groupBy(args: ${n}GroupByArgs): Promise<${n}GroupByResult[]>;
391
+ };
392
+ `;
393
+ }
394
+
395
+ // ─── ViewClient mapped type ───────────────────────────────────────
396
+
397
+ function generateViewClientType(params: { schema: Schema }): string {
398
+ const { schema } = params;
399
+
400
+ const branches = schema.models.map((m) => {
401
+ const modelVar = toCamelCase({ str: m.name });
402
+ return ` K extends "${modelVar}"
403
+ ? ${m.name}ViewDelegate<
404
+ NonNullable<D["${modelVar}"]> extends Record<string, true> ? NonNullable<D["${modelVar}"]> : Record<string, true>,
405
+ D
406
+ >
407
+ :`;
408
+ });
409
+
410
+ return `
411
+ // ─── View Result Type Computation ───────────────────────────────
412
+
413
+ /**
414
+ * Computes the result type for a view query.
415
+ * Picks view fields from the model, adds revealed fields, handles select narrowing.
416
+ */
417
+ type ViewSelectResult<
418
+ Model,
419
+ VF extends Record<string, true>,
420
+ Args,
421
+ > =
422
+ // If user provided select, narrow to selected view fields
423
+ Args extends { select: infer S extends Record<string, boolean> }
424
+ ? Pick<Model, Extract<keyof S & keyof Model, { [K in keyof S]: S[K] extends true ? K : never }[keyof S]>>
425
+ & (Args extends { reveal: infer R extends Record<string, boolean> }
426
+ ? Pick<Model, Extract<keyof R & keyof Model, { [K in keyof R]: R[K] extends true ? K : never }[keyof R]>>
427
+ : unknown)
428
+ : // Default: pick view fields
429
+ Pick<Model, keyof VF & keyof Model>
430
+ & (Args extends { reveal: infer R extends Record<string, boolean> }
431
+ ? Pick<Model, Extract<keyof R & keyof Model, { [K in keyof R]: R[K] extends true ? K : never }[keyof R]>>
432
+ : unknown);
433
+
434
+ // ─── View Client ────────────────────────────────────────────────
435
+
436
+ /**
437
+ * A read-only scoped client created by \`defineView()\`.
438
+ * Only exposes models defined in the view, with field restrictions.
439
+ */
440
+ export type ViewClient<D extends ViewDefinitionInput> = {
441
+ [K in keyof D & string]:
442
+ ${branches.join("\n")}
443
+ never;
444
+ };
445
+ `;
446
+ }