Skip to content

Add a @readonly attribute #2359

@sanny-io

Description

@sanny-io

This attribute would mark a field as readonly, generating a schema that omits the field from create/update/upsert mutation types, but remains present elsewhere. This is useful in restricting fields to their default values.

It could optionally accept a parameter that allows the field to be present in the input, but only during creation. This is useful for allowing relations to be initialized, but not changed at a later time.

The presence of a readonly field in an input where the policy does not allow for it would throw a rejected by policy error.

Note that there is already some overlap in the current @@deny attribute and #671, but this feature would be much more ergonomic for the most common use cases. It improves the readability of zmodel files, and surfaces the restriction to clients at build-time rather than runtime.

Sample Usage

model User {
    id String @id @default(uuid()) @readonly
    name String

    createdAt DateTime @default(now()) @readonly
    updatedAt DateTime @updatedAt @readonly

    posts Post[]
}

model Post {
    id String @id @default(uuid()) @readonly
    title String

    userId String @readonly(creatable: true)
    user User @relation(fields: [userId], references: [id])

    createdAt DateTime @default(now()) @readonly
    updatedAt DateTime @updatedAt @readonly
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions