@roundtreasury/prisma-extension-soft-delete 1.0.0 → 1.0.1

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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <div align="center">
2
2
  <h1>Prisma Extension Soft Delete</h1>
3
3
 
4
- <p>Prisma extension for soft deleting records.</p>
4
+ <p>Round Prisma extension for soft deleting records.</p>
5
5
 
6
6
  <p>
7
7
  Soft deleting records is a common pattern in many applications. This library provides an extension for Prisma that
@@ -1,2 +1,2 @@
1
1
  import { Config } from "./types";
2
- export declare function createSoftDeleteExtension({ models, defaultConfig, }: Config): (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {}, {}, {}> & import("@prisma/client/runtime/library").DefaultArgs>;
2
+ export declare function createSoftDeleteExtension({ models, defaultConfig, dmmf, }: Config): (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {}, {}, {}> & import("@prisma/client/runtime/library").DefaultArgs>;
@@ -1,19 +1,21 @@
1
+ import { Prisma } from "@prisma/client";
1
2
  import { Prisma as PrismaExtensions } from "@prisma/client/extension";
2
- import { withNestedOperations, } from "prisma-extension-nested-operations";
3
- import { createAggregateParams, createCountParams, createDeleteManyParams, createDeleteParams, createFindFirstParams, createFindFirstOrThrowParams, createFindManyParams, createFindUniqueParams, createFindUniqueOrThrowParams, createIncludeParams, createSelectParams, createUpdateManyParams, createUpdateParams, createUpsertParams, createWhereParams, createGroupByParams, } from "./helpers/createParams";
3
+ import { withNestedOperations, } from "@roundtreasury/prisma-extension-nested-operations";
4
+ import { createAggregateParams, createCountParams, createDeleteManyParams, createDeleteParams, createFindFirstParams, createFindFirstOrThrowParams, createFindManyParams, createFindUniqueParams, createFindUniqueOrThrowParams, createIncludeParams, createSelectParams, createUpdateManyParams, createUpdateParams, createUpsertParams, createWhereParams, createGroupByParams, createContext, } from "./helpers/createParams";
4
5
  import { modifyReadResult } from "./helpers/modifyResult";
5
6
  export function createSoftDeleteExtension({ models, defaultConfig = {
6
7
  field: "deleted",
7
8
  createValue: Boolean,
8
9
  allowToOneUpdates: false,
9
10
  allowCompoundUniqueIndexWhere: false,
10
- }, }) {
11
+ }, dmmf, }) {
11
12
  if (!defaultConfig.field) {
12
13
  throw new Error("prisma-extension-soft-delete: defaultConfig.field is required");
13
14
  }
14
15
  if (!defaultConfig.createValue) {
15
16
  throw new Error("prisma-extension-soft-delete: defaultConfig.createValue is required");
16
17
  }
18
+ const dmmfToUse = dmmf !== null && dmmf !== void 0 ? dmmf : Prisma.dmmf;
17
19
  const modelConfig = {};
18
20
  Object.keys(models).forEach((model) => {
19
21
  const modelName = model;
@@ -23,27 +25,28 @@ export function createSoftDeleteExtension({ models, defaultConfig = {
23
25
  typeof config === "boolean" && config ? defaultConfig : config;
24
26
  }
25
27
  });
28
+ const context = createContext(dmmfToUse);
26
29
  const createParamsByModel = Object.keys(modelConfig).reduce((acc, model) => {
27
30
  const config = modelConfig[model];
28
31
  return {
29
32
  ...acc,
30
33
  [model]: {
31
- delete: createDeleteParams.bind(null, config),
32
- deleteMany: createDeleteManyParams.bind(null, config),
33
- update: createUpdateParams.bind(null, config),
34
- updateMany: createUpdateManyParams.bind(null, config),
35
- upsert: createUpsertParams.bind(null, config),
36
- findFirst: createFindFirstParams.bind(null, config),
37
- findFirstOrThrow: createFindFirstOrThrowParams.bind(null, config),
38
- findUnique: createFindUniqueParams.bind(null, config),
39
- findUniqueOrThrow: createFindUniqueOrThrowParams.bind(null, config),
40
- findMany: createFindManyParams.bind(null, config),
41
- count: createCountParams.bind(null, config),
42
- aggregate: createAggregateParams.bind(null, config),
43
- where: createWhereParams.bind(null, config),
44
- include: createIncludeParams.bind(null, config),
45
- select: createSelectParams.bind(null, config),
46
- groupBy: createGroupByParams.bind(null, config),
34
+ delete: createDeleteParams.bind(null, context, config),
35
+ deleteMany: createDeleteManyParams.bind(null, context, config),
36
+ update: createUpdateParams.bind(null, context, config),
37
+ updateMany: createUpdateManyParams.bind(null, context, config),
38
+ upsert: createUpsertParams.bind(null, context, config),
39
+ findFirst: createFindFirstParams.bind(null, context, config),
40
+ findFirstOrThrow: createFindFirstOrThrowParams.bind(null, context, config),
41
+ findUnique: createFindUniqueParams.bind(null, context, config),
42
+ findUniqueOrThrow: createFindUniqueOrThrowParams.bind(null, context, config),
43
+ findMany: createFindManyParams.bind(null, context, config),
44
+ count: createCountParams.bind(null, context, config),
45
+ aggregate: createAggregateParams.bind(null, context, config),
46
+ where: createWhereParams.bind(null, context, config),
47
+ include: createIncludeParams.bind(null, context, config),
48
+ select: createSelectParams.bind(null, context, config),
49
+ groupBy: createGroupByParams.bind(null, context, config),
47
50
  },
48
51
  };
49
52
  }, {});
@@ -52,8 +55,8 @@ export function createSoftDeleteExtension({ models, defaultConfig = {
52
55
  return {
53
56
  ...acc,
54
57
  [model]: {
55
- include: modifyReadResult.bind(null, config),
56
- select: modifyReadResult.bind(null, config),
58
+ include: modifyReadResult.bind(null, context, config),
59
+ select: modifyReadResult.bind(null, context, config),
57
60
  },
58
61
  };
59
62
  }, {});
@@ -65,6 +68,7 @@ export function createSoftDeleteExtension({ models, defaultConfig = {
65
68
  $allModels: {
66
69
  // @ts-expect-error - we don't know what the client is
67
70
  $allOperations: withNestedOperations({
71
+ dmmf: dmmfToUse,
68
72
  async $rootOperation(initialParams) {
69
73
  var _a, _b;
70
74
  const createParams = (_a = createParamsByModel[initialParams.model || ""]) === null || _a === void 0 ? void 0 : _a[initialParams.operation];
@@ -1,5 +1,7 @@
1
- import { NestedParams } from "prisma-extension-nested-operations";
2
- import { ModelConfig } from "../types";
1
+ import { NestedParams } from "@roundtreasury/prisma-extension-nested-operations";
2
+ import { BaseDMMF } from "@prisma/client/runtime/library";
3
+ import { Context, ModelConfig } from "../types";
4
+ export declare const createContext: (dmmf: BaseDMMF) => Context;
3
5
  export type Params = Omit<NestedParams<any>, "operation"> & {
4
6
  operation: string;
5
7
  };
@@ -7,7 +9,7 @@ export type CreateParamsReturn = {
7
9
  params: Params;
8
10
  ctx?: any;
9
11
  };
10
- export type CreateParams = (config: ModelConfig, params: Params) => CreateParamsReturn;
12
+ export type CreateParams = (context: Context, config: ModelConfig, params: Params) => CreateParamsReturn;
11
13
  export declare const createDeleteParams: CreateParams;
12
14
  export declare const createDeleteManyParams: CreateParams;
13
15
  export declare const createUpdateParams: CreateParams;
@@ -1,24 +1,26 @@
1
- import { Prisma } from "@prisma/client";
2
1
  import { addDeletedToSelect } from "../utils/nestedReads";
3
- const uniqueFieldsByModel = {};
4
- const uniqueIndexFieldsByModel = {};
5
- Prisma.dmmf.datamodel.models.forEach((model) => {
6
- // add unique fields derived from indexes
7
- const uniqueIndexFields = [];
8
- model.uniqueFields.forEach((field) => {
9
- uniqueIndexFields.push(field.join("_"));
2
+ export const createContext = (dmmf) => {
3
+ const uniqueFieldsByModel = {};
4
+ const uniqueIndexFieldsByModel = {};
5
+ dmmf.datamodel.models.forEach((model) => {
6
+ // add unique fields derived from indexes
7
+ const uniqueIndexFields = [];
8
+ model.uniqueFields.forEach((field) => {
9
+ uniqueIndexFields.push(field.join("_"));
10
+ });
11
+ uniqueIndexFieldsByModel[model.name] = uniqueIndexFields;
12
+ // add id field and unique fields from @unique decorator
13
+ const uniqueFields = [];
14
+ model.fields.forEach((field) => {
15
+ if (field.isId || field.isUnique) {
16
+ uniqueFields.push(field.name);
17
+ }
18
+ });
19
+ uniqueFieldsByModel[model.name] = uniqueFields;
10
20
  });
11
- uniqueIndexFieldsByModel[model.name] = uniqueIndexFields;
12
- // add id field and unique fields from @unique decorator
13
- const uniqueFields = [];
14
- model.fields.forEach((field) => {
15
- if (field.isId || field.isUnique) {
16
- uniqueFields.push(field.name);
17
- }
18
- });
19
- uniqueFieldsByModel[model.name] = uniqueFields;
20
- });
21
- export const createDeleteParams = ({ field, createValue }, params) => {
21
+ return { uniqueFieldsByModel, uniqueIndexFieldsByModel };
22
+ };
23
+ export const createDeleteParams = (_, { field, createValue }, params) => {
22
24
  var _a, _b;
23
25
  if (!params.model ||
24
26
  // do nothing for delete: false
@@ -54,7 +56,7 @@ export const createDeleteParams = ({ field, createValue }, params) => {
54
56
  },
55
57
  };
56
58
  };
57
- export const createDeleteManyParams = (config, params) => {
59
+ export const createDeleteManyParams = (_, config, params) => {
58
60
  var _a;
59
61
  if (!params.model)
60
62
  return { params };
@@ -75,7 +77,7 @@ export const createDeleteManyParams = (config, params) => {
75
77
  },
76
78
  };
77
79
  };
78
- export const createUpdateParams = (config, params) => {
80
+ export const createUpdateParams = (_, config, params) => {
79
81
  var _a, _b, _c, _d;
80
82
  if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) &&
81
83
  !params.scope.relations.to.isList &&
@@ -89,7 +91,7 @@ export const createUpdateParams = (config, params) => {
89
91
  }
90
92
  return { params };
91
93
  };
92
- export const createUpdateManyParams = (config, params) => {
94
+ export const createUpdateManyParams = (_, config, params) => {
93
95
  var _a, _b, _c;
94
96
  // do nothing if args are not defined to allow Prisma to throw an error
95
97
  if (!params.args)
@@ -108,15 +110,16 @@ export const createUpdateManyParams = (config, params) => {
108
110
  },
109
111
  };
110
112
  };
111
- export const createUpsertParams = (_, params) => {
113
+ export const createUpsertParams = (_, __, params) => {
112
114
  var _a, _b;
113
115
  if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) && !params.scope.relations.to.isList) {
114
116
  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.`);
115
117
  }
116
118
  return { params };
117
119
  };
118
- function validateFindUniqueParams(params, config) {
120
+ function validateFindUniqueParams(context, params, config) {
119
121
  var _a;
122
+ const { uniqueIndexFieldsByModel } = context;
120
123
  const uniqueIndexFields = uniqueIndexFieldsByModel[params.model || ""] || [];
121
124
  const uniqueIndexField = Object.keys(((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || {}).find((key) => uniqueIndexFields.includes(key));
122
125
  // when unique index field is found it is not possible to use findFirst.
@@ -126,8 +129,9 @@ function validateFindUniqueParams(params, config) {
126
129
  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.`);
127
130
  }
128
131
  }
129
- function shouldPassFindUniqueParamsThrough(params, config) {
132
+ function shouldPassFindUniqueParamsThrough(context, params, config) {
130
133
  var _a, _b;
134
+ const { uniqueFieldsByModel, uniqueIndexFieldsByModel } = context;
131
135
  const uniqueFields = uniqueFieldsByModel[params.model || ""] || [];
132
136
  const uniqueIndexFields = uniqueIndexFieldsByModel[params.model || ""] || [];
133
137
  const uniqueIndexField = Object.keys(((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || {}).find((key) => uniqueIndexFields.includes(key));
@@ -142,12 +146,12 @@ function shouldPassFindUniqueParamsThrough(params, config) {
142
146
  // pass through if where object has a unique index field and allowCompoundUniqueIndexWhere is true
143
147
  !!(uniqueIndexField && config.allowCompoundUniqueIndexWhere));
144
148
  }
145
- export const createFindUniqueParams = (config, params) => {
149
+ export const createFindUniqueParams = (context, config, params) => {
146
150
  var _a, _b, _c;
147
- if (shouldPassFindUniqueParamsThrough(params, config)) {
151
+ if (shouldPassFindUniqueParamsThrough(context, params, config)) {
148
152
  return { params };
149
153
  }
150
- validateFindUniqueParams(params, config);
154
+ validateFindUniqueParams(context, params, config);
151
155
  return {
152
156
  params: {
153
157
  ...params,
@@ -163,12 +167,12 @@ export const createFindUniqueParams = (config, params) => {
163
167
  },
164
168
  };
165
169
  };
166
- export const createFindUniqueOrThrowParams = (config, params) => {
170
+ export const createFindUniqueOrThrowParams = (context, config, params) => {
167
171
  var _a, _b, _c;
168
- if (shouldPassFindUniqueParamsThrough(params, config)) {
172
+ if (shouldPassFindUniqueParamsThrough(context, params, config)) {
169
173
  return { params };
170
174
  }
171
- validateFindUniqueParams(params, config);
175
+ validateFindUniqueParams(context, params, config);
172
176
  return {
173
177
  params: {
174
178
  ...params,
@@ -184,7 +188,7 @@ export const createFindUniqueOrThrowParams = (config, params) => {
184
188
  },
185
189
  };
186
190
  };
187
- export const createFindFirstParams = (config, params) => {
191
+ export const createFindFirstParams = (_, config, params) => {
188
192
  var _a, _b, _c;
189
193
  return {
190
194
  params: {
@@ -201,7 +205,7 @@ export const createFindFirstParams = (config, params) => {
201
205
  },
202
206
  };
203
207
  };
204
- export const createFindFirstOrThrowParams = (config, params) => {
208
+ export const createFindFirstOrThrowParams = (_, config, params) => {
205
209
  var _a, _b, _c;
206
210
  return {
207
211
  params: {
@@ -218,7 +222,7 @@ export const createFindFirstOrThrowParams = (config, params) => {
218
222
  },
219
223
  };
220
224
  };
221
- export const createFindManyParams = (config, params) => {
225
+ export const createFindManyParams = (_, config, params) => {
222
226
  var _a, _b, _c;
223
227
  return {
224
228
  params: {
@@ -236,7 +240,7 @@ export const createFindManyParams = (config, params) => {
236
240
  };
237
241
  };
238
242
  /*GroupBy */
239
- export const createGroupByParams = (config, params) => {
243
+ export const createGroupByParams = (_, config, params) => {
240
244
  var _a, _b, _c;
241
245
  return {
242
246
  params: {
@@ -253,7 +257,7 @@ export const createGroupByParams = (config, params) => {
253
257
  },
254
258
  };
255
259
  };
256
- export const createCountParams = (config, params) => {
260
+ export const createCountParams = (_, config, params) => {
257
261
  const args = params.args || {};
258
262
  const where = args.where || {};
259
263
  return {
@@ -270,7 +274,7 @@ export const createCountParams = (config, params) => {
270
274
  },
271
275
  };
272
276
  };
273
- export const createAggregateParams = (config, params) => {
277
+ export const createAggregateParams = (_, config, params) => {
274
278
  const args = params.args || {};
275
279
  const where = args.where || {};
276
280
  return {
@@ -287,7 +291,7 @@ export const createAggregateParams = (config, params) => {
287
291
  },
288
292
  };
289
293
  };
290
- export const createWhereParams = (config, params) => {
294
+ export const createWhereParams = (_, config, params) => {
291
295
  var _a;
292
296
  if (!params.scope)
293
297
  return { params };
@@ -315,7 +319,7 @@ export const createWhereParams = (config, params) => {
315
319
  },
316
320
  };
317
321
  };
318
- export const createIncludeParams = (config, params) => {
322
+ export const createIncludeParams = (_, config, params) => {
319
323
  var _a, _b, _c, _d, _e, _f, _g;
320
324
  // includes of toOne relation cannot filter deleted records using params
321
325
  // instead ensure that the deleted field is selected and filter the results
@@ -342,7 +346,7 @@ export const createIncludeParams = (config, params) => {
342
346
  },
343
347
  };
344
348
  };
345
- export const createSelectParams = (config, params) => {
349
+ export const createSelectParams = (_, config, params) => {
346
350
  var _a, _b, _c, _d, _e, _f, _g;
347
351
  // selects in includes are handled by createIncludeParams
348
352
  if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.parentParams.operation) === "include") {
@@ -1,4 +1,4 @@
1
- import { ModelConfig } from "../types";
1
+ import { Context, ModelConfig } from "../types";
2
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;
3
+ export type ModifyResult = (context: Context, config: ModelConfig, result: any, params: CreateParamsReturn["params"], ctx?: any) => any;
4
+ export declare function modifyReadResult(_: Context, config: ModelConfig, result: any, params: CreateParamsReturn["params"], ctx?: any): CreateParamsReturn;
@@ -1,6 +1,6 @@
1
1
  import { stripDeletedFieldFromResults } from "../utils/nestedReads";
2
2
  import { filterSoftDeletedResults, shouldFilterDeletedFromReadResult, } from "../utils/resultFiltering";
3
- export function modifyReadResult(config, result, params, ctx) {
3
+ export function modifyReadResult(_, config, result, params, ctx) {
4
4
  if (shouldFilterDeletedFromReadResult(params, config)) {
5
5
  const filteredResults = filterSoftDeletedResults(result, config);
6
6
  if (ctx === null || ctx === void 0 ? void 0 : ctx.deletedFieldAdded) {
@@ -1,4 +1,9 @@
1
1
  import { Prisma } from "@prisma/client";
2
+ import { BaseDMMF } from "@prisma/client/runtime/library";
3
+ export type Context = {
4
+ uniqueFieldsByModel: Record<string, string[]>;
5
+ uniqueIndexFieldsByModel: Record<string, string[]>;
6
+ };
2
7
  export type ModelConfig = {
3
8
  field: string;
4
9
  createValue: (deleted: boolean) => any;
@@ -8,4 +13,5 @@ export type ModelConfig = {
8
13
  export type Config = {
9
14
  models: Partial<Record<Prisma.ModelName, ModelConfig | boolean>>;
10
15
  defaultConfig?: ModelConfig;
16
+ dmmf?: BaseDMMF;
11
17
  };
@@ -1,2 +1,2 @@
1
1
  import { Config } from "./types";
2
- export declare function createSoftDeleteExtension({ models, defaultConfig, }: Config): (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {}, {}, {}> & import("@prisma/client/runtime/library").DefaultArgs>;
2
+ export declare function createSoftDeleteExtension({ models, defaultConfig, dmmf, }: Config): (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {}, {}, {}> & import("@prisma/client/runtime/library").DefaultArgs>;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSoftDeleteExtension = void 0;
4
+ const client_1 = require("@prisma/client");
4
5
  const extension_1 = require("@prisma/client/extension");
5
- const prisma_extension_nested_operations_1 = require("prisma-extension-nested-operations");
6
+ const prisma_extension_nested_operations_1 = require("@roundtreasury/prisma-extension-nested-operations");
6
7
  const createParams_1 = require("./helpers/createParams");
7
8
  const modifyResult_1 = require("./helpers/modifyResult");
8
9
  function createSoftDeleteExtension({ models, defaultConfig = {
@@ -10,13 +11,14 @@ function createSoftDeleteExtension({ models, defaultConfig = {
10
11
  createValue: Boolean,
11
12
  allowToOneUpdates: false,
12
13
  allowCompoundUniqueIndexWhere: false,
13
- }, }) {
14
+ }, dmmf, }) {
14
15
  if (!defaultConfig.field) {
15
16
  throw new Error("prisma-extension-soft-delete: defaultConfig.field is required");
16
17
  }
17
18
  if (!defaultConfig.createValue) {
18
19
  throw new Error("prisma-extension-soft-delete: defaultConfig.createValue is required");
19
20
  }
21
+ const dmmfToUse = dmmf !== null && dmmf !== void 0 ? dmmf : client_1.Prisma.dmmf;
20
22
  const modelConfig = {};
21
23
  Object.keys(models).forEach((model) => {
22
24
  const modelName = model;
@@ -26,27 +28,28 @@ function createSoftDeleteExtension({ models, defaultConfig = {
26
28
  typeof config === "boolean" && config ? defaultConfig : config;
27
29
  }
28
30
  });
31
+ const context = (0, createParams_1.createContext)(dmmfToUse);
29
32
  const createParamsByModel = Object.keys(modelConfig).reduce((acc, model) => {
30
33
  const config = modelConfig[model];
31
34
  return {
32
35
  ...acc,
33
36
  [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),
37
+ delete: createParams_1.createDeleteParams.bind(null, context, config),
38
+ deleteMany: createParams_1.createDeleteManyParams.bind(null, context, config),
39
+ update: createParams_1.createUpdateParams.bind(null, context, config),
40
+ updateMany: createParams_1.createUpdateManyParams.bind(null, context, config),
41
+ upsert: createParams_1.createUpsertParams.bind(null, context, config),
42
+ findFirst: createParams_1.createFindFirstParams.bind(null, context, config),
43
+ findFirstOrThrow: createParams_1.createFindFirstOrThrowParams.bind(null, context, config),
44
+ findUnique: createParams_1.createFindUniqueParams.bind(null, context, config),
45
+ findUniqueOrThrow: createParams_1.createFindUniqueOrThrowParams.bind(null, context, config),
46
+ findMany: createParams_1.createFindManyParams.bind(null, context, config),
47
+ count: createParams_1.createCountParams.bind(null, context, config),
48
+ aggregate: createParams_1.createAggregateParams.bind(null, context, config),
49
+ where: createParams_1.createWhereParams.bind(null, context, config),
50
+ include: createParams_1.createIncludeParams.bind(null, context, config),
51
+ select: createParams_1.createSelectParams.bind(null, context, config),
52
+ groupBy: createParams_1.createGroupByParams.bind(null, context, config),
50
53
  },
51
54
  };
52
55
  }, {});
@@ -55,8 +58,8 @@ function createSoftDeleteExtension({ models, defaultConfig = {
55
58
  return {
56
59
  ...acc,
57
60
  [model]: {
58
- include: modifyResult_1.modifyReadResult.bind(null, config),
59
- select: modifyResult_1.modifyReadResult.bind(null, config),
61
+ include: modifyResult_1.modifyReadResult.bind(null, context, config),
62
+ select: modifyResult_1.modifyReadResult.bind(null, context, config),
60
63
  },
61
64
  };
62
65
  }, {});
@@ -68,6 +71,7 @@ function createSoftDeleteExtension({ models, defaultConfig = {
68
71
  $allModels: {
69
72
  // @ts-expect-error - we don't know what the client is
70
73
  $allOperations: (0, prisma_extension_nested_operations_1.withNestedOperations)({
74
+ dmmf: dmmfToUse,
71
75
  async $rootOperation(initialParams) {
72
76
  var _a, _b;
73
77
  const createParams = (_a = createParamsByModel[initialParams.model || ""]) === null || _a === void 0 ? void 0 : _a[initialParams.operation];
@@ -1,5 +1,7 @@
1
- import { NestedParams } from "prisma-extension-nested-operations";
2
- import { ModelConfig } from "../types";
1
+ import { NestedParams } from "@roundtreasury/prisma-extension-nested-operations";
2
+ import { BaseDMMF } from "@prisma/client/runtime/library";
3
+ import { Context, ModelConfig } from "../types";
4
+ export declare const createContext: (dmmf: BaseDMMF) => Context;
3
5
  export type Params = Omit<NestedParams<any>, "operation"> & {
4
6
  operation: string;
5
7
  };
@@ -7,7 +9,7 @@ export type CreateParamsReturn = {
7
9
  params: Params;
8
10
  ctx?: any;
9
11
  };
10
- export type CreateParams = (config: ModelConfig, params: Params) => CreateParamsReturn;
12
+ export type CreateParams = (context: Context, config: ModelConfig, params: Params) => CreateParamsReturn;
11
13
  export declare const createDeleteParams: CreateParams;
12
14
  export declare const createDeleteManyParams: CreateParams;
13
15
  export declare const createUpdateParams: CreateParams;
@@ -1,27 +1,30 @@
1
1
  "use strict";
2
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");
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 = exports.createContext = void 0;
5
4
  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("_"));
5
+ const createContext = (dmmf) => {
6
+ const uniqueFieldsByModel = {};
7
+ const uniqueIndexFieldsByModel = {};
8
+ 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;
13
23
  });
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) => {
24
+ return { uniqueFieldsByModel, uniqueIndexFieldsByModel };
25
+ };
26
+ exports.createContext = createContext;
27
+ const createDeleteParams = (_, { field, createValue }, params) => {
25
28
  var _a, _b;
26
29
  if (!params.model ||
27
30
  // do nothing for delete: false
@@ -58,7 +61,7 @@ const createDeleteParams = ({ field, createValue }, params) => {
58
61
  };
59
62
  };
60
63
  exports.createDeleteParams = createDeleteParams;
61
- const createDeleteManyParams = (config, params) => {
64
+ const createDeleteManyParams = (_, config, params) => {
62
65
  var _a;
63
66
  if (!params.model)
64
67
  return { params };
@@ -80,7 +83,7 @@ const createDeleteManyParams = (config, params) => {
80
83
  };
81
84
  };
82
85
  exports.createDeleteManyParams = createDeleteManyParams;
83
- const createUpdateParams = (config, params) => {
86
+ const createUpdateParams = (_, config, params) => {
84
87
  var _a, _b, _c, _d;
85
88
  if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) &&
86
89
  !params.scope.relations.to.isList &&
@@ -95,7 +98,7 @@ const createUpdateParams = (config, params) => {
95
98
  return { params };
96
99
  };
97
100
  exports.createUpdateParams = createUpdateParams;
98
- const createUpdateManyParams = (config, params) => {
101
+ const createUpdateManyParams = (_, config, params) => {
99
102
  var _a, _b, _c;
100
103
  // do nothing if args are not defined to allow Prisma to throw an error
101
104
  if (!params.args)
@@ -115,7 +118,7 @@ const createUpdateManyParams = (config, params) => {
115
118
  };
116
119
  };
117
120
  exports.createUpdateManyParams = createUpdateManyParams;
118
- const createUpsertParams = (_, params) => {
121
+ const createUpsertParams = (_, __, params) => {
119
122
  var _a, _b;
120
123
  if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.relations) && !params.scope.relations.to.isList) {
121
124
  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.`);
@@ -123,8 +126,9 @@ const createUpsertParams = (_, params) => {
123
126
  return { params };
124
127
  };
125
128
  exports.createUpsertParams = createUpsertParams;
126
- function validateFindUniqueParams(params, config) {
129
+ function validateFindUniqueParams(context, params, config) {
127
130
  var _a;
131
+ const { uniqueIndexFieldsByModel } = context;
128
132
  const uniqueIndexFields = uniqueIndexFieldsByModel[params.model || ""] || [];
129
133
  const uniqueIndexField = Object.keys(((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || {}).find((key) => uniqueIndexFields.includes(key));
130
134
  // when unique index field is found it is not possible to use findFirst.
@@ -134,8 +138,9 @@ function validateFindUniqueParams(params, config) {
134
138
  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
139
  }
136
140
  }
137
- function shouldPassFindUniqueParamsThrough(params, config) {
141
+ function shouldPassFindUniqueParamsThrough(context, params, config) {
138
142
  var _a, _b;
143
+ const { uniqueFieldsByModel, uniqueIndexFieldsByModel } = context;
139
144
  const uniqueFields = uniqueFieldsByModel[params.model || ""] || [];
140
145
  const uniqueIndexFields = uniqueIndexFieldsByModel[params.model || ""] || [];
141
146
  const uniqueIndexField = Object.keys(((_a = params.args) === null || _a === void 0 ? void 0 : _a.where) || {}).find((key) => uniqueIndexFields.includes(key));
@@ -150,12 +155,12 @@ function shouldPassFindUniqueParamsThrough(params, config) {
150
155
  // pass through if where object has a unique index field and allowCompoundUniqueIndexWhere is true
151
156
  !!(uniqueIndexField && config.allowCompoundUniqueIndexWhere));
152
157
  }
153
- const createFindUniqueParams = (config, params) => {
158
+ const createFindUniqueParams = (context, config, params) => {
154
159
  var _a, _b, _c;
155
- if (shouldPassFindUniqueParamsThrough(params, config)) {
160
+ if (shouldPassFindUniqueParamsThrough(context, params, config)) {
156
161
  return { params };
157
162
  }
158
- validateFindUniqueParams(params, config);
163
+ validateFindUniqueParams(context, params, config);
159
164
  return {
160
165
  params: {
161
166
  ...params,
@@ -172,12 +177,12 @@ const createFindUniqueParams = (config, params) => {
172
177
  };
173
178
  };
174
179
  exports.createFindUniqueParams = createFindUniqueParams;
175
- const createFindUniqueOrThrowParams = (config, params) => {
180
+ const createFindUniqueOrThrowParams = (context, config, params) => {
176
181
  var _a, _b, _c;
177
- if (shouldPassFindUniqueParamsThrough(params, config)) {
182
+ if (shouldPassFindUniqueParamsThrough(context, params, config)) {
178
183
  return { params };
179
184
  }
180
- validateFindUniqueParams(params, config);
185
+ validateFindUniqueParams(context, params, config);
181
186
  return {
182
187
  params: {
183
188
  ...params,
@@ -194,7 +199,7 @@ const createFindUniqueOrThrowParams = (config, params) => {
194
199
  };
195
200
  };
196
201
  exports.createFindUniqueOrThrowParams = createFindUniqueOrThrowParams;
197
- const createFindFirstParams = (config, params) => {
202
+ const createFindFirstParams = (_, config, params) => {
198
203
  var _a, _b, _c;
199
204
  return {
200
205
  params: {
@@ -212,7 +217,7 @@ const createFindFirstParams = (config, params) => {
212
217
  };
213
218
  };
214
219
  exports.createFindFirstParams = createFindFirstParams;
215
- const createFindFirstOrThrowParams = (config, params) => {
220
+ const createFindFirstOrThrowParams = (_, config, params) => {
216
221
  var _a, _b, _c;
217
222
  return {
218
223
  params: {
@@ -230,7 +235,7 @@ const createFindFirstOrThrowParams = (config, params) => {
230
235
  };
231
236
  };
232
237
  exports.createFindFirstOrThrowParams = createFindFirstOrThrowParams;
233
- const createFindManyParams = (config, params) => {
238
+ const createFindManyParams = (_, config, params) => {
234
239
  var _a, _b, _c;
235
240
  return {
236
241
  params: {
@@ -249,7 +254,7 @@ const createFindManyParams = (config, params) => {
249
254
  };
250
255
  exports.createFindManyParams = createFindManyParams;
251
256
  /*GroupBy */
252
- const createGroupByParams = (config, params) => {
257
+ const createGroupByParams = (_, config, params) => {
253
258
  var _a, _b, _c;
254
259
  return {
255
260
  params: {
@@ -267,7 +272,7 @@ const createGroupByParams = (config, params) => {
267
272
  };
268
273
  };
269
274
  exports.createGroupByParams = createGroupByParams;
270
- const createCountParams = (config, params) => {
275
+ const createCountParams = (_, config, params) => {
271
276
  const args = params.args || {};
272
277
  const where = args.where || {};
273
278
  return {
@@ -285,7 +290,7 @@ const createCountParams = (config, params) => {
285
290
  };
286
291
  };
287
292
  exports.createCountParams = createCountParams;
288
- const createAggregateParams = (config, params) => {
293
+ const createAggregateParams = (_, config, params) => {
289
294
  const args = params.args || {};
290
295
  const where = args.where || {};
291
296
  return {
@@ -303,7 +308,7 @@ const createAggregateParams = (config, params) => {
303
308
  };
304
309
  };
305
310
  exports.createAggregateParams = createAggregateParams;
306
- const createWhereParams = (config, params) => {
311
+ const createWhereParams = (_, config, params) => {
307
312
  var _a;
308
313
  if (!params.scope)
309
314
  return { params };
@@ -332,7 +337,7 @@ const createWhereParams = (config, params) => {
332
337
  };
333
338
  };
334
339
  exports.createWhereParams = createWhereParams;
335
- const createIncludeParams = (config, params) => {
340
+ const createIncludeParams = (_, config, params) => {
336
341
  var _a, _b, _c, _d, _e, _f, _g;
337
342
  // includes of toOne relation cannot filter deleted records using params
338
343
  // instead ensure that the deleted field is selected and filter the results
@@ -360,7 +365,7 @@ const createIncludeParams = (config, params) => {
360
365
  };
361
366
  };
362
367
  exports.createIncludeParams = createIncludeParams;
363
- const createSelectParams = (config, params) => {
368
+ const createSelectParams = (_, config, params) => {
364
369
  var _a, _b, _c, _d, _e, _f, _g;
365
370
  // selects in includes are handled by createIncludeParams
366
371
  if (((_a = params.scope) === null || _a === void 0 ? void 0 : _a.parentParams.operation) === "include") {
@@ -1,4 +1,4 @@
1
- import { ModelConfig } from "../types";
1
+ import { Context, ModelConfig } from "../types";
2
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;
3
+ export type ModifyResult = (context: Context, config: ModelConfig, result: any, params: CreateParamsReturn["params"], ctx?: any) => any;
4
+ export declare function modifyReadResult(_: Context, config: ModelConfig, result: any, params: CreateParamsReturn["params"], ctx?: any): CreateParamsReturn;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.modifyReadResult = void 0;
4
4
  const nestedReads_1 = require("../utils/nestedReads");
5
5
  const resultFiltering_1 = require("../utils/resultFiltering");
6
- function modifyReadResult(config, result, params, ctx) {
6
+ function modifyReadResult(_, config, result, params, ctx) {
7
7
  if ((0, resultFiltering_1.shouldFilterDeletedFromReadResult)(params, config)) {
8
8
  const filteredResults = (0, resultFiltering_1.filterSoftDeletedResults)(result, config);
9
9
  if (ctx === null || ctx === void 0 ? void 0 : ctx.deletedFieldAdded) {
@@ -1,4 +1,9 @@
1
1
  import { Prisma } from "@prisma/client";
2
+ import { BaseDMMF } from "@prisma/client/runtime/library";
3
+ export type Context = {
4
+ uniqueFieldsByModel: Record<string, string[]>;
5
+ uniqueIndexFieldsByModel: Record<string, string[]>;
6
+ };
2
7
  export type ModelConfig = {
3
8
  field: string;
4
9
  createValue: (deleted: boolean) => any;
@@ -8,4 +13,5 @@ export type ModelConfig = {
8
13
  export type Config = {
9
14
  models: Partial<Record<Prisma.ModelName, ModelConfig | boolean>>;
10
15
  defaultConfig?: ModelConfig;
16
+ dmmf?: BaseDMMF;
11
17
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@roundtreasury/prisma-extension-soft-delete",
3
- "version": "1.0.0",
4
- "description": "Prisma extension for soft deleting records",
3
+ "version": "1.0.1",
4
+ "description": "Round Treasury Prisma extension for soft deleting records",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "module": "dist/esm/index.js",