Skip to content

Commit

Permalink
New id fields (keystonejs#5947)
Browse files Browse the repository at this point in the history
* Id field experimenting

* word

* Try a different default to see what our tests do

* Update things

* snapshots

* Update things

* Remove a thing

* field view

* Fix ts errors

* WIP

* Fix a thing

* Fix more tests

* Fix things

* Fix more things

* More fixes

* Reorder a thing so the failing output is easy to debug

* Try a thing

* Update schemas

* Tests

* Update docs

* Add uuid dep

* Changeset

* Words

* Add another changeset

* Some more tests and cuid change

* Update docs/pages/docs/apis/config.mdx

Co-authored-by: Tim Leslie <timl@thinkmill.com.au>

* Update docs/pages/docs/apis/config.mdx

Co-authored-by: Tim Leslie <timl@thinkmill.com.au>

* Update .changeset/eight-lions-stare.md

Co-authored-by: Tim Leslie <timl@thinkmill.com.au>

Co-authored-by: Tim Leslie <timl@thinkmill.com.au>
  • Loading branch information
2 people authored and Nikitoring committed Sep 13, 2021
1 parent 38b2fc8 commit ef4b136
Show file tree
Hide file tree
Showing 63 changed files with 586 additions and 254 deletions.
8 changes: 8 additions & 0 deletions .changeset/eight-lions-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@keystone-next/keystone': major
'@keystone-next/types': major
---

Replaced the `idField` list configuration option with a more constrained option, `db.idField`, that accepts an object with a `kind` property with a value of `cuid`, `uuid` or `autoincrement`. `db.idField` can be set on either the top level `config` object, or on individual lists.

The default behaviour has changed from using `autoincrement` to using cuids. To keep the current behaviour, you should set `{ kind: 'autoincrement' }` at `db.idField` in your top level config.
5 changes: 5 additions & 0 deletions .changeset/five-needles-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': major
---

Id field filters no longer allow `null` to be passed because ids can never be null. For the `in` and `not_in`, this is reflected in the GraphQL types
6 changes: 6 additions & 0 deletions docs/pages/docs/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Advanced configuration:

- `enableLogging` (default: `false`): Enable logging from the Prisma client.
- `useMigrations` (default: `false`): Determines whether to use migrations or automatically force-update the database with the latest schema and potentially lose data.
- `idField` (default: `{ kind: "cuid" }`): The kind of id field to use, it can be one of: `cuid`, `uuid` or `autoincrement`.
This can also be customised at the list level `db.idField`.

```typescript
export default config({
Expand All @@ -81,6 +83,7 @@ export default config({
// Optional advanced configuration
enableLogging: true,
useMigrations: true,
idField: { kind: 'uuid' },
},
/* ... */
});
Expand All @@ -92,6 +95,8 @@ Advanced configuration:

- `enableLogging` (default: `false`): Enable logging from the Prisma client.
- `useMigrations` (default: `false`): Determines whether to use migrations or automatically force-update the database with the latest schema and potentially lose data.
- `idField` (default: `{ kind: "cuid" }`): The kind of id field to use, it can be one of: `cuid`, `uuid` or `autoincrement`.
This can also be customised at the list level `db.idField`.

```typescript
export default config({
Expand All @@ -102,6 +107,7 @@ export default config({
// Optional advanced configuration
enableLogging: true,
useMigrations: true,
idField: { kind: 'uuid' },
},
/* ... */
});
Expand Down
12 changes: 6 additions & 6 deletions docs/pages/docs/apis/graphql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ input UserWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down Expand Up @@ -164,8 +164,8 @@ input UserWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down Expand Up @@ -218,8 +218,8 @@ input UserWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down
37 changes: 3 additions & 34 deletions docs/pages/docs/apis/schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default config({
lists: createSchema({
ListName: list({
fields: { /* ... */ },
idField: { /* ... */ },
access: { /* ... */ },
ui: { /* ... */ },
hooks: { /* ... */ },
Expand Down Expand Up @@ -54,39 +53,6 @@ export default config({

For full details on the available field types and their configuration options please see the [Fields API](./fields).

## idField

The `idField` option lets you override the default ID field used by Keystone.
By default the `autoIncrement` field type is used as an ID field type.
The default configuration (e.g. when `idField: undefined` ) is equivalent to:

```typescript
import { config, createSchema, list } from '@keystone-next/keystone/schema';
import { autoIncrement } from '@keystone-next/fields';

export default config({
lists: createSchema({
ListName: list({
fields: { /* ... */ },
idField: autoIncrement({
ui: {
createView: { fieldMode: 'hidden' },
itemView: { fieldMode: 'hidden' },
},
}),
/* ... */
}),
/* ... */
}),
/* ... */
});
```

These defaults hide this field in the `create` and `update` views in the Admin UI.
You can override these defaults, or any other field config options, by providing a field type configured as needed.

The `idField` will always be made available in the GraphQL API with a name of `id`.

## access

The `access` option defines the [Access Control](../guides/access-control) rules for the list.
Expand Down Expand Up @@ -213,6 +179,8 @@ The `db` config option allows you to configures certain aspects of the database
Options:

- `searchField` (default: `"name"`): The name of the field to use when performing `search` filters in the GraphQL API.
- `idField` (default: `{ kind: "cuid" }`): The kind of id field to use, it can be one of: `cuid`, `uuid` or `autoincrement`.
The default across all lists can be changed at the root-level `db.idField` config.

```typescript
import { config, createSchema, list } from '@keystone-next/keystone/schema';
Expand All @@ -222,6 +190,7 @@ export default config({
ListName: list({
db: {
searchField: 'email',
idField: { kind: 'uuid' },
},
/* ... */
}),
Expand Down
8 changes: 4 additions & 4 deletions examples-staging/assets-cloud/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ input PostWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
title: String
title_not: String
title_contains: String
Expand Down Expand Up @@ -222,8 +222,8 @@ input AuthorWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down
6 changes: 3 additions & 3 deletions examples-staging/assets-cloud/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ generator client {
}

model Post {
id Int @id @default(autoincrement())
id String @id @default(cuid())
title String?
status String?
content String?
publishDate DateTime?
author Author? @relation("Post_author", fields: [authorId], references: [id])
authorId Int? @map("author")
authorId String? @map("author")
hero_filesize Int?
hero_extension String?
hero_width Int?
Expand All @@ -30,7 +30,7 @@ model Post {
}

model Author {
id Int @id @default(autoincrement())
id String @id @default(cuid())
name String?
email String? @unique
posts Post[] @relation("Post_author")
Expand Down
8 changes: 4 additions & 4 deletions examples-staging/assets-local/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ input PostWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
title: String
title_not: String
title_contains: String
Expand Down Expand Up @@ -200,8 +200,8 @@ input AuthorWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down
6 changes: 3 additions & 3 deletions examples-staging/assets-local/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ generator client {
}

model Post {
id Int @id @default(autoincrement())
id String @id @default(cuid())
title String?
status String?
content String?
publishDate DateTime?
author Author? @relation("Post_author", fields: [authorId], references: [id])
authorId Int? @map("author")
authorId String? @map("author")
hero_filesize Int?
hero_extension String?
hero_width Int?
Expand All @@ -27,7 +27,7 @@ model Post {
}

model Author {
id Int @id @default(autoincrement())
id String @id @default(cuid())
name String?
email String? @unique
posts Post[] @relation("Post_author")
Expand Down
4 changes: 2 additions & 2 deletions examples-staging/auth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ input UserWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down
2 changes: 1 addition & 1 deletion examples-staging/auth/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ generator client {
}

model User {
id Int @id @default(autoincrement())
id String @id @default(cuid())
name String?
email String? @unique
password String?
Expand Down
12 changes: 6 additions & 6 deletions examples-staging/basic/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ input UserWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
name: String
name_not: String
name_contains: String
Expand Down Expand Up @@ -283,8 +283,8 @@ input PhoneNumberWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
user: UserWhereInput
user_is_null: Boolean
type: String
Expand Down Expand Up @@ -371,8 +371,8 @@ input PostWhereInput {
id_lte: ID
id_gt: ID
id_gte: ID
id_in: [ID]
id_not_in: [ID]
id_in: [ID!]
id_not_in: [ID!]
title: String
title_not: String
title_contains: String
Expand Down
10 changes: 5 additions & 5 deletions examples-staging/basic/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ generator client {
}

model User {
id Int @id @default(autoincrement())
id String @id @default(cuid())
name String?
email String? @unique
avatar_filesize Int?
Expand All @@ -29,23 +29,23 @@ model User {
}

model PhoneNumber {
id Int @id @default(autoincrement())
id String @id @default(cuid())
user User? @relation("PhoneNumber_user", fields: [userId], references: [id])
userId Int? @map("user")
userId String? @map("user")
type String?
value String?
@@index([userId])
}

model Post {
id Int @id @default(autoincrement())
id String @id @default(cuid())
title String?
status String?
content String?
publishDate DateTime?
author User? @relation("Post_author", fields: [authorId], references: [id])
authorId Int? @map("author")
authorId String? @map("author")
@@index([authorId])
}
Loading

0 comments on commit ef4b136

Please sign in to comment.