-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathschema.ts
258 lines (243 loc) · 6.38 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Welcome to your schema
// Schema driven development is Keystone's modus operandi
//
// This file is where we define the lists, fields and hooks for our data.
// If you want to learn more about how lists are configured, please read
// - https://keystonejs.com/docs/config/lists
import { list } from "@keystone-6/core";
import { allowAll, denyAll, allOperations } from "@keystone-6/core/access";
// see https://keystonejs.com/docs/fields/overview for the full list of fields
// this is a few common fields for an example
import {
text,
password,
timestamp,
checkbox,
calendarDay,
image,
select,
multiselect,
} from "@keystone-6/core/fields";
// when using Typescript, you can refine your types to a stricter subset by importing
// the generated types from '.keystone/types'
import type { Lists } from ".keystone/types";
export type Session = {
itemId: string;
data: {
name: string;
email: string;
isAdmin: boolean;
};
};
function hasSession({ session }: { session?: Session }) {
return Boolean(session);
}
function isAdmin({ session }: { session?: Session }) {
// you need to have a session to do this
if (!session) return false;
// admins can do anything
if (session.data.isAdmin) return true;
// otherwise, no
return false;
}
function isAdminOrSameUser({
session,
item,
}: {
session?: Session;
item: Lists.User.Item;
}) {
// you need to have a session to do this
if (!session) return false;
// admins can do anything
if (session.data.isAdmin) return true;
// the authenticated user needs to be equal to the user we are updating
return session.itemId === item.id;
}
function isAdminOrSameUserFilter({ session }: { session?: Session }) {
// you need to have a session to do this
if (!session) return false;
// admins can see everything
if (session.data?.isAdmin) return {};
// the authenticated user can only see themselves
return {
id: {
equals: session.itemId,
},
};
}
export const lists: Lists = {
User: list({
access: {
operation: {
...allOperations(isAdmin),
query: hasSession,
},
filter: {
query: isAdminOrSameUserFilter,
},
},
fields: {
name: text(),
email: text({ isIndexed: "unique" }),
password: password({
access: {
read: denyAll, // TODO: is this required?
update: isAdminOrSameUser,
},
validation: {
isRequired: true,
},
ui: {
itemView: {
// don't show this field if it isn't relevant
fieldMode: (args) => (isAdminOrSameUser(args) ? "edit" : "hidden"),
},
listView: {
fieldMode: "hidden", // TODO: is this required?
},
},
}),
isAdmin: checkbox({
access: {
// only the respective user, or an admin can read this field
read: isAdminOrSameUser,
// only admins can create, or update this field
create: isAdmin,
update: isAdmin,
},
defaultValue: false,
ui: {
// only admins can edit this field
createView: {
fieldMode: (args) => (isAdmin(args) ? "edit" : "hidden"),
},
itemView: {
fieldMode: (args) => (isAdmin(args) ? "edit" : "read"),
},
},
}),
createdAt: timestamp({
ui: {
createView: {
fieldMode: "hidden",
},
},
defaultValue: { kind: "now" },
}),
},
}),
Event: list({
access: {
operation: {
...allOperations(isAdmin),
query: allowAll,
},
filter: {},
},
fields: {
cover: image({ storage: "events" }),
name: text({ validation: { isRequired: true } }),
date: calendarDay({
validation: { isRequired: true },
}),
description: text({
validation: { isRequired: false, length: { max: 280 } },
}),
eventWebsite: text({ validation: { isRequired: false } }),
featured: checkbox({
defaultValue: true,
graphql: {
isNonNull: {
read: true,
create: true,
},
},
}),
createdAt: timestamp({
ui: {
createView: {
fieldMode: "hidden",
},
},
defaultValue: { kind: "now" },
}),
},
}),
Location: list({
access: {
operation: {
...allOperations(isAdmin),
query: allowAll,
},
filter: {},
},
fields: {
cover: image({ storage: "locations" }),
name: text({ validation: { isRequired: true } }),
address: text({ validation: { isRequired: true } }),
zip: text({ validation: { isRequired: true } }),
city: text({ validation: { isRequired: true } }),
country: select({
defaultValue: "pt",
options: [{ label: "Portugal", value: "pt" }],
validation: { isRequired: true },
}),
description: text({ validation: { isRequired: true } }),
amenities: multiselect({
type: "enum",
options: [
{ label: "Parking", value: "parking" },
{ label: "Restaurants", value: "restaurants" },
{ label: "Gym", value: "gym" },
{ label: "Beach", value: "beach" },
],
}),
},
}),
Testimonial: list({
access: {
operation: {
...allOperations(isAdmin),
query: allowAll,
},
filter: {},
},
fields: {
avatar: image({ storage: "testimonials" }),
name: text({ validation: { isRequired: true } }),
message: text({ validation: { isRequired: true } }),
twitter: text({ validation: { isRequired: false } }),
featured: checkbox({
defaultValue: true,
graphql: {
isNonNull: {
read: true,
create: true,
},
},
}),
createdAt: timestamp({
ui: {
createView: {
fieldMode: "hidden",
},
},
defaultValue: { kind: "now" },
}),
},
}),
// this last list is our Tag list, it only has a name field for now
Type: list({
access: allowAll,
ui: {
isHidden: true,
},
// this is the fields for our Tag list
fields: {
name: text(),
// this can be helpful to find out all the Posts associated with a Tag
// events: relationship({ ref: "Event.types", many: true }),
},
}),
};