@coffre/db 0.1.14 → 0.1.16

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 (47) hide show
  1. package/dist/connect.d.ts +1 -1
  2. package/dist/{database-CNMfHZwF.d.ts → database-Cq_52uUo.d.ts} +1 -1
  3. package/dist/dialect.d.ts +1 -1
  4. package/dist/hyperdrive.d.ts +1 -1
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.js +8 -4
  7. package/dist/log.d.ts +1 -1
  8. package/dist/migrations/postgres/0002_service_bindings.sql +28 -0
  9. package/dist/migrations/postgres/0003_exchanges.sql +10 -0
  10. package/dist/migrations/postgres/0004_live_indexes.sql +3 -0
  11. package/dist/migrations/postgres/meta/0002_snapshot.json +1870 -0
  12. package/dist/migrations/postgres/meta/0003_snapshot.json +1917 -0
  13. package/dist/migrations/postgres/meta/0004_snapshot.json +2006 -0
  14. package/dist/migrations/postgres/meta/_journal.json +21 -0
  15. package/dist/migrations/sqlite/0002_service_bindings.sql +23 -0
  16. package/dist/migrations/sqlite/0003_exchanges.sql +7 -0
  17. package/dist/migrations/sqlite/0004_live_indexes.sql +3 -0
  18. package/dist/migrations/sqlite/meta/0002_snapshot.json +1736 -0
  19. package/dist/migrations/sqlite/meta/0003_snapshot.json +1781 -0
  20. package/dist/migrations/sqlite/meta/0004_snapshot.json +1811 -0
  21. package/dist/migrations/sqlite/meta/_journal.json +21 -0
  22. package/dist/portable.d.ts +2 -2
  23. package/dist/{schema-AmvM6w0D.d.ts → schema-Wt-p8AOh.d.ts} +305 -2
  24. package/dist/schema-sqlite.d.ts +305 -1
  25. package/dist/schema-sqlite.js +38 -3
  26. package/dist/schema-version.d.ts +1 -1
  27. package/dist/schema-version.js +74 -26
  28. package/dist/schema.d.ts +2 -2
  29. package/dist/schema.js +53 -3
  30. package/package.json +2 -2
  31. package/src/database.ts +15 -5
  32. package/src/migrations/postgres/0002_service_bindings.sql +28 -0
  33. package/src/migrations/postgres/0003_exchanges.sql +10 -0
  34. package/src/migrations/postgres/0004_live_indexes.sql +3 -0
  35. package/src/migrations/postgres/meta/0002_snapshot.json +1870 -0
  36. package/src/migrations/postgres/meta/0003_snapshot.json +1917 -0
  37. package/src/migrations/postgres/meta/0004_snapshot.json +2006 -0
  38. package/src/migrations/postgres/meta/_journal.json +21 -0
  39. package/src/migrations/sqlite/0002_service_bindings.sql +23 -0
  40. package/src/migrations/sqlite/0003_exchanges.sql +7 -0
  41. package/src/migrations/sqlite/0004_live_indexes.sql +3 -0
  42. package/src/migrations/sqlite/meta/0002_snapshot.json +1736 -0
  43. package/src/migrations/sqlite/meta/0003_snapshot.json +1781 -0
  44. package/src/migrations/sqlite/meta/0004_snapshot.json +1811 -0
  45. package/src/migrations/sqlite/meta/_journal.json +21 -0
  46. package/src/schema.sqlite.ts +61 -0
  47. package/src/schema.ts +74 -0
package/dist/connect.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Database } from "./database-CNMfHZwF.js";
1
+ import { t as Database } from "./database-Cq_52uUo.js";
2
2
  import { Engine } from "./dialect.js";
3
3
  import { ClientConfig } from "pg";
4
4
  //#region src/postgres.d.ts
@@ -1,4 +1,4 @@
1
- import { l as schema_d_exports } from "./schema-AmvM6w0D.js";
1
+ import { u as schema_d_exports } from "./schema-Wt-p8AOh.js";
2
2
  import { Table } from "drizzle-orm";
3
3
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
4
4
  import pg from "pg";
package/dist/dialect.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { i as Queryable } from "./database-CNMfHZwF.js";
1
+ import { i as Queryable } from "./database-Cq_52uUo.js";
2
2
  import { SQL } from "drizzle-orm";
3
3
  import { PgTable } from "drizzle-orm/pg-core";
4
4
  //#region src/dialect.d.ts
@@ -1,4 +1,4 @@
1
- import { n as PoolLike } from "./database-CNMfHZwF.js";
1
+ import { n as PoolLike } from "./database-Cq_52uUo.js";
2
2
  import pg from "pg";
3
3
  //#region src/hyperdrive.d.ts
4
4
  /** Postgres through a Hyperdrive binding: `postgres(env.HYPERDRIVE)`. */
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { l as schema_d_exports } from "./schema-AmvM6w0D.js";
2
- import { a as Transaction, c as tablesOf, i as Queryable, n as PoolLike, o as createDatabase, r as QueryOutsideTransaction, s as own, t as Database } from "./database-CNMfHZwF.js";
1
+ import { u as schema_d_exports } from "./schema-Wt-p8AOh.js";
2
+ import { a as Transaction, c as tablesOf, i as Queryable, n as PoolLike, o as createDatabase, r as QueryOutsideTransaction, s as own, t as Database } from "./database-Cq_52uUo.js";
3
3
  export { Database, PoolLike, QueryOutsideTransaction, Queryable, Transaction, createDatabase, own, schema_d_exports as schema, tablesOf };
package/dist/index.js CHANGED
@@ -22,7 +22,7 @@ const inside = new AsyncLocalStorage();
22
22
  /**
23
23
  * `pool`, refusing a query or a transaction asked from inside a transaction
24
24
  * it is already running. Work from any other flow, a concurrent request's,
25
- * or what the transaction left running after it ended, goes on as before.
25
+ * or what the transaction left running once its work was done, goes on as before.
26
26
  */
27
27
  function guarded(pool) {
28
28
  return new Proxy(pool, { get(target, key) {
@@ -49,11 +49,15 @@ function createDatabase(pool) {
49
49
  open: true
50
50
  };
51
51
  try {
52
- return await run((tx) => inside.run(open, () => work(tx)), config);
52
+ return await run((tx) => inside.run(open, async () => {
53
+ try {
54
+ return await work(tx);
55
+ } finally {
56
+ open.open = false;
57
+ }
58
+ }), config);
53
59
  } catch (error) {
54
60
  throw brokenRule(error) ?? error;
55
- } finally {
56
- open.open = false;
57
61
  }
58
62
  });
59
63
  return trackCommits(db);
package/dist/log.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as Transaction } from "./database-CNMfHZwF.js";
1
+ import { a as Transaction } from "./database-Cq_52uUo.js";
2
2
  import { LogKey } from "@coffre/core/audit";
3
3
  //#region src/log.d.ts
4
4
  /**
@@ -0,0 +1,28 @@
1
+ CREATE TABLE "service_bindings" (
2
+ "id" uuid PRIMARY KEY NOT NULL,
3
+ "auth_mac" "bytea" NOT NULL,
4
+ "principal" text NOT NULL,
5
+ "generation" integer NOT NULL,
6
+ "profile" text NOT NULL,
7
+ "issuer" text NOT NULL,
8
+ "jwks_uri" text NOT NULL,
9
+ "claims" text NOT NULL,
10
+ "label" text,
11
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
12
+ "created_by" text NOT NULL,
13
+ "last_used_at" timestamp with time zone,
14
+ "revoked_at" timestamp with time zone,
15
+ "revoked_by" text,
16
+ CONSTRAINT "service_bindings_principal_check" CHECK ("service_bindings"."principal" LIKE 'token:%'),
17
+ CONSTRAINT "service_bindings_auth_mac_check" CHECK (octet_length("service_bindings"."auth_mac") = 32),
18
+ CONSTRAINT "service_bindings_claims_check" CHECK ("service_bindings"."claims"::jsonb IS NOT NULL)
19
+ );
20
+ --> statement-breakpoint
21
+ ALTER TABLE "service_bindings" ADD CONSTRAINT "service_bindings_principal_fkey" FOREIGN KEY ("principal") REFERENCES "public"."vault_members"("principal") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
22
+ CREATE INDEX "service_bindings_principal_idx" ON "service_bindings" USING btree ("principal","issuer");--> statement-breakpoint
23
+ CREATE INDEX "audit_log_unbind_idx" ON "audit_log" USING btree ((("metadata")::jsonb ->> 'bindingId')) WHERE "audit_log"."author" = 'app' AND "audit_log"."action" = 'token.unbind' AND "audit_log"."decision" = 'allow';--> statement-breakpoint
24
+ -- The app reads and adds bindings, and changes only what the MAC allows to
25
+ -- change in place: its label, its last use, and its revocation (with the
26
+ -- MAC that covers it). A binding's policy is never updated, nor deleted.
27
+ GRANT SELECT, INSERT ON service_bindings TO coffre_app;--> statement-breakpoint
28
+ GRANT UPDATE (label, last_used_at, revoked_at, revoked_by, auth_mac) ON service_bindings TO coffre_app;
@@ -0,0 +1,10 @@
1
+ CREATE TABLE "consumed_tokens" (
2
+ "hash" "bytea" PRIMARY KEY NOT NULL,
3
+ "consumed_at" timestamp with time zone DEFAULT now() NOT NULL,
4
+ CONSTRAINT "consumed_tokens_hash_check" CHECK (octet_length("consumed_tokens"."hash") = 32)
5
+ );
6
+ --> statement-breakpoint
7
+ CREATE INDEX "audit_log_exchange_idx" ON "audit_log" USING btree ((("metadata")::jsonb ->> 'credentialId')) WHERE "audit_log"."author" = 'app' AND "audit_log"."action" = 'token.exchange' AND "audit_log"."decision" = 'allow';--> statement-breakpoint
8
+ -- The app records each token it exchanges, and reads whether one was; it
9
+ -- never changes or deletes a record, so a token stays spent.
10
+ GRANT SELECT, INSERT ON consumed_tokens TO coffre_app;
@@ -0,0 +1,3 @@
1
+ CREATE INDEX "credentials_live_idx" ON "credentials" USING btree ("principal","expires_at") WHERE "credentials"."revoked_at" IS NULL;--> statement-breakpoint
2
+ CREATE INDEX "credentials_issued_by_idx" ON "credentials" USING btree ("principal","created_by","created_at");--> statement-breakpoint
3
+ CREATE INDEX "service_bindings_live_idx" ON "service_bindings" USING btree ("principal","issuer","generation","created_at","id") WHERE "service_bindings"."revoked_at" IS NULL;