admindb 2.0.0 → 2.1.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.
- package/README.md +2 -2
- package/dist/public/js/designer.js +17 -2
- package/dist/public/js/schema.js +17 -2
- package/dist/routes/pages.js +10 -2
- package/dist/routes/pages.js.map +1 -1
- package/dist/sql/generator.d.ts +3 -0
- package/dist/sql/generator.js +41 -1
- package/dist/sql/generator.js.map +1 -1
- package/dist/views/layouts/main.hbs +8 -1
- package/dist/views/pages/designer.hbs +22 -7
- package/docs/API.md +144 -0
- package/docs/EXAMPLES.md +576 -0
- package/docs/SECURITY.md +265 -0
- package/package.json +5 -1
package/docs/EXAMPLES.md
ADDED
|
@@ -0,0 +1,576 @@
|
|
|
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
|
+
---
|
|
280
|
+
|
|
281
|
+
### `createRouter(options)` Reference
|
|
282
|
+
|
|
283
|
+
| Option | Type | Default | Description |
|
|
284
|
+
| :--- | :--- | :--- | :--- |
|
|
285
|
+
| `connection` | `string` | — | PostgreSQL connection string or database URI. |
|
|
286
|
+
| `dbPath` | `string` | `'admindb.db'` | Path to a single SQLite database file. |
|
|
287
|
+
| `db` | `IDatabase` | — | An already-opened `SqliteDatabase` or `PostgresDatabase` instance. |
|
|
288
|
+
| `manager` | `DbManager` | — | Enables multi-database management across folders, files, and named connections. |
|
|
289
|
+
| `basePath` | `string` | `''` | URL prefix used by templates and static assets (e.g. `/admin`). |
|
|
290
|
+
| `readonly` | `boolean` | `false` | Open databases in strict read-only mode. |
|
|
291
|
+
| `serverless` | `boolean` | `false` | Serverless mode (SQLite read-only, Postgres editable). |
|
|
292
|
+
| `auth` | `boolean \| AuthConfig` | `true` | Configure built-in authentication or pass `false` to disable it. |
|
|
293
|
+
| `logger` | `Logger` | — | Custom logger instance. |
|
|
294
|
+
| `logLevel` | `'debug'\|'info'\|'warn'\|'error'` | `'info'` | Logging verbosity when no custom logger is provided. |
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
### Example 1: Minimal Single SQLite Database Embedding
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
import express from 'express';
|
|
302
|
+
import { createRouter } from 'admindb';
|
|
303
|
+
|
|
304
|
+
const app = express();
|
|
305
|
+
|
|
306
|
+
app.use('/admin', createRouter({
|
|
307
|
+
dbPath: './data/production.db',
|
|
308
|
+
basePath: '/admin',
|
|
309
|
+
}));
|
|
310
|
+
|
|
311
|
+
app.listen(45531, () => {
|
|
312
|
+
console.log('App running on http://localhost:45531 (Admin: http://localhost:45531/admin)');
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
### Example 2: Single PostgreSQL Database Embedding
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
import express from 'express';
|
|
322
|
+
import { createRouter } from 'admindb';
|
|
323
|
+
|
|
324
|
+
const app = express();
|
|
325
|
+
|
|
326
|
+
app.use('/admin', createRouter({
|
|
327
|
+
connection: process.env.DATABASE_URL || 'postgresql://postgres:secret@localhost:5432/mydb',
|
|
328
|
+
basePath: '/admin',
|
|
329
|
+
}));
|
|
330
|
+
|
|
331
|
+
app.listen(45531, () => {
|
|
332
|
+
console.log('AdminDB running at http://localhost:45531/admin');
|
|
333
|
+
});
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
### Example 3: Multiple PostgreSQL Connections (`DbManager`)
|
|
339
|
+
|
|
340
|
+
Manage multiple PostgreSQL databases under a single AdminDB manager interface:
|
|
341
|
+
|
|
342
|
+
```ts
|
|
343
|
+
import express from 'express';
|
|
344
|
+
import { createRouter, DbManager } from 'admindb';
|
|
345
|
+
|
|
346
|
+
const app = express();
|
|
347
|
+
|
|
348
|
+
// Initialize manager with multiple named PostgreSQL connections
|
|
349
|
+
const manager = new DbManager({
|
|
350
|
+
connections: {
|
|
351
|
+
primary_db: process.env.PRIMARY_DB_URL || 'postgresql://postgres:secret@db1.internal:5432/primary_app',
|
|
352
|
+
analytics_db: process.env.ANALYTICS_DB_URL || 'postgresql://postgres:secret@db2.internal:5432/analytics',
|
|
353
|
+
users_shard: 'postgresql://postgres:secret@db3.internal:5432/users_db',
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// Optionally register another PostgreSQL connection dynamically at runtime
|
|
358
|
+
// (e.g. read-only replica)
|
|
359
|
+
manager.addConnection('reporting_replica', 'postgresql://postgres:secret@replica.internal:5432/reporting_db', /* readonly */ true);
|
|
360
|
+
|
|
361
|
+
// Mount AdminDB manager router
|
|
362
|
+
app.use('/admin', createRouter({
|
|
363
|
+
manager,
|
|
364
|
+
basePath: '/admin',
|
|
365
|
+
}));
|
|
366
|
+
|
|
367
|
+
app.listen(45531, () => {
|
|
368
|
+
console.log('Multi-PostgreSQL Admin running on http://localhost:45531/admin');
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
### Example 4: Mixed Multi-Database Manager (Postgres & SQLite)
|
|
375
|
+
|
|
376
|
+
```ts
|
|
377
|
+
import express from 'express';
|
|
378
|
+
import { createRouter, DbManager } from 'admindb';
|
|
379
|
+
|
|
380
|
+
const app = express();
|
|
381
|
+
|
|
382
|
+
const manager = new DbManager({
|
|
383
|
+
dir: './data/databases',
|
|
384
|
+
connections: {
|
|
385
|
+
prod_pg: 'postgresql://postgres:secret@db.internal:5432/prod_db',
|
|
386
|
+
analytics_pg: 'postgresql://postgres:secret@analytics.internal:5432/warehouse',
|
|
387
|
+
},
|
|
388
|
+
files: ['./legacy/archive.sqlite'],
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
app.use('/admin', createRouter({
|
|
392
|
+
manager,
|
|
393
|
+
basePath: '/admin',
|
|
394
|
+
}));
|
|
395
|
+
|
|
396
|
+
app.listen(45531);
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
---
|
|
400
|
+
|
|
401
|
+
### Example 5: Custom Authentication with Salted scrypt Hash
|
|
402
|
+
|
|
403
|
+
```ts
|
|
404
|
+
import express from 'express';
|
|
405
|
+
import { createRouter } from 'admindb';
|
|
406
|
+
|
|
407
|
+
const app = express();
|
|
408
|
+
|
|
409
|
+
app.use('/admin', createRouter({
|
|
410
|
+
dbPath: './data/app.db',
|
|
411
|
+
basePath: '/admin',
|
|
412
|
+
auth: {
|
|
413
|
+
enabled: true,
|
|
414
|
+
username: 'ops_lead',
|
|
415
|
+
password: 'scrypt:8011bcda719a32dd307cbcbad899e963:854657965139a68b14ffb30f6a64a18d8b10197046e720dbf6e2e26fee2c5dcc2b6a8b8aed514c1a2be7d73279481bf780c4cf381b68eea40d4e2a7b58dd24f8',
|
|
416
|
+
secret: 'my-persistent-session-secret-key-12345',
|
|
417
|
+
},
|
|
418
|
+
}));
|
|
419
|
+
|
|
420
|
+
app.listen(45531);
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
---
|
|
424
|
+
|
|
425
|
+
### Example 6: Disabling Built-in Auth to Use Custom Express Middleware
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
import express from 'express';
|
|
430
|
+
import { createRouter } from 'admindb';
|
|
431
|
+
|
|
432
|
+
const app = express();
|
|
433
|
+
|
|
434
|
+
// Custom organization authentication middleware
|
|
435
|
+
function requireCompanySso(req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
436
|
+
if (req.headers['x-sso-user']) return next();
|
|
437
|
+
res.status(401).send('SSO Authentication Required');
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
app.use('/admin', requireCompanySso, createRouter({
|
|
441
|
+
dbPath: './data/app.db',
|
|
442
|
+
basePath: '/admin',
|
|
443
|
+
auth: false, // Turn off built-in login form
|
|
444
|
+
}));
|
|
445
|
+
|
|
446
|
+
app.listen(45531);
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
---
|
|
450
|
+
|
|
451
|
+
# 🚀 Part 3: Infrastructure & Deployment Recipes
|
|
452
|
+
|
|
453
|
+
---
|
|
454
|
+
|
|
455
|
+
### Recipe 1: Secure Credentials JSON File
|
|
456
|
+
|
|
457
|
+
Store your database connections in a JSON file without exposing secrets in your terminal:
|
|
458
|
+
|
|
459
|
+
#### `name.postgres.json`:
|
|
460
|
+
```json
|
|
461
|
+
{
|
|
462
|
+
"production": "postgresql://postgres:secret@10.0.0.5:5432/prod_db",
|
|
463
|
+
"analytics": "postgresql://postgres:secret@10.0.0.6:5432/analytics_db",
|
|
464
|
+
"local": "./data/local.db"
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
Run:
|
|
469
|
+
```bash
|
|
470
|
+
npx admindb name.postgres.json
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
---
|
|
474
|
+
|
|
475
|
+
### Recipe 2: Direct PostgreSQL CLI Connection
|
|
476
|
+
|
|
477
|
+
```bash
|
|
478
|
+
npx admindb postgresql://postgres:password@localhost:5432/my_database
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
Or using environment variable:
|
|
482
|
+
```bash
|
|
483
|
+
DATABASE_URL="postgresql://postgres:password@localhost:5432/my_database" npx admindb
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
---
|
|
487
|
+
|
|
488
|
+
### Recipe 3: Multi-Database Folder (Read-Only Auditor)
|
|
489
|
+
|
|
490
|
+
```bash
|
|
491
|
+
admindb -d ./company_dbs --readonly -p 8080 -u auditor -P "secure_pass_123"
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
---
|
|
495
|
+
|
|
496
|
+
### Recipe 4: Production Docker & Docker Compose Deployment
|
|
497
|
+
|
|
498
|
+
#### `Dockerfile`
|
|
499
|
+
```dockerfile
|
|
500
|
+
FROM node:20-alpine
|
|
501
|
+
WORKDIR /app
|
|
502
|
+
RUN npm install -g admindb
|
|
503
|
+
EXPOSE 45531
|
|
504
|
+
CMD ["admindb"]
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
#### `docker-compose.yml`
|
|
508
|
+
```yaml
|
|
509
|
+
version: '3.8'
|
|
510
|
+
|
|
511
|
+
services:
|
|
512
|
+
admindb:
|
|
513
|
+
image: node:20-alpine
|
|
514
|
+
command: npx admindb
|
|
515
|
+
restart: unless-stopped
|
|
516
|
+
ports:
|
|
517
|
+
- "45531:45531"
|
|
518
|
+
environment:
|
|
519
|
+
- HOST=0.0.0.0
|
|
520
|
+
- PORT=45531
|
|
521
|
+
- DATABASE_URL=postgresql://postgres:secret@db:5432/mydb
|
|
522
|
+
- BASE_PATH=/admin
|
|
523
|
+
- ADMINDB_USERNAME=admin_ops
|
|
524
|
+
- ADMINDB_PASSWORD=scrypt:8011bcda719a32dd307cbcbad899e963:854657965139a68b14ffb30f6a64a18d8b10197046e720dbf6e2e26fee2c5dcc2b6a8b8aed514c1a2be7d73279481bf780c4cf381b68eea40d4e2a7b58dd24f8
|
|
525
|
+
- ADMINDB_SECRET=c28f93e481b0a6e7d95c1a3f5b7e9d2a
|
|
526
|
+
- LOG_LEVEL=info
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
---
|
|
530
|
+
|
|
531
|
+
### Recipe 5: Nginx Reverse Proxy with Subpath Routing
|
|
532
|
+
|
|
533
|
+
```nginx
|
|
534
|
+
server {
|
|
535
|
+
listen 80;
|
|
536
|
+
server_name db.example.com;
|
|
537
|
+
|
|
538
|
+
location /admin/ {
|
|
539
|
+
proxy_pass http://127.0.0.1:45531/admin/;
|
|
540
|
+
proxy_set_header Host $host;
|
|
541
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
542
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
543
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
Command:
|
|
549
|
+
```bash
|
|
550
|
+
BASE_PATH=/admin HOST=127.0.0.1 PORT=45531 DB_DIR=/var/databases admindb
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
---
|
|
554
|
+
|
|
555
|
+
### Recipe 6: Serverless Deployment (Vercel & AWS Lambda)
|
|
556
|
+
|
|
557
|
+
In serverless mode, local SQLite databases default to read-only safety, while remote PostgreSQL connections are fully editable.
|
|
558
|
+
|
|
559
|
+
#### 1. Vercel API Route (`api/index.ts`):
|
|
560
|
+
```ts
|
|
561
|
+
import { createServerlessHandler } from 'admindb';
|
|
562
|
+
|
|
563
|
+
export default createServerlessHandler({
|
|
564
|
+
connection: process.env.DATABASE_URL,
|
|
565
|
+
basePath: '/admin',
|
|
566
|
+
});
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
#### 2. AWS Lambda with API Gateway (`index.ts`):
|
|
570
|
+
```ts
|
|
571
|
+
import { createLambdaHandler } from 'admindb';
|
|
572
|
+
|
|
573
|
+
export const handler = createLambdaHandler({
|
|
574
|
+
connection: process.env.DATABASE_URL,
|
|
575
|
+
});
|
|
576
|
+
```
|