@zanzojs/drizzle 0.1.0-beta.0 → 0.1.0-beta.2

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.
Files changed (2) hide show
  1. package/README.md +105 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # @zanzojs/drizzle
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@zanzojs/drizzle.svg?style=flat-square)](https://www.npmjs.com/package/@zanzojs/drizzle)
4
+ [![Drizzle ORM](https://img.shields.io/badge/Drizzle-ORM-green.svg?style=flat-square)](https://orm.drizzle.team/)
5
+
6
+ The official Drizzle ORM adapter for ZanzoJS.
7
+
8
+ Translating complex relationship hierarchies into SQL `JOIN`s is historically messy and slow. This adapter implements the "Zanzibar Tuple Pattern", which translates your Zanzo authorization rules into safe, parameterized `EXISTS` subqueries targeting a single, universal table.
9
+
10
+ ## Installation
11
+
12
+ This package requires `@zanzojs/core` and `drizzle-orm` as peer dependencies.
13
+
14
+ ```bash
15
+ pnpm add @zanzojs/core @zanzojs/drizzle drizzle-orm
16
+ ```
17
+
18
+ ## Setup Guide
19
+
20
+ ### 1. The Universal Tuple Table
21
+
22
+ Instead of spreading permission foreign keys across all your tables, you create a single table to hold all application relationships. This table structure is mandatory for the adapter to work.
23
+
24
+ ```typescript
25
+ import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
26
+
27
+ export const zanzoTuples = sqliteTable('zanzo_tuples', {
28
+ object: text('object').notNull(), // e.g., "Project:123"
29
+ relation: text('relation').notNull(), // e.g., "owner"
30
+ subject: text('subject').notNull(), // e.g., "User:999"
31
+ });
32
+ ```
33
+
34
+ ### 2. Creating the Adapter
35
+
36
+ You initialize the adapter by feeding it your core `ZanzoEngine` instance and your Drizzle tuple table reference.
37
+
38
+ ```typescript
39
+ import { createZanzoAdapter } from '@zanzojs/drizzle';
40
+ import { engine } from './zanzo.config';
41
+ import { zanzoTuples } from './schema';
42
+
43
+ export const withPermissions = createZanzoAdapter(engine, zanzoTuples);
44
+ ```
45
+
46
+ ### 3. Query Pushdown (Read Operations)
47
+
48
+ Now, whenever you want to fetch a list of entities (like `Projects`) but only return the ones the user is allowed to read, you use the adapter to generate the `WHERE` clause dynamically.
49
+
50
+ ```typescript
51
+ import { db, projects } from './db';
52
+
53
+ async function getReadableProjects(userId: string) {
54
+ // Generate the AST SQL fragment
55
+ const accessFilter = withPermissions(
56
+ `User:${userId}`,
57
+ 'read',
58
+ 'Project',
59
+ projects.id // The column to match against the tuple's object ID
60
+ );
61
+
62
+ // Apply it to your standard Drizzle query
63
+ return await db.select().from(projects).where(accessFilter);
64
+ }
65
+ ```
66
+
67
+ ### Write Operations (Important!)
68
+
69
+ The SQL adapter prioritizes extreme read performance. As a trade-off, it relies on strict string matching for nested definitions (e.g. `workspace.org.admin`).
70
+
71
+ To make this work, **you must use `@zanzojs/core`'s `expandTuples()` function when writing to the database.** If you skip `expandTuples()` during mutations, deep permission paths will not resolve correctly during Drizzle queries.
72
+
73
+ ```typescript
74
+ import { expandTuples } from '@zanzojs/core';
75
+
76
+ async function grantAccess(userId: string, projectId: string) {
77
+ const baseTuple = {
78
+ subject: `User:${userId}`,
79
+ relation: 'owner',
80
+ object: `Project:${projectId}`,
81
+ };
82
+
83
+ const derived = await expandTuples({
84
+ schema: engine.getSchema(),
85
+ newTuple: baseTuple,
86
+ fetchChildren: async (parentObject, relation) => {
87
+ // Return child object IDs linked to parentObject via relation
88
+ const rows = await db.select({ subject: zanzoTuples.subject })
89
+ .from(zanzoTuples)
90
+ .where(and(
91
+ eq(zanzoTuples.subject, parentObject),
92
+ eq(zanzoTuples.relation, relation),
93
+ ));
94
+ return rows.map(r => r.object);
95
+ },
96
+ });
97
+
98
+ // Insert base tuple + all derived tuples atomically
99
+ await db.insert(zanzoTuples).values([baseTuple, ...derived]);
100
+ }
101
+ ```
102
+
103
+ ## Documentation
104
+
105
+ For full architecture details, refer to the [ZanzoJS Monorepo](https://github.com/GonzaloJeria/zanzo).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zanzojs/drizzle",
3
- "version": "0.1.0-beta.0",
3
+ "version": "0.1.0-beta.2",
4
4
  "description": "Drizzle ORM adapter for Zanzo ReBAC. Zero-config Zanzibar Tuple Pattern with parameterized SQL.",
5
5
  "keywords": [
6
6
  "@zanzojs/core",
@@ -46,7 +46,7 @@
46
46
  "typescript": "^5.7.2",
47
47
  "tsup": "latest",
48
48
  "vitest": "latest",
49
- "@zanzojs/core": "0.1.0-beta.0"
49
+ "@zanzojs/core": "0.1.0-beta.2"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup",