mpx-db 1.0.1 → 1.0.3
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/package.json +4 -10
- package/src/cli.js +12 -1
- package/src/commands/query.js +46 -10
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mpx-db",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Database management CLI - Connect, query, migrate, and manage databases from the terminal",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"mpx-db": "
|
|
7
|
+
"mpx-db": "bin/mpx-db.js"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"scripts": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"repository": {
|
|
28
28
|
"type": "git",
|
|
29
|
-
"url": "https://github.com/mesaplexdev/mpx-db"
|
|
29
|
+
"url": "git+https://github.com/mesaplexdev/mpx-db.git"
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/mesaplexdev/mpx-db#readme",
|
|
32
32
|
"bugs": "https://github.com/mesaplexdev/mpx-db/issues",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"node": ">=18.0.0"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"better-sqlite3": "^12.6.2",
|
|
37
38
|
"chalk": "^5.3.0",
|
|
38
39
|
"cli-table3": "^0.6.5",
|
|
39
40
|
"commander": "^12.1.0",
|
|
@@ -41,14 +42,10 @@
|
|
|
41
42
|
"yaml": "^2.6.1"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
|
-
"better-sqlite3": "^11.0.0",
|
|
45
45
|
"mysql2": "^3.11.5",
|
|
46
46
|
"pg": "^8.13.1"
|
|
47
47
|
},
|
|
48
48
|
"peerDependenciesMeta": {
|
|
49
|
-
"better-sqlite3": {
|
|
50
|
-
"optional": true
|
|
51
|
-
},
|
|
52
49
|
"pg": {
|
|
53
50
|
"optional": true
|
|
54
51
|
},
|
|
@@ -56,9 +53,6 @@
|
|
|
56
53
|
"optional": true
|
|
57
54
|
}
|
|
58
55
|
},
|
|
59
|
-
"devDependencies": {
|
|
60
|
-
"better-sqlite3": "^12.6.2"
|
|
61
|
-
},
|
|
62
56
|
"files": [
|
|
63
57
|
"src/",
|
|
64
58
|
"bin/",
|
package/src/cli.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import { dirname, join } from 'path';
|
|
3
6
|
import { handleConnect, listConnections, removeConnection } from './commands/connections.js';
|
|
4
7
|
import { handleQuery } from './commands/query.js';
|
|
5
8
|
import { showInfo, listTables, describeTable, dumpSchema } from './commands/schema.js';
|
|
@@ -12,12 +15,16 @@ import {
|
|
|
12
15
|
} from './commands/migrate.js';
|
|
13
16
|
import { exportData } from './commands/data.js';
|
|
14
17
|
|
|
18
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
19
|
+
const __dirname = dirname(__filename);
|
|
20
|
+
const pkg = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf-8'));
|
|
21
|
+
|
|
15
22
|
const program = new Command();
|
|
16
23
|
|
|
17
24
|
program
|
|
18
25
|
.name('mpx-db')
|
|
19
26
|
.description('Database management CLI - Connect, query, migrate, and manage databases')
|
|
20
|
-
.version(
|
|
27
|
+
.version(pkg.version);
|
|
21
28
|
|
|
22
29
|
// Connect command
|
|
23
30
|
program
|
|
@@ -134,6 +141,10 @@ program.exitOverride();
|
|
|
134
141
|
try {
|
|
135
142
|
await program.parseAsync(process.argv);
|
|
136
143
|
} catch (err) {
|
|
144
|
+
// Ignore help and version display "errors"
|
|
145
|
+
if (err.code === 'commander.version') {
|
|
146
|
+
process.exit(0);
|
|
147
|
+
}
|
|
137
148
|
if (err.code !== 'commander.help' && err.code !== 'commander.helpDisplayed') {
|
|
138
149
|
console.error(chalk.red(`Error: ${err.message}`));
|
|
139
150
|
process.exit(1);
|
package/src/commands/query.js
CHANGED
|
@@ -4,7 +4,7 @@ import { getConnection } from '../utils/config.js';
|
|
|
4
4
|
import { createConnection } from '../db/connection.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Execute a query
|
|
7
|
+
* Execute a query or statement
|
|
8
8
|
*/
|
|
9
9
|
export async function handleQuery(target, sql, options) {
|
|
10
10
|
let db;
|
|
@@ -16,20 +16,38 @@ export async function handleQuery(target, sql, options) {
|
|
|
16
16
|
// Connect
|
|
17
17
|
db = await createConnection(connectionString);
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// Determine if this is a SELECT query or a DML/DDL statement
|
|
20
|
+
const isSelectQuery = isQueryStatement(sql);
|
|
21
|
+
|
|
22
|
+
// Execute
|
|
20
23
|
const startTime = Date.now();
|
|
21
|
-
const rows = await db.query(sql);
|
|
22
|
-
const duration = Date.now() - startTime;
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
if (isSelectQuery) {
|
|
26
|
+
// SELECT, PRAGMA, EXPLAIN, WITH - returns rows
|
|
27
|
+
const rows = await db.query(sql);
|
|
28
|
+
const duration = Date.now() - startTime;
|
|
29
|
+
|
|
30
|
+
// Display results
|
|
31
|
+
if (rows.length === 0) {
|
|
32
|
+
console.log(chalk.yellow('No rows returned'));
|
|
33
|
+
} else {
|
|
34
|
+
displayTable(rows);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
console.log(chalk.gray(`\n${rows.length} row(s) in ${duration}ms`));
|
|
27
38
|
} else {
|
|
28
|
-
|
|
39
|
+
// INSERT, UPDATE, DELETE, CREATE, DROP, ALTER - returns affected rows
|
|
40
|
+
const result = await db.execute(sql);
|
|
41
|
+
const duration = Date.now() - startTime;
|
|
42
|
+
|
|
43
|
+
console.log(chalk.green('✓ Statement executed successfully'));
|
|
44
|
+
console.log(chalk.gray(` Affected rows: ${result.affectedRows}`));
|
|
45
|
+
if (result.insertId) {
|
|
46
|
+
console.log(chalk.gray(` Insert ID: ${result.insertId}`));
|
|
47
|
+
}
|
|
48
|
+
console.log(chalk.gray(` Duration: ${duration}ms`));
|
|
29
49
|
}
|
|
30
50
|
|
|
31
|
-
console.log(chalk.gray(`\n${rows.length} row(s) in ${duration}ms`));
|
|
32
|
-
|
|
33
51
|
} catch (err) {
|
|
34
52
|
console.error(chalk.red('✗ Query failed'));
|
|
35
53
|
console.error(chalk.red(` ${err.message}`));
|
|
@@ -41,6 +59,24 @@ export async function handleQuery(target, sql, options) {
|
|
|
41
59
|
}
|
|
42
60
|
}
|
|
43
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Determine if SQL is a query (returns rows) or a statement (modifies data)
|
|
64
|
+
*/
|
|
65
|
+
function isQueryStatement(sql) {
|
|
66
|
+
const trimmed = sql.trim().toUpperCase();
|
|
67
|
+
|
|
68
|
+
// These keywords indicate a query that returns rows
|
|
69
|
+
const queryKeywords = ['SELECT', 'PRAGMA', 'EXPLAIN', 'WITH', 'SHOW', 'DESCRIBE', 'DESC'];
|
|
70
|
+
|
|
71
|
+
for (const keyword of queryKeywords) {
|
|
72
|
+
if (trimmed.startsWith(keyword)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
44
80
|
/**
|
|
45
81
|
* Display query results as table
|
|
46
82
|
*/
|