@verikit/prisma 0.24.2 → 0.24.4

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,23 @@ 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
+ ## Field and id mapping
8
+
9
+ Each resource field maps to the Prisma scalar with the same name, and the id to
10
+ a string `id` scalar, so a model that matches its resource needs neither
11
+ `fields` nor `id`. List only the fields whose scalar has a different name, and
12
+ pass `id` for a different scalar or a non-string key:
13
+
14
+ ```ts
15
+ createPrismaAdapter(resource, {
16
+ model: prisma.legacyPost,
17
+ fields: { title: "headline" }, // every other field: same-named scalar
18
+ id: { field: "postId" },
19
+ listTransaction: (operation) =>
20
+ prisma.$transaction((tx) => operation(tx.legacyPost)),
21
+ });
22
+ ```
23
+
7
24
  ## Consistent pagination
8
25
 
9
26
  `listTransaction` is required: `list()`'s records and count queries always run
@@ -15,14 +32,13 @@ transaction on its own pass one built from your `PrismaClient`:
15
32
  ```ts
16
33
  createPrismaAdapter(resource, {
17
34
  model: prisma.post,
18
- fields: { title: "title" },
19
- id: { field: "id" },
20
35
  listTransaction: (operation) =>
21
36
  prisma.$transaction(
22
37
  (tx) => operation(tx.post),
23
38
  { isolationLevel: "RepeatableRead" },
24
39
  ),
25
40
  });
41
+ ```
26
42
 
27
43
  ## Conditional writes for protected resources
28
44
 
@@ -60,6 +76,14 @@ library does not run database migrations. Adapters without conditional-write
60
76
  support return HTTP 501 for protected update/delete; explicitly `"open"` resources
61
77
  retain unconditional writes.
62
78
 
79
+ When a revision column isn't possible, a protected resource can opt out with
80
+ `unsafeUnconditionalWrites: true` in its `createServer()` resource config.
81
+ Update and delete then check permissions against the record as read and write
82
+ without a revision, so a change made in between is not detected. This is only
83
+ safe when the update and delete rules don't depend on record fields that can
84
+ change, such as a rule that checks only the actor's role. Adapters that support
85
+ conditional writes keep using them.
86
+
63
87
  Custom adapter wrappers must forward `expectedRevision` (fourth argument to
64
88
  `update`, third to `delete`). `findForMutation` must capture record and revision
65
89
  atomically and return a detached record. Revisions must be safe nonnegative
@@ -56,15 +56,19 @@ export interface PrismaAdapterOptions<TFields extends FieldMap> {
56
56
  /** The generated Prisma model delegate this adapter reads/writes, e.g. `prisma.post`. */
57
57
  model: PrismaModelDelegate;
58
58
  /**
59
- * Maps every resource field to its Prisma scalar field.
60
- * All fields must be explicitly mapped; relations and Prisma `include`/`select`
61
- * values are not supported.
59
+ * Maps resource fields to Prisma scalar fields when the names differ. A field
60
+ * left out maps to the scalar with the same name, so a model whose scalars
61
+ * match the resource's field names needs no map. Relations and Prisma
62
+ * `include`/`select` values are not supported.
62
63
  */
63
- fields: {
64
- [K in keyof TFields & string]: string;
64
+ fields?: {
65
+ [K in keyof TFields & string]?: string;
65
66
  };
66
- /** Names the model's primary (or other unique) scalar and how to codec it. */
67
- id: PrismaIdConfig;
67
+ /**
68
+ * Names the model's primary (or other unique) scalar and how to codec it.
69
+ * Defaults to a string `id` scalar; numeric or composite IDs need `fromPath`.
70
+ */
71
+ id?: PrismaIdConfig;
68
72
  /** See `PrismaSearchProvider`. */
69
73
  provider?: PrismaSearchProvider;
70
74
  /**
@@ -91,10 +95,9 @@ export interface PrismaResourceRecord extends Record<string, unknown> {
91
95
  }
92
96
  /**
93
97
  * Creates a Prisma-backed `ResourceAdapter`.
94
- * Selects only configured fields plus `id`, never full model rows.
98
+ * Selects only the resource's fields plus `id`, never full model rows.
95
99
  *
96
- * @throws {Error} If a resource field is unmapped or a `.searchable()` field
97
- * is not text-like.
100
+ * @throws {Error} If a `.searchable()` field is not text-like.
98
101
  */
99
102
  export declare function createPrismaAdapter<TFields extends FieldMap, TRelationships extends RelationshipMap, TTable = unknown>(resource: Resource<string, TFields, TTable, TRelationships>, options: PrismaAdapterOptions<TFields>): ResourceAdapter<PrismaResourceRecord>;
100
103
  //# sourceMappingURL=create-prisma-adapter.d.ts.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,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,cAAc,CAAC,EAC3D,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,GACrC,eAAe,CAAC,oBAAoB,CAAC,CAuavC"}
@@ -3,15 +3,20 @@ import { UniqueConstraintError, } from "@verikit/server";
3
3
  import { buildSelect, isRecordNotFoundError, isUniqueConstraintError, mapFilterToPrisma, mapValuesToData, presentRow, uniqueConstraintFields, } from "./mapping.js";
4
4
  /**
5
5
  * Creates a Prisma-backed `ResourceAdapter`.
6
- * Selects only configured fields plus `id`, never full model rows.
6
+ * Selects only the resource's fields plus `id`, never full model rows.
7
7
  *
8
- * @throws {Error} If a resource field is unmapped or a `.searchable()` field
9
- * is not text-like.
8
+ * @throws {Error} If a `.searchable()` field is not text-like.
10
9
  */
11
10
  export function createPrismaAdapter(resource, options) {
12
- const { model, id } = options;
13
- const fields = options.fields;
11
+ const { model } = options;
12
+ const id = options.id ?? { field: "id" };
14
13
  const schema = resource.toSchema();
14
+ // Only declared fields are mapped, so a stray `fields` key can never widen
15
+ // the select.
16
+ const fields = Object.fromEntries(Object.keys(schema.fields).map((name) => [
17
+ name,
18
+ options.fields?.[name] ?? name,
19
+ ]));
15
20
  const versionField = options.versionField;
16
21
  if (versionField !== undefined &&
17
22
  (!versionField ||
@@ -29,10 +34,6 @@ export function createPrismaAdapter(resource, options) {
29
34
  return { [versionField]: expectedRevision };
30
35
  }
31
36
  const conflict = () => new ConflictError("Record changed since authorization. Retry the request.");
32
- const unmapped = Object.keys(schema.fields).filter((name) => !Object.hasOwn(fields, name));
33
- if (unmapped.length > 0) {
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\`.`);
35
- }
36
37
  const searchableScalars = Object.entries(schema.fields)
37
38
  .filter(([, field]) => field.searchable)
38
39
  .map(([name, field]) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verikit/prisma",
3
- "version": "0.24.2",
3
+ "version": "0.24.4",
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.2",
40
- "@verikit/server": "0.24.2"
39
+ "@verikit/core": "0.24.4",
40
+ "@verikit/server": "0.24.4"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@prisma/adapter-better-sqlite3": "^7.9.1",