Realtime (TypeScript)
Overview
The Prisma client lets you subscribe to database events and receive updates in realtime using the $subscribe
property. For each model type in your datamodel, the Prisma client exposes one function on this property named after the model (but lowercased). Subscribing to this function means you're interested in write-events (i.e. create, update, delete) for that model. You can provide a filter object that lets you further constrain the kind of events you want to receive updates for. The function returns an async iterator that emits an event any time one of the specified database event happens.
The $subscribe
API is based on WebSockets.
Examples
Subscribe to "create" and "update" events for the User
mode:
const createdAndUpdatedUserIterator: UserAsyncIterator = await db.$subscribe
.user({
mutation_in: ['CREATED', 'UPDATED'],
})
.node()
Subscribe to any write event for User
models where the user has a an email-address that contains the string gmail
:
const userIterator: UserAsyncIterator = await db.$subscribe
.user({
email_contains: `gmail`,
})
.node()