Database
The database API provides a programmatic API for running CRUD operations against the internal GraphQL resolvers in your system. Importantly, this API bypasses the GraphQL Server itself, instead invoking the resolver functions directly. The return values of this API are internal item objects, which are suitable to be returned from GraphQL resolvers.
This API executes the access control
rules and hooks
defined in your system. To bypass these, you can directly use the Prisma Client at context.prisma
.
For each list in your system the following API is available at context.db.<listName>
.
{findOne({ where: { id } }),findMany({ where, take, skip, orderBy }),count({ where }),createOne({ data }),createMany({ data }),updateOne({ where, data }),updateMany({ data }),deleteOne({ where }),deleteMany({ where }),}
The arguments to these functions closely correspond to their equivalent GraphQL APIs. Unless otherwise specified, the arguments to all functions are required.
findOne
const user = await context.db.User.findOne({where: { id: '...' },});
findMany
All arguments are optional.
const users = await context.db.User.findMany({where: { name: { startsWith: 'A' } },take: 10,skip: 20,orderBy: [{ name: 'asc' }],});
count
All arguments are optional.
const count = await context.db.User.count({where: { name: { startsWith: 'A' } },});
createOne
const user = await context.db.User.createOne({data: {name: 'Alice',posts: { create: [{ title: 'My first post' }] },},});
createMany
const users = await context.db.User.createMany({data: [{name: 'Alice',posts: { create: [{ title: 'Alices first post' }] },},{name: 'Bob',posts: { create: [{ title: 'Bobs first post' }] },},],});
updateOne
const user = await context.db.User.updateOne({where: { id: '...' },data: {name: 'Alice',posts: { create: [{ title: 'My first post' }] },},});
updateMany
const users = await context.db.User.updateMany({data: [{where: { id: '...' },data: {name: 'Alice',posts: { create: [{ title: 'Alices first post' }] },},},{where: { id: '...' },data: {name: 'Bob',posts: { create: [{ title: 'Bobs first post' }] },},},],});
deleteOne
const user = await context.db.User.deleteOne({where: { id: '...' },});
deleteMany
const users = await context.db.User.deleteMany({where: [{ id: '...' }, { id: '...' }],});
Related resources
Query API Reference →
A programmatic API for running CRUD operations against your GraphQL API. For each list in your system you get an API at context.query.<listName>
.
Context API Reference →
The API for run-time functionality in your Keystone system. Use it to write business logic for access control, hooks, testing, GraphQL schema extensions, and more.