admindb 2.4.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/LICENSE +20 -20
  2. package/dist/app.js +48 -25
  3. package/dist/core/context.d.ts +1 -0
  4. package/dist/core/context.js +2 -0
  5. package/dist/core/diff/data-differ.d.ts +1 -0
  6. package/dist/core/diff/data-differ.js +243 -0
  7. package/dist/core/diff/patch-generator.d.ts +1 -0
  8. package/dist/core/diff/patch-generator.js +77 -0
  9. package/dist/core/diff/schema-differ.d.ts +1 -0
  10. package/dist/core/diff/schema-differ.js +229 -0
  11. package/dist/core/errors.d.ts +1 -0
  12. package/dist/core/errors.js +39 -0
  13. package/dist/core/index.d.ts +1 -0
  14. package/dist/core/index.js +20 -0
  15. package/dist/core/result.d.ts +1 -0
  16. package/dist/core/result.js +10 -0
  17. package/dist/core/router.d.ts +1 -0
  18. package/dist/core/router.js +37 -0
  19. package/dist/core/transfer/dialect-mapper.d.ts +1 -0
  20. package/dist/core/transfer/dialect-mapper.js +303 -0
  21. package/dist/core/transfer/masking.d.ts +1 -0
  22. package/dist/core/transfer/masking.js +49 -0
  23. package/dist/core/transfer/sync-engine.d.ts +1 -0
  24. package/dist/core/transfer/sync-engine.js +234 -0
  25. package/dist/core/transfer/topological-sort.d.ts +1 -0
  26. package/dist/core/transfer/topological-sort.js +55 -0
  27. package/dist/data/chain.js +1 -1
  28. package/dist/data/engine.js +1 -1
  29. package/dist/db/database.d.ts +1 -1
  30. package/dist/db/database.js +9 -31
  31. package/dist/db/dialects/index.d.ts +1 -0
  32. package/dist/db/dialects/index.js +58 -0
  33. package/dist/db/dialects/postgres/ddl.d.ts +1 -0
  34. package/dist/db/dialects/postgres/ddl.js +107 -0
  35. package/dist/db/dialects/postgres/dialect.d.ts +1 -0
  36. package/dist/db/dialects/postgres/dialect.js +28 -0
  37. package/dist/db/dialects/postgres/introspector.d.ts +1 -0
  38. package/dist/db/dialects/postgres/introspector.js +265 -0
  39. package/dist/db/dialects/postgres/types.d.ts +1 -0
  40. package/dist/db/dialects/postgres/types.js +83 -0
  41. package/dist/db/dialects/sqlite/ddl.d.ts +1 -0
  42. package/dist/db/dialects/sqlite/ddl.js +244 -0
  43. package/dist/db/dialects/sqlite/dialect.d.ts +1 -0
  44. package/dist/db/dialects/sqlite/dialect.js +25 -0
  45. package/dist/db/dialects/sqlite/introspector.d.ts +1 -0
  46. package/dist/db/dialects/sqlite/introspector.js +217 -0
  47. package/dist/db/dialects/sqlite/types.d.ts +1 -0
  48. package/dist/db/dialects/sqlite/types.js +69 -0
  49. package/dist/db/dialects/types.d.ts +1 -0
  50. package/dist/db/dialects/types.js +15 -0
  51. package/dist/db/export.js +1 -1
  52. package/dist/db/index.d.ts +1 -1
  53. package/dist/db/index.js +1 -1
  54. package/dist/db/manager.d.ts +1 -1
  55. package/dist/db/manager.js +101 -21
  56. package/dist/db/postgres.d.ts +1 -1
  57. package/dist/db/postgres.js +20 -284
  58. package/dist/db/types.d.ts +1 -1
  59. package/dist/db/types.js +0 -4
  60. package/dist/index.d.ts +1 -1
  61. package/dist/index.js +4 -1
  62. package/dist/modules/databases/databases.routes.d.ts +1 -0
  63. package/dist/modules/databases/databases.routes.js +150 -0
  64. package/dist/modules/databases/databases.service.d.ts +1 -0
  65. package/dist/modules/databases/databases.service.js +134 -0
  66. package/dist/modules/databases/index.d.ts +1 -0
  67. package/dist/{routes → modules/databases}/index.js +2 -3
  68. package/dist/modules/erd/erd.routes.d.ts +1 -0
  69. package/dist/modules/erd/erd.routes.js +32 -0
  70. package/dist/modules/erd/erd.service.d.ts +1 -0
  71. package/dist/modules/erd/erd.service.js +39 -0
  72. package/dist/modules/erd/index.d.ts +1 -0
  73. package/dist/modules/erd/index.js +18 -0
  74. package/dist/modules/index.d.ts +1 -0
  75. package/dist/modules/index.js +70 -0
  76. package/dist/modules/query/index.d.ts +1 -0
  77. package/dist/modules/query/index.js +18 -0
  78. package/dist/modules/query/query.routes.d.ts +1 -0
  79. package/dist/modules/query/query.routes.js +54 -0
  80. package/dist/modules/query/query.service.d.ts +1 -0
  81. package/dist/modules/query/query.service.js +95 -0
  82. package/dist/modules/rows/blob.helper.d.ts +1 -0
  83. package/dist/modules/rows/blob.helper.js +40 -0
  84. package/dist/modules/rows/display.helper.d.ts +1 -0
  85. package/dist/modules/rows/display.helper.js +73 -0
  86. package/dist/modules/rows/index.d.ts +1 -0
  87. package/dist/modules/rows/index.js +20 -0
  88. package/dist/modules/rows/rows.routes.d.ts +1 -0
  89. package/dist/modules/rows/rows.routes.js +555 -0
  90. package/dist/modules/rows/rows.service.d.ts +1 -0
  91. package/dist/modules/rows/rows.service.js +433 -0
  92. package/dist/modules/schema/index.d.ts +1 -0
  93. package/dist/modules/schema/index.js +18 -0
  94. package/dist/modules/schema/schema.routes.d.ts +1 -0
  95. package/dist/modules/schema/schema.routes.js +171 -0
  96. package/dist/modules/schema/schema.service.d.ts +1 -0
  97. package/dist/modules/schema/schema.service.js +58 -0
  98. package/dist/modules/search/index.d.ts +1 -0
  99. package/dist/modules/search/index.js +19 -0
  100. package/dist/modules/search/search.routes.d.ts +1 -0
  101. package/dist/modules/search/search.routes.js +38 -0
  102. package/dist/modules/search/search.service.d.ts +1 -0
  103. package/dist/modules/search/search.service.js +458 -0
  104. package/dist/modules/search/search.types.d.ts +1 -0
  105. package/dist/modules/search/search.types.js +2 -0
  106. package/dist/modules/seed/index.d.ts +1 -0
  107. package/dist/modules/seed/index.js +18 -0
  108. package/dist/modules/seed/seed.routes.d.ts +1 -0
  109. package/dist/modules/seed/seed.routes.js +212 -0
  110. package/dist/modules/seed/seed.service.d.ts +1 -0
  111. package/dist/{routes/api/helpers.js → modules/seed/seed.service.js} +86 -109
  112. package/dist/modules/sync/index.d.ts +1 -0
  113. package/dist/modules/sync/index.js +18 -0
  114. package/dist/modules/sync/sync.routes.d.ts +1 -0
  115. package/dist/modules/sync/sync.routes.js +135 -0
  116. package/dist/modules/sync/sync.service.d.ts +1 -0
  117. package/dist/modules/sync/sync.service.js +41 -0
  118. package/dist/modules/tables/index.d.ts +1 -0
  119. package/dist/modules/tables/index.js +18 -0
  120. package/dist/modules/tables/tables.routes.d.ts +1 -0
  121. package/dist/modules/tables/tables.routes.js +194 -0
  122. package/dist/modules/tables/tables.service.d.ts +1 -0
  123. package/dist/modules/tables/tables.service.js +218 -0
  124. package/dist/public/css/app.css +1 -1
  125. package/dist/public/js/browse.js +1 -1
  126. package/dist/public/js/query.js +1 -1
  127. package/dist/public/js/search.js +1 -0
  128. package/dist/public/js/sync.js +1 -0
  129. package/dist/types/api.d.ts +1 -1
  130. package/dist/utils/common.d.ts +1 -1
  131. package/dist/utils/common.js +1 -3
  132. package/dist/utils/icons.js +3 -0
  133. package/dist/utils/stream.d.ts +1 -0
  134. package/dist/utils/stream.js +131 -0
  135. package/dist/views/layouts/main.hbs +1 -1
  136. package/dist/views/pages/error.hbs +10 -1
  137. package/dist/views/pages/home.hbs +1 -1
  138. package/dist/views/pages/query.hbs +1 -1
  139. package/dist/views/pages/seed-select.hbs +1 -252
  140. package/dist/views/pages/seed.hbs +1 -307
  141. package/dist/views/pages/sync.hbs +409 -0
  142. package/dist/views/pages/table.hbs +1 -1
  143. package/dist/views/partials/navbar.hbs +1 -1
  144. package/dist/views/partials/sidebar.hbs +1 -1
  145. package/docs/API.md +4 -17
  146. package/docs/EXAMPLES.md +579 -579
  147. package/docs/SECURITY.md +265 -265
  148. package/package.json +10 -9
  149. package/dist/db/introspection.d.ts +0 -1
  150. package/dist/db/introspection.js +0 -164
  151. package/dist/routes/api/erd.d.ts +0 -1
  152. package/dist/routes/api/erd.js +0 -45
  153. package/dist/routes/api/helpers.d.ts +0 -1
  154. package/dist/routes/api/import-export.d.ts +0 -1
  155. package/dist/routes/api/import-export.js +0 -84
  156. package/dist/routes/api/index.d.ts +0 -1
  157. package/dist/routes/api/index.js +0 -55
  158. package/dist/routes/api/query.d.ts +0 -1
  159. package/dist/routes/api/query.js +0 -77
  160. package/dist/routes/api/rows.d.ts +0 -1
  161. package/dist/routes/api/rows.js +0 -373
  162. package/dist/routes/api/seed.d.ts +0 -1
  163. package/dist/routes/api/seed.js +0 -180
  164. package/dist/routes/api/tables.d.ts +0 -1
  165. package/dist/routes/api/tables.js +0 -230
  166. package/dist/routes/databases.d.ts +0 -1
  167. package/dist/routes/databases.js +0 -212
  168. package/dist/routes/index.d.ts +0 -1
  169. package/dist/routes/pages.d.ts +0 -1
  170. package/dist/routes/pages.js +0 -540
package/docs/EXAMPLES.md CHANGED
@@ -1,579 +1,579 @@
1
- # AdminDB Examples & Configuration Reference
2
-
3
- > A comprehensive, organized reference guide for **Environment Variables**, **JSON Configuration Files**, **Programmatic Code Examples (Express & TypeScript)**, and **Deployment Recipes**.
4
-
5
- ---
6
-
7
- ## 📑 Table of Contents
8
-
9
- - [Overview & Precedence Order](#-overview--precedence-order)
10
- - [Configuration Matrix](#-configuration-matrix)
11
- - [Part 1: Environment Variables Reference](#-part-1-environment-variables-reference)
12
- - [1. PORT / ADMINDB_PORT](#1-port--admindb_port)
13
- - [2. HOST / ADMINDB_HOST](#2-host--admindb_host)
14
- - [3. DATABASE_URL / ADMINDB_CONNECTION / PG_CONNECTION](#3-database_url--admindb_connection--pg_connection)
15
- - [4. ADMINDB_CONFIG (JSON Configuration Files)](#4-admindb_config-json-configuration-files)
16
- - [5. DB_PATH / ADMINDB_DB_PATH / ADMINDB_PATH](#5-db_path--admindb_db_path--admindb_path)
17
- - [6. DB_DIR / ADMINDB_DB_DIR / ADMINDB_DIR](#6-db_dir--admindb_db_dir--admindb_dir)
18
- - [7. DB_FILES / ADMINDB_DB_FILES](#7-db_files--admindb_db_files)
19
- - [8. BASE_PATH / ADMINDB_BASE_PATH](#8-base_path--admindb_base_path)
20
- - [9. READONLY / ADMINDB_READONLY](#9-readonly--admindb_readonly)
21
- - [10. SERVERLESS / ADMINDB_SERVERLESS](#10-serverless--admindb_serverless)
22
- - [11. ADMINDB_AUTH / ADMINDB_NO_AUTH / ADMINDB_DISABLE_AUTH](#11-admindb_auth--admindb_no_auth--admindb_disable_auth)
23
- - [12. ADMINDB_USERNAME / ADMINDB_USER](#12-admindb_username--admindb_user)
24
- - [13. ADMINDB_PASSWORD / ADMINDB_PASS](#13-admindb_password--admindb_pass)
25
- - [14. ADMINDB_SECRET / SESSION_SECRET](#14-admindb_secret--session_secret)
26
- - [15. LOG_LEVEL / ADMINDB_LOG_LEVEL](#15-log_level--admindb_log_level)
27
- - [Part 2: Programmatic Code Examples (Express & TypeScript)](#-part-2-programmatic-code-examples-express--typescript)
28
- - [`createRouter(options)` Reference](#createrouteroptions-reference)
29
- - [Example 1: Minimal Single SQLite Database Embedding](#example-1-minimal-single-sqlite-database-embedding)
30
- - [Example 2: Single PostgreSQL Database Embedding](#example-2-single-postgresql-database-embedding)
31
- - [Example 3: Multiple PostgreSQL Connections (`DbManager`)](#example-3-multiple-postgresql-connections-dbmanager)
32
- - [Example 4: Mixed Multi-Database Manager (Postgres & SQLite)](#example-4-mixed-multi-database-manager-postgres--sqlite)
33
- - [Example 5: Custom Authentication with Salted scrypt Hash](#example-5-custom-authentication-with-salted-scrypt-hash)
34
- - [Example 6: Disabling Built-in Auth to Use Custom Express Middleware](#example-6-disabling-built-in-auth-to-use-custom-express-middleware)
35
-
36
- - [Part 3: Infrastructure & Deployment Recipes](#-part-3-infrastructure--deployment-recipes)
37
- - [Recipe 1: Secure Credentials JSON File](#recipe-1-secure-credentials-json-file)
38
- - [Recipe 2: Direct PostgreSQL CLI Connection](#recipe-2-direct-postgresql-cli-connection)
39
- - [Recipe 3: Multi-Database Folder (Read-Only Auditor)](#recipe-3-multi-database-folder-read-only-auditor)
40
- - [Recipe 4: Production Docker & Docker Compose Deployment](#recipe-4-production-docker--docker-compose-deployment)
41
- - [Recipe 5: Nginx Reverse Proxy with Subpath Routing](#recipe-5-nginx-reverse-proxy-with-subpath-routing)
42
- - [Recipe 6: Serverless Deployment (Vercel & AWS Lambda)](#recipe-6-serverless-deployment-vercel--aws-lambda)
43
-
44
- ---
45
-
46
- ## ⚡ Overview & Precedence Order
47
-
48
- When starting AdminDB standalone:
49
-
50
- ```text
51
- CLI Arguments & Flags (Highest) ➔ JSON Configuration File ➔ Environment Variables ➔ Default Values (Lowest)
52
- ```
53
-
54
- - **CLI flags always override configuration files and environment variables.**
55
- - JSON configuration files (`name.postgres.json`, `--config config.json`) allow storing passwords securely away from shell history.
56
- - Namespaced variables (`ADMINDB_*`) and standard short variables (`PORT`, `HOST`, `DATABASE_URL`, `DB_PATH`, etc.) are both fully supported.
57
-
58
- > [!WARNING]
59
- > **Production Security & Protection Notice:**
60
- > AdminDB's native authentication is designed as a basic convenience layer for single-user local development.
61
- > In production environments or public-facing deployments, **it is the user's sole responsibility to fully protect AdminDB** using your application's own authentication (e.g. NextAuth, Passport, JWT, SSO/OAuth middleware with `auth: false`), a VPN, an IP-allowlist reverse proxy (Nginx, Cloudflare Access), and HTTPS encryption.
62
-
63
-
64
- ---
65
-
66
- ## 📊 Configuration Matrix
67
-
68
- | Feature | CLI Flag & Aliases | Environment Variable & Aliases | Type | Default | Description |
69
- | :--- | :--- | :--- | :--- | :--- | :--- |
70
- | **Config File** | `-C, --config, --json <file.json>` | `ADMINDB_CONFIG` | `string` | — | Path to a JSON configuration file with database credentials |
71
- | **Port** | `-p, --port <port>` | `PORT`, `ADMINDB_PORT` | `number` | `45531` | HTTP port the server listens on |
72
- | **Host** | `-H, --host <host>` | `HOST`, `ADMINDB_HOST` | `string` | `0.0.0.0` | Network interface to bind |
73
- | **Connection URI** | `-c, --connection, --conn, --pg <uri>` | `DATABASE_URL`, `ADMINDB_CONNECTION`, `PG_CONNECTION` | `string` | — | PostgreSQL connection URI or database target |
74
- | **Single DB File** | `-o, --open, --db-path, --file <file>` | `DB_PATH`, `ADMINDB_DB_PATH`, `ADMINDB_PATH` | `string` | `admindb.db` | Path to a single SQLite database file |
75
- | **Database Directory** | `-d, --dir, --db-dir, --folder <dir>` | `DB_DIR`, `ADMINDB_DB_DIR`, `ADMINDB_DIR` | `string` | — | Folder of `.db`/`.sqlite` files to manage |
76
- | **Explicit DB Files** | `--files, --db-files <list>` | `DB_FILES`, `ADMINDB_DB_FILES` | `string` (CSV) | — | Comma-separated database file paths |
77
- | **Base URL Path** | `-b, --base-path, --base <path>` | `BASE_PATH`, `ADMINDB_BASE_PATH` | `string` | `''` (`/`) | URL prefix to serve under (e.g. `/admin`) |
78
- | **Read-Only Mode** | `-r, --readonly, --read-only` | `READONLY`, `ADMINDB_READONLY` | `boolean` | `false` | Disable all insert, update, delete, and DDL operations |
79
- | **Serverless** | `--serverless` | `SERVERLESS`, `ADMINDB_SERVERLESS` | `boolean` | `false` *(auto)* | Serverless mode (SQLite read-only, Postgres editable) |
80
- | **Authentication** | `--auth` / `--no-auth`, `--disable-auth` | `ADMINDB_AUTH`, `ADMINDB_NO_AUTH`, `ADMINDB_DISABLE_AUTH` | `boolean` | `true` | Enable or disable built-in login authentication |
81
- | **Admin Username** | `-u, --username, --user, --auth-username <user>` | `ADMINDB_USERNAME`, `ADMINDB_USER` | `string` | `admin` | Custom administrator username for web login & basic auth |
82
- | **Admin Password** | `-P, --password, --pass, --auth-password <pass>` | `ADMINDB_PASSWORD`, `ADMINDB_PASS` | `string` | `admin` (hash) | Plain text password or salted `scrypt:...` cryptographic hash |
83
- | **Session Secret** | `--auth-secret, --secret, --session-secret <sec>` | `ADMINDB_SECRET`, `SESSION_SECRET` | `string` | *(auto-generated)* | Secret key used to sign HTTP session cookies |
84
- | **Log Level** | `-l, --log-level <level>` | `LOG_LEVEL`, `ADMINDB_LOG_LEVEL` | `enum` | `info` | Logging verbosity: `debug`, `info`, `warn`, `error` |
85
- | **Help** | `-h, --help` | — | `boolean` | `false` | Print CLI help documentation |
86
- | **Version** | `-v, --version` | — | `boolean` | `false` | Print current package version |
87
-
88
- ---
89
-
90
- # 🌐 Part 1: Environment Variables Reference
91
-
92
- Detailed reference for every environment variable supported by AdminDB.
93
-
94
- ---
95
-
96
- ### 1. `PORT` / `ADMINDB_PORT`
97
- * **Type:** `number`
98
- * **Default:** `45531`
99
- * **Why/When to use it:** Specify the TCP port where the web server should accept connections.
100
- * **Configuration Examples:**
101
- ```bash
102
- PORT=8080 npx admindb
103
- ```
104
-
105
- ---
106
-
107
- ### 2. `HOST` / `ADMINDB_HOST`
108
- * **Type:** `string`
109
- * **Default:** `0.0.0.0`
110
- * **Why/When to use it:** Specify which network interface to bind. Use `127.0.0.1` to restrict access strictly to localhost.
111
- * **Configuration Examples:**
112
- ```bash
113
- HOST=127.0.0.1 npx admindb
114
- ```
115
-
116
- ---
117
-
118
- ### 3. `DATABASE_URL` / `ADMINDB_CONNECTION` / `PG_CONNECTION`
119
- * **Type:** `string`
120
- * **Default:** —
121
- * **Why/When to use it:** Connect directly to a PostgreSQL database (e.g. Supabase, Neon, AWS RDS, local PostgreSQL).
122
- * **Configuration Examples:**
123
- ```bash
124
- DATABASE_URL="postgresql://postgres:secret@localhost:5432/my_database" npx admindb
125
- ```
126
-
127
- ---
128
-
129
- ### 4. `ADMINDB_CONFIG` (JSON Configuration Files)
130
- * **Type:** `string` (Path to a `.json` file)
131
- * **Default:** —
132
- * **Why/When to use it:** Store database connection credentials securely in a JSON file without passing raw passwords on the command line.
133
- * **Format:**
134
- ```json
135
- {
136
- "primary": "postgresql://postgres:secret@localhost:5432/primary_db",
137
- "analytics": "postgresql://postgres:secret@localhost:5432/analytics_db",
138
- "local": "./data/local.db"
139
- }
140
- ```
141
- * **Configuration Examples:**
142
- ```bash
143
- npx admindb name.postgres.json
144
- # or
145
- ADMINDB_CONFIG=./connections.json npx admindb
146
- ```
147
-
148
- ---
149
-
150
- ### 5. `DB_PATH` / `ADMINDB_DB_PATH` / `ADMINDB_PATH`
151
- * **Type:** `string`
152
- * **Default:** `admindb.db`
153
- * **Why/When to use it:** Open a single SQLite database file directly.
154
- * **Configuration Examples:**
155
- ```bash
156
- DB_PATH="./data/production.sqlite" npx admindb
157
- ```
158
-
159
- ---
160
-
161
- ### 6. `DB_DIR` / `ADMINDB_DB_DIR` / `ADMINDB_DIR`
162
- * **Type:** `string`
163
- * **Default:** —
164
- * **Why/When to use it:** Scan a folder and manage all SQLite files found in it.
165
- * **Configuration Examples:**
166
- ```bash
167
- DB_DIR="./databases" npx admindb
168
- ```
169
-
170
- ---
171
-
172
- ### 7. `DB_FILES` / `ADMINDB_DB_FILES`
173
- * **Type:** `string` (Comma-separated paths)
174
- * **Default:** —
175
- * **Why/When to use it:** Specify an explicit list of database files located anywhere on disk.
176
- * **Configuration Examples:**
177
- ```bash
178
- DB_FILES="./app.db,/var/data/users.sqlite" npx admindb
179
- ```
180
-
181
- ---
182
-
183
- ### 8. `BASE_PATH` / `ADMINDB_BASE_PATH`
184
- * **Type:** `string`
185
- * **Default:** `''` (`/`)
186
- * **Why/When to use it:** Serve AdminDB under a URL prefix (e.g. `/admin`).
187
- * **Configuration Examples:**
188
- ```bash
189
- BASE_PATH="/admin" npx admindb
190
- ```
191
-
192
- ---
193
-
194
- ### 9. `READONLY` / `ADMINDB_READONLY`
195
- * **Type:** `boolean` (`1`, `true`, `yes`, `on`)
196
- * **Default:** `false`
197
- * **Why/When to use it:** Open databases in **Strict Read-Only Mode**. Disables all insert, update, delete, and DDL operations.
198
- * **Configuration Examples:**
199
- ```bash
200
- READONLY=true npx admindb
201
- ```
202
-
203
- ---
204
-
205
- ### 10. `SERVERLESS` / `ADMINDB_SERVERLESS`
206
- * **Type:** `boolean` (`1`, `true`, `yes`, `on`)
207
- * **Default:** `false` *(automatically detected on Vercel, AWS Lambda, Cloudflare Pages, Netlify, GCP Cloud Functions)*
208
- * **Why/When to use it:** Enforce read-only safety for local SQLite databases while keeping remote PostgreSQL connections fully writable.
209
- * **Configuration Examples:**
210
- ```bash
211
- SERVERLESS=true npx admindb
212
- ```
213
-
214
- ---
215
-
216
- ### 11. `ADMINDB_AUTH` / `ADMINDB_NO_AUTH` / `ADMINDB_DISABLE_AUTH`
217
- * **Type:** `boolean`
218
- * **Default:** `true` (auth enabled)
219
- * **Why/When to use it:** Enable or disable built-in native authentication.
220
- * **Configuration Examples:**
221
- ```bash
222
- ADMINDB_AUTH=false npx admindb
223
- ```
224
-
225
- ---
226
-
227
- ### 12. `ADMINDB_USERNAME` / `ADMINDB_USER`
228
- * **Type:** `string`
229
- * **Default:** `admin`
230
- * **Why/When to use it:** Change the administrator username.
231
- * **Configuration Examples:**
232
- ```bash
233
- ADMINDB_USERNAME=superadmin npx admindb
234
- ```
235
-
236
- ---
237
-
238
- ### 13. `ADMINDB_PASSWORD` / `ADMINDB_PASS`
239
- * **Type:** `string` (Plain text or salted cryptographic `scrypt` hash)
240
- * **Default:** `admin` *(hash)*
241
- * **Why/When to use it:** Secure your instance with a custom password or scrypt hash.
242
- * **Generating a Hash:**
243
- ```bash
244
- npm run generatehash
245
- ```
246
- * **Configuration Examples:**
247
- ```bash
248
- ADMINDB_PASSWORD="scrypt:3f8e02d9a1c4b7e8...:cb3032b16f29c8d44f75..." npx admindb
249
- ```
250
-
251
- ---
252
-
253
- ### 14. `ADMINDB_SECRET` / `SESSION_SECRET`
254
- * **Type:** `string`
255
- * **Default:** Auto-generated randomly per process instance
256
- * **Why/When to use it:** Fixed secret key used to sign HTTP session cookies.
257
- * **Configuration Examples:**
258
- ```bash
259
- ADMINDB_SECRET="a7f8e92c4b1d6e3f8a0b5c7d9e1f2a3b4c5d6e7f8" npx admindb
260
- ```
261
-
262
- ---
263
-
264
- ### 15. `LOG_LEVEL` / `ADMINDB_LOG_LEVEL`
265
- * **Type:** `enum`: `debug` | `info` | `warn` | `error`
266
- * **Default:** `info`
267
- * **Why/When to use it:** Control log output verbosity in the terminal.
268
- * **Configuration Examples:**
269
- ```bash
270
- LOG_LEVEL=warn npx admindb
271
- ```
272
-
273
- ---
274
-
275
- # 💻 Part 2: Programmatic Code Examples (Express & TypeScript)
276
-
277
- AdminDB can be mounted directly into any existing Express application as a sub-router on the same port.
278
-
279
- > **💡 Note on ESM vs CJS:**
280
- > AdminDB natively supports both **ES Modules** (`import { createRouter } from 'admindb'`) and **CommonJS** (`const { createRouter } = require('admindb')`). The examples below use TypeScript/ESM syntax, but they work identically in pure Node.js CommonJS.
281
-
282
- ---
283
-
284
- ### `createRouter(options)` Reference
285
-
286
- | Option | Type | Default | Description |
287
- | :--- | :--- | :--- | :--- |
288
- | `connection` | `string` | — | PostgreSQL connection string or database URI. |
289
- | `dbPath` | `string` | `'admindb.db'` | Path to a single SQLite database file. |
290
- | `db` | `IDatabase` | — | An already-opened `SqliteDatabase` or `PostgresDatabase` instance. |
291
- | `manager` | `DbManager` | — | Enables multi-database management across folders, files, and named connections. |
292
- | `basePath` | `string` | `''` | URL prefix used by templates and static assets (e.g. `/admin`). |
293
- | `readonly` | `boolean` | `false` | Open databases in strict read-only mode. |
294
- | `serverless` | `boolean` | `false` | Serverless mode (SQLite read-only, Postgres editable). |
295
- | `auth` | `boolean \| AuthConfig` | `true` | Configure built-in authentication or pass `false` to disable it. |
296
- | `logger` | `Logger` | — | Custom logger instance. |
297
- | `logLevel` | `'debug'\|'info'\|'warn'\|'error'` | `'info'` | Logging verbosity when no custom logger is provided. |
298
-
299
- ---
300
-
301
- ### Example 1: Minimal Single SQLite Database Embedding
302
-
303
- ```ts
304
- import express from 'express';
305
- import { createRouter } from 'admindb';
306
-
307
- const app = express();
308
-
309
- app.use('/admin', createRouter({
310
- dbPath: './data/production.db',
311
- basePath: '/admin',
312
- }));
313
-
314
- app.listen(45531, () => {
315
- console.log('App running on http://localhost:45531 (Admin: http://localhost:45531/admin)');
316
- });
317
- ```
318
-
319
- ---
320
-
321
- ### Example 2: Single PostgreSQL Database Embedding
322
-
323
- ```ts
324
- import express from 'express';
325
- import { createRouter } from 'admindb';
326
-
327
- const app = express();
328
-
329
- app.use('/admin', createRouter({
330
- connection: process.env.DATABASE_URL || 'postgresql://postgres:secret@localhost:5432/mydb',
331
- basePath: '/admin',
332
- }));
333
-
334
- app.listen(45531, () => {
335
- console.log('AdminDB running at http://localhost:45531/admin');
336
- });
337
- ```
338
-
339
- ---
340
-
341
- ### Example 3: Multiple PostgreSQL Connections (`DbManager`)
342
-
343
- Manage multiple PostgreSQL databases under a single AdminDB manager interface:
344
-
345
- ```ts
346
- import express from 'express';
347
- import { createRouter, DbManager } from 'admindb';
348
-
349
- const app = express();
350
-
351
- // Initialize manager with multiple named PostgreSQL connections
352
- const manager = new DbManager({
353
- connections: {
354
- primary_db: process.env.PRIMARY_DB_URL || 'postgresql://postgres:secret@db1.internal:5432/primary_app',
355
- analytics_db: process.env.ANALYTICS_DB_URL || 'postgresql://postgres:secret@db2.internal:5432/analytics',
356
- users_shard: 'postgresql://postgres:secret@db3.internal:5432/users_db',
357
- },
358
- });
359
-
360
- // Optionally register another PostgreSQL connection dynamically at runtime
361
- // (e.g. read-only replica)
362
- manager.addConnection('reporting_replica', 'postgresql://postgres:secret@replica.internal:5432/reporting_db', /* readonly */ true);
363
-
364
- // Mount AdminDB manager router
365
- app.use('/admin', createRouter({
366
- manager,
367
- basePath: '/admin',
368
- }));
369
-
370
- app.listen(45531, () => {
371
- console.log('Multi-PostgreSQL Admin running on http://localhost:45531/admin');
372
- });
373
- ```
374
-
375
- ---
376
-
377
- ### Example 4: Mixed Multi-Database Manager (Postgres & SQLite)
378
-
379
- ```ts
380
- import express from 'express';
381
- import { createRouter, DbManager } from 'admindb';
382
-
383
- const app = express();
384
-
385
- const manager = new DbManager({
386
- dir: './data/databases',
387
- connections: {
388
- prod_pg: 'postgresql://postgres:secret@db.internal:5432/prod_db',
389
- analytics_pg: 'postgresql://postgres:secret@analytics.internal:5432/warehouse',
390
- },
391
- files: ['./legacy/archive.sqlite'],
392
- });
393
-
394
- app.use('/admin', createRouter({
395
- manager,
396
- basePath: '/admin',
397
- }));
398
-
399
- app.listen(45531);
400
- ```
401
-
402
- ---
403
-
404
- ### Example 5: Custom Authentication with Salted scrypt Hash
405
-
406
- ```ts
407
- import express from 'express';
408
- import { createRouter } from 'admindb';
409
-
410
- const app = express();
411
-
412
- app.use('/admin', createRouter({
413
- dbPath: './data/app.db',
414
- basePath: '/admin',
415
- auth: {
416
- enabled: true,
417
- username: 'ops_lead',
418
- password: 'scrypt:8011bcda719a32dd307cbcbad899e963:854657965139a68b14ffb30f6a64a18d8b10197046e720dbf6e2e26fee2c5dcc2b6a8b8aed514c1a2be7d73279481bf780c4cf381b68eea40d4e2a7b58dd24f8',
419
- secret: 'my-persistent-session-secret-key-12345',
420
- },
421
- }));
422
-
423
- app.listen(45531);
424
- ```
425
-
426
- ---
427
-
428
- ### Example 6: Disabling Built-in Auth to Use Custom Express Middleware
429
-
430
-
431
- ```ts
432
- import express from 'express';
433
- import { createRouter } from 'admindb';
434
-
435
- const app = express();
436
-
437
- // Custom organization authentication middleware
438
- function requireCompanySso(req: express.Request, res: express.Response, next: express.NextFunction) {
439
- if (req.headers['x-sso-user']) return next();
440
- res.status(401).send('SSO Authentication Required');
441
- }
442
-
443
- app.use('/admin', requireCompanySso, createRouter({
444
- dbPath: './data/app.db',
445
- basePath: '/admin',
446
- auth: false, // Turn off built-in login form
447
- }));
448
-
449
- app.listen(45531);
450
- ```
451
-
452
- ---
453
-
454
- # 🚀 Part 3: Infrastructure & Deployment Recipes
455
-
456
- ---
457
-
458
- ### Recipe 1: Secure Credentials JSON File
459
-
460
- Store your database connections in a JSON file without exposing secrets in your terminal:
461
-
462
- #### `name.postgres.json`:
463
- ```json
464
- {
465
- "production": "postgresql://postgres:secret@10.0.0.5:5432/prod_db",
466
- "analytics": "postgresql://postgres:secret@10.0.0.6:5432/analytics_db",
467
- "local": "./data/local.db"
468
- }
469
- ```
470
-
471
- Run:
472
- ```bash
473
- npx admindb name.postgres.json
474
- ```
475
-
476
- ---
477
-
478
- ### Recipe 2: Direct PostgreSQL CLI Connection
479
-
480
- ```bash
481
- npx admindb postgresql://postgres:password@localhost:5432/my_database
482
- ```
483
-
484
- Or using environment variable:
485
- ```bash
486
- DATABASE_URL="postgresql://postgres:password@localhost:5432/my_database" npx admindb
487
- ```
488
-
489
- ---
490
-
491
- ### Recipe 3: Multi-Database Folder (Read-Only Auditor)
492
-
493
- ```bash
494
- admindb -d ./company_dbs --readonly -p 8080 -u auditor -P "secure_pass_123"
495
- ```
496
-
497
- ---
498
-
499
- ### Recipe 4: Production Docker & Docker Compose Deployment
500
-
501
- #### `Dockerfile`
502
- ```dockerfile
503
- FROM node:20-alpine
504
- WORKDIR /app
505
- RUN npm install -g admindb
506
- EXPOSE 45531
507
- CMD ["admindb"]
508
- ```
509
-
510
- #### `docker-compose.yml`
511
- ```yaml
512
- version: '3.8'
513
-
514
- services:
515
- admindb:
516
- image: node:20-alpine
517
- command: npx admindb
518
- restart: unless-stopped
519
- ports:
520
- - "45531:45531"
521
- environment:
522
- - HOST=0.0.0.0
523
- - PORT=45531
524
- - DATABASE_URL=postgresql://postgres:secret@db:5432/mydb
525
- - BASE_PATH=/admin
526
- - ADMINDB_USERNAME=admin_ops
527
- - ADMINDB_PASSWORD=scrypt:8011bcda719a32dd307cbcbad899e963:854657965139a68b14ffb30f6a64a18d8b10197046e720dbf6e2e26fee2c5dcc2b6a8b8aed514c1a2be7d73279481bf780c4cf381b68eea40d4e2a7b58dd24f8
528
- - ADMINDB_SECRET=c28f93e481b0a6e7d95c1a3f5b7e9d2a
529
- - LOG_LEVEL=info
530
- ```
531
-
532
- ---
533
-
534
- ### Recipe 5: Nginx Reverse Proxy with Subpath Routing
535
-
536
- ```nginx
537
- server {
538
- listen 80;
539
- server_name db.example.com;
540
-
541
- location /admin/ {
542
- proxy_pass http://127.0.0.1:45531/admin/;
543
- proxy_set_header Host $host;
544
- proxy_set_header X-Real-IP $remote_addr;
545
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
546
- proxy_set_header X-Forwarded-Proto $scheme;
547
- }
548
- }
549
- ```
550
-
551
- Command:
552
- ```bash
553
- BASE_PATH=/admin HOST=127.0.0.1 PORT=45531 DB_DIR=/var/databases admindb
554
- ```
555
-
556
- ---
557
-
558
- ### Recipe 6: Serverless Deployment (Vercel & AWS Lambda)
559
-
560
- In serverless mode, local SQLite databases default to read-only safety, while remote PostgreSQL connections are fully editable.
561
-
562
- #### 1. Vercel API Route (`api/index.ts`):
563
- ```ts
564
- import { createServerlessHandler } from 'admindb';
565
-
566
- export default createServerlessHandler({
567
- connection: process.env.DATABASE_URL,
568
- basePath: '/admin',
569
- });
570
- ```
571
-
572
- #### 2. AWS Lambda with API Gateway (`index.ts`):
573
- ```ts
574
- import { createLambdaHandler } from 'admindb';
575
-
576
- export const handler = createLambdaHandler({
577
- connection: process.env.DATABASE_URL,
578
- });
579
- ```
1
+ # AdminDB Examples & Configuration Reference
2
+
3
+ > A comprehensive, organized reference guide for **Environment Variables**, **JSON Configuration Files**, **Programmatic Code Examples (Express & TypeScript)**, and **Deployment Recipes**.
4
+
5
+ ---
6
+
7
+ ## 📑 Table of Contents
8
+
9
+ - [Overview & Precedence Order](#-overview--precedence-order)
10
+ - [Configuration Matrix](#-configuration-matrix)
11
+ - [Part 1: Environment Variables Reference](#-part-1-environment-variables-reference)
12
+ - [1. PORT / ADMINDB_PORT](#1-port--admindb_port)
13
+ - [2. HOST / ADMINDB_HOST](#2-host--admindb_host)
14
+ - [3. DATABASE_URL / ADMINDB_CONNECTION / PG_CONNECTION](#3-database_url--admindb_connection--pg_connection)
15
+ - [4. ADMINDB_CONFIG (JSON Configuration Files)](#4-admindb_config-json-configuration-files)
16
+ - [5. DB_PATH / ADMINDB_DB_PATH / ADMINDB_PATH](#5-db_path--admindb_db_path--admindb_path)
17
+ - [6. DB_DIR / ADMINDB_DB_DIR / ADMINDB_DIR](#6-db_dir--admindb_db_dir--admindb_dir)
18
+ - [7. DB_FILES / ADMINDB_DB_FILES](#7-db_files--admindb_db_files)
19
+ - [8. BASE_PATH / ADMINDB_BASE_PATH](#8-base_path--admindb_base_path)
20
+ - [9. READONLY / ADMINDB_READONLY](#9-readonly--admindb_readonly)
21
+ - [10. SERVERLESS / ADMINDB_SERVERLESS](#10-serverless--admindb_serverless)
22
+ - [11. ADMINDB_AUTH / ADMINDB_NO_AUTH / ADMINDB_DISABLE_AUTH](#11-admindb_auth--admindb_no_auth--admindb_disable_auth)
23
+ - [12. ADMINDB_USERNAME / ADMINDB_USER](#12-admindb_username--admindb_user)
24
+ - [13. ADMINDB_PASSWORD / ADMINDB_PASS](#13-admindb_password--admindb_pass)
25
+ - [14. ADMINDB_SECRET / SESSION_SECRET](#14-admindb_secret--session_secret)
26
+ - [15. LOG_LEVEL / ADMINDB_LOG_LEVEL](#15-log_level--admindb_log_level)
27
+ - [Part 2: Programmatic Code Examples (Express & TypeScript)](#-part-2-programmatic-code-examples-express--typescript)
28
+ - [`createRouter(options)` Reference](#createrouteroptions-reference)
29
+ - [Example 1: Minimal Single SQLite Database Embedding](#example-1-minimal-single-sqlite-database-embedding)
30
+ - [Example 2: Single PostgreSQL Database Embedding](#example-2-single-postgresql-database-embedding)
31
+ - [Example 3: Multiple PostgreSQL Connections (`DbManager`)](#example-3-multiple-postgresql-connections-dbmanager)
32
+ - [Example 4: Mixed Multi-Database Manager (Postgres & SQLite)](#example-4-mixed-multi-database-manager-postgres--sqlite)
33
+ - [Example 5: Custom Authentication with Salted scrypt Hash](#example-5-custom-authentication-with-salted-scrypt-hash)
34
+ - [Example 6: Disabling Built-in Auth to Use Custom Express Middleware](#example-6-disabling-built-in-auth-to-use-custom-express-middleware)
35
+
36
+ - [Part 3: Infrastructure & Deployment Recipes](#-part-3-infrastructure--deployment-recipes)
37
+ - [Recipe 1: Secure Credentials JSON File](#recipe-1-secure-credentials-json-file)
38
+ - [Recipe 2: Direct PostgreSQL CLI Connection](#recipe-2-direct-postgresql-cli-connection)
39
+ - [Recipe 3: Multi-Database Folder (Read-Only Auditor)](#recipe-3-multi-database-folder-read-only-auditor)
40
+ - [Recipe 4: Production Docker & Docker Compose Deployment](#recipe-4-production-docker--docker-compose-deployment)
41
+ - [Recipe 5: Nginx Reverse Proxy with Subpath Routing](#recipe-5-nginx-reverse-proxy-with-subpath-routing)
42
+ - [Recipe 6: Serverless Deployment (Vercel & AWS Lambda)](#recipe-6-serverless-deployment-vercel--aws-lambda)
43
+
44
+ ---
45
+
46
+ ## ⚡ Overview & Precedence Order
47
+
48
+ When starting AdminDB standalone:
49
+
50
+ ```text
51
+ CLI Arguments & Flags (Highest) ➔ JSON Configuration File ➔ Environment Variables ➔ Default Values (Lowest)
52
+ ```
53
+
54
+ - **CLI flags always override configuration files and environment variables.**
55
+ - JSON configuration files (`name.postgres.json`, `--config config.json`) allow storing passwords securely away from shell history.
56
+ - Namespaced variables (`ADMINDB_*`) and standard short variables (`PORT`, `HOST`, `DATABASE_URL`, `DB_PATH`, etc.) are both fully supported.
57
+
58
+ > [!WARNING]
59
+ > **Production Security & Protection Notice:**
60
+ > AdminDB's native authentication is designed as a basic convenience layer for single-user local development.
61
+ > In production environments or public-facing deployments, **it is the user's sole responsibility to fully protect AdminDB** using your application's own authentication (e.g. NextAuth, Passport, JWT, SSO/OAuth middleware with `auth: false`), a VPN, an IP-allowlist reverse proxy (Nginx, Cloudflare Access), and HTTPS encryption.
62
+
63
+
64
+ ---
65
+
66
+ ## 📊 Configuration Matrix
67
+
68
+ | Feature | CLI Flag & Aliases | Environment Variable & Aliases | Type | Default | Description |
69
+ | :--- | :--- | :--- | :--- | :--- | :--- |
70
+ | **Config File** | `-C, --config, --json <file.json>` | `ADMINDB_CONFIG` | `string` | — | Path to a JSON configuration file with database credentials |
71
+ | **Port** | `-p, --port <port>` | `PORT`, `ADMINDB_PORT` | `number` | `45531` | HTTP port the server listens on |
72
+ | **Host** | `-H, --host <host>` | `HOST`, `ADMINDB_HOST` | `string` | `0.0.0.0` | Network interface to bind |
73
+ | **Connection URI** | `-c, --connection, --conn, --pg <uri>` | `DATABASE_URL`, `ADMINDB_CONNECTION`, `PG_CONNECTION` | `string` | — | PostgreSQL connection URI or database target |
74
+ | **Single DB File** | `-o, --open, --db-path, --file <file>` | `DB_PATH`, `ADMINDB_DB_PATH`, `ADMINDB_PATH` | `string` | `admindb.db` | Path to a single SQLite database file |
75
+ | **Database Directory** | `-d, --dir, --db-dir, --folder <dir>` | `DB_DIR`, `ADMINDB_DB_DIR`, `ADMINDB_DIR` | `string` | — | Folder of `.db`/`.sqlite` files to manage |
76
+ | **Explicit DB Files** | `--files, --db-files <list>` | `DB_FILES`, `ADMINDB_DB_FILES` | `string` (CSV) | — | Comma-separated database file paths |
77
+ | **Base URL Path** | `-b, --base-path, --base <path>` | `BASE_PATH`, `ADMINDB_BASE_PATH` | `string` | `''` (`/`) | URL prefix to serve under (e.g. `/admin`) |
78
+ | **Read-Only Mode** | `-r, --readonly, --read-only` | `READONLY`, `ADMINDB_READONLY` | `boolean` | `false` | Disable all insert, update, delete, and DDL operations |
79
+ | **Serverless** | `--serverless` | `SERVERLESS`, `ADMINDB_SERVERLESS` | `boolean` | `false` *(auto)* | Serverless mode (SQLite read-only, Postgres editable) |
80
+ | **Authentication** | `--auth` / `--no-auth`, `--disable-auth` | `ADMINDB_AUTH`, `ADMINDB_NO_AUTH`, `ADMINDB_DISABLE_AUTH` | `boolean` | `true` | Enable or disable built-in login authentication |
81
+ | **Admin Username** | `-u, --username, --user, --auth-username <user>` | `ADMINDB_USERNAME`, `ADMINDB_USER` | `string` | `admin` | Custom administrator username for web login & basic auth |
82
+ | **Admin Password** | `-P, --password, --pass, --auth-password <pass>` | `ADMINDB_PASSWORD`, `ADMINDB_PASS` | `string` | `admin` (hash) | Plain text password or salted `scrypt:...` cryptographic hash |
83
+ | **Session Secret** | `--auth-secret, --secret, --session-secret <sec>` | `ADMINDB_SECRET`, `SESSION_SECRET` | `string` | *(auto-generated)* | Secret key used to sign HTTP session cookies |
84
+ | **Log Level** | `-l, --log-level <level>` | `LOG_LEVEL`, `ADMINDB_LOG_LEVEL` | `enum` | `info` | Logging verbosity: `debug`, `info`, `warn`, `error` |
85
+ | **Help** | `-h, --help` | — | `boolean` | `false` | Print CLI help documentation |
86
+ | **Version** | `-v, --version` | — | `boolean` | `false` | Print current package version |
87
+
88
+ ---
89
+
90
+ # 🌐 Part 1: Environment Variables Reference
91
+
92
+ Detailed reference for every environment variable supported by AdminDB.
93
+
94
+ ---
95
+
96
+ ### 1. `PORT` / `ADMINDB_PORT`
97
+ * **Type:** `number`
98
+ * **Default:** `45531`
99
+ * **Why/When to use it:** Specify the TCP port where the web server should accept connections.
100
+ * **Configuration Examples:**
101
+ ```bash
102
+ PORT=8080 npx admindb
103
+ ```
104
+
105
+ ---
106
+
107
+ ### 2. `HOST` / `ADMINDB_HOST`
108
+ * **Type:** `string`
109
+ * **Default:** `0.0.0.0`
110
+ * **Why/When to use it:** Specify which network interface to bind. Use `127.0.0.1` to restrict access strictly to localhost.
111
+ * **Configuration Examples:**
112
+ ```bash
113
+ HOST=127.0.0.1 npx admindb
114
+ ```
115
+
116
+ ---
117
+
118
+ ### 3. `DATABASE_URL` / `ADMINDB_CONNECTION` / `PG_CONNECTION`
119
+ * **Type:** `string`
120
+ * **Default:** —
121
+ * **Why/When to use it:** Connect directly to a PostgreSQL database (e.g. Supabase, Neon, AWS RDS, local PostgreSQL).
122
+ * **Configuration Examples:**
123
+ ```bash
124
+ DATABASE_URL="postgresql://postgres:secret@localhost:5432/my_database" npx admindb
125
+ ```
126
+
127
+ ---
128
+
129
+ ### 4. `ADMINDB_CONFIG` (JSON Configuration Files)
130
+ * **Type:** `string` (Path to a `.json` file)
131
+ * **Default:** —
132
+ * **Why/When to use it:** Store database connection credentials securely in a JSON file without passing raw passwords on the command line.
133
+ * **Format:**
134
+ ```json
135
+ {
136
+ "primary": "postgresql://postgres:secret@localhost:5432/primary_db",
137
+ "analytics": "postgresql://postgres:secret@localhost:5432/analytics_db",
138
+ "local": "./data/local.db"
139
+ }
140
+ ```
141
+ * **Configuration Examples:**
142
+ ```bash
143
+ npx admindb name.postgres.json
144
+ # or
145
+ ADMINDB_CONFIG=./connections.json npx admindb
146
+ ```
147
+
148
+ ---
149
+
150
+ ### 5. `DB_PATH` / `ADMINDB_DB_PATH` / `ADMINDB_PATH`
151
+ * **Type:** `string`
152
+ * **Default:** `admindb.db`
153
+ * **Why/When to use it:** Open a single SQLite database file directly.
154
+ * **Configuration Examples:**
155
+ ```bash
156
+ DB_PATH="./data/production.sqlite" npx admindb
157
+ ```
158
+
159
+ ---
160
+
161
+ ### 6. `DB_DIR` / `ADMINDB_DB_DIR` / `ADMINDB_DIR`
162
+ * **Type:** `string`
163
+ * **Default:** —
164
+ * **Why/When to use it:** Scan a folder and manage all SQLite files found in it.
165
+ * **Configuration Examples:**
166
+ ```bash
167
+ DB_DIR="./databases" npx admindb
168
+ ```
169
+
170
+ ---
171
+
172
+ ### 7. `DB_FILES` / `ADMINDB_DB_FILES`
173
+ * **Type:** `string` (Comma-separated paths)
174
+ * **Default:** —
175
+ * **Why/When to use it:** Specify an explicit list of database files located anywhere on disk.
176
+ * **Configuration Examples:**
177
+ ```bash
178
+ DB_FILES="./app.db,/var/data/users.sqlite" npx admindb
179
+ ```
180
+
181
+ ---
182
+
183
+ ### 8. `BASE_PATH` / `ADMINDB_BASE_PATH`
184
+ * **Type:** `string`
185
+ * **Default:** `''` (`/`)
186
+ * **Why/When to use it:** Serve AdminDB under a URL prefix (e.g. `/admin`).
187
+ * **Configuration Examples:**
188
+ ```bash
189
+ BASE_PATH="/admin" npx admindb
190
+ ```
191
+
192
+ ---
193
+
194
+ ### 9. `READONLY` / `ADMINDB_READONLY`
195
+ * **Type:** `boolean` (`1`, `true`, `yes`, `on`)
196
+ * **Default:** `false`
197
+ * **Why/When to use it:** Open databases in **Strict Read-Only Mode**. Disables all insert, update, delete, and DDL operations.
198
+ * **Configuration Examples:**
199
+ ```bash
200
+ READONLY=true npx admindb
201
+ ```
202
+
203
+ ---
204
+
205
+ ### 10. `SERVERLESS` / `ADMINDB_SERVERLESS`
206
+ * **Type:** `boolean` (`1`, `true`, `yes`, `on`)
207
+ * **Default:** `false` *(automatically detected on Vercel, AWS Lambda, Cloudflare Pages, Netlify, GCP Cloud Functions)*
208
+ * **Why/When to use it:** Enforce read-only safety for local SQLite databases while keeping remote PostgreSQL connections fully writable.
209
+ * **Configuration Examples:**
210
+ ```bash
211
+ SERVERLESS=true npx admindb
212
+ ```
213
+
214
+ ---
215
+
216
+ ### 11. `ADMINDB_AUTH` / `ADMINDB_NO_AUTH` / `ADMINDB_DISABLE_AUTH`
217
+ * **Type:** `boolean`
218
+ * **Default:** `true` (auth enabled)
219
+ * **Why/When to use it:** Enable or disable built-in native authentication.
220
+ * **Configuration Examples:**
221
+ ```bash
222
+ ADMINDB_AUTH=false npx admindb
223
+ ```
224
+
225
+ ---
226
+
227
+ ### 12. `ADMINDB_USERNAME` / `ADMINDB_USER`
228
+ * **Type:** `string`
229
+ * **Default:** `admin`
230
+ * **Why/When to use it:** Change the administrator username.
231
+ * **Configuration Examples:**
232
+ ```bash
233
+ ADMINDB_USERNAME=superadmin npx admindb
234
+ ```
235
+
236
+ ---
237
+
238
+ ### 13. `ADMINDB_PASSWORD` / `ADMINDB_PASS`
239
+ * **Type:** `string` (Plain text or salted cryptographic `scrypt` hash)
240
+ * **Default:** `admin` *(hash)*
241
+ * **Why/When to use it:** Secure your instance with a custom password or scrypt hash.
242
+ * **Generating a Hash:**
243
+ ```bash
244
+ npm run generatehash
245
+ ```
246
+ * **Configuration Examples:**
247
+ ```bash
248
+ ADMINDB_PASSWORD="scrypt:3f8e02d9a1c4b7e8...:cb3032b16f29c8d44f75..." npx admindb
249
+ ```
250
+
251
+ ---
252
+
253
+ ### 14. `ADMINDB_SECRET` / `SESSION_SECRET`
254
+ * **Type:** `string`
255
+ * **Default:** Auto-generated randomly per process instance
256
+ * **Why/When to use it:** Fixed secret key used to sign HTTP session cookies.
257
+ * **Configuration Examples:**
258
+ ```bash
259
+ ADMINDB_SECRET="a7f8e92c4b1d6e3f8a0b5c7d9e1f2a3b4c5d6e7f8" npx admindb
260
+ ```
261
+
262
+ ---
263
+
264
+ ### 15. `LOG_LEVEL` / `ADMINDB_LOG_LEVEL`
265
+ * **Type:** `enum`: `debug` | `info` | `warn` | `error`
266
+ * **Default:** `info`
267
+ * **Why/When to use it:** Control log output verbosity in the terminal.
268
+ * **Configuration Examples:**
269
+ ```bash
270
+ LOG_LEVEL=warn npx admindb
271
+ ```
272
+
273
+ ---
274
+
275
+ # 💻 Part 2: Programmatic Code Examples (Express & TypeScript)
276
+
277
+ AdminDB can be mounted directly into any existing Express application as a sub-router on the same port.
278
+
279
+ > **💡 Note on ESM vs CJS:**
280
+ > AdminDB natively supports both **ES Modules** (`import { createRouter } from 'admindb'`) and **CommonJS** (`const { createRouter } = require('admindb')`). The examples below use TypeScript/ESM syntax, but they work identically in pure Node.js CommonJS.
281
+
282
+ ---
283
+
284
+ ### `createRouter(options)` Reference
285
+
286
+ | Option | Type | Default | Description |
287
+ | :--- | :--- | :--- | :--- |
288
+ | `connection` | `string` | — | PostgreSQL connection string or database URI. |
289
+ | `dbPath` | `string` | `'admindb.db'` | Path to a single SQLite database file. |
290
+ | `db` | `IDatabase` | — | An already-opened `SqliteDatabase` or `PostgresDatabase` instance. |
291
+ | `manager` | `DbManager` | — | Enables multi-database management across folders, files, and named connections. |
292
+ | `basePath` | `string` | `''` | URL prefix used by templates and static assets (e.g. `/admin`). |
293
+ | `readonly` | `boolean` | `false` | Open databases in strict read-only mode. |
294
+ | `serverless` | `boolean` | `false` | Serverless mode (SQLite read-only, Postgres editable). |
295
+ | `auth` | `boolean \| AuthConfig` | `true` | Configure built-in authentication or pass `false` to disable it. |
296
+ | `logger` | `Logger` | — | Custom logger instance. |
297
+ | `logLevel` | `'debug'\|'info'\|'warn'\|'error'` | `'info'` | Logging verbosity when no custom logger is provided. |
298
+
299
+ ---
300
+
301
+ ### Example 1: Minimal Single SQLite Database Embedding
302
+
303
+ ```ts
304
+ import express from 'express';
305
+ import { createRouter } from 'admindb';
306
+
307
+ const app = express();
308
+
309
+ app.use('/admin', createRouter({
310
+ dbPath: './data/production.db',
311
+ basePath: '/admin',
312
+ }));
313
+
314
+ app.listen(45531, () => {
315
+ console.log('App running on http://localhost:45531 (Admin: http://localhost:45531/admin)');
316
+ });
317
+ ```
318
+
319
+ ---
320
+
321
+ ### Example 2: Single PostgreSQL Database Embedding
322
+
323
+ ```ts
324
+ import express from 'express';
325
+ import { createRouter } from 'admindb';
326
+
327
+ const app = express();
328
+
329
+ app.use('/admin', createRouter({
330
+ connection: process.env.DATABASE_URL || 'postgresql://postgres:secret@localhost:5432/mydb',
331
+ basePath: '/admin',
332
+ }));
333
+
334
+ app.listen(45531, () => {
335
+ console.log('AdminDB running at http://localhost:45531/admin');
336
+ });
337
+ ```
338
+
339
+ ---
340
+
341
+ ### Example 3: Multiple PostgreSQL Connections (`DbManager`)
342
+
343
+ Manage multiple PostgreSQL databases under a single AdminDB manager interface:
344
+
345
+ ```ts
346
+ import express from 'express';
347
+ import { createRouter, DbManager } from 'admindb';
348
+
349
+ const app = express();
350
+
351
+ // Initialize manager with multiple named PostgreSQL connections
352
+ const manager = new DbManager({
353
+ connections: {
354
+ primary_db: process.env.PRIMARY_DB_URL || 'postgresql://postgres:secret@db1.internal:5432/primary_app',
355
+ analytics_db: process.env.ANALYTICS_DB_URL || 'postgresql://postgres:secret@db2.internal:5432/analytics',
356
+ users_shard: 'postgresql://postgres:secret@db3.internal:5432/users_db',
357
+ },
358
+ });
359
+
360
+ // Optionally register another PostgreSQL connection dynamically at runtime
361
+ // (e.g. read-only replica)
362
+ manager.addConnection('reporting_replica', 'postgresql://postgres:secret@replica.internal:5432/reporting_db', /* readonly */ true);
363
+
364
+ // Mount AdminDB manager router
365
+ app.use('/admin', createRouter({
366
+ manager,
367
+ basePath: '/admin',
368
+ }));
369
+
370
+ app.listen(45531, () => {
371
+ console.log('Multi-PostgreSQL Admin running on http://localhost:45531/admin');
372
+ });
373
+ ```
374
+
375
+ ---
376
+
377
+ ### Example 4: Mixed Multi-Database Manager (Postgres & SQLite)
378
+
379
+ ```ts
380
+ import express from 'express';
381
+ import { createRouter, DbManager } from 'admindb';
382
+
383
+ const app = express();
384
+
385
+ const manager = new DbManager({
386
+ dir: './data/databases',
387
+ connections: {
388
+ prod_pg: 'postgresql://postgres:secret@db.internal:5432/prod_db',
389
+ analytics_pg: 'postgresql://postgres:secret@analytics.internal:5432/warehouse',
390
+ },
391
+ files: ['./legacy/archive.sqlite'],
392
+ });
393
+
394
+ app.use('/admin', createRouter({
395
+ manager,
396
+ basePath: '/admin',
397
+ }));
398
+
399
+ app.listen(45531);
400
+ ```
401
+
402
+ ---
403
+
404
+ ### Example 5: Custom Authentication with Salted scrypt Hash
405
+
406
+ ```ts
407
+ import express from 'express';
408
+ import { createRouter } from 'admindb';
409
+
410
+ const app = express();
411
+
412
+ app.use('/admin', createRouter({
413
+ dbPath: './data/app.db',
414
+ basePath: '/admin',
415
+ auth: {
416
+ enabled: true,
417
+ username: 'ops_lead',
418
+ password: 'scrypt:8011bcda719a32dd307cbcbad899e963:854657965139a68b14ffb30f6a64a18d8b10197046e720dbf6e2e26fee2c5dcc2b6a8b8aed514c1a2be7d73279481bf780c4cf381b68eea40d4e2a7b58dd24f8',
419
+ secret: 'my-persistent-session-secret-key-12345',
420
+ },
421
+ }));
422
+
423
+ app.listen(45531);
424
+ ```
425
+
426
+ ---
427
+
428
+ ### Example 6: Disabling Built-in Auth to Use Custom Express Middleware
429
+
430
+
431
+ ```ts
432
+ import express from 'express';
433
+ import { createRouter } from 'admindb';
434
+
435
+ const app = express();
436
+
437
+ // Custom organization authentication middleware
438
+ function requireCompanySso(req: express.Request, res: express.Response, next: express.NextFunction) {
439
+ if (req.headers['x-sso-user']) return next();
440
+ res.status(401).send('SSO Authentication Required');
441
+ }
442
+
443
+ app.use('/admin', requireCompanySso, createRouter({
444
+ dbPath: './data/app.db',
445
+ basePath: '/admin',
446
+ auth: false, // Turn off built-in login form
447
+ }));
448
+
449
+ app.listen(45531);
450
+ ```
451
+
452
+ ---
453
+
454
+ # 🚀 Part 3: Infrastructure & Deployment Recipes
455
+
456
+ ---
457
+
458
+ ### Recipe 1: Secure Credentials JSON File
459
+
460
+ Store your database connections in a JSON file without exposing secrets in your terminal:
461
+
462
+ #### `name.postgres.json`:
463
+ ```json
464
+ {
465
+ "production": "postgresql://postgres:secret@10.0.0.5:5432/prod_db",
466
+ "analytics": "postgresql://postgres:secret@10.0.0.6:5432/analytics_db",
467
+ "local": "./data/local.db"
468
+ }
469
+ ```
470
+
471
+ Run:
472
+ ```bash
473
+ npx admindb name.postgres.json
474
+ ```
475
+
476
+ ---
477
+
478
+ ### Recipe 2: Direct PostgreSQL CLI Connection
479
+
480
+ ```bash
481
+ npx admindb postgresql://postgres:password@localhost:5432/my_database
482
+ ```
483
+
484
+ Or using environment variable:
485
+ ```bash
486
+ DATABASE_URL="postgresql://postgres:password@localhost:5432/my_database" npx admindb
487
+ ```
488
+
489
+ ---
490
+
491
+ ### Recipe 3: Multi-Database Folder (Read-Only Auditor)
492
+
493
+ ```bash
494
+ admindb -d ./company_dbs --readonly -p 8080 -u auditor -P "secure_pass_123"
495
+ ```
496
+
497
+ ---
498
+
499
+ ### Recipe 4: Production Docker & Docker Compose Deployment
500
+
501
+ #### `Dockerfile`
502
+ ```dockerfile
503
+ FROM node:20-alpine
504
+ WORKDIR /app
505
+ RUN npm install -g admindb
506
+ EXPOSE 45531
507
+ CMD ["admindb"]
508
+ ```
509
+
510
+ #### `docker-compose.yml`
511
+ ```yaml
512
+ version: '3.8'
513
+
514
+ services:
515
+ admindb:
516
+ image: node:20-alpine
517
+ command: npx admindb
518
+ restart: unless-stopped
519
+ ports:
520
+ - "45531:45531"
521
+ environment:
522
+ - HOST=0.0.0.0
523
+ - PORT=45531
524
+ - DATABASE_URL=postgresql://postgres:secret@db:5432/mydb
525
+ - BASE_PATH=/admin
526
+ - ADMINDB_USERNAME=admin_ops
527
+ - ADMINDB_PASSWORD=scrypt:8011bcda719a32dd307cbcbad899e963:854657965139a68b14ffb30f6a64a18d8b10197046e720dbf6e2e26fee2c5dcc2b6a8b8aed514c1a2be7d73279481bf780c4cf381b68eea40d4e2a7b58dd24f8
528
+ - ADMINDB_SECRET=c28f93e481b0a6e7d95c1a3f5b7e9d2a
529
+ - LOG_LEVEL=info
530
+ ```
531
+
532
+ ---
533
+
534
+ ### Recipe 5: Nginx Reverse Proxy with Subpath Routing
535
+
536
+ ```nginx
537
+ server {
538
+ listen 80;
539
+ server_name db.example.com;
540
+
541
+ location /admin/ {
542
+ proxy_pass http://127.0.0.1:45531/admin/;
543
+ proxy_set_header Host $host;
544
+ proxy_set_header X-Real-IP $remote_addr;
545
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
546
+ proxy_set_header X-Forwarded-Proto $scheme;
547
+ }
548
+ }
549
+ ```
550
+
551
+ Command:
552
+ ```bash
553
+ BASE_PATH=/admin HOST=127.0.0.1 PORT=45531 DB_DIR=/var/databases admindb
554
+ ```
555
+
556
+ ---
557
+
558
+ ### Recipe 6: Serverless Deployment (Vercel & AWS Lambda)
559
+
560
+ In serverless mode, local SQLite databases default to read-only safety, while remote PostgreSQL connections are fully editable.
561
+
562
+ #### 1. Vercel API Route (`api/index.ts`):
563
+ ```ts
564
+ import { createServerlessHandler } from 'admindb';
565
+
566
+ export default createServerlessHandler({
567
+ connection: process.env.DATABASE_URL,
568
+ basePath: '/admin',
569
+ });
570
+ ```
571
+
572
+ #### 2. AWS Lambda with API Gateway (`index.ts`):
573
+ ```ts
574
+ import { createLambdaHandler } from 'admindb';
575
+
576
+ export const handler = createLambdaHandler({
577
+ connection: process.env.DATABASE_URL,
578
+ });
579
+ ```