diff --git a/examples/usecase-blog/schema.ts b/examples/usecase-blog/schema.ts index 4d8474baf77..ad20c432515 100644 --- a/examples/usecase-blog/schema.ts +++ b/examples/usecase-blog/schema.ts @@ -37,7 +37,10 @@ export const lists: Lists = { // we can use this field to see what Posts this Author has authored // more on that in the Post list below - posts: relationship({ ref: 'Post.author', many: true }), + posts: relationship({ + ref: 'Post.author', + many: true, + }), createdAt: timestamp({ // default this timestamp to Date.now() when first created @@ -50,6 +53,22 @@ export const lists: Lists = { // WARNING - for this example, anyone can create, query, update and delete anything access: allowAll, + hooks: { + resolveInput: async ({ item, resolvedData }) => { + console.log('list hook:resolveInput', { item }); + return resolvedData; + }, + validateInput: async ({ item }) => { + console.log('list hook:validateInput', { item }); + }, + beforeOperation: async ({ item }) => { + console.log('list hook:beforeOperation', { item }); + }, + afterOperation: async ({ item }) => { + console.log('list hook:afterOperation', { item }); + }, + }, + fields: { title: text({ validation: { isRequired: true } }), @@ -83,6 +102,22 @@ export const lists: Lists = { }, many: false, // only 1 author for each Post (the default) + + hooks: { + resolveInput: ({ item, resolvedData, fieldKey }) => { + console.log('field hook:resolveInput', item); + return resolvedData[fieldKey]; + }, + validateInput: async ({ item }) => { + console.log('field hook:validateInput', item); + }, + beforeOperation: async ({ item }) => { + console.log('field hook:beforeOperation', item); + }, + afterOperation: async ({ item }) => { + console.log('field hook:afterOperation', item); + }, + }, }), // with this field, you can add some Tags to Posts diff --git a/packages/core/src/lib/core/mutations/access-control.ts b/packages/core/src/lib/core/mutations/access-control.ts index 6e176100e65..90b44abb0c5 100644 --- a/packages/core/src/lib/core/mutations/access-control.ts +++ b/packages/core/src/lib/core/mutations/access-control.ts @@ -30,8 +30,41 @@ async function getFilteredItem( where = { AND: [where, await resolveWhereInput(accessFilters, list, context)] }; } - const item = await runWithPrisma(context, list, model => model.findFirst({ where })); - if (item !== null) return item; + const manyRelationsFields = Object.entries(list.fields).filter(([_key, field]) => { + return field.dbField.kind === 'relation' && field.dbField.mode === 'many'; + }); + + let include = {}; + manyRelationsFields.forEach(([key]) => { + include[key] = { + select: { id: true }, + }; + }); + + const foundItem = await runWithPrisma(context, list, model => + model.findFirst({ + where, + include, + }) + ); + + if (foundItem !== null) { + const item = { ...foundItem }; + + manyRelationsFields.forEach(([key]) => { + const value = item[key]; + delete item[key]; + + const name = key + 'Ids'; + if (Array.isArray(value)) { + item[name] = value.map(v => v.id); + } else { + item[name] = value ?? []; + } + }); + + return item; + } throw accessDeniedError(cannotForItem(operation, list)); }