API Reference

The Accelerate API reference documentation is based on the following schema:

model User {
id Int @id @default(autoincrement())
name String?
email String @unique
}

All example are based on the User model.

cacheStrategy

With the Accelerate extension for Prisma Client, you can use the cacheStrategy parameter for model queries and use the ttl and swr parameters to define a cache strategy for Accelerate. The Accelerate extension requires that you install Prisma Client version 4.10.0.

Options

The cacheStrategy parameter takes an option with the following keys:

OptionExampleTypeRequiredDescription
swr60IntNoThe stale-while-revalidate time in seconds.
ttl60IntNoThe time-to-live time in seconds.

Examples

Add a caching strategy to a query that defines a 60-second stale-while-revalidate value and 60-second time-to-live value:

await prisma.user.findMany({
where: {
email: {
contains: 'alice@prisma.io',
},
},
cacheStrategy: { swr: 60, ttl: 60 },
})

Supported Prisma Client operations

The following is a list of all read query operations and support cacheStrategy:

The cacheStrategy parameter is not supported on any write operations, such as create.

withAccelerateInfo

Any query that supports the cacheStrategy can append withAccelerateInfo() to wrap the response data and include additional information about the Accelerate response.

To retrieve the status of the response, use:

const { data, info } = await prisma.user
.count({
cacheStrategy: { ttl: 60, swr: 600 },
where: { myField: 'value' },
})
.withAccelerateInfo()
console.dir(info)

Notice the info property of the response object. This is where the request information is stored.

Return type

The info object is of type AccelerateInfo and follows the interface below:

interface AccelerateInfo {
cacheStatus: 'ttl' | 'swr' | 'miss' | 'none'
lastModified: Date
region: string
requestId: string
signature: string
}
PropertyTypeDescription
cacheStatus"ttl" | "swr" | "miss" | "none" The cache status of the response.
  • ttl indicates a cache hit within the ttl duration and no database query was executed
  • swr indicates a cache hit within the swr duration and the data is being refreshed by Accelerate in the background
  • miss indicates that both ttl and swr have expired and the database query was executed by the request
  • none indicates that no cache strategy was specified and the database query was executed by the request
lastModifiedDateThe date the response was last refreshed.
regionStringThe data center region that received the request.
requestIdStringUnique identifier of the request. Useful for troubleshooting.
signatureStringThe unique signature of the Prisma operation.

Errors

Prisma Accelerate-related errors start with P6xxx.

You can find the full error code reference for Prisma Accelerate here.