@vibeorm/generator 1.1.3 → 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
|
|
|
@@ -47,7 +47,7 @@ export function generateDelegates(params: { schema: Schema }): string {
|
|
|
47
47
|
const extraImports = hasJsonField ? ", JsonValue" : "";
|
|
48
48
|
parts.push(`import type { ${payloadImports}${extraImports} } from "./models.ts";`);
|
|
49
49
|
|
|
50
|
-
parts.push(`import type { GetResult } from "./result.ts";`);
|
|
50
|
+
parts.push(`import type { GetResult, Exact } from "./result.ts";`);
|
|
51
51
|
|
|
52
52
|
// Import enums if any models have enum fields
|
|
53
53
|
if (schema.enums.length > 0) {
|
|
@@ -123,7 +123,7 @@ ${findManyWhereBlock}${includeBlock}
|
|
|
123
123
|
* });
|
|
124
124
|
*/
|
|
125
125
|
findMany<T extends ${model.name}FindManyArgs>(
|
|
126
|
-
args?: T,
|
|
126
|
+
args?: Exact<T, ${model.name}FindManyArgs>,
|
|
127
127
|
): Promise<GetResult<${p}, T, "findMany">>;
|
|
128
128
|
|
|
129
129
|
/**
|
|
@@ -138,7 +138,7 @@ ${findManyWhereBlock}
|
|
|
138
138
|
* });
|
|
139
139
|
*/
|
|
140
140
|
findFirst<T extends ${model.name}FindFirstArgs>(
|
|
141
|
-
args?: T,
|
|
141
|
+
args?: Exact<T, ${model.name}FindFirstArgs>,
|
|
142
142
|
): Promise<GetResult<${p}, T, "findFirst">>;
|
|
143
143
|
|
|
144
144
|
/**
|
|
@@ -150,7 +150,7 @@ ${uniqueWhereBlock}
|
|
|
150
150
|
* });
|
|
151
151
|
*/
|
|
152
152
|
findUnique<T extends ${model.name}FindUniqueArgs>(
|
|
153
|
-
args: T,
|
|
153
|
+
args: Exact<T, ${model.name}FindUniqueArgs>,
|
|
154
154
|
): Promise<GetResult<${p}, T, "findUnique">>;
|
|
155
155
|
|
|
156
156
|
/**
|
|
@@ -162,14 +162,14 @@ ${uniqueWhereBlock}
|
|
|
162
162
|
* });
|
|
163
163
|
*/
|
|
164
164
|
findUniqueOrThrow<T extends ${model.name}FindUniqueArgs>(
|
|
165
|
-
args: T,
|
|
165
|
+
args: Exact<T, ${model.name}FindUniqueArgs>,
|
|
166
166
|
): Promise<GetResult<${p}, T, "findUniqueOrThrow">>;
|
|
167
167
|
|
|
168
168
|
/**
|
|
169
169
|
* Find the first ${model.name} record matching a filter, or throw if not found.
|
|
170
170
|
*/
|
|
171
171
|
findFirstOrThrow<T extends ${model.name}FindFirstArgs>(
|
|
172
|
-
args?: T,
|
|
172
|
+
args?: Exact<T, ${model.name}FindFirstArgs>,
|
|
173
173
|
): Promise<GetResult<${p}, T, "findFirstOrThrow">>;
|
|
174
174
|
|
|
175
175
|
/**
|
|
@@ -181,7 +181,7 @@ ${createDataBlock}
|
|
|
181
181
|
* });
|
|
182
182
|
*/
|
|
183
183
|
create<T extends ${model.name}CreateArgs>(
|
|
184
|
-
args: T,
|
|
184
|
+
args: Exact<T, ${model.name}CreateArgs>,
|
|
185
185
|
): Promise<GetResult<${p}, T, "create">>;
|
|
186
186
|
|
|
187
187
|
/**
|
|
@@ -209,7 +209,7 @@ ${createManyDataBlock}
|
|
|
209
209
|
* });
|
|
210
210
|
*/
|
|
211
211
|
createManyAndReturn<T extends ${model.name}CreateManyAndReturnArgs>(
|
|
212
|
-
args: T,
|
|
212
|
+
args: Exact<T, ${model.name}CreateManyAndReturnArgs>,
|
|
213
213
|
): Promise<GetResult<${p}, T, "createManyAndReturn">>;
|
|
214
214
|
|
|
215
215
|
/**
|
|
@@ -222,7 +222,7 @@ ${updateDataBlock}
|
|
|
222
222
|
* });
|
|
223
223
|
*/
|
|
224
224
|
update<T extends ${model.name}UpdateArgs>(
|
|
225
|
-
args: T,
|
|
225
|
+
args: Exact<T, ${model.name}UpdateArgs>,
|
|
226
226
|
): Promise<GetResult<${p}, T, "update">>;
|
|
227
227
|
|
|
228
228
|
/**
|
|
@@ -236,7 +236,7 @@ ${upsertUpdateBlock}
|
|
|
236
236
|
* });
|
|
237
237
|
*/
|
|
238
238
|
upsert<T extends ${model.name}UpsertArgs>(
|
|
239
|
-
args: T,
|
|
239
|
+
args: Exact<T, ${model.name}UpsertArgs>,
|
|
240
240
|
): Promise<GetResult<${p}, T, "upsert">>;
|
|
241
241
|
|
|
242
242
|
/**
|
|
@@ -248,7 +248,7 @@ ${uniqueWhereBlock}
|
|
|
248
248
|
* });
|
|
249
249
|
*/
|
|
250
250
|
delete<T extends ${model.name}DeleteArgs>(
|
|
251
|
-
args: T,
|
|
251
|
+
args: Exact<T, ${model.name}DeleteArgs>,
|
|
252
252
|
): Promise<GetResult<${p}, T, "delete">>;
|
|
253
253
|
|
|
254
254
|
/**
|
|
@@ -36,6 +36,25 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
|
36
36
|
/** Flatten intersection types for cleaner hover tooltips */
|
|
37
37
|
type Compute<T> = T extends Function ? T : { [K in keyof T]: T[K] } & unknown;
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Enforces strict type safety by recursively rejecting excess properties.
|
|
41
|
+
*
|
|
42
|
+
* TypeScript's structural subtyping allows objects to have extra properties
|
|
43
|
+
* when passed through generics (\`<T extends Args>\`). This utility type
|
|
44
|
+
* maps any key in A that is NOT in W to \`never\`, causing a compile-time
|
|
45
|
+
* error when unknown keys (e.g. typos in where/select/include) are used.
|
|
46
|
+
*
|
|
47
|
+
* Primitives, arrays, functions, and Dates are passed through unchanged
|
|
48
|
+
* to preserve literal types for return type narrowing via GetFindResult.
|
|
49
|
+
*/
|
|
50
|
+
export type Exact<A, W> = W extends unknown
|
|
51
|
+
? A extends W
|
|
52
|
+
? A extends Record<string, unknown>
|
|
53
|
+
? { [K in keyof A]: K extends keyof W ? Exact<A[K], W[K]> : never }
|
|
54
|
+
: A
|
|
55
|
+
: W
|
|
56
|
+
: never;
|
|
57
|
+
|
|
39
58
|
/** Extract scalar fields from a payload (default selection) */
|
|
40
59
|
type DefaultSelection<P extends OperationPayload> = P["scalars"];
|
|
41
60
|
|
|
@@ -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
|
+
}
|