@typed-policy/drizzle 0.2.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/LICENSE +21 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +128 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Togle Labs <m.ihsan.vp@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Expr, EvalContext } from '@typed-policy/core';
|
|
2
|
+
import { AnyColumn, SQL } from 'drizzle-orm';
|
|
3
|
+
|
|
4
|
+
/** Policy action type - matches the type in policy.ts */
|
|
5
|
+
type PolicyAction<T, A> = Expr<T, A> | boolean | ((ctx: EvalContext<A>) => boolean | Expr<T, A>);
|
|
6
|
+
type TableMapping<T> = {
|
|
7
|
+
[K in keyof T]: {
|
|
8
|
+
[P in keyof T[K]]: AnyColumn;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
type CompileOptions<T, A> = {
|
|
12
|
+
actor: A;
|
|
13
|
+
tables: TableMapping<T>;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Compile a policy action to Drizzle SQL
|
|
17
|
+
*
|
|
18
|
+
* The key insight: since functions are pure, we execute them during compilation
|
|
19
|
+
* with the provided actor context. This gives us the resulting expression,
|
|
20
|
+
* which we then compile to SQL.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const listCondition = compile(postPolicy.actions.list, {
|
|
25
|
+
* actor,
|
|
26
|
+
* tables: {
|
|
27
|
+
* post: {
|
|
28
|
+
* id: posts.id,
|
|
29
|
+
* ownerId: posts.ownerId,
|
|
30
|
+
* published: posts.published
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function compile<T, A>(action: PolicyAction<T, A>, options: CompileOptions<T, A>): SQL;
|
|
37
|
+
|
|
38
|
+
type PathMapping<T> = {
|
|
39
|
+
[K in keyof T]?: AnyColumn;
|
|
40
|
+
};
|
|
41
|
+
declare function createMapping<T>(mappings: PathMapping<T>): PathMapping<T>;
|
|
42
|
+
declare function validateMapping<T>(path: string, mapping: PathMapping<T>): boolean;
|
|
43
|
+
|
|
44
|
+
export { type CompileOptions, type PathMapping, type TableMapping, compile, createMapping, validateMapping };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// src/compile.ts
|
|
2
|
+
import { and as drizzleAnd, eq as drizzleEq, or as drizzleOr, sql } from "drizzle-orm";
|
|
3
|
+
function getColumnFromPath(path, tables) {
|
|
4
|
+
const parts = path.split(".");
|
|
5
|
+
const tableKey = parts[0];
|
|
6
|
+
const columnKey = parts[1];
|
|
7
|
+
const table = tables[tableKey];
|
|
8
|
+
if (!table) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
`Cannot compile path "${path}" to SQL: "${String(tableKey)}" is not a subject table. Only subject paths (mapped in tables) are allowed in SQL compilation. Available subject tables: ${Object.keys(tables).join(", ")}`
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
const column = table[columnKey];
|
|
14
|
+
if (!column) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Cannot compile path "${path}" to SQL: "${String(columnKey)}" is not a valid column on table "${String(tableKey)}". Available columns: ${Object.keys(table).join(", ")}`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return column;
|
|
20
|
+
}
|
|
21
|
+
function isActorPath(path, tables) {
|
|
22
|
+
const parts = path.split(".");
|
|
23
|
+
const tableKey = parts[0];
|
|
24
|
+
return !(tableKey in tables);
|
|
25
|
+
}
|
|
26
|
+
function getActorValueFromPath(path, actor) {
|
|
27
|
+
const parts = path.split(".");
|
|
28
|
+
let current = actor;
|
|
29
|
+
for (let i = 0; i < parts.length; i++) {
|
|
30
|
+
if (current === null || current === void 0) {
|
|
31
|
+
return void 0;
|
|
32
|
+
}
|
|
33
|
+
current = current[parts[i]];
|
|
34
|
+
}
|
|
35
|
+
return current;
|
|
36
|
+
}
|
|
37
|
+
function resolveRightValue(right, tables, actor) {
|
|
38
|
+
if (typeof right === "string" && right.includes(".")) {
|
|
39
|
+
if (isActorPath(right, tables)) {
|
|
40
|
+
const value = getActorValueFromPath(right, actor);
|
|
41
|
+
if (value === void 0) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Actor value not found for path: ${right}. Ensure the path exists in the actor object provided to compile().`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Cannot use subject path "${right}" on the right side of eq(). SQL compilation only supports subject paths on the left side (for column references) and actor paths on the right side (for parameterized values).`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return right;
|
|
53
|
+
}
|
|
54
|
+
function compile(action, options) {
|
|
55
|
+
const { actor, tables } = options;
|
|
56
|
+
if (typeof action === "boolean") {
|
|
57
|
+
return action ? sql`1 = 1` : sql`1 = 0`;
|
|
58
|
+
}
|
|
59
|
+
if (typeof action === "function") {
|
|
60
|
+
const result = action({
|
|
61
|
+
actor
|
|
62
|
+
});
|
|
63
|
+
if (typeof result === "boolean") {
|
|
64
|
+
return result ? sql`1 = 1` : sql`1 = 0`;
|
|
65
|
+
}
|
|
66
|
+
return compileExpr(result, tables, actor);
|
|
67
|
+
}
|
|
68
|
+
return compileExpr(action, tables, actor);
|
|
69
|
+
}
|
|
70
|
+
function compileExpr(expr, tables, actor) {
|
|
71
|
+
switch (expr.kind) {
|
|
72
|
+
case "literal": {
|
|
73
|
+
return expr.value ? sql`1 = 1` : sql`1 = 0`;
|
|
74
|
+
}
|
|
75
|
+
case "eq": {
|
|
76
|
+
const column = getColumnFromPath(expr.left, tables);
|
|
77
|
+
const value = resolveRightValue(
|
|
78
|
+
expr.right,
|
|
79
|
+
tables,
|
|
80
|
+
actor
|
|
81
|
+
);
|
|
82
|
+
return drizzleEq(column, value);
|
|
83
|
+
}
|
|
84
|
+
case "and": {
|
|
85
|
+
if (expr.rules.length === 0) {
|
|
86
|
+
return sql`1 = 1`;
|
|
87
|
+
}
|
|
88
|
+
const conditions = expr.rules.map((rule) => compileExpr(rule, tables, actor));
|
|
89
|
+
const result = drizzleAnd(...conditions);
|
|
90
|
+
return result || sql`1 = 1`;
|
|
91
|
+
}
|
|
92
|
+
case "or": {
|
|
93
|
+
if (expr.rules.length === 0) {
|
|
94
|
+
return sql`1 = 0`;
|
|
95
|
+
}
|
|
96
|
+
const conditions = expr.rules.map((rule) => compileExpr(rule, tables, actor));
|
|
97
|
+
const result = drizzleOr(...conditions);
|
|
98
|
+
return result || sql`1 = 0`;
|
|
99
|
+
}
|
|
100
|
+
case "function": {
|
|
101
|
+
const result = expr.fn({
|
|
102
|
+
actor
|
|
103
|
+
});
|
|
104
|
+
if (typeof result === "boolean") {
|
|
105
|
+
return result ? sql`1 = 1` : sql`1 = 0`;
|
|
106
|
+
}
|
|
107
|
+
return compileExpr(result, tables, actor);
|
|
108
|
+
}
|
|
109
|
+
default: {
|
|
110
|
+
throw new Error(`Unknown expression kind: ${JSON.stringify(expr)}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/mapping.ts
|
|
116
|
+
function createMapping(mappings) {
|
|
117
|
+
return mappings;
|
|
118
|
+
}
|
|
119
|
+
function validateMapping(path, mapping) {
|
|
120
|
+
const parts = path.split(".");
|
|
121
|
+
const root = parts[0];
|
|
122
|
+
return root in mapping;
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
compile,
|
|
126
|
+
createMapping,
|
|
127
|
+
validateMapping
|
|
128
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typed-policy/drizzle",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Drizzle ORM SQL compiler for typed policies",
|
|
5
|
+
"author": "Ihsan VP <m.ihsan.vp@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/toglelabs/typed-policy.git",
|
|
10
|
+
"directory": "packages/drizzle"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/toglelabs/typed-policy/tree/main/packages/drizzle#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/toglelabs/typed-policy/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"typescript",
|
|
18
|
+
"policy",
|
|
19
|
+
"authorization",
|
|
20
|
+
"drizzle",
|
|
21
|
+
"orm",
|
|
22
|
+
"sql",
|
|
23
|
+
"database"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@typed-policy/core": "0.2.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"drizzle-orm": ">=0.29.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@biomejs/biome": "^1.9.4",
|
|
43
|
+
"@types/node": "^22.0.0",
|
|
44
|
+
"typescript": "^5.7.0",
|
|
45
|
+
"tsup": "^8.3.0",
|
|
46
|
+
"vitest": "^2.0.0",
|
|
47
|
+
"drizzle-orm": "^0.38.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest",
|
|
53
|
+
"clean": "rm -rf dist",
|
|
54
|
+
"lint": "biome check src",
|
|
55
|
+
"format": "biome format --write src"
|
|
56
|
+
}
|
|
57
|
+
}
|