UNPKG

matcha-keystone-utils

Version:

Useful features for Keystone 6: validators, fields, componentBlocks...

27 lines (26 loc) 1.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.beforeOperationDeleteMany = exports.beforeOperationDeleteOne = void 0; const beforeOperationDeleteOne = ({ ref }) => async ({ item, context, fieldKey, operation, }) => { if (operation === "delete") { const idPath = fieldKey + "Id"; const id = item[idPath]; if (id) await context.query[ref].deleteOne({ where: { id } }); } }; exports.beforeOperationDeleteOne = beforeOperationDeleteOne; const beforeOperationDeleteMany = ({ ref }) => async ({ item, context, fieldKey, listKey, operation, }) => { if (operation === "delete") { const { id } = item; const parentWithChildren = await context.query[listKey].findOne({ where: { id }, query: `${fieldKey} { id }`, }); const children = parentWithChildren[fieldKey]; const ids = (children || []).map(({ id }) => id); const where = ids.map(id => ({ id })); await context.query[ref].deleteMany({ where }); } }; exports.beforeOperationDeleteMany = beforeOperationDeleteMany;