Virtual Fields
A virtual field is computed synchronously over query results and is never written to the database. Both the field value and its return type are inferred automatically. Virtual fields are attached with $extend; connection and runtime options remain on createClient.
Definition
ts
import { createClient } from "./generated/client.ts";
const base = createClient({ connection: "mysql://root:test_password@127.0.0.1:3306/app" });
const db = base.$extend({
virtuals: {
user: {
displayName: {
needs: { name: true, email: true },
compute: (row) => row.name ?? row.email,
},
},
},
});1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
| Property | Description |
|---|---|
needs | The physical scalar fields the computation depends on |
compute | A synchronous pure function; typed with the full row, but at runtime only the fields declared in needs are exposed |
Sharing Across Models
When several models need the same virtual field, lift the definition into a constant and spread it into each model; model-specific fields go inline after the spread:
ts
const shared = {
emailDomain: {
needs: { email: true },
compute: (row: Readonly<{ email: string }>) => row.email.split("@")[1] ?? "",
},
} as const;
const db = createClient({ connection: "mysql://root:test_password@127.0.0.1:3306/app" }).$extend({
virtuals: {
user: {
...shared,
displayName: {
needs: { name: true, email: true },
compute: (row) => row.name ?? row.email,
},
},
account: shared,
},
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- The shared constant maps field names to definitions and must be declared
as const, otherwise thetrueinneedswidens tobooleanand the assignment fails. Because it is a named constant, itscomputeargument must be annotated with the common subset of fields, which must exist on every model that uses it; inline model-specific fields need no annotation — theircomputeargument is automatically the full row of the owning model. needsfields are validated per model when the client is extended; a missing field is an error.
Layering, lifecycle sharing, and chaining: Extensions.
Reading
The default full record includes virtual fields; you can also pick them with select or drop them with omit:
ts
const rows = await db.user.findMany({ select: { displayName: true } });
const others = await db.user.findMany({ omit: { displayName: true } });1
2
2
Dependency fields load automatically; select determines the result fields.
Rules
computemust be synchronous and may read only declared dependencies; returning a Promise or reading an undeclared dependency raises an error.- Dependencies may only be physical scalar fields; depending on another virtual field, circular dependencies, and name conflicts are rejected when the client is extended.
- Virtual fields appear only in query results and are never stored in the database.
Next Steps
- Extensions: layering several virtual field definitions
- Queries: selecting and omitting fields in a query