@roundtreasury/prisma-extension-soft-delete 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSoftDeleteExtension = void 0;
4
+ const extension_1 = require("@prisma/client/extension");
5
+ const prisma_extension_nested_operations_1 = require("prisma-extension-nested-operations");
6
+ const createParams_1 = require("./helpers/createParams");
7
+ const modifyResult_1 = require("./helpers/modifyResult");
8
+ function createSoftDeleteExtension({ models, defaultConfig = {
9
+ field: "deleted",
10
+ createValue: Boolean,
11
+ allowToOneUpdates: false,
12
+ allowCompoundUniqueIndexWhere: false,
13
+ }, }) {
14
+ if (!defaultConfig.field) {
15
+ throw new Error("prisma-extension-soft-delete: defaultConfig.field is required");
16
+ }
17
+ if (!defaultConfig.createValue) {
18
+ throw new Error("prisma-extension-soft-delete: defaultConfig.createValue is required");
19
+ }
20
+ const modelConfig = {};
21
+ Object.keys(models).forEach((model) => {
22
+ const modelName = model;
23
+ const config = models[modelName];
24
+ if (config) {
25
+ modelConfig[modelName] =
26
+ typeof config === "boolean" && config ? defaultConfig : config;
27
+ }
28
+ });
29
+ const createParamsByModel = Object.keys(modelConfig).reduce((acc, model) => {
30
+ const config = modelConfig[model];
31
+ return {
32
+ ...acc,
33
+ [model]: {
34
+ delete: createParams_1.createDeleteParams.bind(null, config),
35
+ deleteMany: createParams_1.createDeleteManyParams.bind(null, config),
36
+ update: createParams_1.createUpdateParams.bind(null, config),
37
+ updateMany: createParams_1.createUpdateManyParams.bind(null, config),
38
+ upsert: createParams_1.createUpsertParams.bind(null, config),
39
+ findFirst: createParams_1.createFindFirstParams.bind(null, config),
40
+ findFirstOrThrow: createParams_1.createFindFirstOrThrowParams.bind(null, config),
41
+ findUnique: createParams_1.createFindUniqueParams.bind(null, config),
42
+ findUniqueOrThrow: createParams_1.createFindUniqueOrThrowParams.bind(null, config),
43
+ findMany: createParams_1.createFindManyParams.bind(null, config),
44
+ count: createParams_1.createCountParams.bind(null, config),
45
+ aggregate: createParams_1.createAggregateParams.bind(null, config),
46
+ where: createParams_1.createWhereParams.bind(null, config),
47
+ include: createParams_1.createIncludeParams.bind(null, config),
48
+ select: createParams_1.createSelectParams.bind(null, config),
49
+ groupBy: createParams_1.createGroupByParams.bind(null, config),
50
+ },
51
+ };
52
+ }, {});
53
+ const modifyResultByModel = Object.keys(modelConfig).reduce((acc, model) => {
54
+ const config = modelConfig[model];
55
+ return {
56
+ ...acc,
57
+ [model]: {
58
+ include: modifyResult_1.modifyReadResult.bind(null, config),
59
+ select: modifyResult_1.modifyReadResult.bind(null, config),
60
+ },
61
+ };
62
+ }, {});
63
+ // before handling root params generate deleted value so it is consistent
64
+ // for the query. Add it to root params and get it from scope?
65
+ return extension_1.Prisma.defineExtension((client) => {
66
+ return client.$extends({
67
+ query: {
68
+ $allModels: {
69
+ // @ts-expect-error - we don't know what the client is
70
+ $allOperations: (0, prisma_extension_nested_operations_1.withNestedOperations)({
71
+ async $rootOperation(initialParams) {
72
+ var _a, _b;
73
+ const createParams = (_a = createParamsByModel[initialParams.model || ""]) === null || _a === void 0 ? void 0 : _a[initialParams.operation];
74
+ if (!createParams)
75
+ return initialParams.query(initialParams.args);
76
+ const { params, ctx } = createParams(initialParams);
77
+ const { model } = params;
78
+ const operationChanged = params.operation !== initialParams.operation;
79
+ const result = operationChanged
80
+ ? // @ts-expect-error - we don't know what the client is
81
+ await client[model[0].toLowerCase() + model.slice(1)][params.operation](params.args)
82
+ : await params.query(params.args);
83
+ const modifyResult = (_b = modifyResultByModel[params.model || ""]) === null || _b === void 0 ? void 0 : _b[params.operation];
84
+ if (!modifyResult)
85
+ return result;
86
+ return modifyResult(result, params, ctx);
87
+ },
88
+ async $allNestedOperations(initialParams) {
89
+ var _a, _b;
90
+ const createParams = (_a = createParamsByModel[initialParams.model || ""]) === null || _a === void 0 ? void 0 : _a[initialParams.operation];
91
+ if (!createParams)
92
+ return initialParams.query(initialParams.args);
93
+ const { params, ctx } = createParams(initialParams);
94
+ const result = await params.query(params.args, params.operation);
95
+ const modifyResult = (_b = modifyResultByModel[params.model || ""]) === null || _b === void 0 ? void 0 : _b[params.operation];
96
+ if (!modifyResult)
97
+ return result;
98
+ return modifyResult(result, params, ctx);
99
+ },
100
+ }),
101
+ },
102
+ },
103
+ });
104
+ });
105
+ }
106
+ exports.createSoftDeleteExtension = createSoftDeleteExtension;
@@ -0,0 +1,26 @@
1
+ import { NestedParams } from "prisma-extension-nested-operations";
2
+ import { ModelConfig } from "../types";
3
+ export type Params = Omit<NestedParams<any>, "operation"> & {
4
+ operation: string;
5
+ };
6
+ export type CreateParamsReturn = {
7
+ params: Params;
8
+ ctx?: any;
9
+ };
10
+ export type CreateParams = (config: ModelConfig, params: Params) => CreateParamsReturn;
11
+ export declare const createDeleteParams: CreateParams;
12
+ export declare const createDeleteManyParams: CreateParams;
13
+ export declare const createUpdateParams: CreateParams;
14
+ export declare const createUpdateManyParams: CreateParams;
15
+ export declare const createUpsertParams: CreateParams;
16
+ export declare const createFindUniqueParams: CreateParams;
17
+ export declare const createFindUniqueOrThrowParams: CreateParams;
18
+ export declare const createFindFirstParams: CreateParams;
19
+ export declare const createFindFirstOrThrowParams: CreateParams;
20
+ export declare const createFindManyParams: CreateParams;
21
+ export declare const createGroupByParams: CreateParams;
22
+ export declare const createCountParams: CreateParams;
23
+ export declare const createAggregateParams: CreateParams;
24
+ export declare const createWhereParams: CreateParams;
25
+ export declare const createIncludeParams: CreateParams;
26
+ export declare const createSelectParams: CreateParams;
@@ -0,0 +1,393 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSelectParams = exports.createIncludeParams = exports.createWhereParams = exports.createAggregateParams = exports.createCountParams = exports.createGroupByParams = exports.createFindManyParams = exports.createFindFirstOrThrowParams = exports.createFindFirstParams = exports.createFindUniqueOrThrowParams = exports.createFindUniqueParams = exports.createUpsertParams = exports.createUpdateManyParams = exports.createUpdateParams = exports.createDeleteManyParams = exports.createDeleteParams = void 0;
4
+ const client_1 = require("@prisma/client");
5
+ const nestedReads_1 = require("../utils/nestedReads");
6
+ const uniqueFieldsByModel = {};
7
+ const uniqueIndexFieldsByModel = {};
8
+ client_1.Prisma.dmmf.datamodel.models.forEach((model) => {
9
+ // add unique fields derived from indexes
10
+ const uniqueIndexFields = [];
11
+ model.uniqueFields.forEach((field) => {
12
+ uniqueIndexFields.push(field.join("_"));
13
+ });
14
+ uniqueIndexFieldsByModel[model.name] = uniqueIndexFields;
15
+ // add id field and unique fields from @unique decorator
16
+ const uniqueFields = [];
17
+ model.fields.forEach((field) => {
18
+ if (field.isId || field.isUnique) {
19
+ uniqueFields.push(field.name);
20
+ }
21
+ });
22
+ uniqueFieldsByModel[model.name] = uniqueFields;
23
+ });
24
+ const createDeleteParams = ({ field, createValue }, params) => {
25
+ var _a, _b;
26
+ if (!params.model ||
27
+ // do nothing for delete: false
28
+ (typeof params.args === "boolean" && !params.args) ||
29
+ // do nothing for root delete without where to allow Prisma to throw
30
+ (!params.scope && !((_a = params.args) === null || _a === void 0 ? void 0 : _a.where))) {
31
+ return {
32
+ params,
33
+ };
34
+ }
35
+ if (typeof params.args === "boolean") {
36
+ return {
37
+ params: {
38
+ ...params,
39
+ operation: "update",
40
+ args: {
41
+ __passUpdateThrough: true,
42
+ [field]: createValue(true),
43
+ },
44
+ },
45
+ };
46
+ }
47
+ return {
48
+ params: {
49
+ ...params,
50
+ operation: "update",
51
+ args: {
52
+ where: ((_b = params.args) === null || _b === void 0 ? void 0 : _b.where) || params.args,
53
+ data: {
54
+ [field]: createValue(true),
55
+ },
56
+ },
57
+ },
58
+ };
59
+ };
60
+ exports.createDeleteParams = createDeleteParams;
61
+ const createDeleteManyParams = (config, params) => {
62
+ var _a;
63
+ if (!params.model)
64
+ return { params };
65
+ const where = ((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || params.args;
66
+ return {
67
+ params: {
68
+ ...params,
69
+ operation: "updateMany",
70
+ args: {
71
+ where: {
72
+ ...where,
73
+ [config.field]: config.createValue(false),
74
+ },
75
+ data: {
76
+ [config.field]: config.createValue(true),
77
+ },
78
+ },
79
+ },
80
+ };
81
+ };
82
+ exports.createDeleteManyParams = createDeleteManyParams;
83
+ const createUpdateParams = (config, params) => {
84
+ var _a, _b, _c, _d;
85
+ if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) &&
86
+ !params.scope.relations.to.isList &&
87
+ !config.allowToOneUpdates &&
88
+ !((_b = params.args) === null || _b === void 0 ? void 0 : _b.__passUpdateThrough)) {
89
+ throw new Error(`prisma-extension-soft-delete: update of model "${params.model}" through "${(_c = params.scope) === null || _c === void 0 ? void 0 : _c.parentParams.model}.${params.scope.relations.to.name}" found. Updates of soft deleted models through a toOne relation is not supported as it is possible to update a soft deleted record.`);
90
+ }
91
+ // remove __passUpdateThrough from args
92
+ if ((_d = params.args) === null || _d === void 0 ? void 0 : _d.__passUpdateThrough) {
93
+ delete params.args.__passUpdateThrough;
94
+ }
95
+ return { params };
96
+ };
97
+ exports.createUpdateParams = createUpdateParams;
98
+ const createUpdateManyParams = (config, params) => {
99
+ var _a, _b, _c;
100
+ // do nothing if args are not defined to allow Prisma to throw an error
101
+ if (!params.args)
102
+ return { params };
103
+ return {
104
+ params: {
105
+ ...params,
106
+ args: {
107
+ ...params.args,
108
+ where: {
109
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
110
+ // allow overriding the deleted field in where
111
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
112
+ },
113
+ },
114
+ },
115
+ };
116
+ };
117
+ exports.createUpdateManyParams = createUpdateManyParams;
118
+ const createUpsertParams = (_, params) => {
119
+ var _a, _b;
120
+ if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) && !params.scope.relations.to.isList) {
121
+ throw new Error(`prisma-extension-soft-delete: upsert of model "${params.model}" through "${(_b = params.scope) === null || _b === void 0 ? void 0 : _b.parentParams.model}.${params.scope.relations.to.name}" found. Upserts of soft deleted models through a toOne relation is not supported as it is possible to update a soft deleted record.`);
122
+ }
123
+ return { params };
124
+ };
125
+ exports.createUpsertParams = createUpsertParams;
126
+ function validateFindUniqueParams(params, config) {
127
+ var _a;
128
+ const uniqueIndexFields = uniqueIndexFieldsByModel[params.model || ""] || [];
129
+ const uniqueIndexField = Object.keys(((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || {}).find((key) => uniqueIndexFields.includes(key));
130
+ // when unique index field is found it is not possible to use findFirst.
131
+ // Instead warn the user that soft-deleted models will not be excluded from
132
+ // this query unless warnForUniqueIndexes is false.
133
+ if (uniqueIndexField && !config.allowCompoundUniqueIndexWhere) {
134
+ throw new Error(`prisma-extension-soft-delete: query of model "${params.model}" through compound unique index field "${uniqueIndexField}" found. Queries of soft deleted models through a unique index are not supported. Set "allowCompoundUniqueIndexWhere" to true to override this behaviour.`);
135
+ }
136
+ }
137
+ function shouldPassFindUniqueParamsThrough(params, config) {
138
+ var _a, _b;
139
+ const uniqueFields = uniqueFieldsByModel[params.model || ""] || [];
140
+ const uniqueIndexFields = uniqueIndexFieldsByModel[params.model || ""] || [];
141
+ const uniqueIndexField = Object.keys(((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || {}).find((key) => uniqueIndexFields.includes(key));
142
+ // pass through invalid args so Prisma throws an error
143
+ return (
144
+ // findUnique must have a where object
145
+ !((_b = params.args) === null || _b === void 0 ? void 0 : _b.where) ||
146
+ typeof params.args.where !== "object" ||
147
+ // where object must have at least one defined unique field
148
+ !Object.entries(params.args.where).some(([key, val]) => (uniqueFields.includes(key) || uniqueIndexFields.includes(key)) &&
149
+ typeof val !== "undefined") ||
150
+ // pass through if where object has a unique index field and allowCompoundUniqueIndexWhere is true
151
+ !!(uniqueIndexField && config.allowCompoundUniqueIndexWhere));
152
+ }
153
+ const createFindUniqueParams = (config, params) => {
154
+ var _a, _b, _c;
155
+ if (shouldPassFindUniqueParamsThrough(params, config)) {
156
+ return { params };
157
+ }
158
+ validateFindUniqueParams(params, config);
159
+ return {
160
+ params: {
161
+ ...params,
162
+ operation: "findFirst",
163
+ args: {
164
+ ...params.args,
165
+ where: {
166
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
167
+ // allow overriding the deleted field in where
168
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
169
+ },
170
+ },
171
+ },
172
+ };
173
+ };
174
+ exports.createFindUniqueParams = createFindUniqueParams;
175
+ const createFindUniqueOrThrowParams = (config, params) => {
176
+ var _a, _b, _c;
177
+ if (shouldPassFindUniqueParamsThrough(params, config)) {
178
+ return { params };
179
+ }
180
+ validateFindUniqueParams(params, config);
181
+ return {
182
+ params: {
183
+ ...params,
184
+ operation: "findFirstOrThrow",
185
+ args: {
186
+ ...params.args,
187
+ where: {
188
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
189
+ // allow overriding the deleted field in where
190
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
191
+ },
192
+ },
193
+ },
194
+ };
195
+ };
196
+ exports.createFindUniqueOrThrowParams = createFindUniqueOrThrowParams;
197
+ const createFindFirstParams = (config, params) => {
198
+ var _a, _b, _c;
199
+ return {
200
+ params: {
201
+ ...params,
202
+ operation: "findFirst",
203
+ args: {
204
+ ...params.args,
205
+ where: {
206
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
207
+ // allow overriding the deleted field in where
208
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
209
+ },
210
+ },
211
+ },
212
+ };
213
+ };
214
+ exports.createFindFirstParams = createFindFirstParams;
215
+ const createFindFirstOrThrowParams = (config, params) => {
216
+ var _a, _b, _c;
217
+ return {
218
+ params: {
219
+ ...params,
220
+ operation: "findFirstOrThrow",
221
+ args: {
222
+ ...params.args,
223
+ where: {
224
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
225
+ // allow overriding the deleted field in where
226
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
227
+ },
228
+ },
229
+ },
230
+ };
231
+ };
232
+ exports.createFindFirstOrThrowParams = createFindFirstOrThrowParams;
233
+ const createFindManyParams = (config, params) => {
234
+ var _a, _b, _c;
235
+ return {
236
+ params: {
237
+ ...params,
238
+ operation: "findMany",
239
+ args: {
240
+ ...params.args,
241
+ where: {
242
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
243
+ // allow overriding the deleted field in where
244
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
245
+ },
246
+ },
247
+ },
248
+ };
249
+ };
250
+ exports.createFindManyParams = createFindManyParams;
251
+ /*GroupBy */
252
+ const createGroupByParams = (config, params) => {
253
+ var _a, _b, _c;
254
+ return {
255
+ params: {
256
+ ...params,
257
+ operation: "groupBy",
258
+ args: {
259
+ ...params.args,
260
+ where: {
261
+ ...(_a = params.args) === null || _a === void 0 ? void 0 : _a.where,
262
+ // allow overriding the deleted field in where
263
+ [config.field]: ((_c = (_b = params.args) === null || _b === void 0 ? void 0 : _b.where) === null || _c === void 0 ? void 0 : _c[config.field]) || config.createValue(false),
264
+ },
265
+ },
266
+ },
267
+ };
268
+ };
269
+ exports.createGroupByParams = createGroupByParams;
270
+ const createCountParams = (config, params) => {
271
+ const args = params.args || {};
272
+ const where = args.where || {};
273
+ return {
274
+ params: {
275
+ ...params,
276
+ args: {
277
+ ...args,
278
+ where: {
279
+ ...where,
280
+ // allow overriding the deleted field in where
281
+ [config.field]: where[config.field] || config.createValue(false),
282
+ },
283
+ },
284
+ },
285
+ };
286
+ };
287
+ exports.createCountParams = createCountParams;
288
+ const createAggregateParams = (config, params) => {
289
+ const args = params.args || {};
290
+ const where = args.where || {};
291
+ return {
292
+ params: {
293
+ ...params,
294
+ args: {
295
+ ...args,
296
+ where: {
297
+ ...where,
298
+ // allow overriding the deleted field in where
299
+ [config.field]: where[config.field] || config.createValue(false),
300
+ },
301
+ },
302
+ },
303
+ };
304
+ };
305
+ exports.createAggregateParams = createAggregateParams;
306
+ const createWhereParams = (config, params) => {
307
+ var _a;
308
+ if (!params.scope)
309
+ return { params };
310
+ // customise list queries with every modifier unless the deleted field is set
311
+ if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.modifier) === "every" && !params.args[config.field]) {
312
+ return {
313
+ params: {
314
+ ...params,
315
+ args: {
316
+ OR: [
317
+ { [config.field]: { not: config.createValue(false) } },
318
+ params.args,
319
+ ],
320
+ },
321
+ },
322
+ };
323
+ }
324
+ return {
325
+ params: {
326
+ ...params,
327
+ args: {
328
+ ...params.args,
329
+ [config.field]: params.args[config.field] || config.createValue(false),
330
+ },
331
+ },
332
+ };
333
+ };
334
+ exports.createWhereParams = createWhereParams;
335
+ const createIncludeParams = (config, params) => {
336
+ var _a, _b, _c, _d, _e, _f, _g;
337
+ // includes of toOne relation cannot filter deleted records using params
338
+ // instead ensure that the deleted field is selected and filter the results
339
+ if (((_b = (_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) === null || _b === void 0 ? void 0 : _b.to.isList) === false) {
340
+ if (((_c = params.args) === null || _c === void 0 ? void 0 : _c.select) && !((_d = params.args) === null || _d === void 0 ? void 0 : _d.select[config.field])) {
341
+ return {
342
+ params: (0, nestedReads_1.addDeletedToSelect)(params, config),
343
+ ctx: { deletedFieldAdded: true },
344
+ };
345
+ }
346
+ return { params };
347
+ }
348
+ return {
349
+ params: {
350
+ ...params,
351
+ args: {
352
+ ...params.args,
353
+ where: {
354
+ ...(_e = params.args) === null || _e === void 0 ? void 0 : _e.where,
355
+ // allow overriding the deleted field in where
356
+ [config.field]: ((_g = (_f = params.args) === null || _f === void 0 ? void 0 : _f.where) === null || _g === void 0 ? void 0 : _g[config.field]) || config.createValue(false),
357
+ },
358
+ },
359
+ },
360
+ };
361
+ };
362
+ exports.createIncludeParams = createIncludeParams;
363
+ const createSelectParams = (config, params) => {
364
+ var _a, _b, _c, _d, _e, _f, _g;
365
+ // selects in includes are handled by createIncludeParams
366
+ if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.parentParams.operation) === "include") {
367
+ return { params };
368
+ }
369
+ // selects of toOne relation cannot filter deleted records using params
370
+ if (((_c = (_b = params.scope) === null || _b === void 0 ? void 0 : _b.relations) === null || _c === void 0 ? void 0 : _c.to.isList) === false) {
371
+ if (((_d = params.args) === null || _d === void 0 ? void 0 : _d.select) && !params.args.select[config.field]) {
372
+ return {
373
+ params: (0, nestedReads_1.addDeletedToSelect)(params, config),
374
+ ctx: { deletedFieldAdded: true },
375
+ };
376
+ }
377
+ return { params };
378
+ }
379
+ return {
380
+ params: {
381
+ ...params,
382
+ args: {
383
+ ...params.args,
384
+ where: {
385
+ ...(_e = params.args) === null || _e === void 0 ? void 0 : _e.where,
386
+ // allow overriding the deleted field in where
387
+ [config.field]: ((_g = (_f = params.args) === null || _f === void 0 ? void 0 : _f.where) === null || _g === void 0 ? void 0 : _g[config.field]) || config.createValue(false),
388
+ },
389
+ },
390
+ },
391
+ };
392
+ };
393
+ exports.createSelectParams = createSelectParams;
@@ -0,0 +1,4 @@
1
+ import { ModelConfig } from "../types";
2
+ import { CreateParamsReturn } from "./createParams";
3
+ export type ModifyResult = (config: ModelConfig, result: any, params: CreateParamsReturn["params"], ctx?: any) => any;
4
+ export declare function modifyReadResult(config: ModelConfig, result: any, params: CreateParamsReturn["params"], ctx?: any): CreateParamsReturn;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.modifyReadResult = void 0;
4
+ const nestedReads_1 = require("../utils/nestedReads");
5
+ const resultFiltering_1 = require("../utils/resultFiltering");
6
+ function modifyReadResult(config, result, params, ctx) {
7
+ if ((0, resultFiltering_1.shouldFilterDeletedFromReadResult)(params, config)) {
8
+ const filteredResults = (0, resultFiltering_1.filterSoftDeletedResults)(result, config);
9
+ if (ctx === null || ctx === void 0 ? void 0 : ctx.deletedFieldAdded) {
10
+ (0, nestedReads_1.stripDeletedFieldFromResults)(filteredResults, config);
11
+ }
12
+ return filteredResults;
13
+ }
14
+ return result;
15
+ }
16
+ exports.modifyReadResult = modifyReadResult;
@@ -0,0 +1,11 @@
1
+ import { Prisma } from "@prisma/client";
2
+ export type ModelConfig = {
3
+ field: string;
4
+ createValue: (deleted: boolean) => any;
5
+ allowToOneUpdates?: boolean;
6
+ allowCompoundUniqueIndexWhere?: boolean;
7
+ };
8
+ export type Config = {
9
+ models: Partial<Record<Prisma.ModelName, ModelConfig | boolean>>;
10
+ defaultConfig?: ModelConfig;
11
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { ModelConfig } from "../types";
2
+ export declare function addDeletedToSelect<T extends {
3
+ args?: any;
4
+ }>(params: T, config: ModelConfig): T;
5
+ export declare function stripDeletedFieldFromResults(results: any, config: ModelConfig): any;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stripDeletedFieldFromResults = exports.addDeletedToSelect = void 0;
4
+ function addDeletedToSelect(params, config) {
5
+ if (params.args.select && !params.args.select[config.field]) {
6
+ return {
7
+ ...params,
8
+ args: {
9
+ ...params.args,
10
+ select: {
11
+ ...params.args.select,
12
+ [config.field]: true,
13
+ },
14
+ },
15
+ };
16
+ }
17
+ return params;
18
+ }
19
+ exports.addDeletedToSelect = addDeletedToSelect;
20
+ function stripDeletedFieldFromResults(results, config) {
21
+ if (Array.isArray(results)) {
22
+ results === null || results === void 0 ? void 0 : results.forEach((item) => {
23
+ delete item[config.field];
24
+ });
25
+ }
26
+ else if (results) {
27
+ delete results[config.field];
28
+ }
29
+ return results;
30
+ }
31
+ exports.stripDeletedFieldFromResults = stripDeletedFieldFromResults;
@@ -0,0 +1,5 @@
1
+ import { ModelConfig } from "../types";
2
+ export declare function shouldFilterDeletedFromReadResult(params: {
3
+ args: any;
4
+ }, config: ModelConfig): boolean;
5
+ export declare function filterSoftDeletedResults(result: any, config: ModelConfig): any;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterSoftDeletedResults = exports.shouldFilterDeletedFromReadResult = void 0;
4
+ // Maybe this should return true for non-list relations only?
5
+ function shouldFilterDeletedFromReadResult(params, config) {
6
+ return (!params.args.where ||
7
+ typeof params.args.where[config.field] === "undefined" ||
8
+ !params.args.where[config.field]);
9
+ }
10
+ exports.shouldFilterDeletedFromReadResult = shouldFilterDeletedFromReadResult;
11
+ function filterSoftDeletedResults(result, config) {
12
+ // filter out deleted records from array results
13
+ if (result && Array.isArray(result)) {
14
+ return result.filter((item) => !item[config.field]);
15
+ }
16
+ // if the result is deleted return null
17
+ if (result && result[config.field]) {
18
+ return null;
19
+ }
20
+ return result;
21
+ }
22
+ exports.filterSoftDeletedResults = filterSoftDeletedResults;