npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@stratum-hq/mysql

v0.1.1

Published

MySQL tenant isolation for Stratum — shared table, table-per-tenant, and database-per-tenant strategies

Readme

@stratum-hq/mysql

MySQL tenant isolation for Stratum. Three isolation strategies, ORM integrations, and MySQL View utilities.

Isolation Strategies

| Strategy | Mechanism | Security Level | |----------|-----------|----------------| | Shared table | Application-level WHERE tenant_id = ? on all queries | App-level | | Table-per-tenant | Separate tables named {base}_{tenantSlug} | Structural | | Database-per-tenant | Separate databases per tenant, LRU-managed pool | Full |

Quick Start

npm install @stratum-hq/mysql mysql2

Shared Table (recommended for most apps)

import mysql from "mysql2/promise";
import { MysqlSharedAdapter } from "@stratum-hq/mysql";

const pool = mysql.createPool(process.env.MYSQL_URL);
const adapter = new MysqlSharedAdapter({ pool, databaseName: "myapp" });

// Structured query methods auto-inject tenant_id
const users = await adapter.scopedSelect("tenant-a", "users");
await adapter.scopedInsert("tenant-a", "users", { name: "Alice" });
await adapter.scopedUpdate("tenant-a", "users", { name: "Bob" }, { id: 1 });
await adapter.scopedDelete("tenant-a", "users", { id: 1 });

// Raw escape hatch (you own the WHERE clause)
await adapter.unscopedRawQuery("SELECT * FROM users WHERE tenant_id = ? AND active = ?", ["tenant-a", true]);

// GDPR purge
await adapter.purgeTenantData("tenant-a");

Table-per-Tenant

import { MysqlTableAdapter } from "@stratum-hq/mysql";

const adapter = new MysqlTableAdapter({ pool, databaseName: "myapp" });

// Returns escaped table name: `users_tenanta`
const tableName = adapter.scopedTable("tenanta", "users");

// Use the pool directly with the scoped table name
const [rows] = await pool.query(`SELECT * FROM ${tableName}`);

Database-per-Tenant

import { MysqlDatabaseAdapter } from "@stratum-hq/mysql";

const adapter = new MysqlDatabaseAdapter({
  createPool: (uri) => mysql.createPool(uri),
  baseUri: "mysql://root@localhost:3306/placeholder",
  maxPools: 20,
  idleTimeoutMs: 60000,
});

// Returns a pool connected to stratum_tenant_tenanta
const tenantPool = await adapter.getPool("tenanta");
const [rows] = await tenantPool.query("SELECT * FROM users");

// Clean up on shutdown
await adapter.closeAll();

ORM Integrations

TypeORM Subscriber (writes only)

import { StratumTypeOrmSubscriber } from "@stratum-hq/mysql";

// Add to your TypeORM data source subscribers
const dataSource = new DataSource({
  subscribers: [StratumTypeOrmSubscriber],
});

Limitation: TypeORM subscribers can intercept writes but not reads. Use the shared-table adapter's structured methods for tenant-scoped reads.

Knex Helper

import { withTenantScope } from "@stratum-hq/mysql";

const tenantKnex = withTenantScope(knex, "tenant-a");
const users = await tenantKnex("users").select("*");
// Automatically adds: WHERE tenant_id = 'tenant-a'

Sequelize Adapter

import { withMysqlTenantScope } from "@stratum-hq/mysql";

await withMysqlTenantScope(sequelize, "tenant-a", async (scoped) => {
  // Session variable @stratum_tenant_id is set for this scope
  // Guaranteed cleanup via try/finally, even on errors
  const users = await scoped.query("SELECT * FROM users_view");
});

MySQL Views (convenience layer)

Views provide a convenient way to create tenant-scoped read access using MySQL session variables.

import { createTenantView, setTenantSession } from "@stratum-hq/mysql";

// Create a view that filters by @stratum_tenant_id
await createTenantView(pool, "users");
// Creates: CREATE OR REPLACE VIEW users_tenant_view AS
//          SELECT * FROM users WHERE tenant_id = @stratum_tenant_id

// Set the session variable, then query the view
const conn = await pool.getConnection();
await setTenantSession(conn, "tenant-a");
const [rows] = await conn.query("SELECT * FROM users_tenant_view");
conn.release();

Important: Views are NOT a security boundary (unlike Postgres RLS). Queries to underlying tables bypass isolation entirely. Views referencing session variables may not use indexes efficiently on large tables. Use the shared-table adapter's structured methods for high-performance workloads.

GDPR Compliance

All three adapters implement purgeTenantData(tenantSlug):

  • Shared table: discovers tenant tables via INFORMATION_SCHEMA, then DELETE FROM table WHERE tenant_id = ?
  • Table-per-tenant: discovers tables via SHOW TABLES LIKE '%_slug', then DROP TABLE
  • Database-per-tenant: DROP DATABASE stratum_tenant_slug

License

MIT