@riligar/elysia-sqlite 1.2.2 → 1.4.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 +29 -6
  2. package/package.json +2 -1
  3. package/src/index.js +13 -13
package/README.md CHANGED
@@ -40,16 +40,16 @@ const app = new Elysia()
40
40
  .use(
41
41
  sqliteAdmin({
42
42
  dbPath: 'demo.db',
43
- prefix: '/admin', // Optional: defaults to /admin
43
+ prefix: '/database', // Optional: defaults to /database
44
44
  })
45
45
  )
46
46
  .listen(3000)
47
47
 
48
48
  console.log('🦊 Server running at http://localhost:3000')
49
- console.log('📊 Admin Dashboard at http://localhost:3000/admin')
49
+ console.log('📊 Admin Dashboard at http://localhost:3000/database')
50
50
  ```
51
51
 
52
- On first run, navigate to `/admin` (or your configured prefix) to start the onboarding wizard and configure your admin credentials.
52
+ On first run, navigate to `/database` (or your configured prefix) to start the onboarding wizard and configure your admin credentials.
53
53
 
54
54
  ## ⚙️ Configuration
55
55
 
@@ -60,8 +60,8 @@ These options are passed to the `sqliteAdmin` plugin at initialization.
60
60
  | Option | Type | Default | Description |
61
61
  | ------------ | ------ | ----------------------------- | --------------------------------------------------------------- |
62
62
  | `dbPath` | string | **Required** | Path to the SQLite database file |
63
- | `prefix` | string | `"/admin"` | URL prefix for the admin dashboard and API |
64
- | `configPath` | string | `"./sqlite-admin-config.json"`| Path to save the runtime authentication config (JSON) |
63
+ | `prefix` | string | `"/database"` | URL prefix for the admin dashboard and API |
64
+ | `configPath` | string | Same directory as `dbPath` | Path to save the runtime authentication config (JSON). Defaults to `sqlite-admin-config.json` in the same directory as your database file |
65
65
 
66
66
  ### Runtime Configuration (via UI)
67
67
 
@@ -75,9 +75,32 @@ The following settings are managed via the **Settings** tab in the dashboard and
75
75
 
76
76
  > **Note:** The configuration file contains sensitive credentials. Ensure it is included in your `.gitignore` if necessary or secured appropriately in production environments.
77
77
 
78
+ ### Cloud Deployment (Fly.io, Railway, etc.)
79
+
80
+ When deploying to cloud platforms with ephemeral filesystems, ensure your database and config files are stored on a **persistent volume**.
81
+
82
+ **Fly.io Example:**
83
+
84
+ ```toml
85
+ # fly.toml
86
+ [mounts]
87
+ source = "data"
88
+ destination = "/data"
89
+ ```
90
+
91
+ ```javascript
92
+ // Your app
93
+ sqliteAdmin({
94
+ dbPath: '/data/app.db',
95
+ // configPath automatically uses /data/sqlite-admin-config.json
96
+ })
97
+ ```
98
+
99
+ The config file is automatically stored alongside your database, so both will persist across deployments when using the same volume.
100
+
78
101
  ## 🔌 API Endpoints
79
102
 
80
- The plugin adds the following routes under your configured `prefix` (default `/admin`):
103
+ The plugin adds the following routes under your configured `prefix` (default `/database`):
81
104
 
82
105
  ### Authentication
83
106
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riligar/elysia-sqlite",
3
- "version": "1.2.2",
3
+ "version": "1.4.0",
4
4
  "description": "Plugin ElysiaJS para gerenciamento de bancos de dados SQLite",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -51,6 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "@elysiajs/static": "^1.2.0",
54
+ "elysia-rate-limit": "^4.4.2",
54
55
  "otplib": "^12.0.1",
55
56
  "qrcode": "^1.5.4"
56
57
  },
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Elysia } from "elysia";
2
2
  import { Database } from "bun:sqlite";
3
- import { join } from "path";
3
+ import { join, dirname } from "path";
4
4
  import { existsSync, readFileSync, readdirSync } from 'node:fs';
5
5
  import { writeFile } from 'node:fs/promises';
6
6
  import { createSessionManager } from './core/session.js';
@@ -22,13 +22,17 @@ const mimeTypes = {
22
22
  * Plugin de administração SQLite para ElysiaJS
23
23
  * @param {Object} config - Configuração do plugin
24
24
  * @param {string} config.dbPath - Caminho para o arquivo do banco SQLite
25
- * @param {string} config.prefix - Prefixo da rota (ex: '/admin')
26
- * @param {string} config.configPath - Caminho para o arquivo de configuração de auth (ex: './sqlite-admin-config.json')
25
+ * @param {string} config.prefix - Prefixo da rota (ex: '/database')
26
+ * @param {string} config.configPath - Caminho para o arquivo de configuração de auth.
27
+ * Se não especificado, será salvo no mesmo diretório do banco de dados (recomendado para persistência em ambientes cloud como Fly.io)
27
28
  */
28
- export const sqliteAdmin = ({ dbPath, prefix = "/admin", configPath = "./sqlite-admin-config.json" }) => {
29
+ export const sqliteAdmin = ({ dbPath, prefix = "/database", configPath }) => {
29
30
  const db = new Database(dbPath);
30
31
  const uiPath = join(import.meta.dir, "ui", "dist");
31
- console.log("[SQLite Admin] UI Path:", uiPath);
32
+
33
+ // Se configPath não for especificado, deriva do diretório do banco de dados
34
+ // Isso garante que a configuração fique no mesmo volume persistente do banco
35
+ const resolvedConfigPath = configPath || join(dirname(dbPath), "sqlite-admin-config.json");
32
36
 
33
37
  // Gerenciador de Sessão
34
38
  const sessionManager = createSessionManager();
@@ -36,9 +40,9 @@ export const sqliteAdmin = ({ dbPath, prefix = "/admin", configPath = "./sqlite-
36
40
  // Carregar Configuração
37
41
  let config = {};
38
42
  const loadConfig = () => {
39
- if (existsSync(configPath)) {
43
+ if (existsSync(resolvedConfigPath)) {
40
44
  try {
41
- config = JSON.parse(readFileSync(configPath, 'utf-8'));
45
+ config = JSON.parse(readFileSync(resolvedConfigPath, 'utf-8'));
42
46
  } catch (e) {
43
47
  console.error("Failed to load config", e);
44
48
  }
@@ -49,7 +53,7 @@ export const sqliteAdmin = ({ dbPath, prefix = "/admin", configPath = "./sqlite-
49
53
  const saveConfig = async (newConfig) => {
50
54
  config = { ...config, ...newConfig };
51
55
  try {
52
- await writeFile(configPath, JSON.stringify(config, null, 2));
56
+ await writeFile(resolvedConfigPath, JSON.stringify(config, null, 2));
53
57
  return true;
54
58
  } catch (e) {
55
59
  console.error("Failed to save config", e);
@@ -61,9 +65,6 @@ export const sqliteAdmin = ({ dbPath, prefix = "/admin", configPath = "./sqlite-
61
65
 
62
66
  return (
63
67
  new Elysia({ prefix })
64
- .onRequest(({ request }) => {
65
- console.log(`[SQLite Admin] Incoming Request: ${request.url}`);
66
- })
67
68
  // Middleware de Autenticação
68
69
  .derive(({ headers }) => {
69
70
  const cookies = headers.cookie || '';
@@ -245,7 +246,7 @@ export const sqliteAdmin = ({ dbPath, prefix = "/admin", configPath = "./sqlite-
245
246
  config = newConfig; // Local update
246
247
 
247
248
  try {
248
- await writeFile(configPath, JSON.stringify(config, null, 2));
249
+ await writeFile(resolvedConfigPath, JSON.stringify(config, null, 2));
249
250
  return { success: true };
250
251
  } catch(e) {
251
252
  return { success: false, error: "Failed to save" };
@@ -272,7 +273,6 @@ export const sqliteAdmin = ({ dbPath, prefix = "/admin", configPath = "./sqlite-
272
273
  const exists = await file.exists();
273
274
 
274
275
  if (!exists) {
275
- console.log(`[SQLite Admin] Asset not found: ${filePath}`);
276
276
  return new Response("Not found", { status: 404 });
277
277
  }
278
278