authorship() function

Build the query extension that stamps who created, updated or deleted a row.

Signature:

export declare function authorship(input: AuthorshipOptions): (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/client").InternalArgs<{}, {}, {}, {}>>;

Parameters

Parameter

Type

Description

input

AuthorshipOptions

The per-model column config and the actor resolver.

Returns:

(client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/client").InternalArgs<{}, {}, {}, {}>>

A Prisma extension to pass to client.$extends().

Remarks

Five operations are covered. create and createMany stamp createdBy and updatedBy (each element of a createMany array individually). update and updateMany stamp updatedBy, plus deletedBy when the write is also setting the model's deletedAt column — which is what a soft delete rerouted by softDelete() looks like from here. upsert stamps its create and update branches with the matching rule, resolving the actor once for both.

Authorship cannot be spoofed: any caller-supplied value for the three authorship columns is stripped from data before the stamp is applied, so this extension is their sole writer. That holds even when there is no actor — with getActorId returning null, the caller's values are still removed and nothing is written in their place, so a system write leaves the columns untouched rather than taking whatever the caller passed.

Models absent from models pass through completely untouched, authorship columns included.

Example

const client = new PrismaClient().$extends(authorship({
    models: AUTHORSHIP_MODELS,
    getActorId: () => context.get()?.userId ?? null,
}));

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.