docs/content/docs/context/db-items.md
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 approximate their equivalent GraphQL APIs.
const user = await context.db.User.findOne({
where: { id: '...' },
});
const users = await context.db.User.findMany({
where: { name: { startsWith: 'A' } },
take: 10,
skip: 20,
orderBy: [{ name: 'asc' }],
});
const count = await context.db.User.count({
where: { name: { startsWith: 'A' } },
});
const user = await context.db.User.createOne({
data: {
name: 'Alice',
posts: { create: [{ title: 'My first post' }] },
},
});
const users = await context.db.User.createMany({
data: [
{
name: 'Alice',
posts: { create: [{ title: 'Alices first post' }] },
},
{
name: 'Bob',
posts: { create: [{ title: 'Bobs first post' }] },
},
],
});
const user = await context.db.User.updateOne({
where: { id: '...' },
data: {
name: 'Alice',
posts: { create: [{ title: 'My first post' }] },
},
});
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' }] },
},
},
],
});
const user = await context.db.User.deleteOne({
where: { id: '...' },
});
const users = await context.db.User.deleteMany({
where: [{ id: '...' }, { id: '...' }],
});
{% related-content %}
{% well
heading="Query API Reference"
href="/docs/context/query" %}
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>.
{% /well %}
{% well
heading="Context API Reference"
href="/docs/context/overview" %}
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.
{% /well %}
{% /related-content %}