-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.ts
159 lines (142 loc) · 5.05 KB
/
relation.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
import type { Prisma } from '@prisma/client'
type PrismaAction = Prisma.PrismaAction
type ModelName = Prisma.ModelName
type MiddlewareParams = Prisma.MiddlewareParams
type MiddlewareNext = <T>(params: MiddlewareParams) => Promise<T>
// ==
/**
* The possible locations on `params.args` where a relation's unique identifier will be added.
*/
export type RelationQueryArg = 'data' | 'where'
/**
* A `RelationActionMap` maps a `PrismaAction` to its `RelationQueryArg`
*/
export type RelationActionMap = Partial<{
[key in PrismaAction]: RelationQueryArg
}>
/**
* Options to configure the relationship middleware callback.
*/
export interface RelationOptions {
/**
* This option can be used to disable updating a created model's `idField`, once it has been created.
*
* Setting this option to `false` will allow you to overwrite the value given by the middleware during creation.
*
* **Note:** This option **does not** reject the entire query, only the portion which would update the tenant which "owns" the instance of a model.
*
* **Note:** [`upsert`](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#upsert) actions are still allowed,
* to not loose the "or create" functionality of the action.
*
* @default true
*/
disableUpdates?: boolean
/**
* Your application's models which this middleware should be enabled for.
*
* These models should have corresponding fields dedicated to storing their
* relationship to an organization. See `idField` for more.
*
* These models should have an n-1 relationship with the configured relation-model.
*
* @default []
*/
models: ModelName[]
/**
* A field on each of your middleware-enabled models used to identify the instance of a model it relates to.
*/
idField: string
/**
* An (optionally) asynchronous function, used as the `idField` getter.
*
* This function should return a string, which is used as the value for `idField`.
*/
idGetter: () => string | Promise<string>
/**
* The model (created by you) which will form a relationship with all enabled models.
*
* The relation-model should have a 1-n relationship with enabled-models.
*
* Note: If it does not already exist, you will need to create the default model and draw relations
* between all of your existing models.
*/
relationModel: ModelName
}
// ===
/**
* Default `PrismaActions` which are supported by the multitenancy middleware.
*
* If a key is *not* present, that is inferred as it not being supported.
* The multitenancy middleware does **not** take effect on actions which it does not support.
*
* **Note:** This object maybe useful if you plan to add additional actions, and just want to spread the defaults.
*/
export const RelationActionMap: RelationActionMap = {
aggregate: 'where',
count: 'where',
create: 'data',
createMany: 'data',
deleteMany: 'where',
findFirst: 'where',
findMany: 'where',
upsert: 'data',
}
/**
* `PrismaActions` considered "updative" - they cause a transformation to existing records.
*/
const RelationUpdateActions: PrismaAction[] = ['update', 'updateMany']
// ===
/**
* Configurable [Prisma Middleware](https://www.prisma.io/docs/concepts/components/prisma-client/middleware)
* for automating the process of defining 1-n relationships between a model and many enabled-models.
*/
export const relationMiddleware = <T>({
disableUpdates = true,
models = [],
idField = undefined,
idGetter = undefined,
relationModel = undefined,
}: RelationOptions) => {
const relationField =
relationModel.charAt(0).toLowerCase() + relationModel.slice(1)
return async (params: MiddlewareParams, next: MiddlewareNext) => {
if (
params.model !== relationModel &&
models.includes(params.model) &&
Object.keys(RelationActionMap).includes(params.action)
) {
const arg = RelationActionMap[params.action]
if (typeof params.args === 'undefined') {
params.args = {}
}
if (typeof params.args[arg] === 'undefined') {
params.args[arg] = {}
}
const oldId = params.args[arg][idField]
const oldRelation = params.args[arg][relationField]
if (typeof oldId === 'undefined' && typeof oldRelation === 'undefined') {
params.args[arg][idField] = await idGetter()
}
}
// Flow is nested in different levels to:
// A) Emphasize the derterministic behavior of this logic (if: X==true then: Y==true then: Z..).
// B) Allow heavy opinionations on the method used to determine if a field is present, and should therefore be removed.
if (disableUpdates) {
if (
RelationUpdateActions.includes(params.action) &&
typeof params.args.data === 'object'
) {
const oldId = params.args.data[idField]
const oldRelation = params.args.data[relationField]
if (
typeof oldId !== 'undefined' ||
typeof oldRelation !== 'undefined'
) {
delete params.args.data[idField]
delete params.args.data[relationField]
}
}
}
return next<T>(params)
}
}