@toride/drizzle 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,59 @@
1
+ import { ConstraintAdapter, ResourceRef } from 'toride';
2
+
3
+ declare const VERSION = "0.0.1";
4
+
5
+ /**
6
+ * Drizzle query representation.
7
+ * Produces intermediate objects that describe the query operation.
8
+ * Users can pass these to their own Drizzle query builder or use
9
+ * the provided `toDrizzle()` helper when drizzle-orm is available.
10
+ */
11
+ type DrizzleQuery = Record<string, unknown>;
12
+ /** Table reference type (generic for any Drizzle table). */
13
+ type AnyTable = Record<string, any>;
14
+ /** Relation configuration for the adapter. */
15
+ interface RelationConfig {
16
+ table: AnyTable;
17
+ foreignKey: string;
18
+ }
19
+ /** Role assignment configuration. */
20
+ interface RoleAssignmentConfig {
21
+ table: AnyTable;
22
+ userIdColumn: string;
23
+ roleColumn: string;
24
+ }
25
+ /** Options for createDrizzleAdapter. */
26
+ interface DrizzleAdapterOptions {
27
+ /** Maps relation fields to Drizzle table references and foreign keys. */
28
+ relations?: Record<string, RelationConfig>;
29
+ /** Role assignment table configuration. */
30
+ roleAssignments?: RoleAssignmentConfig;
31
+ }
32
+ /**
33
+ * Create a Drizzle constraint adapter for a specific table.
34
+ *
35
+ * Produces intermediate query description objects that capture the
36
+ * operation type, field, and value. These can be used with drizzle-orm
37
+ * operators or processed by custom query builders.
38
+ */
39
+ declare function createDrizzleAdapter(table: AnyTable, options?: DrizzleAdapterOptions): ConstraintAdapter<DrizzleQuery>;
40
+ /** Options for createDrizzleResolver. */
41
+ interface DrizzleResolverOptions {
42
+ /** Column used as the resource ID. Defaults to "id". */
43
+ idColumn?: string;
44
+ }
45
+ /**
46
+ * Creates a resolver function for a Drizzle table.
47
+ * Wraps a Drizzle select query into the ResourceResolver signature.
48
+ *
49
+ * The db and table parameters are duck-typed — no direct drizzle-orm
50
+ * dependency is required. The db object must support `db.select().from(table).where(condition)`.
51
+ *
52
+ * @param db - A Drizzle database instance (duck-typed).
53
+ * @param table - A Drizzle table reference (duck-typed).
54
+ * @param options - Optional configuration.
55
+ * @returns A ResourceResolver function.
56
+ */
57
+ declare function createDrizzleResolver(db: any, table: any, options?: DrizzleResolverOptions): (ref: ResourceRef) => Promise<Record<string, unknown>>;
58
+
59
+ export { type AnyTable, type DrizzleAdapterOptions, type DrizzleQuery, type DrizzleResolverOptions, type RelationConfig, type RoleAssignmentConfig, VERSION, createDrizzleAdapter, createDrizzleResolver };
package/dist/index.js ADDED
@@ -0,0 +1,95 @@
1
+ // src/index.ts
2
+ var VERSION = "0.0.1";
3
+ function createDrizzleAdapter(table, options) {
4
+ const relations = options?.relations ?? {};
5
+ const roleAssignments = options?.roleAssignments;
6
+ return {
7
+ translate(constraint) {
8
+ switch (constraint.type) {
9
+ case "field_eq":
10
+ return { _op: "eq", field: constraint.field, value: constraint.value, table };
11
+ case "field_neq":
12
+ return { _op: "ne", field: constraint.field, value: constraint.value, table };
13
+ case "field_gt":
14
+ return { _op: "gt", field: constraint.field, value: constraint.value, table };
15
+ case "field_gte":
16
+ return { _op: "gte", field: constraint.field, value: constraint.value, table };
17
+ case "field_lt":
18
+ return { _op: "lt", field: constraint.field, value: constraint.value, table };
19
+ case "field_lte":
20
+ return { _op: "lte", field: constraint.field, value: constraint.value, table };
21
+ case "field_in":
22
+ return { _op: "inArray", field: constraint.field, values: constraint.values, table };
23
+ case "field_nin":
24
+ return { _op: "notInArray", field: constraint.field, values: constraint.values, table };
25
+ case "field_exists":
26
+ return constraint.exists ? { _op: "isNotNull", field: constraint.field, table } : { _op: "isNull", field: constraint.field, table };
27
+ case "field_includes":
28
+ return { _op: "arrayContains", field: constraint.field, value: constraint.value, table };
29
+ case "field_contains": {
30
+ const escaped = String(constraint.value).replace(/[%_\\]/g, "\\$&");
31
+ return { _op: "like", field: constraint.field, pattern: `%${escaped}%`, table };
32
+ }
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 relationConfig = relations[field];
41
+ if (relationConfig) {
42
+ return {
43
+ _op: "relation",
44
+ field,
45
+ resourceType,
46
+ child: childQuery,
47
+ relatedTable: relationConfig.table,
48
+ foreignKey: relationConfig.foreignKey
49
+ };
50
+ }
51
+ return { _op: "relation", field, resourceType, child: childQuery };
52
+ },
53
+ hasRole(actorId, actorType, role) {
54
+ if (roleAssignments) {
55
+ return {
56
+ _op: "hasRole",
57
+ actorId,
58
+ actorType,
59
+ role,
60
+ roleTable: roleAssignments.table,
61
+ userIdColumn: roleAssignments.userIdColumn,
62
+ roleColumn: roleAssignments.roleColumn
63
+ };
64
+ }
65
+ return { _op: "hasRole", actorId, actorType, role };
66
+ },
67
+ unknown(_name) {
68
+ return { _op: "literal", value: true };
69
+ },
70
+ and(queries) {
71
+ return { _op: "and", children: queries };
72
+ },
73
+ or(queries) {
74
+ return { _op: "or", children: queries };
75
+ },
76
+ not(query) {
77
+ return { _op: "not", child: query };
78
+ }
79
+ };
80
+ }
81
+ function createDrizzleResolver(db, table, options) {
82
+ const idColumn = options?.idColumn ?? "id";
83
+ return async (ref) => {
84
+ const rows = await db.select().from(table).where({ [idColumn]: ref.id });
85
+ if (!rows || rows.length === 0) {
86
+ return {};
87
+ }
88
+ return rows[0];
89
+ };
90
+ }
91
+ export {
92
+ VERSION,
93
+ createDrizzleAdapter,
94
+ createDrizzleResolver
95
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@toride/drizzle",
3
+ "version": "0.1.0",
4
+ "description": "Drizzle ORM 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
+ "peerDependencies": {
26
+ "drizzle-orm": ">=0.29.0"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "drizzle-orm": {
30
+ "optional": true
31
+ }
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "keywords": [
37
+ "authorization",
38
+ "drizzle",
39
+ "toride"
40
+ ],
41
+ "license": "MIT",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/toride-auth/toride"
45
+ },
46
+ "scripts": {
47
+ "build": "tsup",
48
+ "test": "vitest run",
49
+ "lint": "tsc --noEmit"
50
+ }
51
+ }