How vineettt/access-matrix works
Purpose and Public Surface
Access Matrix is a TypeScript Node.js library for persisted role- and permission-based access control. Its public package entry point is src/index.ts; core exports and construction are organized in src/core/index.ts and src/core/factory.ts. Consumers asynchronously create an AccessControl instance against a configured SQLite or PostgreSQL store, then manage authorization data and optionally synchronize discovered application routes.
Core Access-Control Facade
The core layer owns the library-facing API, generic route typing, and authorization orchestration. src/core/factory.ts selects and initializes a database adapter from explicit options or configuration; src/core/index.ts exposes the main access-control implementation; src/core/routes.ts defines route-domain behavior. This boundary isolates callers from SQL dialects and database connection details.
Adapter Persistence Boundary
A common persistence contract in src/adapters/base/adapter.ts defines the operations required by access-control logic. Database-specific implementations live in src/adapters/sqlite/sqlite-adapter.ts and src/adapters/postgres/postgres-adapter.ts. SQLite is the built-in local/file-oriented backend; PostgreSQL is the server database backend, including configurations intended for hosted PostgreSQL such as Neon. Core code should depend on the base adapter, not concrete SQL engines.
Configuration Resolution
Configuration parsing and normalization is centralized in src/utils/config.ts. It supports programmatic creation inputs and JSON configuration paths, resolving database selection and route-loading settings before factory/loader execution. Repository samples demonstrate SQLite, PostgreSQL, Neon, and automatic database/route configurations under config/database.auto.sqlite.json, config/database.postgres.json, and config/database.neon.sample.json.
Route Discovery Pipeline
Automatic route ingestion is separate from the authorization engine. src/utils/route-autoloader.ts scans configured source directories and patterns, recognizing Express, Fastify, NestJS, and Koa route declarations. It produces normalized route data consumed by the core routes layer. Source discovery is a filesystem/code-parsing boundary: applications supply directories, glob patterns, recursion, and exclusions rather than framework runtime objects.
Route Synchronization
src/utils/route-sync.ts compares discovered route data with persisted route records and applies additions/removals through the access-control instance and its selected adapter. The flow is: load config → create access-control/adaptor → discover source routes → normalize routes → synchronize database state. This enables authorization policies to target a durable inventory of application endpoints without making this package an HTTP server or middleware framework.
Runtime and Integration Model
The package runs in Node.js and is authored in TypeScript (src/index.ts, tsconfig.json). Its material external integrations are SQLite and PostgreSQL databases plus source trees using supported Node web frameworks. Usage examples exercise the main modes: direct use in examples/basic-usage.ts, PostgreSQL in examples/postgres-usage.ts, and source route auto-loading in examples/auto-loading-demo.ts.
Main entry points: index.ts AccessControl facade · adapter.ts Adapter contract · factory.ts AccessControl factory · config.ts Configuration resolution
Components 14
Library API
- Package entry
src/index.ts
Authorization Core
- AccessControl facade · Owns authorization orchestration and the main instance API.
src/core/index.ts - AccessControl factory · Selects and initializes the configured persistence adapter.
src/core/factory.ts - Route domain · Applies route-domain behavior through the access-control layer.
src/core/routes.ts
Persistence Adapters
- Adapter contract · Shared persistence operations required by core logic.
src/adapters/base/adapter.ts - SQLite adapter · Built-in local or file-oriented backend.
src/adapters/sqlite/sqlite-adapter.ts - PostgreSQL adapter · Server and hosted PostgreSQL backend, including Neon.
src/adapters/postgres/postgres-adapter.ts
Route Discovery & Sync
- Configuration resolution · Normalizes programmatic inputs and JSON configuration.
src/utils/config.ts - Route autoloader · Scans configured source trees for supported framework route declarations.
src/utils/route-autoloader.ts - Route synchronizer · Reconciles discovered routes with durable route records.
src/utils/route-sync.ts
Other components
- Consumer application · Supplies authorization inputs and optional route source directories.
- Application source tree · Express, Fastify, NestJS, or Koa source files.
- SQLite database
- PostgreSQL database
Connections 18
- Consumer application → Package entry: imports
- Package entry → AccessControl facade: exports
- Consumer application → AccessControl factory: creates instance
- Configuration resolution → AccessControl factory: database options
- AccessControl factory → AccessControl facade: constructs
- AccessControl factory → Adapter contract: selects by contract
- AccessControl facade → Adapter contract: persists authorization data
- AccessControl facade → Route domain: manages routes
- Adapter contract → SQLite adapter: implemented by
- Adapter contract → PostgreSQL adapter: implemented by
- SQLite adapter → SQLite database: stores data
- PostgreSQL adapter → PostgreSQL database: stores data
- Consumer application → Configuration resolution: inputs or JSON path
- Configuration resolution → Route autoloader: source patterns
- Application source tree → Route autoloader: scanned
- Route autoloader → Route synchronizer: normalized routes
- Route synchronizer → AccessControl facade: applies additions and removals
- Route synchronizer → Adapter contract: reconciles persisted records