feat(backend-postgres): isolate rows per owner with row-level security #44

Merged
crapougnax merged 5 commits from feat/backend-postgres-row-isolation into develop 2026-11-14 17:12:58 +00:00
Member

Summary

One database can hold the data of several owners that never see each other's rows, including when a query forgets its filter.

  • config.sessionSettings: function returning settings applied to each statement for one transaction only, so pooled connections never keep them.
  • rowIsolationSql(): DDL enabling and forcing row-level security on a table, comparing its owner column with a session setting.
  • assertRowSecurityRole(): start-up check that the role does not bypass policies.

Without an owner, protected tables look empty and refuse writes. The adapter behaves as before when sessionSettings is absent.

A restricted role cannot create tables: automatic table creation does not apply to it, tables come from migrations (README and new HOWTO.md).

Depends on #33

This branch is built on feat/backend-unique-properties. Until #33 is merged, the diff also shows its commits; merge #33 first.

Tests

RowIsolation.test.ts needs a PostgreSQL database and is skipped without one.

To decide

  • RowIsolationOptions has no Interface suffix.
  • The integration test lives in src/__test__, like the existing ones.

Closes #42

🤖 Generated with Claude Code

## Summary One database can hold the data of several owners that never see each other's rows, including when a query forgets its filter. - `config.sessionSettings`: function returning settings applied to each statement for one transaction only, so pooled connections never keep them. - `rowIsolationSql()`: DDL enabling and forcing row-level security on a table, comparing its owner column with a session setting. - `assertRowSecurityRole()`: start-up check that the role does not bypass policies. Without an owner, protected tables look empty and refuse writes. The adapter behaves as before when `sessionSettings` is absent. A restricted role cannot create tables: automatic table creation does not apply to it, tables come from migrations (README and new `HOWTO.md`). ## Depends on #33 This branch is built on `feat/backend-unique-properties`. Until #33 is merged, the diff also shows its commits; merge #33 first. ## Tests `RowIsolation.test.ts` needs a PostgreSQL database and is skipped without one. ## To decide - `RowIsolationOptions` has no `Interface` suffix. - The integration test lives in `src/__test__`, like the existing ones. Closes #42 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A property can now be declared `unique: true`. Backends able to do so
refuse a second record of the collection holding the same value, at
creation and at update, including under concurrent writes.

- core: `unique` option and getter on properties;
- types: ConflictError, for writes colliding with the current state;
- backend: UniqueConstraintError (collection, property) and
  uniquePropertyNames(); the mock adapter enforces the rule;
- backend-postgres, backend-sqlite: UNIQUE column in created tables and
  generated migrations, constraint violations mapped to
  UniqueConstraintError.

Missing values may repeat. Soft-deleted records keep their value: it is
not reusable. Tables that already exist are not altered: add the
constraint with a migration. Adapters that do not implement the option
(Firestore, REST) ignore it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
fix(backend-sqlite): report a record identifier already taken as a unique violation
Some checks failed
Quatrain Core Sovereign CI / Monorepo Build, Tests & Quality Audit (push) Has started running
Quatrain Core Sovereign CI / Publish Changed Packages to npm (push) Has been cancelled
15e1604b67
Creating a record with an explicit identifier that already exists threw
a generic BackendError on SQLite, and a UniqueConstraintError on
PostgreSQL. Both adapters now throw UniqueConstraintError, without a
property name since the collision is on the identifier.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
feat(backend-postgres): isolate rows per owner with row-level security
Some checks failed
Quatrain Core Sovereign CI / Monorepo Build, Tests & Quality Audit (push) Has started running
Quatrain Core Sovereign CI / Publish Changed Packages to npm (push) Has been cancelled
28ba299323
One database can now hold the data of several owners (tenants) that
never see each other's rows, including when a query forgets its filter.

- config.sessionSettings: function returning settings applied to each
  statement for one transaction only, so pooled connections never keep
  them;
- rowIsolationSql(): DDL enabling and forcing row-level security on a
  table, comparing its owner column with a session setting;
- assertRowSecurityRole(): start-up check that the role does not bypass
  policies.

Without an owner, protected tables look empty and refuse writes. The
adapter behaves as before when sessionSettings is absent.

A restricted role cannot create tables: automatic table creation does
not apply to it, tables come from migrations. Documented in the README
and in a new HOWTO.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
test(backend-postgres): create the complete users table whichever test runs first
Some checks failed
Quatrain Core Sovereign CI / Monorepo Build, Tests & Quality Audit (push) Successful in 5m56s
Quatrain Core Sovereign CI / Publish Changed Packages to npm (push) Has been skipped
Quatrain Core Sovereign CI / Monorepo Build, Tests & Quality Audit (pull_request) Has been cancelled
Quatrain Core Sovereign CI / Publish Changed Packages to npm (pull_request) Has been cancelled
214f52a14e
On an empty database, a test whose model only refers to users made the
adapter create a placeholder users table with a single column; the tests
running afterwards then failed on missing columns. The shared test setup
now registers the User class, so the table is created complete.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
test(backend-postgres): register the User class in the row isolation test
Some checks failed
Quatrain Core Sovereign CI / Monorepo Build, Tests & Quality Audit (push) Successful in 6m25s
Quatrain Core Sovereign CI / Publish Changed Packages to npm (push) Has been skipped
Quatrain Core Sovereign CI / Monorepo Build, Tests & Quality Audit (pull_request) Failing after 18m2s
Quatrain Core Sovereign CI / Publish Changed Packages to npm (pull_request) Has been cancelled
d058ad3c8e
Same cause as the shared setup: run first on an empty database, the test
made the adapter create a placeholder users table that broke the tests
running afterwards.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Merge branch 'develop' into feat/backend-postgres-row-isolation
All checks were successful
Quatrain Core CI / Monorepo Build, Tests & Quality Audit (push) Successful in 4m25s
Quatrain Core CI / Monorepo Build, Tests & Quality Audit (pull_request) Successful in 4m32s
Quatrain Core CI / Publish Changed Packages to npm (pull_request) Has been skipped
Quatrain Core CI / Publish Changed Packages to npm (push) Has been skipped
160cf0e752
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Merge branch 'develop' into feat/backend-postgres-row-isolation
Some checks failed
Quatrain Core CI / Monorepo Build, Tests & Quality Audit (push) Successful in 8m14s
Quatrain Core CI / Publish Changed Packages to npm (push) Has been skipped
Quatrain Core CI / Monorepo Build, Tests & Quality Audit (pull_request) Failing after 16m46s
Quatrain Core CI / Publish Changed Packages to npm (pull_request) Has been cancelled
bb35d0bdf2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
crapougnax merged commit c9aa496b39 into develop 2026-11-14 17:12:58 +00:00
crapougnax deleted branch feat/backend-postgres-row-isolation 2026-11-14 17:12:58 +00:00
Sign in to join this conversation.
No description provided.