Skip to main content
Version: 3.0 Beta

Strongly Typed JSON

🔋 ZenStack vs Prisma

Strongly typed JSON is a ZModel feature and doesn't exist in Prisma.

ZModel allows you to define custom types and use them to type JSON fields. The ORM respects such fields in two ways:

  1. The return type of such fields is typed as TypeScript types derived from the ZModel custom type definition.
  2. When creating or updating such fields, the ORM validates the input against the custom type definition. The engine "loosely" validates the mutation input and doesn't prevent you from including fields not defined in the custom type.

Samples​

Click here to open an interactive playground.
zenstack/schema.zmodel
datasource db {
provider = 'sqlite'
}

type Profile {
age Int
gender String?
}

model User {
id Int @id @default(autoincrement())
email String @unique
profile Profile @json
}
main.ts
import { createClient } from './db';

async function main() {
const db = await createClient();

try {
await db.user.create({
// @ts-expect-error
data: { email: 'u1@test.com', profile: { gender: 'male' } }
});
} catch (err: any) {
console.log('Got expected error:', err.message);
}

// query results have the `profile` file strongly typed
const user = await db.user.create({
data: { email: 'u1@test.com', profile: { gender: 'male', age: 20 } }
});
console.log(`User created: age ${user.profile.age}, gender ${user.profile.gender}`);

// it doesn't prevent you from adding extra fields to the object
console.log('Update typed-JSON field with extra fields');
console.log(await db.user.update({
where: { id: user.id },
data: { profile: { ...user.profile, tag: 'vip' }}
}));
}

main();
Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below