@verikit/prisma 0.24.3 → 0.25.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.
package/README.md
CHANGED
|
@@ -4,6 +4,36 @@ Prisma ORM adapter for VeriKit server resources.
|
|
|
4
4
|
|
|
5
5
|
See the [VeriKit documentation](https://verikit.dev) for setup and usage.
|
|
6
6
|
|
|
7
|
+
## One adapter for every resource
|
|
8
|
+
|
|
9
|
+
`prismaAdapter()` builds an adapter for each resource, so `createServer` only
|
|
10
|
+
needs it once. Each resource uses the client's delegate named after it (`post`
|
|
11
|
+
→ `prisma.post`, `BlogPost` → `prisma.blogPost`), and `list()` runs in a
|
|
12
|
+
`$transaction` on the same client:
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { createServer } from "@verikit/server";
|
|
16
|
+
import { prismaAdapter } from "@verikit/prisma";
|
|
17
|
+
|
|
18
|
+
const api = createServer({
|
|
19
|
+
adapter: prismaAdapter(prisma, {
|
|
20
|
+
transactionOptions: { isolationLevel: "RepeatableRead" },
|
|
21
|
+
resources: {
|
|
22
|
+
// Only resources that don't follow the defaults need an entry.
|
|
23
|
+
article: { model: "legacyPost", fields: { title: "headline" } },
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
resources: [
|
|
27
|
+
{ resource: post, permissions: "open" },
|
|
28
|
+
{ resource: article, permissions: articlePermissions },
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`provider`, `versionField`, and `literalSearchCandidateLimit` can be set once
|
|
34
|
+
for every resource; an entry in `resources` overrides them. A resource that
|
|
35
|
+
sets its own `adapter` in `createServer` keeps it.
|
|
36
|
+
|
|
7
37
|
## Field and id mapping
|
|
8
38
|
|
|
9
39
|
Each resource field maps to the Prisma scalar with the same name, and the id to
|
|
@@ -27,7 +57,7 @@ createPrismaAdapter(resource, {
|
|
|
27
57
|
through it, in the same transaction, so a concurrent write between them can
|
|
28
58
|
never desync a page from `meta.total`. The adapter only ever sees a single
|
|
29
59
|
model delegate, not the full Prisma client, so it has no way to open a
|
|
30
|
-
transaction on its own
|
|
60
|
+
transaction on its own, so pass one built from your `PrismaClient`:
|
|
31
61
|
|
|
32
62
|
```ts
|
|
33
63
|
createPrismaAdapter(resource, {
|
|
@@ -40,7 +70,11 @@ createPrismaAdapter(resource, {
|
|
|
40
70
|
});
|
|
41
71
|
```
|
|
42
72
|
|
|
43
|
-
##
|
|
73
|
+
## Production
|
|
74
|
+
|
|
75
|
+
You don't need this to get started. Read it before you deploy.
|
|
76
|
+
|
|
77
|
+
### Conditional writes for protected resources
|
|
44
78
|
|
|
45
79
|
Update and delete on resources with permissions require an adapter implementing
|
|
46
80
|
`findForMutation`. The server authorizes its detached record snapshot and passes
|
|
@@ -100,4 +100,55 @@ export interface PrismaResourceRecord extends Record<string, unknown> {
|
|
|
100
100
|
* @throws {Error} If a `.searchable()` field is not text-like.
|
|
101
101
|
*/
|
|
102
102
|
export declare function createPrismaAdapter<TFields extends FieldMap, TRelationships extends RelationshipMap, TTable = unknown>(resource: Resource<string, TFields, TTable, TRelationships>, options: PrismaAdapterOptions<TFields>): ResourceAdapter<PrismaResourceRecord>;
|
|
103
|
+
/**
|
|
104
|
+
* Structural subset of a `PrismaClient` used by {@link prismaAdapter}: the
|
|
105
|
+
* interactive form of `$transaction`. Model delegates are read by key.
|
|
106
|
+
*/
|
|
107
|
+
export interface PrismaClientLike {
|
|
108
|
+
$transaction<T>(fn: (tx: any) => Promise<T>, options?: any): Promise<T>;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Per-resource options for {@link prismaAdapter}: everything
|
|
112
|
+
* {@link createPrismaAdapter} takes except the delegate and transaction, which
|
|
113
|
+
* the factory builds from the client.
|
|
114
|
+
*/
|
|
115
|
+
export interface PrismaResourceAdapterOptions extends Omit<PrismaAdapterOptions<FieldMap>, "model" | "listTransaction"> {
|
|
116
|
+
/**
|
|
117
|
+
* The model's delegate key on the client, such as `"legacyPost"` for
|
|
118
|
+
* `prisma.legacyPost`. Defaults to the resource name with its first letter
|
|
119
|
+
* lowercased, which is how Prisma names delegates (`Post` → `prisma.post`).
|
|
120
|
+
*/
|
|
121
|
+
model?: string;
|
|
122
|
+
}
|
|
123
|
+
/** Options for {@link prismaAdapter}. */
|
|
124
|
+
export interface PrismaAdapterFactoryOptions {
|
|
125
|
+
/**
|
|
126
|
+
* Options by resource name, for resources whose model, fields, or id don't
|
|
127
|
+
* follow the defaults. A value here overrides the shared options below.
|
|
128
|
+
*/
|
|
129
|
+
resources?: Readonly<Record<string, PrismaResourceAdapterOptions>>;
|
|
130
|
+
/** See `PrismaSearchProvider`. Applies to every resource. */
|
|
131
|
+
provider?: PrismaSearchProvider;
|
|
132
|
+
/**
|
|
133
|
+
* Revision scalar used by every resource, for conditional writes. Only set
|
|
134
|
+
* this when every model has it; otherwise set it per resource.
|
|
135
|
+
*/
|
|
136
|
+
versionField?: string;
|
|
137
|
+
/** See `PrismaAdapterOptions.literalSearchCandidateLimit`. Applies to every resource. */
|
|
138
|
+
literalSearchCandidateLimit?: number;
|
|
139
|
+
/**
|
|
140
|
+
* Passed to `$transaction` for `list()`'s records and count queries, such
|
|
141
|
+
* as `{ isolationLevel: "RepeatableRead" }` for a stable pagination
|
|
142
|
+
* snapshot.
|
|
143
|
+
*/
|
|
144
|
+
transactionOptions?: Readonly<Record<string, unknown>>;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Builds adapters for every resource, for `createServer({ adapter })`. Each
|
|
148
|
+
* resource reads and writes the client's delegate named after it (or its
|
|
149
|
+
* `resources[name].model`), and `list()` runs in a `$transaction` on the same
|
|
150
|
+
* client. @throws {Error} When a resource is built, if the client has no
|
|
151
|
+
* delegate under its model key.
|
|
152
|
+
*/
|
|
153
|
+
export declare function prismaAdapter(client: PrismaClientLike, { resources, transactionOptions, ...shared }?: PrismaAdapterFactoryOptions): (resource: Resource) => ResourceAdapter<PrismaResourceRecord>;
|
|
103
154
|
//# sourceMappingURL=create-prisma-adapter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACnC,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACpC,SAAS,CAAC,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACpC,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAChC,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAChC,UAAU,CAAC,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACnD,mBAAmB,CAAC,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IAChD,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAChC,UAAU,CAAC,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACpD;AAGD;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GA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"}
|
|
@@ -34,12 +34,18 @@ export function createPrismaAdapter(resource, options) {
|
|
|
34
34
|
return { [versionField]: expectedRevision };
|
|
35
35
|
}
|
|
36
36
|
const conflict = () => new ConflictError("Record changed since authorization. Retry the request.");
|
|
37
|
+
const isTextField = (field) => field.fieldType === "text" ||
|
|
38
|
+
field.fieldType === "textarea" ||
|
|
39
|
+
field.fieldType === "email";
|
|
40
|
+
// Any text field can be searched when the server names it in
|
|
41
|
+
// `searchFields` (such as a relationship picker's displayField).
|
|
42
|
+
const textScalars = new Map(Object.entries(schema.fields)
|
|
43
|
+
.filter(([, field]) => isTextField(field))
|
|
44
|
+
.map(([name]) => [name, fields[name]]));
|
|
37
45
|
const searchableScalars = Object.entries(schema.fields)
|
|
38
46
|
.filter(([, field]) => field.searchable)
|
|
39
47
|
.map(([name, field]) => {
|
|
40
|
-
if (field
|
|
41
|
-
field.fieldType !== "textarea" &&
|
|
42
|
-
field.fieldType !== "email") {
|
|
48
|
+
if (!isTextField(field)) {
|
|
43
49
|
throw new Error(`@verikit/prisma: resource "${resource.name}"'s searchable field "${name}" has fieldType "${field.fieldType}". Search only supports text fields.`);
|
|
44
50
|
}
|
|
45
51
|
return [name, fields[name]];
|
|
@@ -139,20 +145,33 @@ export function createPrismaAdapter(resource, options) {
|
|
|
139
145
|
async list(params) {
|
|
140
146
|
// Search with no searchable fields can never match; return zero results.
|
|
141
147
|
const permittedSearchScalars = params.searchFields
|
|
142
|
-
?
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
? params.searchFields.flatMap((name) => {
|
|
149
|
+
const scalar = textScalars.get(name);
|
|
150
|
+
return scalar ? [scalar] : [];
|
|
151
|
+
})
|
|
145
152
|
: searchableScalars.map(([, scalar]) => scalar);
|
|
146
153
|
if (params.search && permittedSearchScalars.length === 0) {
|
|
147
154
|
return { records: [], total: 0 };
|
|
148
155
|
}
|
|
156
|
+
// Ids that `id.fromPath` can't decode match nothing.
|
|
157
|
+
const ids = params.ids?.flatMap((pathId) => {
|
|
158
|
+
const value = fromPath(pathId);
|
|
159
|
+
return value === undefined ? [] : [value];
|
|
160
|
+
});
|
|
161
|
+
if (ids && ids.length === 0) {
|
|
162
|
+
return { records: [], total: 0 };
|
|
163
|
+
}
|
|
149
164
|
// Prisma `contains` can't escape LIKE metacharacters across all providers.
|
|
150
165
|
// Apply literal matching locally after a bounded candidate read so `%`, `_`,
|
|
151
166
|
// and `\` cannot widen results or load an entire scoped table into memory.
|
|
152
167
|
const literalSearch = params.search !== undefined && /[\\%_]/.test(params.search);
|
|
153
|
-
const
|
|
168
|
+
const searchConstraint = params.search && !literalSearch
|
|
154
169
|
? searchWhere(params.search, permittedSearchScalars)
|
|
155
|
-
: undefined
|
|
170
|
+
: undefined;
|
|
171
|
+
const idConstraint = ids && { [id.field]: { in: ids } };
|
|
172
|
+
const where = combinedWhere(params.scope, idConstraint && searchConstraint
|
|
173
|
+
? { AND: [idConstraint, searchConstraint] }
|
|
174
|
+
: (idConstraint ?? searchConstraint), params.filters);
|
|
156
175
|
const sort = params.sort;
|
|
157
176
|
const sortScalar = sort && fields[sort.field];
|
|
158
177
|
const orderBy = sort && sortScalar ? { [sortScalar]: sort.direction } : undefined;
|
|
@@ -330,3 +349,36 @@ export function createPrismaAdapter(resource, options) {
|
|
|
330
349
|
},
|
|
331
350
|
};
|
|
332
351
|
}
|
|
352
|
+
function lowerFirst(text) {
|
|
353
|
+
return text.charAt(0).toLowerCase() + text.slice(1);
|
|
354
|
+
}
|
|
355
|
+
function isModelDelegate(value) {
|
|
356
|
+
return (typeof value === "object" &&
|
|
357
|
+
value !== null &&
|
|
358
|
+
typeof value.findMany === "function");
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Builds adapters for every resource, for `createServer({ adapter })`. Each
|
|
362
|
+
* resource reads and writes the client's delegate named after it (or its
|
|
363
|
+
* `resources[name].model`), and `list()` runs in a `$transaction` on the same
|
|
364
|
+
* client. @throws {Error} When a resource is built, if the client has no
|
|
365
|
+
* delegate under its model key.
|
|
366
|
+
*/
|
|
367
|
+
export function prismaAdapter(client, { resources = {}, transactionOptions, ...shared } = {}) {
|
|
368
|
+
return (resource) => {
|
|
369
|
+
const { model: modelKey = lowerFirst(resource.name), ...options } = Object.hasOwn(resources, resource.name) ? resources[resource.name] : {};
|
|
370
|
+
const model = client[modelKey];
|
|
371
|
+
if (!isModelDelegate(model)) {
|
|
372
|
+
throw new Error(`@verikit/prisma: no model "${modelKey}" on the Prisma client for resource "${resource.name}". Set \`resources.${resource.name}.model\` in prismaAdapter's options to the client's delegate key.`);
|
|
373
|
+
}
|
|
374
|
+
const listTransaction = (operation) => transactionOptions
|
|
375
|
+
? client.$transaction((tx) => operation(tx[modelKey]), transactionOptions)
|
|
376
|
+
: client.$transaction((tx) => operation(tx[modelKey]));
|
|
377
|
+
return createPrismaAdapter(resource, {
|
|
378
|
+
...shared,
|
|
379
|
+
...options,
|
|
380
|
+
model,
|
|
381
|
+
listTransaction,
|
|
382
|
+
});
|
|
383
|
+
};
|
|
384
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verikit/prisma",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
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.
|
|
40
|
-
"@verikit/server": "0.
|
|
39
|
+
"@verikit/core": "0.25.0",
|
|
40
|
+
"@verikit/server": "0.25.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@prisma/adapter-better-sqlite3": "^7.9.1",
|
|
@@ -49,6 +49,11 @@
|
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@prisma/client": "^6.0.0 || ^7.0.0"
|
|
51
51
|
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"@prisma/client": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
52
57
|
"scripts": {
|
|
53
58
|
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
54
59
|
"build": "pnpm clean && tsc -b",
|