sqlite-hub 0.2.0 → 0.3.2

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/server/server.js CHANGED
@@ -7,6 +7,7 @@ const { ConnectionManager } = require("./services/sqlite/connectionManager");
7
7
  const { OverviewService } = require("./services/sqlite/overviewService");
8
8
  const { SqlExecutor } = require("./services/sqlite/sqlExecutor");
9
9
  const { ImportService } = require("./services/sqlite/importService");
10
+ const { BackupService } = require("./services/sqlite/backupService");
10
11
  const { ExportService } = require("./services/sqlite/exportService");
11
12
  const { StructureService } = require("./services/sqlite/structureService");
12
13
  const { DataBrowserService } = require("./services/sqlite/dataBrowserService");
@@ -34,8 +35,10 @@ const connectionManager = new ConnectionManager({ appStateStore });
34
35
  const overviewService = new OverviewService({ connectionManager });
35
36
  const sqlExecutor = new SqlExecutor({ connectionManager, appStateStore });
36
37
  const importService = new ImportService({ connectionManager });
38
+ const backupService = new BackupService({ connectionManager });
37
39
  const exportService = new ExportService({
38
40
  appStateStore,
41
+ connectionManager,
39
42
  sqlExecutor,
40
43
  });
41
44
  const structureService = new StructureService({ connectionManager, appStateStore });
@@ -65,6 +68,7 @@ app.use(
65
68
  createConnectionsRouter({
66
69
  connectionManager,
67
70
  importService,
71
+ backupService,
68
72
  })
69
73
  );
70
74
  app.use("/api/db", createOverviewRouter({ overviewService }));
@@ -86,12 +90,44 @@ app.get("/index.html", (req, res) => {
86
90
  res.sendFile(path.resolve(__dirname, "..", "index.html"));
87
91
  });
88
92
 
93
+ app.use(
94
+ "/vendor/cytoscape",
95
+ express.static(path.resolve(__dirname, "..", "node_modules", "cytoscape"))
96
+ );
97
+ app.use(
98
+ "/vendor/cytoscape-elk",
99
+ express.static(path.resolve(__dirname, "..", "node_modules", "cytoscape-elk"))
100
+ );
101
+ app.use(
102
+ "/vendor/elkjs",
103
+ express.static(path.resolve(__dirname, "..", "node_modules", "elkjs"))
104
+ );
89
105
  app.use("/js", express.static(path.resolve(__dirname, "..", "js")));
90
106
  app.use("/styles", express.static(path.resolve(__dirname, "..", "styles")));
91
107
  app.use("/assets", express.static(path.resolve(__dirname, "..", "assets")));
92
108
  app.use(errorMiddleware);
93
109
 
94
- function resolvePort(value = process.env.PORT) {
110
+ function parsePortArgument(argv = process.argv.slice(2)) {
111
+ for (let index = 0; index < argv.length; index += 1) {
112
+ const argument = argv[index];
113
+
114
+ if (argument.startsWith("--port:")) {
115
+ return argument.slice("--port:".length);
116
+ }
117
+
118
+ if (argument.startsWith("--port=")) {
119
+ return argument.slice("--port=".length);
120
+ }
121
+
122
+ if (argument === "--port") {
123
+ return argv[index + 1];
124
+ }
125
+ }
126
+
127
+ return undefined;
128
+ }
129
+
130
+ function resolvePort(value = process.env.PORT ?? parsePortArgument()) {
95
131
  if (value === undefined || value === null || value === "") {
96
132
  return DEFAULT_PORT;
97
133
  }
@@ -137,6 +173,7 @@ module.exports = {
137
173
  appStateStore,
138
174
  connectionManager,
139
175
  DEFAULT_PORT,
176
+ parsePortArgument,
140
177
  resolvePort,
141
178
  startServer,
142
179
  };
@@ -0,0 +1,112 @@
1
+ const fs = require("node:fs");
2
+ const path = require("node:path");
3
+ const { DatabaseRequiredError } = require("../../utils/errors");
4
+ const {
5
+ SQLITE_EXTENSIONS,
6
+ ensureParentDirectory,
7
+ getFileMetadata,
8
+ validateSqlitePath,
9
+ } = require("../../utils/fileValidation");
10
+
11
+ function padTimestampPart(value) {
12
+ return String(value).padStart(2, "0");
13
+ }
14
+
15
+ function formatBackupTimestamp(date) {
16
+ return [
17
+ date.getFullYear(),
18
+ padTimestampPart(date.getMonth() + 1),
19
+ padTimestampPart(date.getDate()),
20
+ ].join("-")
21
+ + "_"
22
+ + [
23
+ padTimestampPart(date.getHours()),
24
+ padTimestampPart(date.getMinutes()),
25
+ padTimestampPart(date.getSeconds()),
26
+ ].join("-");
27
+ }
28
+
29
+ function sanitizeBackupBaseName(value) {
30
+ const normalized = String(value ?? "").trim().replace(/[<>:"/\\|?*\u0000-\u001F]+/g, "_");
31
+ return normalized || "database";
32
+ }
33
+
34
+ function normalizeBackupLabelBase(value, fallback) {
35
+ const normalized = String(value ?? "").trim() || String(fallback ?? "").trim() || "database";
36
+ const extension = path.extname(normalized).toLowerCase();
37
+
38
+ if (SQLITE_EXTENSIONS.has(extension)) {
39
+ return normalized.slice(0, -extension.length) || "database";
40
+ }
41
+
42
+ return normalized;
43
+ }
44
+
45
+ class BackupService {
46
+ constructor({ connectionManager }) {
47
+ this.connectionManager = connectionManager;
48
+ }
49
+
50
+ createActiveBackup() {
51
+ const activeConnection = this.connectionManager.getActiveConnection();
52
+
53
+ if (!activeConnection) {
54
+ throw new DatabaseRequiredError("No active SQLite database selected for backup.");
55
+ }
56
+
57
+ const sourcePath = validateSqlitePath(activeConnection.path, { mustExist: true });
58
+ const { backupPath, connectionLabel } = this.buildBackupPath(sourcePath, activeConnection.label);
59
+
60
+ ensureParentDirectory(backupPath);
61
+ fs.copyFileSync(sourcePath, backupPath, fs.constants.COPYFILE_EXCL);
62
+
63
+ const backupConnection = this.connectionManager.rememberConnection({
64
+ filePath: backupPath,
65
+ label: connectionLabel,
66
+ makeActive: false,
67
+ });
68
+ const metadata = getFileMetadata(backupPath);
69
+
70
+ return {
71
+ sourcePath,
72
+ backupPath,
73
+ directory: path.dirname(backupPath),
74
+ fileName: path.basename(backupPath),
75
+ createdAt: new Date().toISOString(),
76
+ sizeBytes: metadata.sizeBytes,
77
+ connection: backupConnection,
78
+ };
79
+ }
80
+
81
+ buildBackupPath(sourcePath, sourceLabel) {
82
+ const parsedSource = path.parse(sourcePath);
83
+ const backupDirectory = path.join(parsedSource.dir, "backups");
84
+ const extension = parsedSource.ext || ".sqlite";
85
+ const displayBaseName = normalizeBackupLabelBase(sourceLabel, parsedSource.name);
86
+ const fileBaseName = sanitizeBackupBaseName(displayBaseName);
87
+ const timestamp = formatBackupTimestamp(new Date());
88
+ let attempt = 1;
89
+
90
+ while (true) {
91
+ const suffix = attempt === 1 ? "" : `_${String(attempt).padStart(2, "0")}`;
92
+ const fileStem = `${fileBaseName}_${timestamp}${suffix}`;
93
+ const candidate = path.join(
94
+ backupDirectory,
95
+ `${fileStem}${extension}`
96
+ );
97
+
98
+ if (!fs.existsSync(candidate)) {
99
+ return {
100
+ backupPath: candidate,
101
+ connectionLabel: `${displayBaseName}_${timestamp}${suffix}`,
102
+ };
103
+ }
104
+
105
+ attempt += 1;
106
+ }
107
+ }
108
+ }
109
+
110
+ module.exports = {
111
+ BackupService,
112
+ };
@@ -121,6 +121,21 @@ class ConnectionManager {
121
121
  return this.getActiveConnection();
122
122
  }
123
123
 
124
+ rememberConnection({ filePath, label, readOnly = false, makeActive = false }) {
125
+ const resolvedPath = validateSqlitePath(filePath, { mustExist: true });
126
+ const connection = this.buildConnectionRecord(resolvedPath, {
127
+ label,
128
+ readOnly,
129
+ });
130
+
131
+ this.appStateStore.upsertRecentConnection(connection, { makeActive });
132
+
133
+ return {
134
+ ...connection,
135
+ isActive: this.current?.id === connection.id,
136
+ };
137
+ }
138
+
124
139
  createConnection({ filePath, label }) {
125
140
  const resolvedPath = resolveUserPath(filePath);
126
141
  ensureFileDoesNotExist(resolvedPath, "SQLite database");
@@ -171,6 +171,36 @@ class DataBrowserService {
171
171
  };
172
172
  }
173
173
 
174
+ deleteTableRow(tableName, payload = {}) {
175
+ this.connectionManager.assertWritable();
176
+
177
+ const db = this.connectionManager.getActiveDatabase();
178
+ const tableDetail = getTableDetail(db, tableName, { includeRowCount: false });
179
+ const identity = payload.identity ?? null;
180
+
181
+ if (tableDetail.notSafelyUpdatable) {
182
+ throw new ValidationError(
183
+ `Table ${tableName} cannot be safely updated because it has no stable row identity.`
184
+ );
185
+ }
186
+
187
+ const where = this.buildWhereClause(tableDetail, identity);
188
+ const result = db
189
+ .prepare(`DELETE FROM ${quoteIdentifier(tableName)} WHERE ${where.clause}`)
190
+ .run(...where.params);
191
+
192
+ if (!result.changes) {
193
+ throw new NotFoundError(`Row not found in table: ${tableName}`);
194
+ }
195
+
196
+ return {
197
+ tableName,
198
+ deleted: true,
199
+ identity,
200
+ affectedRowCount: result.changes,
201
+ };
202
+ }
203
+
174
204
  buildWhereClause(tableDetail, identity) {
175
205
  if (tableDetail.identityStrategy?.type === "rowid") {
176
206
  const rowid = identity?.values?.rowid;
@@ -1,8 +1,12 @@
1
+ const { quoteIdentifier } = require("../../utils/identifier");
2
+ const { serializeRows } = require("../../utils/sqliteTypes");
1
3
  const { rowsToCsv } = require("../../utils/csv");
4
+ const { getTableDetail } = require("./introspection");
2
5
 
3
6
  class ExportService {
4
- constructor({ appStateStore, sqlExecutor }) {
7
+ constructor({ appStateStore, connectionManager, sqlExecutor }) {
5
8
  this.appStateStore = appStateStore;
9
+ this.connectionManager = connectionManager;
6
10
  this.sqlExecutor = sqlExecutor;
7
11
  }
8
12
 
@@ -27,6 +31,47 @@ class ExportService {
27
31
  rowCount: result.rows.length,
28
32
  };
29
33
  }
34
+
35
+ exportTable(tableName) {
36
+ const db = this.connectionManager.getActiveDatabase();
37
+ const tableDetail = getTableDetail(db, tableName, { includeRowCount: false });
38
+ const orderClause = this.buildOrderClause(tableDetail);
39
+ const statement = db.prepare(
40
+ [
41
+ `SELECT * FROM ${quoteIdentifier(tableName)}`,
42
+ orderClause ? `ORDER BY ${orderClause}` : "",
43
+ ]
44
+ .filter(Boolean)
45
+ .join(" ")
46
+ );
47
+ const rows = serializeRows(statement.all());
48
+ const columns = statement.columns().map((column) => column.name);
49
+
50
+ return {
51
+ filename: `${tableName}.csv`,
52
+ csv: rowsToCsv({
53
+ columns,
54
+ rows,
55
+ delimiter: this.getDelimiter(),
56
+ }),
57
+ columns,
58
+ rowCount: rows.length,
59
+ };
60
+ }
61
+
62
+ buildOrderClause(tableDetail) {
63
+ if (tableDetail.identityStrategy?.type === "rowid") {
64
+ return "rowid ASC";
65
+ }
66
+
67
+ if (tableDetail.identityStrategy?.type === "primaryKey") {
68
+ return tableDetail.identityStrategy.columns
69
+ .map((columnName) => `${quoteIdentifier(columnName)} ASC`)
70
+ .join(", ");
71
+ }
72
+
73
+ return "";
74
+ }
30
75
  }
31
76
 
32
77
  module.exports = {
@@ -11,6 +11,18 @@ class StructureService {
11
11
  getStructureOverview() {
12
12
  const db = this.connectionManager.getActiveDatabase();
13
13
  const entries = getRawStructureEntries(db);
14
+ const tables = entries
15
+ .filter((entry) => entry.type === "table")
16
+ .map((entry) => getTableDetail(db, entry.name, { includeRowCount: false }));
17
+ const relationshipCount = tables.reduce(
18
+ (count, table) =>
19
+ count +
20
+ table.foreignKeys.reduce(
21
+ (tableCount, foreignKey) => tableCount + foreignKey.mappings.length,
22
+ 0
23
+ ),
24
+ 0
25
+ );
14
26
 
15
27
  return {
16
28
  entries,
@@ -20,6 +32,20 @@ class StructureService {
20
32
  indexes: entries.filter((entry) => entry.type === "index"),
21
33
  triggers: entries.filter((entry) => entry.type === "trigger"),
22
34
  },
35
+ graph: {
36
+ tables: tables.map((table) => ({
37
+ type: table.type,
38
+ name: table.name,
39
+ ddl: table.ddl,
40
+ withoutRowId: table.withoutRowId,
41
+ strict: table.strict,
42
+ columns: table.columns,
43
+ foreignKeys: table.foreignKeys,
44
+ identityStrategy: table.identityStrategy,
45
+ notSafelyUpdatable: table.notSafelyUpdatable,
46
+ })),
47
+ relationshipCount,
48
+ },
23
49
  };
24
50
  }
25
51
 
@@ -444,7 +444,9 @@ class AppStateStore {
444
444
  }));
445
445
  }
446
446
 
447
- upsertRecentConnection(connection) {
447
+ upsertRecentConnection(connection, options = {}) {
448
+ const makeActive = options.makeActive !== false;
449
+
448
450
  this.db.transaction(() => {
449
451
  this.db
450
452
  .prepare(`
@@ -476,7 +478,10 @@ class AppStateStore {
476
478
  connection.readOnly ? 1 : 0
477
479
  );
478
480
 
479
- this.setMetaValue("activeConnectionId", connection.id);
481
+ if (makeActive) {
482
+ this.setMetaValue("activeConnectionId", connection.id);
483
+ }
484
+
480
485
  this.trimRecentConnections();
481
486
  })();
482
487