-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
303 lines (250 loc) · 8.12 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// 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, unfiltered } 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,
relationship,
password,
timestamp,
select,
checkbox,
image,
} from '@keystone-6/core/fields';
// the document field is a more complicated field, so it has it's own package
import { document } from '@keystone-6/fields-document';
// if you want to make your own fields, see https://keystonejs.com/docs/guides/custom-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';
import { hooks } from './hooks/hooks';
import { postHooks } from './hooks/post-hooks';
/* ************************************************************************************* */
export type Session = {
listKey: string,
itemId: string,
data: {
isAdmin: boolean;
}
};
function hasSession({ session }: { session?: Session }) {
return Boolean(session);
}
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,
},
};
}
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;
}
/* ************************************************************************************* */
const dateMatch = {
regex: RegExp(/^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/),
explanation: "Cette date doit correspondre au format YYYY-MM-DD",
};
/* ************************************************************************************* */
export const lists: Lists = {
User: list({
// More about access at https://keystonejs.com/docs/guides/auth-and-access-control
//access: allowAll,
access: {
operation: {
query: hasSession,
create: isAdmin,
update: isAdmin,
delete: isAdmin,
},
filter: {
query: isAdminOrSameUserFilter,
},
},
// this is the fields for our User list
fields: {
// by adding isRequired, we enforce that every User should have a name
// if no name is provided, an error will be displayed
name: text({ validation: { isRequired: true } }),
email: text({
validation: { isRequired: true },
// by adding isIndexed: 'unique', we're saying that no user can have the same
// email as another user - this may or may not be a good idea for your project
isIndexed: 'unique',
}),
password: password({ validation: { isRequired: true } }),
// we can use this field to see what Experiences this User has authored
// more on that in the Experience list below
// experiences: relationship({ ref: 'Experience.author', many: true }),
createdAt: timestamp({
// this sets the timestamp to Date.now() when the user is first created
defaultValue: { kind: 'now' },
}),
isAdmin: checkbox(),
},
hooks,
}),
Experience: list({
// More about access at https://keystonejs.com/docs/guides/auth-and-access-control
//access: allowAll,
access: {
operation: {
query: allowAll,
create: isAdmin,
update: isAdmin,
delete: isAdmin,
},
filter: {
// this is redundant as it is the default
// but it may help readability
query: unfiltered,
},
},
// this is the fields for our Experience list
fields: {
title: text({ validation: { isRequired: true } }),
// the document field can be used for making rich editable content
// you can find out more at https://keystonejs.com/docs/guides/document-fields
content: document({
formatting: true,
layouts: [
[1, 1],
[1, 1, 1],
[2, 1],
[1, 2],
[1, 2, 1],
],
links: true,
dividers: true,
}),
startDate: text({
validation: {
match: dateMatch,
},
}),
endDate: text({
validation: {
match: dateMatch,
},
}),
/*
// with this field, you can set a User as the author for a Experience
author: relationship({
// we could have used 'User', but then the relationship would only be 1-way
ref: 'User.experiences',
// this is some customisations for changing how this will look in the AdminUI
ui: {
displayMode: 'cards',
cardFields: ['name', 'email'],
inlineEdit: { fields: ['name', 'email'] },
linkToItem: true,
inlineConnect: true,
},
// a Experience can only have one author
// this is the default, but we show it here for verbosity
many: false,
}),
*/
// with this field, you can add some Technos to Experiences
technos: relationship({
// we could have used 'Techno', but then the relationship would only be 1-way
ref: 'Techno.experiences',
// a Experience can have many Technos, not just one
many: true,
// this is some customisations for changing how this will look in the AdminUI
ui: {
displayMode: 'cards',
cardFields: ['name'],
inlineEdit: { fields: ['name'] },
linkToItem: true,
inlineConnect: true,
inlineCreate: { fields: ['name'] },
},
}),
image: image({ storage: 'sylstudio_S3_images' }),
blurhash: text(),
image_40: text(),
image_150: text(),
image_300: text(),
image_600: text(),
},
hooks: {
...hooks, ...postHooks
},
}),
// this last list is our Techno list, it only has a name field for now
Techno: list({
// More about access at https://keystonejs.com/docs/guides/auth-and-access-control
//access: allowAll,
access: {
operation: {
query: allowAll,
create: isAdmin,
update: isAdmin,
delete: isAdmin,
},
filter: {
// this is redundant as it is the default
// but it may help readability
query: unfiltered,
},
},
// setting this to isHidden for the user interface prevents this list being visible in the Admin UI
ui: {
// isHidden: true,
},
// this is the fields for our Techno list
fields: {
name: text(),
// the document field can be used for making rich editable content
// you can find out more at https://keystonejs.com/docs/guides/document-fields
content: document({
formatting: true,
layouts: [
[1, 1],
[1, 1, 1],
[2, 1],
[1, 2],
[1, 2, 1],
],
links: true,
dividers: true,
}),
// this can be helpful to find out all the Experiences associated with a Techno
experiences: relationship({ ref: 'Experience.technos', many: true }),
image: image({ storage: 'sylstudio_S3_images' }),
blurhash: text(),
image_40: text(),
image_150: text(),
image_300: text(),
image_600: text(),
},
hooks: {
...hooks, ...postHooks
},
}),
};