Stratum + Knex
SQL query builder for Node.js
Knex is a SQL query builder, not a full ORM, giving you direct control over your queries while handling connection pooling, migrations, and schema building. Stratum pairs naturally with Knex because both operate close to PostgreSQL. Knex builds your queries. Stratum manages tenant hierarchy, config, isolation, and compliance.
Step-by-step setup: PostgreSQL adapters in the Stratum docs.
Feature comparison
CapabilityKnexStratum
Schema managementKnex migrations (imperative)Stratum manages tenant tables only
RLS isolationManual via knex.raw()Automatic policy generation
Schema-per-tenantknex.withSchema() per queryAutomatic search_path switching
Raw SQL accessknex.raw(): first-classUses pg Pool directly
Config inheritanceNot availableBuilt-in, root-to-leaf resolution
Transaction supportknex.transaction()Tenant-scoped transactions
GDPR export/purgeBuild it yourselfBuilt-in per-tenant operations
Connection poolingTarn.js (built-in)pg Pool
Getting started
knex-with-stratum.ts
import Knex from "knex"; import { Pool } from "pg"; import { Stratum } from "@stratum-hq/lib"; // Knex for query building const knex = Knex({ client: "pg", connection: process.env.DATABASE_URL, }); // Stratum for tenancy const pool = new Pool(); const stratum = new Stratum({ pool }); await stratum.initialize(); // Use Knex for app queries const rows = await knex("users") .withSchema("tenant_acme") .select("*");
Things to know
- Knex and Stratum use separate connection pools. Budget your PostgreSQL max_connections accordingly.
- Knex's withSchema() works well with Stratum's schema-per-tenant mode.
- Run Knex migrations and Stratum initialization in sequence during deployment.
Start building with Knex + Stratum
npm install @stratum-hq/lib pg