@toride/prisma 0.2.0 → 0.4.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/dist/index.d.ts +11 -3
- package/dist/index.js +26 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TorideSchema, DefaultSchema, VirtualFieldsConfig, ConstraintAdapter, ResourceRef } from 'toride';
|
|
2
2
|
|
|
3
3
|
declare const VERSION = "0.0.1";
|
|
4
4
|
|
|
5
5
|
/** Prisma WHERE clause type (plain object). */
|
|
6
6
|
type PrismaWhere = Record<string, unknown>;
|
|
7
7
|
/** Options for createPrismaAdapter. */
|
|
8
|
-
interface PrismaAdapterOptions {
|
|
8
|
+
interface PrismaAdapterOptions<S extends TorideSchema = DefaultSchema, TModelMap = never> {
|
|
9
9
|
/** Maps constraint relation fields to Prisma relation names. */
|
|
10
10
|
relationMapping?: Record<string, string>;
|
|
11
11
|
/** Prisma table name for role assignments. Default: "roleAssignments". */
|
|
@@ -15,6 +15,8 @@ interface PrismaAdapterOptions {
|
|
|
15
15
|
userId?: string;
|
|
16
16
|
role?: string;
|
|
17
17
|
};
|
|
18
|
+
/** Maps virtual fields to their relation queries for field_includes constraints. */
|
|
19
|
+
virtualFields?: VirtualFieldsConfig<S, TModelMap>;
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
20
22
|
* Create a Prisma constraint adapter that translates constraint AST
|
|
@@ -22,8 +24,14 @@ interface PrismaAdapterOptions {
|
|
|
22
24
|
*
|
|
23
25
|
* No Prisma dependency required - produces plain JS objects matching
|
|
24
26
|
* Prisma's WHERE clause structure.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam S - The Toride schema type. Defaults to `DefaultSchema` for backward compatibility.
|
|
29
|
+
* @typeParam TModelMap - Maps resource type names to their model field shapes. Used for model-aware
|
|
30
|
+
* virtual field detection. Defaults to `never` for backward compatibility.
|
|
31
|
+
* @typeParam TQueryMap - Maps resource type names to their Prisma WHERE clause types.
|
|
32
|
+
* Defaults to `Record<string, PrismaWhere>` for backward compatibility.
|
|
25
33
|
*/
|
|
26
|
-
declare function createPrismaAdapter(options?: PrismaAdapterOptions): ConstraintAdapter<
|
|
34
|
+
declare function createPrismaAdapter<S extends TorideSchema = DefaultSchema, TModelMap = never, TQueryMap extends Record<string, PrismaWhere> = Record<string, PrismaWhere>>(options?: PrismaAdapterOptions<S, TModelMap>): ConstraintAdapter<TQueryMap>;
|
|
27
35
|
/** Options for createPrismaResolver. */
|
|
28
36
|
interface PrismaResolverOptions {
|
|
29
37
|
/** Fields to select. Defaults to all scalar fields. */
|
package/dist/index.js
CHANGED
|
@@ -5,8 +5,34 @@ function createPrismaAdapter(options) {
|
|
|
5
5
|
const roleTable = options?.roleAssignmentTable ?? "roleAssignments";
|
|
6
6
|
const userIdField = options?.roleAssignmentFields?.userId ?? "userId";
|
|
7
7
|
const roleField = options?.roleAssignmentFields?.role ?? "role";
|
|
8
|
+
const flatVirtualFields = {};
|
|
9
|
+
if (options?.virtualFields) {
|
|
10
|
+
for (const [_resource, resourceFields] of Object.entries(options.virtualFields)) {
|
|
11
|
+
if (resourceFields) {
|
|
12
|
+
for (const field of Object.keys(resourceFields)) {
|
|
13
|
+
if (flatVirtualFields[field]) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`Virtual field "${field}" is defined in multiple resources. The adapter cannot disambiguate at translation time. Use unique field names per resource.`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
flatVirtualFields[field] = resourceFields[field];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
8
23
|
return {
|
|
9
24
|
translate(constraint) {
|
|
25
|
+
const vf = flatVirtualFields[constraint.field];
|
|
26
|
+
if (vf && constraint.type === "field_includes") {
|
|
27
|
+
return {
|
|
28
|
+
[vf.relation]: {
|
|
29
|
+
some: {
|
|
30
|
+
[vf.matchField]: constraint.value,
|
|
31
|
+
...vf.filter
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
10
36
|
switch (constraint.type) {
|
|
11
37
|
case "field_eq":
|
|
12
38
|
return { [constraint.field]: constraint.value };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toride/prisma",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Prisma integration for Toride authorization engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
]
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"toride": "0.
|
|
23
|
+
"toride": "0.4.0"
|
|
24
24
|
},
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public"
|