Skip to content

Commit

Permalink
rewrite and adapt number fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Nov 25, 2024
1 parent a0ed52d commit 7c4bd4b
Show file tree
Hide file tree
Showing 18 changed files with 1,102 additions and 763 deletions.
13 changes: 13 additions & 0 deletions examples/number-fields/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { config } from '@keystone-6/core'
import { lists } from './schema'

export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',

// WARNING: this is only needed for our monorepo examples, dont do this
prismaClientPath: 'node_modules/myprisma',
},
lists,
})
20 changes: 20 additions & 0 deletions examples/number-fields/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@keystone-6/example-number-fields",
"version": null,
"private": true,
"license": "MIT",
"scripts": {
"dev": "keystone dev",
"start": "keystone start",
"build": "keystone build",
"postinstall": "keystone postinstall"
},
"dependencies": {
"@keystone-6/core": "^6.3.1",
"@prisma/client": "5.19.0"
},
"devDependencies": {
"prisma": "5.19.0",
"typescript": "^5.5.0"
}
}
7 changes: 7 additions & 0 deletions examples/number-fields/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template": "node",
"container": {
"startScript": "keystone dev",
"node": "20"
}
}
253 changes: 253 additions & 0 deletions examples/number-fields/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
# This file is automatically generated by Keystone, do not modify it manually.
# Modify your Keystone config when you want to change this.

type Example {
id: ID!
bigInt: BigInt
float: Float
integer: Int
}

scalar BigInt

input ExampleWhereUniqueInput {
id: ID
}

input ExampleWhereInput {
AND: [ExampleWhereInput!]
OR: [ExampleWhereInput!]
NOT: [ExampleWhereInput!]
id: IDFilter
bigInt: BigIntNullableFilter
float: FloatNullableFilter
integer: IntNullableFilter
}

input IDFilter {
equals: ID
in: [ID!]
notIn: [ID!]
lt: ID
lte: ID
gt: ID
gte: ID
not: IDFilter
}

input BigIntNullableFilter {
equals: BigInt
in: [BigInt!]
notIn: [BigInt!]
lt: BigInt
lte: BigInt
gt: BigInt
gte: BigInt
not: BigIntNullableFilter
}

input FloatNullableFilter {
equals: Float
in: [Float!]
notIn: [Float!]
lt: Float
lte: Float
gt: Float
gte: Float
not: FloatNullableFilter
}

input IntNullableFilter {
equals: Int
in: [Int!]
notIn: [Int!]
lt: Int
lte: Int
gt: Int
gte: Int
not: IntNullableFilter
}

input ExampleOrderByInput {
id: OrderDirection
bigInt: OrderDirection
float: OrderDirection
integer: OrderDirection
}

enum OrderDirection {
asc
desc
}

input ExampleUpdateInput {
bigInt: BigInt
float: Float
integer: Int
}

input ExampleUpdateArgs {
where: ExampleWhereUniqueInput!
data: ExampleUpdateInput!
}

input ExampleCreateInput {
bigInt: BigInt
float: Float
integer: Int
}

"""
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

type Mutation {
createExample(data: ExampleCreateInput!): Example
createExamples(data: [ExampleCreateInput!]!): [Example]
updateExample(where: ExampleWhereUniqueInput!, data: ExampleUpdateInput!): Example
updateExamples(data: [ExampleUpdateArgs!]!): [Example]
deleteExample(where: ExampleWhereUniqueInput!): Example
deleteExamples(where: [ExampleWhereUniqueInput!]!): [Example]
}

type Query {
example(where: ExampleWhereUniqueInput!): Example
examples(where: ExampleWhereInput! = {}, orderBy: [ExampleOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: ExampleWhereUniqueInput): [Example!]
examplesCount(where: ExampleWhereInput! = {}): Int
keystone: KeystoneMeta!
}

type KeystoneMeta {
adminMeta: KeystoneAdminMeta!
}

type KeystoneAdminMeta {
lists: [KeystoneAdminUIListMeta!]!
list(key: String!): KeystoneAdminUIListMeta
}

type KeystoneAdminUIListMeta {
key: String!
path: String!
description: String
label: String!
labelField: String!
singular: String!
plural: String!
fields: [KeystoneAdminUIFieldMeta!]!
groups: [KeystoneAdminUIFieldGroupMeta!]!
graphql: KeystoneAdminUIGraphQL!
pageSize: Int!
initialColumns: [String!]!
initialSearchFields: [String!]!
initialSort: KeystoneAdminUISort
isSingleton: Boolean!
hideNavigation: Boolean!
hideCreate: Boolean!
hideDelete: Boolean!
}

type KeystoneAdminUIFieldMeta {
path: String!
label: String!
description: String
isOrderable: Boolean!
isFilterable: Boolean!
isNonNull: [KeystoneAdminUIFieldMetaIsNonNull!]
fieldMeta: JSON
viewsIndex: Int!
customViewsIndex: Int
createView: KeystoneAdminUIFieldMetaCreateView!
listView: KeystoneAdminUIFieldMetaListView!
itemView(id: ID): KeystoneAdminUIFieldMetaItemView
search: QueryMode
}

enum KeystoneAdminUIFieldMetaIsNonNull {
read
create
update
}

type KeystoneAdminUIFieldMetaCreateView {
fieldMode: KeystoneAdminUIFieldMetaCreateViewFieldMode!
}

enum KeystoneAdminUIFieldMetaCreateViewFieldMode {
edit
hidden
}

type KeystoneAdminUIFieldMetaListView {
fieldMode: KeystoneAdminUIFieldMetaListViewFieldMode!
}

enum KeystoneAdminUIFieldMetaListViewFieldMode {
read
hidden
}

type KeystoneAdminUIFieldMetaItemView {
fieldMode: KeystoneAdminUIFieldMetaItemViewFieldMode
fieldPosition: KeystoneAdminUIFieldMetaItemViewFieldPosition
}

enum KeystoneAdminUIFieldMetaItemViewFieldMode {
edit
read
hidden
}

enum KeystoneAdminUIFieldMetaItemViewFieldPosition {
form
sidebar
}

enum QueryMode {
default
insensitive
}

type KeystoneAdminUIFieldGroupMeta {
label: String!
description: String
fields: [KeystoneAdminUIFieldMeta!]!
}

type KeystoneAdminUIGraphQL {
names: KeystoneAdminUIGraphQLNames!
}

type KeystoneAdminUIGraphQLNames {
outputTypeName: String!
whereInputName: String!
whereUniqueInputName: String!
createInputName: String!
createMutationName: String!
createManyMutationName: String!
relateToOneForCreateInputName: String!
relateToManyForCreateInputName: String!
itemQueryName: String!
listOrderName: String!
listQueryCountName: String!
listQueryName: String!
updateInputName: String!
updateMutationName: String!
updateManyInputName: String!
updateManyMutationName: String!
relateToOneForUpdateInputName: String!
relateToManyForUpdateInputName: String!
deleteMutationName: String!
deleteManyMutationName: String!
}

type KeystoneAdminUISort {
field: String!
direction: KeystoneAdminUISortDirection!
}

enum KeystoneAdminUISortDirection {
ASC
DESC
}
20 changes: 20 additions & 0 deletions examples/number-fields/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This file is automatically generated by Keystone, do not modify it manually.
// Modify your Keystone config when you want to change this.

datasource sqlite {
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
provider = "sqlite"
}

generator client {
provider = "prisma-client-js"
output = "node_modules/myprisma"
}

model Example {
id String @id @default(cuid())
bigInt BigInt?
float Float?
integer Int?
}
19 changes: 19 additions & 0 deletions examples/number-fields/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { list } from '@keystone-6/core'
import {
bigInt,
float,
integer
} from '@keystone-6/core/fields'
import { allowAll } from '@keystone-6/core/access'
import type { Lists } from '.keystone/types'

export const lists = {
Example: list({
access: allowAll,
fields: {
bigInt: bigInt(),
float: float(),
integer: integer(),
},
}),
} satisfies Lists
5 changes: 3 additions & 2 deletions packages/core/src/fields/types/bigInt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export type BigIntFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
const MAX_INT = 9223372036854775807n
const MIN_INT = -9223372036854775808n

// TODO: https://github.com/Thinkmill/keystatic/blob/main/design-system/pkg/src/number-field/NumberField.tsx
export function bigInt <ListTypeInfo extends BaseListTypeInfo> (config: BigIntFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> {
const {
defaultValue: defaultValue_,
defaultValue: defaultValue_ = null,
isIndexed,
validation = {},
} = config
Expand Down Expand Up @@ -64,7 +65,7 @@ export function bigInt <ListTypeInfo extends BaseListTypeInfo> (config: BigIntFi
throw new Error(`${meta.listKey}.${meta.fieldKey} defaultValue: { kind: 'autoincrement' } conflicts with validation.isRequired: true`)
}
}
if (defaultValue !== null && (typeof defaultValue === 'bigint' && !Number.isInteger(defaultValue))) {
if (defaultValue !== null && typeof defaultValue !== 'bigint') {
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies a default value of: ${defaultValue} but it must be a valid finite number`)
}
if (min !== undefined && !Number.isInteger(min)) {
Expand Down
Loading

0 comments on commit 7c4bd4b

Please sign in to comment.