Skip to content

Commit

Permalink
add field config
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Calder committed Jan 12, 2023
1 parent cb2e8d3 commit e201b49
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 28 deletions.
41 changes: 30 additions & 11 deletions examples/custom-prisma-multischema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ input PostRelateToManyForCreateInput {
type PhoneNumber {
id: ID!
label: String
user: User
user(where: UserWhereInput! = {}, orderBy: [UserOrderByInput!]! = [], take: Int, skip: Int! = 0): [User!]
userCount(where: UserWhereInput! = {}): Int
type: String
value: String
}
Expand All @@ -173,11 +174,17 @@ input PhoneNumberWhereInput {
OR: [PhoneNumberWhereInput!]
NOT: [PhoneNumberWhereInput!]
id: IDFilter
user: UserWhereInput
user: UserManyRelationFilter
type: StringNullableFilter
value: StringFilter
}

input UserManyRelationFilter {
every: UserWhereInput
some: UserWhereInput
none: UserWhereInput
}

input StringNullableFilter {
equals: String
in: [String!]
Expand Down Expand Up @@ -214,15 +221,16 @@ input PhoneNumberOrderByInput {
}

input PhoneNumberUpdateInput {
user: UserRelateToOneForUpdateInput
user: UserRelateToManyForUpdateInput
type: String
value: String
}

input UserRelateToOneForUpdateInput {
create: UserCreateInput
connect: UserWhereUniqueInput
disconnect: Boolean
input UserRelateToManyForUpdateInput {
disconnect: [UserWhereUniqueInput!]
set: [UserWhereUniqueInput!]
create: [UserCreateInput!]
connect: [UserWhereUniqueInput!]
}

input PhoneNumberUpdateArgs {
Expand All @@ -231,14 +239,14 @@ input PhoneNumberUpdateArgs {
}

input PhoneNumberCreateInput {
user: UserRelateToOneForCreateInput
user: UserRelateToManyForCreateInput
type: String
value: String
}

input UserRelateToOneForCreateInput {
create: UserCreateInput
connect: UserWhereUniqueInput
input UserRelateToManyForCreateInput {
create: [UserCreateInput!]
connect: [UserWhereUniqueInput!]
}

type Post {
Expand Down Expand Up @@ -297,6 +305,12 @@ input PostUpdateInput {
author: UserRelateToOneForUpdateInput
}

input UserRelateToOneForUpdateInput {
create: UserCreateInput
connect: UserWhereUniqueInput
disconnect: Boolean
}

input PostUpdateArgs {
where: PostWhereUniqueInput!
data: PostUpdateInput!
Expand All @@ -310,6 +324,11 @@ input PostCreateInput {
author: UserRelateToOneForCreateInput
}

input UserRelateToOneForCreateInput {
create: UserCreateInput
connect: UserWhereUniqueInput
}

"""
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
Expand Down
16 changes: 7 additions & 9 deletions examples/custom-prisma-multischema/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ model User {
id String @id @default(cuid())
name String @default("") @map("RealName")
email String @unique @default("")
password String?
password String? @map("Password")
roles String @default("")
phoneNumbers PhoneNumber[] @relation("PhoneNumber_user")
posts Post[] @relation("Post_author")
Expand All @@ -27,23 +27,21 @@ model User {
}

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

model Post {
id String @id @default(cuid())
title String @default("")
status String @default("draft")
content Json @default("[{\"type\":\"paragraph\",\"children\":[{\"text\":\"\"}]}]")
content Json @default("[{\"type\":\"paragraph\",\"children\":[{\"text\":\"\"}]}]") @map("Content")
publishDate DateTime?
author User? @relation("Post_author", fields: [authorId], references: [id])
author User? @relation("Post_author", fields: [authorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
authorId String? @map("author")
@@index([authorId])
Expand Down
37 changes: 34 additions & 3 deletions examples/custom-prisma-multischema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ const User: Lists.User = list({
isIndexed: 'unique',
validation: { isRequired: true },
}),
password: password(),
password: password({ db: { extendPrismaField: list => list + ' @map("Password")' } }),
roles: text({}),
phoneNumbers: relationship({
ref: 'PhoneNumber.user',
db: {
extendPrismaField: field => {
console.log('Phone Number - User: ', field);
return field;
},
},
many: true,
ui: {
displayMode: 'cards',
Expand All @@ -38,7 +44,10 @@ const User: Lists.User = list({
linkToItem: true,
},
}),
posts: relationship({ ref: 'Post.author', many: true }),
posts: relationship({
ref: 'Post.author',
many: true,
}),
randomNumber: virtual({
field: graphql.field({
type: graphql.Float,
Expand Down Expand Up @@ -78,7 +87,16 @@ export const lists: Lists = {
},
},
}),
user: relationship({ ref: 'User.phoneNumbers' }),
user: relationship({
ref: 'User.phoneNumbers',
many: true,
db: {
extendPrismaField: field => {
console.log('Phone Number: ', field);
return field;
},
},
}),
type: select({
options: [
{ label: 'Home', value: 'home' },
Expand All @@ -88,6 +106,9 @@ export const lists: Lists = {
ui: {
displayMode: 'segmented-control',
},
db: {
extendPrismaField: list => list + ' @map("Type")',
},
}),
value: text({}),
},
Expand Down Expand Up @@ -129,9 +150,19 @@ export const lists: Lists = {
],
links: true,
dividers: true,
db: {
extendPrismaField: list => list + ' @map("Content")',
},
}),
publishDate: timestamp(),
author: relationship({
db: {
extendPrismaField: () =>
`\nauthor User? @relation("Post_author", fields: [authorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
authorId String? @map("author")
@@index([authorId])`,
},
ref: 'User.posts',
ui: {
displayMode: 'cards',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/fields/types/bigInt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type BigIntFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
db?: {
isNullable?: boolean;
map?: string;
extendPrismaField?: (field: string) => string;
};
};

Expand Down Expand Up @@ -111,6 +112,7 @@ export const bigInt =
? { kind: 'autoincrement' }
: undefined,
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
})({
...config,
hooks: {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/fields/types/calendarDay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type CalendarDayFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
};
db?: {
isNullable?: boolean;
extendPrismaField?: (field: string) => string;
map?: string;
};
};
Expand Down Expand Up @@ -84,6 +85,7 @@ export const calendarDay =
}
: undefined,
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
nativeType: usesNativeDateType ? 'Date' : undefined,
})({
...config,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/fields/types/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export type CheckboxFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
read?: { isNonNull?: boolean };
create?: { isNonNull?: boolean };
};
db?: { map?: string };
db?: {
map?: string;
extendPrismaField?: (field: string) => string;
};
};

export const checkbox =
Expand All @@ -39,6 +42,7 @@ export const checkbox =
scalar: 'Boolean',
default: { kind: 'literal', value: defaultValue },
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
})({
...config,
input: {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/fields/types/decimal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export type DecimalFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
defaultValue?: string;
isIndexed?: boolean | 'unique';
graphql?: { create?: { isNonNull?: boolean }; read?: { isNonNull?: boolean } };
db?: { isNullable?: boolean; map?: string };
db?: {
isNullable?: boolean;
map?: string;
extendPrismaField?: (field: string) => string;
};
};

function parseDecimalValueOption(meta: FieldData, value: string, name: string) {
Expand Down Expand Up @@ -121,6 +125,7 @@ export const decimal =
default:
defaultValue === undefined ? undefined : { kind: 'literal' as const, value: defaultValue },
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
} as const;
return fieldType(dbField)({
...config,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/fields/types/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { graphql } from '../../..';

export type FileFieldConfig<ListTypeInfo extends BaseListTypeInfo> = {
storage: string;
db?: {
extendPrismaField?: (field: string) => string;
};
} & CommonFieldConfig<ListTypeInfo>;

const FileFieldInput = graphql.inputObject({
Expand Down Expand Up @@ -70,6 +73,7 @@ export const file =
filesize: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
filename: { kind: 'scalar', scalar: 'String', mode: 'optional' },
},
extendPrismaField: config.db?.extendPrismaField,
})({
...config,
hooks: storage.preserve
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/fields/types/float/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type FloatFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
db?: {
isNullable?: boolean;
map?: string;
extendPrismaField?: (field: string) => string;
};
};

Expand Down Expand Up @@ -101,6 +102,7 @@ export const float =
default:
typeof defaultValue === 'number' ? { kind: 'literal', value: defaultValue } : undefined,
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
})({
...config,
hooks: {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/fields/types/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { SUPPORTED_IMAGE_EXTENSIONS } from './utils';

export type ImageFieldConfig<ListTypeInfo extends BaseListTypeInfo> = {
storage: string;
db?: {
extendPrismaField?: (field: string) => string;
};
} & CommonFieldConfig<ListTypeInfo>;

const ImageExtensionEnum = graphql.enum({
Expand Down Expand Up @@ -82,6 +85,7 @@ export const image =

return fieldType({
kind: 'multi',
extendPrismaField: config.db?.extendPrismaField,
fields: {
filesize: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
extension: { kind: 'scalar', scalar: 'String', mode: 'optional' },
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/fields/types/integer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type IntegerFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
db?: {
isNullable?: boolean;
map?: string;
extendPrismaField?: (field: string) => string;
};
};

Expand Down Expand Up @@ -125,6 +126,7 @@ export const integer =
? { kind: 'autoincrement' }
: undefined,
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
})({
...config,
hooks: {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/fields/types/json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { graphql } from '../../..';
export type JsonFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo> & {
defaultValue?: JSONValue;
db?: { map?: string };
db?: { map?: string; extendPrismaField?: (field: string) => string };
};

export const json =
Expand Down Expand Up @@ -47,6 +47,7 @@ export const json =
? undefined
: { kind: 'literal', value: JSON.stringify(defaultValue) },
map: config.db?.map,
extendPrismaField: config.db?.extendPrismaField,
}
);
};
2 changes: 2 additions & 0 deletions packages/core/src/fields/types/multiselect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type MultiselectFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
};
db?: {
map?: string;
extendPrismaField?: (field: string) => string;
};
};

Expand Down Expand Up @@ -150,6 +151,7 @@ export const multiselect =
{
mode: 'required',
map: config?.db?.map,
extendPrismaField: config.db?.extendPrismaField,
default: { kind: 'literal', value: JSON.stringify(defaultValue) },
}
);
Expand Down

0 comments on commit e201b49

Please sign in to comment.