Skip to Page NavigationSkip to Page NavigationSkip to Content
Keystone 6 is in Community Preview! What does that mean? see our Roadmap. For Keystone 5 docs, visit v5.keystonejs.com

Database Items API

The database items 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.

Refer to the internal items guide for details on how to work with internal items in Keystone.

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.lists.<listName>.

{
findOne({ where: { id } }),
findMany({ where, first, skip, sortBy }),
count({ where, first, skip }),
createOne({ data }),
createMany({ data }),
updateOne({ id, data }),
updateMany({ data }),
deleteOne({ id }),
deleteMany({ ids }),
}

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.lists.User.findOne({
where: { id: '...' },
});

findMany

All arguments are optional.

const users = await context.db.lists.User.findMany({
where: { name_starts_with: 'A' },
first: 10,
skip: 20,
sortBy: ['name_ASC'],
});

count

All arguments are optional.

const count = await context.db.lists.User.count({
where: { name_starts_with: 'A' },
first: 10,
skip: 20,
});

createOne

const user = await context.db.lists.User.createOne({
data: {
name: 'Alice',
posts: { create: [{ title: 'My first post' }] },
},
});

createMany

const users = await context.db.lists.User.createMany({
data: [
{
data: {
name: 'Alice',
posts: [{ create: { title: 'Alices first post' } }],
},
},
{
data: {
name: 'Bob',
posts: [{ create: { title: 'Bobs first post' } }],
},
},
],
});

updateOne

const user = await context.db.lists.User.updateOne({
id: '...',
data: {
name: 'Alice',
posts: { create: [{ title: 'My first post' }] },
},
});

updateMany

const users = await context.db.lists.User.updateMany({
data: [
{
id: '...',
data: {
name: 'Alice',
posts: [{ create: { title: 'Alices first post' } }],
},
},
{
id: '...',
data: {
name: 'Bob',
posts: [{ create: { title: 'Bobs first post' } }],
},
},
],
});

deleteOne

const user = await context.db.lists.User.deleteOne({
id: '...',
});

deleteMany

const user = await context.db.lists.User.deleteMany({
ids: ['...', '...'],
});