# vineettt/access-matrix architecture

> Architecture diagram of the GitHub repository vineettt/access-matrix, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vineettt/access-matrix
- Repository: https://github.com/vineettt/access-matrix

## Overview

#### 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: [`src/core/index.ts`](https://github.com/vineettt/access-matrix/blob/main/src/core/index.ts) (AccessControl facade), [`src/adapters/base/adapter.ts`](https://github.com/vineettt/access-matrix/blob/main/src/adapters/base/adapter.ts) (Adapter contract), [`src/core/factory.ts`](https://github.com/vineettt/access-matrix/blob/main/src/core/factory.ts) (AccessControl factory), [`src/utils/config.ts`](https://github.com/vineettt/access-matrix/blob/main/src/utils/config.ts) (Configuration resolution)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Library API"]
  node_entry["Package entry<br/>TypeScript entry point<br/>[index.ts]"]
end

subgraph group_core["Authorization Core"]
  node_core_index["AccessControl facade<br/>library API<br/>[index.ts]"]
  node_factory["AccessControl factory<br/>construction<br/>[factory.ts]"]
  node_core_routes["Route domain<br/>route API<br/>[routes.ts]"]
end

subgraph group_persistence["Persistence Adapters"]
  node_adapter_contract["Adapter contract<br/>persistence interface<br/>[adapter.ts]"]
  node_sqlite_adapter[("SQLite adapter<br/>database adapter<br/>[sqlite-adapter.ts]")]
  node_postgres_adapter[("PostgreSQL adapter<br/>database adapter")]
end

subgraph group_routes["Route Discovery &amp; Sync"]
  node_config["Configuration resolution<br/>config parser<br/>[config.ts]"]
  node_route_loader["Route autoloader<br/>source scanner"]
  node_route_sync["Route synchronizer<br/>sync pipeline<br/>[route-sync.ts]"]
end

node_consumer["Consumer application<br/>Node.js application"]
node_source_tree["Application source tree<br/>route declarations"]
node_sqlite_db[("SQLite database<br/>persistent store")]
node_postgres_db[("PostgreSQL database<br/>persistent store")]

node_consumer -->|"imports"| node_entry
node_entry -->|"exports"| node_core_index
node_consumer -->|"creates instance"| node_factory
node_config -->|"database options"| node_factory
node_factory -->|"constructs"| node_core_index
node_factory -->|"selects by contract"| node_adapter_contract
node_core_index -->|"persists authorization data"| node_adapter_contract
node_core_index -->|"manages routes"| node_core_routes
node_adapter_contract -->|"implemented by"| node_sqlite_adapter
node_adapter_contract -->|"implemented by"| node_postgres_adapter
node_sqlite_adapter -->|"stores data"| node_sqlite_db
node_postgres_adapter -->|"stores data"| node_postgres_db
node_consumer -->|"inputs or JSON path"| node_config
node_config -->|"source patterns"| node_route_loader
node_source_tree -->|"scanned"| node_route_loader
node_route_loader -->|"normalized routes"| node_route_sync
node_route_sync -->|"applies additions and removals"| node_core_index
node_route_sync -->|"reconciles persisted records"| node_adapter_contract

click node_entry "https://github.com/vineettt/access-matrix/blob/main/src/index.ts"
click node_core_index "https://github.com/vineettt/access-matrix/blob/main/src/core/index.ts"
click node_factory "https://github.com/vineettt/access-matrix/blob/main/src/core/factory.ts"
click node_core_routes "https://github.com/vineettt/access-matrix/blob/main/src/core/routes.ts"
click node_config "https://github.com/vineettt/access-matrix/blob/main/src/utils/config.ts"
click node_route_loader "https://github.com/vineettt/access-matrix/blob/main/src/utils/route-autoloader.ts"
click node_route_sync "https://github.com/vineettt/access-matrix/blob/main/src/utils/route-sync.ts"
click node_adapter_contract "https://github.com/vineettt/access-matrix/blob/main/src/adapters/base/adapter.ts"
click node_sqlite_adapter "https://github.com/vineettt/access-matrix/blob/main/src/adapters/sqlite/sqlite-adapter.ts"
click node_postgres_adapter "https://github.com/vineettt/access-matrix/blob/main/src/adapters/postgres/postgres-adapter.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry toneBlue
class node_core_index,node_factory,node_core_routes toneAmber
class node_adapter_contract,node_sqlite_adapter,node_postgres_adapter toneMint
class node_config,node_route_loader,node_route_sync toneRose
class node_consumer,node_source_tree,node_sqlite_db,node_postgres_db toneNeutral
```

## Components

### Library API

- [Package entry](https://github.com/vineettt/access-matrix/blob/main/src/index.ts): TypeScript entry point · `src/index.ts`

### Authorization Core

- [AccessControl facade](https://github.com/vineettt/access-matrix/blob/main/src/core/index.ts): library API · Owns authorization orchestration and the main instance API. · `src/core/index.ts`
- [AccessControl factory](https://github.com/vineettt/access-matrix/blob/main/src/core/factory.ts): construction · Selects and initializes the configured persistence adapter. · `src/core/factory.ts`
- [Route domain](https://github.com/vineettt/access-matrix/blob/main/src/core/routes.ts): route API · Applies route-domain behavior through the access-control layer. · `src/core/routes.ts`

### Persistence Adapters

- [Adapter contract](https://github.com/vineettt/access-matrix/blob/main/src/adapters/base/adapter.ts): persistence interface · Shared persistence operations required by core logic. · `src/adapters/base/adapter.ts`
- [SQLite adapter](https://github.com/vineettt/access-matrix/blob/main/src/adapters/sqlite/sqlite-adapter.ts): database adapter · Built-in local or file-oriented backend. · `src/adapters/sqlite/sqlite-adapter.ts`
- [PostgreSQL adapter](https://github.com/vineettt/access-matrix/blob/main/src/adapters/postgres/postgres-adapter.ts): database adapter · Server and hosted PostgreSQL backend, including Neon. · `src/adapters/postgres/postgres-adapter.ts`

### Route Discovery & Sync

- [Configuration resolution](https://github.com/vineettt/access-matrix/blob/main/src/utils/config.ts): config parser · Normalizes programmatic inputs and JSON configuration. · `src/utils/config.ts`
- [Route autoloader](https://github.com/vineettt/access-matrix/blob/main/src/utils/route-autoloader.ts): source scanner · Scans configured source trees for supported framework route declarations. · `src/utils/route-autoloader.ts`
- [Route synchronizer](https://github.com/vineettt/access-matrix/blob/main/src/utils/route-sync.ts): sync pipeline · Reconciles discovered routes with durable route records. · `src/utils/route-sync.ts`

### Other components

- **Consumer application**: Node.js application · Supplies authorization inputs and optional route source directories.
- **Application source tree**: route declarations · Express, Fastify, NestJS, or Koa source files.
- **SQLite database**: persistent store
- **PostgreSQL database**: persistent store

## Connections

- 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
