@dads-framework/postgres-access 0.1.0 → 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.
Files changed (2) hide show
  1. package/README.md +84 -21
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -54,41 +54,104 @@ in an existing transaction, use `inTransaction` or `inDrizzleTransaction`.
54
54
 
55
55
  ## Try it
56
56
 
57
+ In the generated starter, [`assembly/services.ts`](../../../apps/kit-workbench/assembly/services.ts) creates the Postgres
58
+ adapter and connects it to tenant services using the application database.
59
+ [`assembly/protected-write.ts`](../../../apps/kit-workbench/assembly/protected-write.ts) checks permission in the transaction
60
+ that commits the protected write.
61
+
62
+ Use Node.js 22 or later and an empty development Postgres database. Set
63
+ `DATABASE_URL` to a connection string for a role that can create tables and
64
+ indexes. The script installs the relationship schema and uses a fresh
65
+ namespace per run. In an application, apply the SQL through your migration
66
+ runner and use a stable namespace.
67
+
68
+ ```sh
69
+ npm install @dads-framework/access @dads-framework/postgres @dads-framework/postgres-access zod
70
+ npm install -D tsx
71
+ ```
72
+
73
+ Save as `check-postgres-access.mts` and run `npx tsx check-postgres-access.mts`.
74
+
75
+ <!-- dads:check-example -->
76
+
57
77
  ```ts
78
+ import { randomUUID } from "node:crypto";
79
+ import { access, accessModel, entityHandle } from "@dads-framework/access";
80
+ import { relationshipWrite } from "@dads-framework/access/runtime";
81
+ import { createPostgresConnection } from "@dads-framework/postgres";
58
82
  import {
59
- AlreadyAppliedAccessChanges,
60
83
  PostgresAuthorization,
61
- TransactionalAccessChanges,
62
84
  relationshipSchemaMigration,
63
85
  } from "@dads-framework/postgres-access";
64
- import {
65
- authorizationKinds,
66
- DEFAULT_AUTHORIZATION_GUARD_MARKS,
67
- GuardedAuthorization,
68
- } from "@dads-framework/authorization";
69
-
70
- // Install relationshipSchemaMigration.sql through the application's migration runner.
71
- // connection and model come from the application composition root.
72
- const backend = new PostgresAuthorization({
73
- model,
74
- namespace: "app",
75
- executor: connection.pool,
86
+ import { z } from "zod";
87
+
88
+ const connectionString = process.env.DATABASE_URL;
89
+ if (!connectionString) throw new Error("Set DATABASE_URL.");
90
+
91
+ const user = entityHandle({ wireName: "user", idSchema: z.string() });
92
+ const team = entityHandle({
93
+ wireName: "team",
94
+ idSchema: z.string(),
95
+ kind: "scope",
96
+ });
97
+ const teamAccess = access(team, {
98
+ roles: { member: user },
99
+ permissions: (roles) => ({ view: [roles.member] }),
76
100
  });
77
- const authorization = new GuardedAuthorization(
78
- backend,
79
- DEFAULT_AUTHORIZATION_GUARD_MARKS,
80
- authorizationKinds(model),
101
+ const model = accessModel([
102
+ access(user, { roles: {}, permissions: () => ({}) }),
103
+ teamAccess,
104
+ ]);
105
+
106
+ const connection = createPostgresConnection(
107
+ { connectionString, schema: {} },
108
+ console,
81
109
  );
82
- const changes = new TransactionalAccessChanges(backend, connection.drizzle);
83
- const afterCommit = new AlreadyAppliedAccessChanges();
110
+ try {
111
+ await connection.pool.query(relationshipSchemaMigration.sql);
112
+ const authorization = new PostgresAuthorization({
113
+ model,
114
+ namespace: randomUUID(),
115
+ executor: connection.pool,
116
+ });
117
+ const resource = { type: team.wireName, id: "team-1" };
118
+ const subject = { type: user.wireName, id: "alice" };
119
+ const relation = teamAccess.roles.member.roleName;
120
+ const query = {
121
+ resource,
122
+ subject,
123
+ permission: teamAccess.permissions.view.permissionName,
124
+ };
125
+
126
+ console.log(await authorization.check(query));
127
+ await authorization.writeRelationships([
128
+ relationshipWrite(resource, relation, subject),
129
+ ]);
130
+ console.log(await authorization.check(query));
131
+ await authorization.deleteRelationships({
132
+ resourceType: resource.type,
133
+ resourceId: resource.id,
134
+ relation,
135
+ subject,
136
+ });
137
+ console.log(await authorization.check(query));
138
+ } finally {
139
+ await connection.close();
140
+ }
84
141
  ```
85
142
 
86
- Bind `authorization` as the authorization port, `changes` as the enqueue port, and `afterCommit` as the after-commit dispatcher. Writers keep calling `enqueue(entries, tx)`; the tuples and the business row commit or roll back together. Use `backend.inTransaction(client)` for a read or explicit tuple operation that must see the caller's pending writes. Use `backend.inDrizzleTransaction(tx)` when the application owns a Drizzle transaction.
143
+ The script prints `false`, `true`, and `false` as the membership is granted
144
+ and revoked.
87
145
 
88
146
  ## Use it
89
147
 
90
148
  Choose Postgres for an app that already uses Postgres and can keep relationship facts beside its data. Install the relationship schema through the app's migration runner. Configure a stable namespace for this model, and select the backend at application composition. Use the same model for Postgres policy compilation and HTTP scope planning.
91
149
 
150
+ Bind `TransactionalAccessChanges` as the enqueue port and
151
+ `AlreadyAppliedAccessChanges` as the after-commit dispatcher. The
152
+ application passes its active transaction to `enqueue(entries, tx)`.
153
+ Relationship facts and the business row then commit or roll back together.
154
+
92
155
  In Postgres mode, start neither the access-change projector nor the SpiceDB migration runner. Successful writes return an empty consistency token. Data visibility follows the caller's Postgres transaction. The after-commit dispatcher is a no-op because committed facts are already visible. Keep any application locks that coordinate work beyond projecting authorization facts.
93
156
 
94
157
  Choose the backend based on the model features the application needs, its
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dads-framework/postgres-access",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22"
@@ -17,12 +17,12 @@
17
17
  "BENCHMARK.md"
18
18
  ],
19
19
  "dependencies": {
20
- "@dads-framework/access": "0.1.0",
21
- "@dads-framework/postgres": "0.1.0",
20
+ "@dads-framework/access": "0.2.0",
21
+ "@dads-framework/postgres": "0.2.0",
22
22
  "drizzle-orm": "^0.45.2"
23
23
  },
24
24
  "devDependencies": {
25
- "@dads-framework/authorization": "0.1.0",
25
+ "@dads-framework/authorization": "0.2.0",
26
26
  "vitest": "~4.1.0",
27
27
  "zod": "^4.4.3"
28
28
  },