docs/content/blog/singleton.md
Singleton is one of the most popular community requested features and it is an easy way to work with lists that will only ever have one item like a website configuration or user settings.
You describe the list (Eg. WebsiteConfiguration) like you describe any other list and add the property isSingleton: true to it.
// schema.ts
export default config({
lists: ({
WebsiteConfiguration: list({
...
isSingleton: true,
fields: {
websiteName: text(),
copyrightText: text(),
}
}),
}),
});
In GraphQL, you can read and write from this list without needing an identifier.
query {
websiteConfiguration {
websiteName
copyrightText
}
}
mutation createNew {
createWebsiteConfiguration(
data: { websiteName: "cool website", copyrightText: "do not copy me" }
) {
websiteName
copyrightText
}
}
mutation updateExisting {
updateWebsiteConfiguration(
data: { websiteName: "cool website", copyrightText: "do not copy me" }
) {
websiteName
copyrightText
}
}
Within the server environment, you can read and write from this list using the default identifier 1.
// read
context.query.WebsiteConfiguration.findOne({ where: { id: '1' } });
// update
context.query.WebsiteConfiguration.updateOne({
where: {
id: '1',
},
data: {
websiteName: 'name to update',
copyrightText: 'text to update',
},
});
Read our API docs or refer to our Singleton example to learn more about Singleton.
If you like using Keystone, we'd appreciate a shout out in Twitter and a star in GitHub.