Architecture

Tenant isolation

A tenant is a logical customer or organization sharing an application. Every data operation must apply a tenant scope derived from trusted authenticated context, not an arbitrary request field. A pooled table can use a composite tenant and resource key to avoid confusing equal IDs. Scope reads, updates and deletes; writes must set tenant from trusted context. Cache keys and jobs need the same boundary. PostgreSQL row-level security can provide another layer, but owners, privileged roles and policy configuration require explicit testing. SQLite below exercises repository logic, not PostgreSQL RLS. A global resource ID or an AsyncLocalStorage context alone is not authorization. Missing context should deny access. Sources: https://www.postgresql.org/docs/18/ddl-rowsecurity.html.

Formal Syntax & Structure

nodejs syntaxnodejs
Read only the selected tenant row (conceptual contract; see example)

Working Example

Example Snippetnodejs
import {DatabaseSync} from 'node:sqlite';import assert from 'node:assert/strict';const db=new DatabaseSync(':memory:');try{db.exec("CREATE TABLE books(tenant TEXT,id TEXT,title TEXT,PRIMARY KEY(tenant,id)); INSERT INTO books VALUES('a','1','River'),('b','1','Forest')");const get=db.prepare('SELECT title FROM books WHERE tenant=? AND id=?');assert.equal(get.get('a','1').title,'River');assert.equal(get.get('c','1'),undefined);console.log(get.get('b','1').title);}finally{db.close();}
Node.js Reference