Skip to content

Commit

Permalink
Singletons (#7863)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
emmatown and dcousens committed Sep 7, 2022
1 parent f251e51 commit 524431b
Show file tree
Hide file tree
Showing 87 changed files with 1,258 additions and 151 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-experts-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Adds a new `singleton` list function for configuring Singleton lists
49 changes: 49 additions & 0 deletions ;e
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { KeystoneConfig } from '../../types';
import { idFieldType } from '../id-field';

/* Validate lists config and default the id field */
export function applyIdFieldDefaults(config: KeystoneConfig): KeystoneConfig['lists'] {
const lists: KeystoneConfig['lists'] = {};
const defaultIdField = config.db.idField ?? { kind: 'cuid' };
if (
defaultIdField.kind === 'autoincrement' &&
defaultIdField.type === 'BigInt' &&
config.db.provider === 'sqlite'
) {
throw new Error(
'BigInt autoincrements are not supported on SQLite but they are configured as the global id field type at db.idField'
);
}
Object.keys(config.lists).forEach(key => {
const listConfig = config.lists[key];
if (listConfig.fields.id) {
throw new Error(
`A field with the \`id\` path is defined in the fields object on the ${JSON.stringify(
key
)} list. This is not allowed, use the idField option instead.`
);
}
if (
listConfig.db?.idField?.kind === 'autoincrement' &&
listConfig.db.idField.type === 'BigInt' &&
config.db.provider === 'sqlite'
) {
throw new Error(
`BigInt autoincrements are not supported on SQLite but they are configured at db.idField on the ${key} list`
);
}

if (listConfig.isSingleton && listConfig.db?.idField) {
throw new Error(
`A singleton list cannot specify an idField, but it is configured at db.idField on the ${key} list`
);
}
const idFieldConfig = listConfig.db?.idField ?? defaultIdField;

const idField = idFieldType(idFieldConfig, !!listConfig.isSingleton);

const fields = { id: idField, ...listConfig.fields };
lists[key] = { ...listConfig, fields };
});
return lists;
}
1 change: 1 addition & 0 deletions examples/assets-local/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/assets-s3/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/auth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/basic/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/blog/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/custom-admin-ui-logo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/custom-admin-ui-navigation/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/custom-admin-ui-pages/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/custom-field-view/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/custom-field/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/custom-session-validation/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/default-values/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.1",
"private": true,
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/document-field-customisation/keystone-server",
"scripts": {
"scripts": {
"dev": "keystone dev --seed-database",
"start": "keystone start --seed-database",
"build": "keystone build"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/document-field/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
3 changes: 1 addition & 2 deletions examples/e2e-boilerplate/keystone-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"version": "0.0.1",
"private": true,
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/e2e-boilerplate/keystone-server",
"scripts": {

"scripts": {
"dev": "keystone dev --seed-database",
"start": "keystone start --seed-database",
"build": "keystone build"
Expand Down
1 change: 1 addition & 0 deletions examples/e2e-boilerplate/keystone-server/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/ecommerce/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/embedded-nextjs/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/extend-graphql-schema-graphql-ts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/extend-graphql-schema-nexus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/extend-graphql-schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/extend-graphql-subscriptions/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/feature-boilerplate/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/graphql-api-endpoint/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/graphql-ts-gql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/json/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/rest-api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/roles/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ type KeystoneAdminUIListMeta {
fields: [KeystoneAdminUIFieldMeta!]!
initialSort: KeystoneAdminUISort
isHidden: Boolean!
isSingleton: Boolean
}

type KeystoneAdminUIFieldMeta {
Expand Down
1 change: 1 addition & 0 deletions examples/singleton/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Base Project - Singleton
17 changes: 17 additions & 0 deletions examples/singleton/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { insertSeedData } from './seed-data';
import { Context } from '.keystone/types';

export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
async onConnect(context: Context) {
if (process.argv.includes('--seed-data')) {
await insertSeedData(context);
}
},
},
lists,
});
19 changes: 19 additions & 0 deletions examples/singleton/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@keystone-6/singleton",
"version": "0.0.4",
"private": true,
"license": "MIT",
"scripts": {
"dev": "keystone dev",
"start": "keystone start",
"build": "keystone build",
"seed-data": "keystone --seed-data"
},
"dependencies": {
"@keystone-6/core": "^2.2.0"
},
"devDependencies": {
"typescript": "^4.7.4"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/singleton"
}
7 changes: 7 additions & 0 deletions examples/singleton/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template": "node",
"container": {
"startScript": "keystone dev",
"node": "14"
}
}
Loading

0 comments on commit 524431b

Please sign in to comment.