33 * Provides database operations for MySQL, PostgreSQL, and MongoDB
44 */
55
6- import { logger } from './logger.js' ;
7-
86// Supported database types
97export const DB_TYPES = {
108 MYSQL : 'mysql' ,
@@ -254,7 +252,7 @@ export function buildMySQLListDatabasesCommand(options) {
254252 if ( password ) command += ` -p'${ password } '` ;
255253 if ( host ) command += ` -h ${ host } ` ;
256254 if ( port ) command += ` -P ${ port } ` ;
257- command += ` -e "SHOW DATABASES;" | tail -n +2` ;
255+ command += ' -e "SHOW DATABASES;" | tail -n +2' ;
258256
259257 return command ;
260258}
@@ -290,7 +288,7 @@ export function buildPostgreSQLListDatabasesCommand(options) {
290288 if ( user ) command += ` -U ${ user } ` ;
291289 if ( host ) command += ` -h ${ host } ` ;
292290 if ( port ) command += ` -p ${ port } ` ;
293- command += ` -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" | sed '/^$/d' | sed 's/^[ \\t]*//'` ;
291+ command += ' -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" | sed \ '/^$/d\ ' | sed \ 's/^[ \\t]*//\'' ;
294292
295293 return command ;
296294}
@@ -311,7 +309,7 @@ export function buildPostgreSQLListTablesCommand(options) {
311309 if ( host ) command += ` -h ${ host } ` ;
312310 if ( port ) command += ` -p ${ port } ` ;
313311 command += ` -d ${ database } ` ;
314- command += ` -t -c "SELECT tablename FROM pg_tables WHERE schemaname = 'public';" | sed '/^$/d' | sed 's/^[ \\t]*//'` ;
312+ command += ' -t -c "SELECT tablename FROM pg_tables WHERE schemaname = \ 'public\ ';" | sed \ '/^$/d\ ' | sed \ 's/^[ \\t]*//\'' ;
315313
316314 return command ;
317315}
@@ -327,7 +325,7 @@ export function buildMongoDBListDatabasesCommand(options) {
327325 if ( port ) command += ` --port ${ port } ` ;
328326 if ( user ) command += ` --username ${ user } ` ;
329327 if ( password ) command += ` --password '${ password } '` ;
330- command += ` --quiet --eval "db.adminCommand('listDatabases').databases.forEach(function(d){print(d.name)})"` ;
328+ command += ' --quiet --eval "db.adminCommand(\ 'listDatabases\ ').databases.forEach(function(d){print(d.name)})"' ;
331329
332330 return command ;
333331}
@@ -344,7 +342,7 @@ export function buildMongoDBListCollectionsCommand(options) {
344342 if ( user ) command += ` --username ${ user } ` ;
345343 if ( password ) command += ` --password '${ password } '` ;
346344 command += ` ${ database } ` ;
347- command += ` --quiet --eval "db.getCollectionNames().forEach(function(c){print(c)})"` ;
345+ command += ' --quiet --eval "db.getCollectionNames().forEach(function(c){print(c)})"' ;
348346
349347 return command ;
350348}
@@ -479,43 +477,43 @@ export function buildEstimateSizeCommand(type, database, options = {}) {
479477 const { user, password, host = 'localhost' , port } = options ;
480478
481479 switch ( type ) {
482- case DB_TYPES . MYSQL : {
483- let command = 'mysql' ;
484- if ( user ) command += ` -u${ user } ` ;
485- if ( password ) command += ` -p'${ password } '` ;
486- if ( host ) command += ` -h ${ host } ` ;
487- if ( port ) command += ` -P ${ port } ` ;
488- command += ` -e "SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema='${ database } ';" | tail -n 1` ;
489- return command ;
490- }
480+ case DB_TYPES . MYSQL : {
481+ let command = 'mysql' ;
482+ if ( user ) command += ` -u${ user } ` ;
483+ if ( password ) command += ` -p'${ password } '` ;
484+ if ( host ) command += ` -h ${ host } ` ;
485+ if ( port ) command += ` -P ${ port } ` ;
486+ command += ` -e "SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema='${ database } ';" | tail -n 1` ;
487+ return command ;
488+ }
491489
492- case DB_TYPES . POSTGRESQL : {
493- let command = '' ;
494- if ( password ) {
495- command = `PGPASSWORD='${ password } ' ` ;
496- }
497- command += 'psql' ;
498- if ( user ) command += ` -U ${ user } ` ;
499- if ( host ) command += ` -h ${ host } ` ;
500- if ( port ) command += ` -p ${ port } ` ;
501- command += ` -d ${ database } ` ;
502- command += ` -t -c "SELECT pg_database_size('${ database } ');" | sed 's/^[ \\t]*//'` ;
503- return command ;
490+ case DB_TYPES . POSTGRESQL : {
491+ let command = '' ;
492+ if ( password ) {
493+ command = `PGPASSWORD='${ password } ' ` ;
504494 }
495+ command += 'psql' ;
496+ if ( user ) command += ` -U ${ user } ` ;
497+ if ( host ) command += ` -h ${ host } ` ;
498+ if ( port ) command += ` -p ${ port } ` ;
499+ command += ` -d ${ database } ` ;
500+ command += ` -t -c "SELECT pg_database_size('${ database } ');" | sed 's/^[ \\t]*//'` ;
501+ return command ;
502+ }
505503
506- case DB_TYPES . MONGODB : {
507- let command = 'mongo' ;
508- if ( host ) command += ` --host ${ host } ` ;
509- if ( port ) command += ` --port ${ port } ` ;
510- if ( user ) command += ` --username ${ user } ` ;
511- if ( password ) command += ` --password '${ password } '` ;
512- command += ` ${ database } ` ;
513- command += ` --quiet --eval "db.stats().dataSize"` ;
514- return command ;
515- }
504+ case DB_TYPES . MONGODB : {
505+ let command = 'mongo' ;
506+ if ( host ) command += ` --host ${ host } ` ;
507+ if ( port ) command += ` --port ${ port } ` ;
508+ if ( user ) command += ` --username ${ user } ` ;
509+ if ( password ) command += ` --password '${ password } '` ;
510+ command += ` ${ database } ` ;
511+ command += ' --quiet --eval "db.stats().dataSize"' ;
512+ return command ;
513+ }
516514
517- default :
518- throw new Error ( `Unknown database type: ${ type } ` ) ;
515+ default :
516+ throw new Error ( `Unknown database type: ${ type } ` ) ;
519517 }
520518}
521519
0 commit comments