@verikit/prisma 0.24.1 → 0.24.2
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
|
@@ -22,4 +22,47 @@ createPrismaAdapter(resource, {
|
|
|
22
22
|
(tx) => operation(tx.post),
|
|
23
23
|
{ isolationLevel: "RepeatableRead" },
|
|
24
24
|
),
|
|
25
|
-
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
## Conditional writes for protected resources
|
|
28
|
+
|
|
29
|
+
Update and delete on resources with permissions require an adapter implementing
|
|
30
|
+
`findForMutation`. The server authorizes its detached record snapshot and passes
|
|
31
|
+
its storage-authored `revision` to `update`/`delete` as `expectedRevision`. The
|
|
32
|
+
adapter must compare that revision **in the same database statement as the write**,
|
|
33
|
+
combine it with the ID and scope, and throw `ConflictError` (HTTP 409) when no row
|
|
34
|
+
matches. Missing/deleted rows and stale revisions produce the same conflict. There
|
|
35
|
+
is no automatic retry; retrying must repeat authorization and validation.
|
|
36
|
+
|
|
37
|
+
The in-memory adapter supports this automatically. Database adapters require a
|
|
38
|
+
migration adding a dedicated non-null integer revision column with default `0`,
|
|
39
|
+
excluded from the resource field map:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// Drizzle: revision: integer("revision").notNull().default(0)
|
|
43
|
+
const adapter = createDrizzleAdapter(db, resource, {
|
|
44
|
+
versionColumn: table.revision,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Prisma model: revision Int @default(0)
|
|
48
|
+
const adapter = createPrismaAdapter(resource, {
|
|
49
|
+
// ...existing model, fields, id, and listTransaction options
|
|
50
|
+
versionField: "revision",
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Configured adapters increment revisions on every update, including empty updates
|
|
55
|
+
and updates without an expected revision. Revisions never appear in API responses.
|
|
56
|
+
**All other writers must increment the same revision atomically**, including jobs,
|
|
57
|
+
custom actions, raw SQL, and other adapter instances. Do not reset revisions or
|
|
58
|
+
reuse a deleted record's ID. Provision the column before enabling the option; this
|
|
59
|
+
library does not run database migrations. Adapters without conditional-write
|
|
60
|
+
support return HTTP 501 for protected update/delete; explicitly `"open"` resources
|
|
61
|
+
retain unconditional writes.
|
|
62
|
+
|
|
63
|
+
Custom adapter wrappers must forward `expectedRevision` (fourth argument to
|
|
64
|
+
`update`, third to `delete`). `findForMutation` must capture record and revision
|
|
65
|
+
atomically and return a detached record. Revisions must be safe nonnegative
|
|
66
|
+
integers with room to increment. This protects rules based on the target record;
|
|
67
|
+
rules depending on other records or external services need application-level
|
|
68
|
+
transactional coordination as well.
|
|
@@ -51,6 +51,8 @@ export interface PrismaIdConfig<TId = unknown> {
|
|
|
51
51
|
*/
|
|
52
52
|
export type PrismaSearchProvider = "postgresql" | "sqlite" | "mysql";
|
|
53
53
|
export interface PrismaAdapterOptions<TFields extends FieldMap> {
|
|
54
|
+
/** Dedicated non-null Int revision scalar, excluded from resource fields. All external writers must increment it. */
|
|
55
|
+
versionField?: string;
|
|
54
56
|
/** The generated Prisma model delegate this adapter reads/writes, e.g. `prisma.post`. */
|
|
55
57
|
model: PrismaModelDelegate;
|
|
56
58
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-prisma-adapter.d.ts","sourceRoot":"","sources":["../src/create-prisma-adapter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-prisma-adapter.d.ts","sourceRoot":"","sources":["../src/create-prisma-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,EAEL,KAAK,eAAe,EAErB,MAAM,iBAAiB,CAAC;AAYzB;;;GAGG;AAEH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IACrC,KAAK,CAAC,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AAG,YAAY,GAAG,QAAQ,GAAG,OAAO,CAAC;AAErE,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,QAAQ;IAC5D,qHAAqH;IACrH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yFAAyF;IACzF,KAAK,EAAE,mBAAmB,CAAC;IAC3B;;;;OAIG;IACH,MAAM,EAAE;SAAG,CAAC,IAAI,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM;KAAE,CAAC;IAClD,8EAA8E;IAC9E,EAAE,EAAE,cAAc,CAAC;IACnB,kCAAkC;IAClC,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAChC;;;;;;OAMG;IACH,2BAA2B,CAAC,EAAE,MAAM,CAAC;IACrC;;;;;;OAMG;IACH,eAAe,EAAE,qBAAqB,CAAC;CACxC;AAED,6DAA6D;AAC7D,MAAM,WAAW,oBAAqB,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACnE,mEAAmE;IACnE,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,SAAS,QAAQ,EACxB,cAAc,SAAS,eAAe,EACtC,MAAM,GAAG,OAAO,EAEhB,QAAQ,EAAE,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,cAAc,CAAC,EAC3D,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,GACrC,eAAe,CAAC,oBAAoB,CAAC,CAyavC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ConflictError } from "@verikit/core";
|
|
1
2
|
import { UniqueConstraintError, } from "@verikit/server";
|
|
2
3
|
import { buildSelect, isRecordNotFoundError, isUniqueConstraintError, mapFilterToPrisma, mapValuesToData, presentRow, uniqueConstraintFields, } from "./mapping.js";
|
|
3
4
|
/**
|
|
@@ -11,6 +12,23 @@ export function createPrismaAdapter(resource, options) {
|
|
|
11
12
|
const { model, id } = options;
|
|
12
13
|
const fields = options.fields;
|
|
13
14
|
const schema = resource.toSchema();
|
|
15
|
+
const versionField = options.versionField;
|
|
16
|
+
if (versionField !== undefined &&
|
|
17
|
+
(!versionField ||
|
|
18
|
+
versionField === id.field ||
|
|
19
|
+
Object.values(fields).includes(versionField))) {
|
|
20
|
+
throw new Error("versionField must be a dedicated revision scalar, not an ID or resource field.");
|
|
21
|
+
}
|
|
22
|
+
function revisionWhere(expectedRevision) {
|
|
23
|
+
if (!versionField ||
|
|
24
|
+
!Number.isSafeInteger(expectedRevision) ||
|
|
25
|
+
expectedRevision < 0 ||
|
|
26
|
+
expectedRevision >= Number.MAX_SAFE_INTEGER) {
|
|
27
|
+
throw new Error("Conditional writes require a configured versionField and a valid revision.");
|
|
28
|
+
}
|
|
29
|
+
return { [versionField]: expectedRevision };
|
|
30
|
+
}
|
|
31
|
+
const conflict = () => new ConflictError("Record changed since authorization. Retry the request.");
|
|
14
32
|
const unmapped = Object.keys(schema.fields).filter((name) => !Object.hasOwn(fields, name));
|
|
15
33
|
if (unmapped.length > 0) {
|
|
16
34
|
throw new Error(`@verikit/prisma: resource "${resource.name}" has no Prisma field mapping for: ${unmapped.join(", ")}. Every declared field needs an entry in \`fields\`.`);
|
|
@@ -101,6 +119,22 @@ export function createPrismaAdapter(resource, options) {
|
|
|
101
119
|
return present(row);
|
|
102
120
|
}
|
|
103
121
|
return {
|
|
122
|
+
...(versionField && {
|
|
123
|
+
async findForMutation(pathId, scope) {
|
|
124
|
+
const value = fromPath(pathId);
|
|
125
|
+
if (value === undefined)
|
|
126
|
+
return undefined;
|
|
127
|
+
// Prisma 6/7 extended unique filters retain the unique ID at the top level.
|
|
128
|
+
const row = await model.findUnique({
|
|
129
|
+
where: { [id.field]: value, AND: scopeWhere(scope) ?? {} },
|
|
130
|
+
select: { ...select, [versionField]: true },
|
|
131
|
+
});
|
|
132
|
+
if (!row)
|
|
133
|
+
return undefined;
|
|
134
|
+
revisionWhere(row[versionField]);
|
|
135
|
+
return { record: present(row), revision: row[versionField] };
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
104
138
|
async list(params) {
|
|
105
139
|
// Search with no searchable fields can never match; return zero results.
|
|
106
140
|
const permittedSearchScalars = params.searchFields
|
|
@@ -173,12 +207,37 @@ export function createPrismaAdapter(resource, options) {
|
|
|
173
207
|
throw error;
|
|
174
208
|
}
|
|
175
209
|
},
|
|
176
|
-
async update(pathId, values, scope) {
|
|
210
|
+
async update(pathId, values, scope, expectedRevision) {
|
|
177
211
|
const value = fromPath(pathId);
|
|
178
212
|
if (value === undefined) {
|
|
213
|
+
if (expectedRevision !== undefined)
|
|
214
|
+
throw conflict();
|
|
179
215
|
return undefined;
|
|
180
216
|
}
|
|
181
217
|
const data = mapValuesToData(values, fields);
|
|
218
|
+
if (versionField)
|
|
219
|
+
data[versionField] = { increment: 1 };
|
|
220
|
+
if (expectedRevision !== undefined) {
|
|
221
|
+
const revision = revisionWhere(expectedRevision);
|
|
222
|
+
try {
|
|
223
|
+
const row = await model.update({
|
|
224
|
+
where: {
|
|
225
|
+
[id.field]: value,
|
|
226
|
+
AND: [scopeWhere(scope) ?? {}, revision],
|
|
227
|
+
},
|
|
228
|
+
data,
|
|
229
|
+
select,
|
|
230
|
+
});
|
|
231
|
+
return present(row);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
if (isRecordNotFoundError(error))
|
|
235
|
+
throw conflict();
|
|
236
|
+
if (isUniqueConstraintError(error))
|
|
237
|
+
throw new UniqueConstraintError(uniqueConstraintFields(error, fields));
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
182
241
|
// Prisma's update() only accepts a unique selector, so it cannot express
|
|
183
242
|
// `{ id, organizationId }`. updateMany/updateManyAndReturn make the tenant
|
|
184
243
|
// predicate atomic.
|
|
@@ -226,11 +285,30 @@ export function createPrismaAdapter(resource, options) {
|
|
|
226
285
|
throw error;
|
|
227
286
|
}
|
|
228
287
|
},
|
|
229
|
-
async delete(pathId, scope) {
|
|
288
|
+
async delete(pathId, scope, expectedRevision) {
|
|
230
289
|
const value = fromPath(pathId);
|
|
231
290
|
if (value === undefined) {
|
|
291
|
+
if (expectedRevision !== undefined)
|
|
292
|
+
throw conflict();
|
|
232
293
|
return;
|
|
233
294
|
}
|
|
295
|
+
if (expectedRevision !== undefined) {
|
|
296
|
+
const revision = revisionWhere(expectedRevision);
|
|
297
|
+
try {
|
|
298
|
+
await model.delete({
|
|
299
|
+
where: {
|
|
300
|
+
[id.field]: value,
|
|
301
|
+
AND: [scopeWhere(scope) ?? {}, revision],
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
if (isRecordNotFoundError(error))
|
|
308
|
+
throw conflict();
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
234
312
|
if (scope) {
|
|
235
313
|
if (!model.deleteMany) {
|
|
236
314
|
throw new Error("@verikit/prisma: scoped deletes require a Prisma delegate with deleteMany().");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verikit/prisma",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.2",
|
|
4
4
|
"description": "Prisma ORM storage adapter for @verikit/server resources.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@verikit/core": "0.24.
|
|
40
|
-
"@verikit/server": "0.24.
|
|
39
|
+
"@verikit/core": "0.24.2",
|
|
40
|
+
"@verikit/server": "0.24.2"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@prisma/adapter-better-sqlite3": "^7.9.1",
|