scdb-web 1.0.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 (3) hide show
  1. package/README.md +60 -0
  2. package/index.js +60 -0
  3. package/package.json +13 -0
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Secure DB
2
+
3
+ A secure SQLite-based database with a web panel, terminal CLI, and Node.js client. Uses **sql.js** (no native build). Features authentication, RBAC, and audit logging.
4
+
5
+ ## Structure
6
+
7
+ - **packages/core** – sql.js DB, parameterized queries, auth, audit
8
+ - **packages/api** – HTTP API (exportable `createApp`) + serves web panel
9
+ - **packages/web** – One-line embed: `import { init } from 'scdb-web'; init(env);`
10
+ - **packages/lib** – Node.js client (`createClient`, `query`, `execute`)
11
+ - **packages/web-panel** – Panel assets (HTML/CSS/JS) served by API
12
+ - **packages/cli** – Terminal CLI (interactive + one-shot)
13
+
14
+ ## Setup
15
+
16
+ 1. Copy `.env.example` to `.env` and set `DATABASE_PATH` and `SESSION_SECRET`.
17
+ 2. Run `npm install` in the project root.
18
+ 3. Start the server: `npm start` (API + panel at http://localhost:3000).
19
+ 4. First time: open http://localhost:3000 and use **First-time setup** to create an admin user, then log in.
20
+ 5. Create an API key (for CLI/lib): as admin, call `POST /api-keys` with body `{ "name": "cli", "role": "readwrite" }`. Use the returned `key` in `SECURE_DB_API_KEY`.
21
+
22
+ ## Usage
23
+
24
+ - **Web panel**: Open http://localhost:3000 — Tables, SQL editor, Audit log (admin).
25
+ - **CLI**: `SECURE_DB_API_KEY=yourkey npm run cli` for interactive mode; `npm run cli -- run "SELECT 1"` or `npm run cli -- run -f query.sql` for one-shot.
26
+ - **Node.js**: `import { createClient } from 'scdb-lib'; const client = createClient({ url: 'http://localhost:3000', apiKey: '...' }); const rows = await client.query('SELECT * FROM users', []);`
27
+
28
+ ## Integrating in other Node.js projects
29
+
30
+ - **Web (API + panel)**
31
+ Install and start the server with one call:
32
+ ```bash
33
+ npm install scdb-web
34
+ ```
35
+ ```js
36
+ import { init } from 'scdb-web';
37
+ init({ port: 3000, databasePath: './data/app.sqlite', sessionSecret: 'your-secret' });
38
+ ```
39
+ Optional `env` keys: `port`, `host`, `databasePath`, `sessionSecret`, `docsDir`.
40
+
41
+ - **CLI**
42
+ Install and run with env (or add to your `package.json` scripts):
43
+ ```bash
44
+ npm install scdb-cli
45
+ SECURE_DB_API_KEY=xxx SECURE_DB_API_URL=http://localhost:3000 npx secure-db
46
+ SECURE_DB_API_KEY=xxx npx secure-db run "SELECT 1"
47
+ SECURE_DB_API_KEY=xxx npx secure-db run -f script.sql
48
+ ```
49
+
50
+ ## Security
51
+
52
+ - Auth: API keys and/or username/password; passwords hashed with bcrypt.
53
+ - RBAC: admin, readwrite, readonly.
54
+ - All queries are parameterized; audit log records who ran what and when.
55
+ - Use HTTPS in production.
56
+
57
+ ## Documentation
58
+
59
+ - **Admin guide**: operational steps, creating users and API keys, using the web panel, backups, and security notes – see `docs/ADMIN_GUIDE.md`.
60
+ - **User/developer guide**: how to use the CLI and Node.js client, and how roles affect what you can do – see `docs/USER_GUIDE.md`.
package/index.js ADDED
@@ -0,0 +1,60 @@
1
+ import path from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ import { createRequire } from 'module';
4
+ import { createApp } from 'scdb-api';
5
+
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const require = createRequire(import.meta.url);
8
+
9
+ /**
10
+ * Resolve the directory containing the web panel static assets (from scdb-web-panel).
11
+ * Works when this package is installed in another project (hoisted or nested).
12
+ */
13
+ function getStaticDir() {
14
+ try {
15
+ const pkgPath = require.resolve('scdb-web-panel/package.json');
16
+ return path.join(path.dirname(pkgPath), 'public');
17
+ } catch {
18
+ return path.join(__dirname, 'node_modules', 'scdb-web-panel', 'public');
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Start the Secure DB server (API + web panel). Returns the HTTP server.
24
+ * @param {{
25
+ * port?: number;
26
+ * host?: string;
27
+ * databasePath?: string;
28
+ * sessionSecret?: string;
29
+ * docsDir?: string | null;
30
+ * }} env - Optional. port (default 3000), host (default '0.0.0.0'), databasePath, sessionSecret, docsDir.
31
+ * @returns {import('http').Server} The listening HTTP server.
32
+ */
33
+ export function init(env = {}) {
34
+ const port = env.port ?? Number(process.env.PORT) ?? 3000;
35
+ const host = env.host ?? process.env.HOST ?? '0.0.0.0';
36
+ const staticDir = getStaticDir();
37
+ const app = createApp({
38
+ databasePath: env.databasePath,
39
+ sessionSecret: env.sessionSecret,
40
+ staticDir,
41
+ docsDir: env.docsDir,
42
+ port: port,
43
+ });
44
+ const server = app.listen(port, host, () => {
45
+ console.log(`Secure DB listening on http://${host}:${port}`);
46
+ });
47
+ return server;
48
+ }
49
+
50
+ const isMain =
51
+ process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url));
52
+ if (isMain) {
53
+ init({
54
+ port: process.env.PORT ? Number(process.env.PORT) : undefined,
55
+ host: process.env.HOST,
56
+ databasePath: process.env.DATABASE_PATH,
57
+ sessionSecret: process.env.SESSION_SECRET,
58
+ docsDir: process.env.DOCS_DIR ?? null,
59
+ });
60
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "scdb-web",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "type": "module",
6
+ "scripts": {
7
+ "start": "node index.js"
8
+ },
9
+ "dependencies": {
10
+ "scdb-api": "1.0.0",
11
+ "scdb-web-panel": "1.0.0"
12
+ }
13
+ }