@vibeorm/generator 1.1.4 → 1.1.5
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.
|
|
3
|
+
"version": "1.1.5",
|
|
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.
|
|
40
|
+
"@vibeorm/parser": "1.1.4"
|
|
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,319 @@
|
|
|
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
|
+
|
|
78
|
+
// ─── ViewDefinitionInput ──────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
parts.push(`// ─── View Definition ──────────────────────────────────────────────`);
|
|
81
|
+
parts.push(``);
|
|
82
|
+
parts.push(`/**`);
|
|
83
|
+
parts.push(` * Restricts what can be passed to defineView({ definition }).`);
|
|
84
|
+
parts.push(` * Maps model camelCase keys to boolean field maps.`);
|
|
85
|
+
parts.push(` */`);
|
|
86
|
+
|
|
87
|
+
const defEntries = schema.models.map((m) => {
|
|
88
|
+
const modelVar = toCamelCase({ str: m.name });
|
|
89
|
+
const scalarFields = m.fields
|
|
90
|
+
.filter((f) => f.kind === "scalar" || f.kind === "enum")
|
|
91
|
+
.map((f) => `"${f.name}"`)
|
|
92
|
+
.join(" | ");
|
|
93
|
+
return ` ${modelVar}?: Partial<Record<${scalarFields}, true>>;`;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
parts.push(`export type ViewDefinitionInput = {`);
|
|
97
|
+
parts.push(defEntries.join("\n"));
|
|
98
|
+
parts.push(`};`);
|
|
99
|
+
parts.push(``);
|
|
100
|
+
|
|
101
|
+
// ─── Per-model scalar field keys type ─────────────────────────────
|
|
102
|
+
|
|
103
|
+
for (const model of schema.models) {
|
|
104
|
+
const scalarFieldNames = model.fields
|
|
105
|
+
.filter((f) => f.kind === "scalar" || f.kind === "enum")
|
|
106
|
+
.map((f) => `"${f.name}"`)
|
|
107
|
+
.join(" | ");
|
|
108
|
+
parts.push(`type ${model.name}ScalarFieldKeys = ${scalarFieldNames};`);
|
|
109
|
+
}
|
|
110
|
+
parts.push(``);
|
|
111
|
+
|
|
112
|
+
// ─── Per-model ViewFindArgs ───────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
for (const model of schema.models) {
|
|
115
|
+
parts.push(generateViewFindArgs({ model, schema }));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ─── Per-model ViewDelegate ───────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
for (const model of schema.models) {
|
|
121
|
+
parts.push(generateViewDelegate({ model, schema }));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ─── ViewClient mapped type ───────────────────────────────────────
|
|
125
|
+
|
|
126
|
+
parts.push(generateViewClientType({ schema }));
|
|
127
|
+
|
|
128
|
+
return parts.join("\n");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ─── ViewFindArgs (per model) ─────────────────────────────────────
|
|
132
|
+
|
|
133
|
+
function generateViewFindArgs(params: { model: Model; schema: Schema }): string {
|
|
134
|
+
const { model, schema } = params;
|
|
135
|
+
const n = model.name;
|
|
136
|
+
|
|
137
|
+
const relationFields = model.fields.filter(
|
|
138
|
+
(f): f is RelationField => f.kind === "relation"
|
|
139
|
+
);
|
|
140
|
+
const hasRelations = relationFields.length > 0;
|
|
141
|
+
|
|
142
|
+
// Build the include type for views (maps relation names to view-aware nested args)
|
|
143
|
+
let viewIncludeType = "Record<string, never>";
|
|
144
|
+
if (hasRelations) {
|
|
145
|
+
const includeEntries = relationFields.map((f) => {
|
|
146
|
+
const relModelVar = toCamelCase({ str: f.relatedModel });
|
|
147
|
+
if (f.isList) {
|
|
148
|
+
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>;`;
|
|
149
|
+
}
|
|
150
|
+
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>;`;
|
|
151
|
+
});
|
|
152
|
+
viewIncludeType = `{\n${includeEntries.join("\n")}\n }`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return `
|
|
156
|
+
// ─── ${n} View Args ─────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
export type ${n}ViewFindManyArgs<
|
|
159
|
+
VF extends Record<string, true> = Record<string, true>,
|
|
160
|
+
VD extends ViewDefinitionInput = ViewDefinitionInput,
|
|
161
|
+
> = {
|
|
162
|
+
select?: Partial<Record<keyof VF & ${n}ScalarFieldKeys, boolean>>;
|
|
163
|
+
reveal?: Partial<Record<Exclude<${n}ScalarFieldKeys, keyof VF>, boolean>>;
|
|
164
|
+
include?: ${hasRelations ? viewIncludeType : "Record<string, never>"};
|
|
165
|
+
where?: ${n}WhereInput;
|
|
166
|
+
orderBy?: ${n}OrderByInput | ${n}OrderByInput[];
|
|
167
|
+
take?: number;
|
|
168
|
+
skip?: number;
|
|
169
|
+
cursor?: ${n}WhereUniqueInput;
|
|
170
|
+
distinct?: (keyof ${n})[];
|
|
171
|
+
relationStrategy?: "query" | "join";
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export type ${n}ViewFindFirstArgs<
|
|
175
|
+
VF extends Record<string, true> = Record<string, true>,
|
|
176
|
+
VD extends ViewDefinitionInput = ViewDefinitionInput,
|
|
177
|
+
> = ${n}ViewFindManyArgs<VF, VD>;
|
|
178
|
+
|
|
179
|
+
export type ${n}ViewFindUniqueArgs<
|
|
180
|
+
VF extends Record<string, true> = Record<string, true>,
|
|
181
|
+
VD extends ViewDefinitionInput = ViewDefinitionInput,
|
|
182
|
+
> = {
|
|
183
|
+
select?: Partial<Record<keyof VF & ${n}ScalarFieldKeys, boolean>>;
|
|
184
|
+
reveal?: Partial<Record<Exclude<${n}ScalarFieldKeys, keyof VF>, boolean>>;
|
|
185
|
+
include?: ${hasRelations ? viewIncludeType : "Record<string, never>"};
|
|
186
|
+
where: ${n}WhereUniqueInput;
|
|
187
|
+
relationStrategy?: "query" | "join";
|
|
188
|
+
};
|
|
189
|
+
`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ─── ViewDelegate (per model) ─────────────────────────────────────
|
|
193
|
+
|
|
194
|
+
function generateViewDelegate(params: { model: Model; schema: Schema }): string {
|
|
195
|
+
const { model } = params;
|
|
196
|
+
const n = model.name;
|
|
197
|
+
|
|
198
|
+
return `
|
|
199
|
+
// ─── ${n} View Delegate ─────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
export type ${n}ViewDelegate<
|
|
202
|
+
VF extends Record<string, true> = Record<string, true>,
|
|
203
|
+
VD extends ViewDefinitionInput = ViewDefinitionInput,
|
|
204
|
+
> = {
|
|
205
|
+
/**
|
|
206
|
+
* Find zero or more ${n} records, restricted to view fields.
|
|
207
|
+
*/
|
|
208
|
+
findMany<T extends ${n}ViewFindManyArgs<VF, VD>>(
|
|
209
|
+
args?: Exact<T, ${n}ViewFindManyArgs<VF, VD>>
|
|
210
|
+
): Promise<ViewResultWrapper<
|
|
211
|
+
ViewSelectResult<${n}, VF, T>
|
|
212
|
+
>[]>;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Find the first ${n} matching the filter, or null.
|
|
216
|
+
*/
|
|
217
|
+
findFirst<T extends ${n}ViewFindFirstArgs<VF, VD>>(
|
|
218
|
+
args?: Exact<T, ${n}ViewFindFirstArgs<VF, VD>>
|
|
219
|
+
): Promise<ViewResultWrapper<
|
|
220
|
+
ViewSelectResult<${n}, VF, T>
|
|
221
|
+
> | null>;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Find a unique ${n} by its primary key or unique field, or null.
|
|
225
|
+
*/
|
|
226
|
+
findUnique<T extends ${n}ViewFindUniqueArgs<VF, VD>>(
|
|
227
|
+
args: Exact<T, ${n}ViewFindUniqueArgs<VF, VD>>
|
|
228
|
+
): Promise<ViewResultWrapper<
|
|
229
|
+
ViewSelectResult<${n}, VF, T>
|
|
230
|
+
> | null>;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Find a unique ${n} or throw if not found.
|
|
234
|
+
*/
|
|
235
|
+
findUniqueOrThrow<T extends ${n}ViewFindUniqueArgs<VF, VD>>(
|
|
236
|
+
args: Exact<T, ${n}ViewFindUniqueArgs<VF, VD>>
|
|
237
|
+
): Promise<ViewResultWrapper<
|
|
238
|
+
ViewSelectResult<${n}, VF, T>
|
|
239
|
+
>>;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Find the first ${n} matching the filter, or throw.
|
|
243
|
+
*/
|
|
244
|
+
findFirstOrThrow<T extends ${n}ViewFindFirstArgs<VF, VD>>(
|
|
245
|
+
args?: Exact<T, ${n}ViewFindFirstArgs<VF, VD>>
|
|
246
|
+
): Promise<ViewResultWrapper<
|
|
247
|
+
ViewSelectResult<${n}, VF, T>
|
|
248
|
+
>>;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Count matching ${n} records.
|
|
252
|
+
*/
|
|
253
|
+
count(args?: ${n}CountArgs): Promise<number>;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Aggregate ${n} records.
|
|
257
|
+
*/
|
|
258
|
+
aggregate(args: ${n}AggregateArgs): Promise<Aggregate${n}Result>;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Group ${n} records.
|
|
262
|
+
*/
|
|
263
|
+
groupBy(args: ${n}GroupByArgs): Promise<${n}GroupByResult[]>;
|
|
264
|
+
};
|
|
265
|
+
`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ─── ViewClient mapped type ───────────────────────────────────────
|
|
269
|
+
|
|
270
|
+
function generateViewClientType(params: { schema: Schema }): string {
|
|
271
|
+
const { schema } = params;
|
|
272
|
+
|
|
273
|
+
const branches = schema.models.map((m) => {
|
|
274
|
+
const modelVar = toCamelCase({ str: m.name });
|
|
275
|
+
return ` K extends "${modelVar}"
|
|
276
|
+
? ${m.name}ViewDelegate<
|
|
277
|
+
NonNullable<D["${modelVar}"]> extends Record<string, true> ? NonNullable<D["${modelVar}"]> : Record<string, true>,
|
|
278
|
+
D
|
|
279
|
+
>
|
|
280
|
+
:`;
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
return `
|
|
284
|
+
// ─── View Result Type Computation ───────────────────────────────
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Computes the result type for a view query.
|
|
288
|
+
* Picks view fields from the model, adds revealed fields, handles select narrowing.
|
|
289
|
+
*/
|
|
290
|
+
type ViewSelectResult<
|
|
291
|
+
Model,
|
|
292
|
+
VF extends Record<string, true>,
|
|
293
|
+
Args,
|
|
294
|
+
> =
|
|
295
|
+
// If user provided select, narrow to selected view fields
|
|
296
|
+
Args extends { select: infer S extends Record<string, boolean> }
|
|
297
|
+
? Pick<Model, Extract<keyof S & keyof Model, { [K in keyof S]: S[K] extends true ? K : never }[keyof S]>>
|
|
298
|
+
& (Args extends { reveal: infer R extends Record<string, boolean> }
|
|
299
|
+
? Pick<Model, Extract<keyof R & keyof Model, { [K in keyof R]: R[K] extends true ? K : never }[keyof R]>>
|
|
300
|
+
: unknown)
|
|
301
|
+
: // Default: pick view fields
|
|
302
|
+
Pick<Model, keyof VF & keyof Model>
|
|
303
|
+
& (Args extends { reveal: infer R extends Record<string, boolean> }
|
|
304
|
+
? Pick<Model, Extract<keyof R & keyof Model, { [K in keyof R]: R[K] extends true ? K : never }[keyof R]>>
|
|
305
|
+
: unknown);
|
|
306
|
+
|
|
307
|
+
// ─── View Client ────────────────────────────────────────────────
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* A read-only scoped client created by \`defineView()\`.
|
|
311
|
+
* Only exposes models defined in the view, with field restrictions.
|
|
312
|
+
*/
|
|
313
|
+
export type ViewClient<D extends ViewDefinitionInput> = {
|
|
314
|
+
[K in keyof D & string]:
|
|
315
|
+
${branches.join("\n")}
|
|
316
|
+
never;
|
|
317
|
+
};
|
|
318
|
+
`;
|
|
319
|
+
}
|