@toride/prisma 0.1.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.
@@ -0,0 +1,46 @@
1
+ import { ConstraintAdapter, ResourceRef } from 'toride';
2
+
3
+ declare const VERSION = "0.0.1";
4
+
5
+ /** Prisma WHERE clause type (plain object). */
6
+ type PrismaWhere = Record<string, unknown>;
7
+ /** Options for createPrismaAdapter. */
8
+ interface PrismaAdapterOptions {
9
+ /** Maps constraint relation fields to Prisma relation names. */
10
+ relationMapping?: Record<string, string>;
11
+ /** Prisma table name for role assignments. Default: "roleAssignments". */
12
+ roleAssignmentTable?: string;
13
+ /** Field names in the role assignment table. */
14
+ roleAssignmentFields?: {
15
+ userId?: string;
16
+ role?: string;
17
+ };
18
+ }
19
+ /**
20
+ * Create a Prisma constraint adapter that translates constraint AST
21
+ * nodes into Prisma WHERE clause objects.
22
+ *
23
+ * No Prisma dependency required - produces plain JS objects matching
24
+ * Prisma's WHERE clause structure.
25
+ */
26
+ declare function createPrismaAdapter(options?: PrismaAdapterOptions): ConstraintAdapter<PrismaWhere>;
27
+ /** Options for createPrismaResolver. */
28
+ interface PrismaResolverOptions {
29
+ /** Fields to select. Defaults to all scalar fields. */
30
+ select?: Record<string, boolean>;
31
+ }
32
+ /**
33
+ * Creates a resolver function for a Prisma model.
34
+ * Wraps a Prisma findUnique query into the ResourceResolver signature.
35
+ *
36
+ * The client parameter is duck-typed — no direct @prisma/client dependency
37
+ * is required. The client must support `client[modelName].findUnique({ where: { id } })`.
38
+ *
39
+ * @param client - A Prisma client instance (duck-typed).
40
+ * @param modelName - The Prisma model name (lowercase, e.g. "document").
41
+ * @param options - Optional configuration.
42
+ * @returns A ResourceResolver function.
43
+ */
44
+ declare function createPrismaResolver(client: any, modelName: string, options?: PrismaResolverOptions): (ref: ResourceRef) => Promise<Record<string, unknown>>;
45
+
46
+ export { type PrismaAdapterOptions, type PrismaResolverOptions, type PrismaWhere, VERSION, createPrismaAdapter, createPrismaResolver };
package/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ // src/index.ts
2
+ var VERSION = "0.0.1";
3
+ function createPrismaAdapter(options) {
4
+ const relationMapping = options?.relationMapping ?? {};
5
+ const roleTable = options?.roleAssignmentTable ?? "roleAssignments";
6
+ const userIdField = options?.roleAssignmentFields?.userId ?? "userId";
7
+ const roleField = options?.roleAssignmentFields?.role ?? "role";
8
+ return {
9
+ translate(constraint) {
10
+ switch (constraint.type) {
11
+ case "field_eq":
12
+ return { [constraint.field]: constraint.value };
13
+ case "field_neq":
14
+ return { [constraint.field]: { not: constraint.value } };
15
+ case "field_gt":
16
+ return { [constraint.field]: { gt: constraint.value } };
17
+ case "field_gte":
18
+ return { [constraint.field]: { gte: constraint.value } };
19
+ case "field_lt":
20
+ return { [constraint.field]: { lt: constraint.value } };
21
+ case "field_lte":
22
+ return { [constraint.field]: { lte: constraint.value } };
23
+ case "field_in":
24
+ return { [constraint.field]: { in: constraint.values } };
25
+ case "field_nin":
26
+ return { [constraint.field]: { notIn: constraint.values } };
27
+ case "field_exists":
28
+ return constraint.exists ? { [constraint.field]: { not: null } } : { [constraint.field]: null };
29
+ case "field_includes":
30
+ return { [constraint.field]: { has: constraint.value } };
31
+ case "field_contains":
32
+ return { [constraint.field]: { contains: constraint.value } };
33
+ default: {
34
+ const _exhaustive = constraint;
35
+ throw new Error(`Unknown constraint type: ${constraint.type}`);
36
+ }
37
+ }
38
+ },
39
+ relation(field, _resourceType, childQuery) {
40
+ const prismaRelation = relationMapping[field] ?? field;
41
+ return { [prismaRelation]: childQuery };
42
+ },
43
+ hasRole(actorId, _actorType, role) {
44
+ return {
45
+ [roleTable]: {
46
+ some: {
47
+ [userIdField]: actorId,
48
+ [roleField]: role
49
+ }
50
+ }
51
+ };
52
+ },
53
+ unknown(_name) {
54
+ return {};
55
+ },
56
+ and(queries) {
57
+ return { AND: queries };
58
+ },
59
+ or(queries) {
60
+ return { OR: queries };
61
+ },
62
+ not(query) {
63
+ return { NOT: query };
64
+ }
65
+ };
66
+ }
67
+ function createPrismaResolver(client, modelName, options) {
68
+ return async (ref) => {
69
+ const model = client[modelName];
70
+ const query = { where: { id: ref.id } };
71
+ if (options?.select) {
72
+ query.select = options.select;
73
+ }
74
+ const result = await model.findUnique(query);
75
+ return result ?? {};
76
+ };
77
+ }
78
+ export {
79
+ VERSION,
80
+ createPrismaAdapter,
81
+ createPrismaResolver
82
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@toride/prisma",
3
+ "version": "0.1.0",
4
+ "description": "Prisma integration for Toride authorization engine",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "nx": {
18
+ "tags": [
19
+ "type:integration"
20
+ ]
21
+ },
22
+ "dependencies": {
23
+ "toride": "0.1.0"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "keywords": [
29
+ "authorization",
30
+ "prisma",
31
+ "toride"
32
+ ],
33
+ "license": "MIT",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/toride-auth/toride"
37
+ },
38
+ "scripts": {
39
+ "build": "tsup",
40
+ "test": "vitest run",
41
+ "lint": "tsc --noEmit"
42
+ }
43
+ }