# sbusanelli/claude-code-hooks-demo architecture

> Architecture diagram of the GitHub repository sbusanelli/claude-code-hooks-demo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sbusanelli/claude-code-hooks-demo
- Repository: https://github.com/sbusanelli/claude-code-hooks-demo

## Overview

1. **Repository purpose**
   This is a local Node.js/TypeScript demo focused on two things: a small e-commerce database layer built on `better-sqlite3`, and Claude Code hook automation around file/tool usage. There is no visible web server or distributed backend; the main runtime boundary is an in-process SQLite database accessed from TypeScript.

2. **Top-level runtime boundary**
   The application runs under Node.js with TypeScript sources in `src/`. `better-sqlite3` is the core persistence engine, so database access is synchronous and happens directly in-process rather than through an ORM or remote DB service.

3. **Application entry points**
   `src/main.ts` is the primary executable entry point, and `src/example.ts` appears to provide a runnable demo path. At the root, `sdk.ts` / `sdk.js` likely expose a packaged surface for consumption or build output, but the architecture is centered on the TypeScript app in `src/`.

4. **Domain/data model layer**
   `src/schema.ts` defines the database schema, and `src/types.ts` defines the TypeScript shapes used across the app. The schema represents an e-commerce domain with customers, products, orders, reviews, promotions, inventory, shipping, and analytics-related data.

5. **Query subsystem**
   `src/queries/` is the main data-access layer. It is split by business area:
   customer, product, order, analytics, inventory, promotion, review, and shipping queries. These modules encapsulate SQL statements and use the synchronous `better-sqlite3` API (`prepare`, `get`, `all`, `run`) with parameterized inputs.

6. **Data flow through the app**
   The typical flow is: entry point initializes/uses the schema, then calls a domain query module, which executes SQL against the SQLite database, and returns typed results to the caller. This keeps SQL isolated from the rest of the app and makes each business domain independently testable.

7. **Claude Code hook subsystem**
   The `hooks/` directory contains the automation/security layer: `read_hook.js`, `write_hook.js`, `query_hook.js`, and `tsc.js`. These hooks sit at the boundary of Claude Code operations and can block sensitive reads/writes, deduplicate queries, and trigger TypeScript compilation on file changes.

8. **Claude configuration boundary**
   `.claude/` plus `scripts/init-claude.js` are the setup/configuration layer for hook activation. The repo is designed so the Claude Code runtime uses local settings to enforce its pre-tool-use workflow rules.

9. **Security model**
   The main security boundary is file access and secret handling. The read/write hooks are intended to block `.env`, key material, certificates, and secret-like patterns, while the SQL layer uses parameterized queries to reduce injection risk.

10. **Testing subsystem**
   `jest.config.cjs` and `tests/` define a separate verification boundary around the same database layer. Tests cover connection management, schema validation, CRUD behavior, transactions, foreign keys, and performance, with `tests/setup.ts` providing shared test initialization.

11. **Build and developer workflow**
   `package.json`, `tsconfig.json`, and the npm scripts drive setup, development, build, type-checking, and test execution. The project is optimized for local iteration: compile TypeScript, run the demo, execute tests, and validate hook behavior.

12. **Architectural summary for a diagram**
   Draw this as three main zones:  
   - **Claude Code hook layer** around file/tool operations  
   - **TypeScript app layer** with entry points, schema/types, and per-domain SQL modules  
   - **Embedded SQLite database** as the persistence core  
   Then show tests as a parallel verification path against the same schema/query layer, not as a separate service.

Main entry points: [`src/main.ts`](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/main.ts) (Main), [`src/schema.ts`](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/schema.ts) (Schema), [`.claude/settings.local.json`](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/.claude/settings.local.json) (Claude settings), [`src/queries/order_queries.ts`](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/order_queries.ts) (Order queries)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_hooks["Claude Code hooks"]
  node_scripts_init_claude["Claude setup<br/>bootstrap script<br/>[init-claude.js]"]
  node_claude_settings["Claude settings<br/>local config"]
  node_read_hook["Read hook<br/>file guard<br/>[read_hook.js]"]
  node_write_hook["Write hook<br/>file guard<br/>[write_hook.js]"]
  node_query_hook["Query hook<br/>tool guard<br/>[query_hook.js]"]
  node_tsc_hook["TSC hook<br/>compile trigger<br/>[tsc.js]"]
end

subgraph group_app["TypeScript app"]
  node_main["Main<br/>entrypoint<br/>[main.ts]"]
  node_example["Example<br/>demo entry<br/>[example.ts]"]
  node_sdk["SDK surface<br/>package api<br/>[sdk.ts]"]
  node_schema["Schema<br/>[schema.ts]"]
  node_types["Types<br/>ts models<br/>[types.ts]"]
  node_customer_queries["Customer queries<br/>SQL module"]
  node_product_queries["Product queries<br/>SQL module<br/>[product_queries.ts]"]
  node_order_queries["Order queries<br/>SQL module<br/>[order_queries.ts]"]
  node_inventory_queries["Inventory queries<br/>SQL module"]
  node_analytics_queries["Analytics queries<br/>SQL module"]
  node_promo_queries["Promo queries<br/>SQL module"]
  node_review_queries["Review queries<br/>SQL module<br/>[review_queries.ts]"]
  node_shipping_queries["Shipping queries<br/>SQL module"]
end

subgraph group_persistence["SQLite core"]
  node_sqlite_db[("SQLite DB<br/>better-sqlite3")]
end

subgraph group_verification["Tests"]
  node_test_setup["Test setup<br/>test bootstrap<br/>[setup.ts]"]
  node_connection_test["Connection test<br/>integration test"]
  node_schema_test["Schema test<br/>integration test"]
  node_integration_test["Integration test"]
  node_crud_test["CRUD test<br/>unit test<br/>[database.test.ts]"]
end

node_scripts_init_claude -->|"installs"| node_claude_settings
node_claude_settings -->|"enables"| node_read_hook
node_claude_settings -->|"enables"| node_write_hook
node_claude_settings -->|"enables"| node_query_hook
node_claude_settings -->|"enables"| node_tsc_hook
node_main -->|"initializes"| node_schema
node_main -->|"uses"| node_types
node_main -->|"calls"| node_customer_queries
node_main -->|"calls"| node_order_queries
node_example -->|"initializes"| node_schema
node_example -->|"calls"| node_product_queries
node_sdk -->|"wraps"| node_main
node_schema -->|"defines"| node_sqlite_db
node_customer_queries -->|"executes"| node_sqlite_db
node_product_queries -->|"executes"| node_sqlite_db
node_order_queries -->|"executes"| node_sqlite_db
node_inventory_queries -->|"executes"| node_sqlite_db
node_analytics_queries -->|"executes"| node_sqlite_db
node_promo_queries -->|"executes"| node_sqlite_db
node_review_queries -->|"executes"| node_sqlite_db
node_shipping_queries -->|"executes"| node_sqlite_db
node_test_setup -->|"prepares"| node_sqlite_db
node_connection_test -->|"verifies"| node_sqlite_db
node_schema_test -->|"verifies"| node_schema
node_integration_test -->|"exercises"| node_sqlite_db
node_integration_test -->|"exercises"| node_order_queries
node_crud_test -->|"checks"| node_schema
node_crud_test -->|"checks"| node_customer_queries
node_read_hook -->|"guards"| node_schema
node_write_hook -->|"guards"| node_main
node_query_hook -->|"guards"| node_order_queries
node_tsc_hook -->|"compiles"| node_main

click node_scripts_init_claude "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/scripts/init-claude.js"
click node_claude_settings "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/.claude/settings.local.json"
click node_read_hook "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/read_hook.js"
click node_write_hook "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/write_hook.js"
click node_query_hook "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/query_hook.js"
click node_tsc_hook "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/tsc.js"
click node_main "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/main.ts"
click node_example "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/example.ts"
click node_sdk "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/sdk.ts"
click node_schema "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/schema.ts"
click node_types "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/types.ts"
click node_customer_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/customer_queries.ts"
click node_product_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/product_queries.ts"
click node_order_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/order_queries.ts"
click node_inventory_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/inventory_queries.ts"
click node_analytics_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/analytics_queries.ts"
click node_promo_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/promotion_queries.ts"
click node_review_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/review_queries.ts"
click node_shipping_queries "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/shipping_queries.ts"
click node_test_setup "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/setup.ts"
click node_connection_test "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.connection.test.ts"
click node_schema_test "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.schema.test.ts"
click node_integration_test "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.integration.test.ts"
click node_crud_test "https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.test.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_scripts_init_claude,node_claude_settings,node_read_hook,node_write_hook,node_query_hook,node_tsc_hook toneBlue
class node_main,node_example,node_sdk,node_schema,node_types,node_customer_queries,node_product_queries,node_order_queries,node_inventory_queries,node_analytics_queries,node_promo_queries,node_review_queries,node_shipping_queries toneAmber
class node_sqlite_db toneMint
class node_test_setup,node_connection_test,node_schema_test,node_integration_test,node_crud_test toneRose
```

## Components

### Claude Code hooks

File/tool boundary enforcement and local Claude configuration.

- [Claude setup](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/scripts/init-claude.js): bootstrap script · Installs or initializes local Claude hook settings. · `scripts/init-claude.js`
- [Claude settings](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/.claude/settings.local.json): local config · Local Claude configuration that activates the hook workflow. · `.claude/settings.local.json`
- [Read hook](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/read_hook.js): file guard · Blocks or filters sensitive reads. · `hooks/read_hook.js`
- [Write hook](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/write_hook.js): file guard · Blocks sensitive writes and secret-like content. · `hooks/write_hook.js`
- [Query hook](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/query_hook.js): tool guard · Deduplicates or constrains query-like tool usage. · `hooks/query_hook.js`
- [TSC hook](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/hooks/tsc.js): compile trigger · Runs TypeScript compilation on relevant file changes. · `hooks/tsc.js`

### TypeScript app

Node.js/TypeScript demo with e-commerce schema and domain SQL modules.

- [Main](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/main.ts): entrypoint · Primary executable entry point. · `src/main.ts`
- [Example](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/example.ts): demo entry · Runnable demo path for the database layer. · `src/example.ts`
- [SDK surface](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/sdk.ts): package api · Packaged surface exposed from the repository root. · `sdk.ts`
- [Schema](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/schema.ts): db schema · Defines the SQLite tables and relationships. · `src/schema.ts`
- [Types](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/types.ts): ts models · Shared TypeScript shapes used across the app. · `src/types.ts`
- [Customer queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/customer_queries.ts): SQL module · Customer-facing data access and CRUD SQL. · `src/queries/customer_queries.ts`
- [Product queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/product_queries.ts): SQL module · Product catalog and lookup SQL. · `src/queries/product_queries.ts`
- [Order queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/order_queries.ts): SQL module · Order placement, status, and history SQL. · `src/queries/order_queries.ts`
- [Inventory queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/inventory_queries.ts): SQL module · Stock and availability SQL. · `src/queries/inventory_queries.ts`
- [Analytics queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/analytics_queries.ts): SQL module · Reporting and aggregate SQL. · `src/queries/analytics_queries.ts`
- [Promo queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/promotion_queries.ts): SQL module · Promotion and discount SQL. · `src/queries/promotion_queries.ts`
- [Review queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/review_queries.ts): SQL module · Product review SQL. · `src/queries/review_queries.ts`
- [Shipping queries](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/src/queries/shipping_queries.ts): SQL module · Fulfillment and shipment SQL. · `src/queries/shipping_queries.ts`

### SQLite core

Embedded in-process database accessed directly by queries and tests.

- **SQLite DB**: better-sqlite3 · Embedded synchronous persistence core.

### Tests

Parallel validation path for schema, CRUD, transactions, and connection behavior.

- [Test setup](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/setup.ts): test bootstrap · Shared test initialization and fixtures. · `tests/setup.ts`
- [Connection test](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.connection.test.ts): integration test · Verifies database connection behavior. · `tests/database.connection.test.ts`
- [Schema test](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.schema.test.ts): integration test · Validates schema definitions and migration behavior. · `tests/database.schema.test.ts`
- [Integration test](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.integration.test.ts): integration test · Exercises end-to-end database operations. · `tests/database.integration.test.ts`
- [CRUD test](https://github.com/sbusanelli/claude-code-hooks-demo/blob/main/tests/database.test.ts): unit test · Checks CRUD behavior and transactional paths. · `tests/database.test.ts`

## Connections

- Claude setup → Claude settings: installs · Creates or updates the local Claude configuration.
- Claude settings → Read hook: enables · Activates the read boundary hook.
- Claude settings → Write hook: enables · Activates the write boundary hook.
- Claude settings → Query hook: enables · Activates the query boundary hook.
- Claude settings → TSC hook: enables · Activates the compilation hook.
- Main → Schema: initializes · Main entry point uses the schema before data operations.
- Main → Types: uses · Main entry point relies on shared domain types.
- Main → Customer queries: calls · Primary runtime reaches customer data access.
- Main → Order queries: calls · Primary runtime reaches order data access.
- Example → Schema: initializes · Demo path starts from the schema layer.
- Example → Product queries: calls · Demo path exercises product data access.
- SDK surface → Main: wraps · Package surface exposes the app runtime.
- Schema → SQLite DB: defines · Schema materializes inside the embedded database.
- Customer queries → SQLite DB: executes · Customer SQL runs directly against SQLite.
- Product queries → SQLite DB: executes · Product SQL runs directly against SQLite.
- Order queries → SQLite DB: executes · Order SQL runs directly against SQLite.
- Inventory queries → SQLite DB: executes · Inventory SQL runs directly against SQLite.
- Analytics queries → SQLite DB: executes · Analytics SQL runs directly against SQLite.
- Promo queries → SQLite DB: executes · Promotion SQL runs directly against SQLite.
- Review queries → SQLite DB: executes · Review SQL runs directly against SQLite.
- Shipping queries → SQLite DB: executes · Shipping SQL runs directly against SQLite.
- Test setup → SQLite DB: prepares · Shared setup initializes the database for tests.
- Connection test → SQLite DB: verifies · Connection tests hit the embedded database.
- Schema test → Schema: verifies · Schema tests validate the table model.
- Integration test → SQLite DB: exercises · Integration tests run against the same SQLite core.
- Integration test → Order queries: exercises · Integration tests cover representative domain SQL.
- CRUD test → Schema: checks · CRUD tests depend on the schema definition.
- CRUD test → Customer queries: checks · CRUD tests exercise representative business queries.
- Read hook → Schema: guards · Read hook sits in front of sensitive source files.
- Write hook → Main: guards · Write hook protects application sources.
- Query hook → Order queries: guards · Query hook constrains query-style operations.
- TSC hook → Main: compiles · Compilation hook validates TypeScript sources.
