diff --git a/dist/package.json b/dist/package.json index 38925159..baa20ff6 100644 --- a/dist/package.json +++ b/dist/package.json @@ -4,23 +4,20 @@ }, "dependencies": { "@types/moo": "^0.5.3", - "@types/mysql": "^2.15.9", "@types/nearley": "^2.11.1", "@types/node": "^14.0.14", + "@types/pg": "^7.14.11", "@types/sprintf-js": "^1.1.2", - "any-db": "^2.2.1", - "any-db-mysql": "^2.1.2", - "any-db-postgres": "^2.1.5", "commander": "^5.1.0", "moo": "^0.5.1", - "mysql": "^2.16.0", + "mysql2": "^2.2.5", "nearley": "^2.19.3", "param-case": "^3.0.3", + "pg": "^8.6.0", "sprintf-js": "^1.1.2" }, "description": "An intelligent SQL linter and checker", "devDependencies": { - "@types/any-db": "^2.1.30", "@types/jest": "^26.0.3", "jest": "^26.1.0", "pkg": "^4.3.7", @@ -79,5 +76,5 @@ "test": "jest test --coverage --silent", "nearley": "nearleyc" }, - "version": "0.0.16" + "version": "0.0.17" } diff --git a/dist/src/checker/checkerRunner.js b/dist/src/checker/checkerRunner.js index a88b9f08..3e55bab2 100644 --- a/dist/src/checker/checkerRunner.js +++ b/dist/src/checker/checkerRunner.js @@ -56,7 +56,7 @@ class CheckerRunner { if (checker.requiresConnection && database && checker.appliesTo.includes(category)) { - database.lintQuery(database.connection, content, (results) => { + database.lintQuery(content, (results) => { const sqlChecker = new checks_1.MySqlError(results); printer.printCheck(sqlChecker, tokenised, prefix); }); diff --git a/dist/src/checker/checkerRunner.js.map b/dist/src/checker/checkerRunner.js.map index 7579164a..b19d2d74 100644 --- a/dist/src/checker/checkerRunner.js.map +++ b/dist/src/checker/checkerRunner.js.map @@ -1 +1 @@ -{"version":3,"file":"checkerRunner.js","sourceRoot":"","sources":["../../../src/checker/checkerRunner.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAC7B,iDAA8C;AAI9C,0CAAsD;AACtD,6CAA8C;AAE9C;;GAEG;AACH,MAAM,aAAa;IACV,GAAG,CACR,UAAmB,EACnB,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,OAAO,CAAC,CAAC;gBAErC,IAAI,CAAC,QAAQ,EAAE;oBACb,OAAO,CAAC,8BAA8B,CAAC,OAAO,CAAC,CAAC;iBACjD;gBAED,MAAM,SAAS,GAAU,gBAAQ,CAAC,KAAK,CAAC,CAAC;gBAEzC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;oBACvB,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBAErC,gBAAgB;oBAChB,IACE,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC;wBACpC,CAAC,OAAO,CAAC,kBAAkB,EAC3B;wBACA,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;qBAChD;oBAED,mBAAmB;oBACnB,IACE,OAAO,CAAC,kBAAkB;wBAC1B,QAAQ;wBACR,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,EACpC;wBACA,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC,OAAY,EAAE,EAAE;4BAChE,MAAM,UAAU,GAAG,IAAI,mBAAU,CAAC,OAAO,CAAC,CAAC;4BAC3C,OAAO,CAAC,UAAU,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;wBACpD,CAAC,CAAC,CAAC;qBACJ;gBACH,CAAC,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAEQ,sCAAa"} \ No newline at end of file +{"version":3,"file":"checkerRunner.js","sourceRoot":"","sources":["../../../src/checker/checkerRunner.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAC7B,iDAA8C;AAI9C,0CAAsD;AACtD,6CAA8C;AAE9C;;GAEG;AACH,MAAM,aAAa;IACV,GAAG,CACR,UAAmB,EACnB,OAAgB,EAChB,MAAc,EACd,aAAuB,EACvB,MAAc,EACd,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AE;oBACb,OAAO,CAAC,8BAA8B,CAAC,OAAO,CAAC,CAAC;iBACjD;gBAED,MAAM,SAAS,GAAU,gBAAQ,CAAC,KAAK,CAAC,CAAC;gBAEzC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;oBACvB,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBAErC,gBAAgB;oBAChB,IACE,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC;wBACpC,CAAC,OAAO,CAAC,kBAAkB,EAC3B;wBACA,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;qBAChD;oBAED,mBAAmB;oBACnB,IACE,OAAO,CAAC,kBAAkB;wBAC1B,QAAQ;wBACR,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,EACpC;wBACA,QAAQ,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,OAAY,EAAE,EAAE;4BAC3C,MAAM,UAAU,GAAG,IAAI,mBAAU,CAAC,OAAO,CAAC,CAAC;4BAC3C,OAAO,CAAC,UAAU,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;wBACpD,CAAC,CAAC,CAAC;qBACJ;gBACH,CAAC,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAEQ,sCAAa"} \ No newline at end of file diff --git a/dist/src/database.js b/dist/src/database.js deleted file mode 100644 index f00dea42..00000000 --- a/dist/src/database.js +++ /dev/null @@ -1,30 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.Database = void 0; -const anyDB = require("any-db"); -class Database { - constructor(driver, host, user, password, port) { - this.connection = anyDB.createConnection(`${driver}://${user}:${password}@${host}:${port}`); - } - getDatabases(connection, callback) { - connection.query("SHOW DATABASES", (error, results) => { - if (error) { - return console.log(error); - } - callback(results); - }); - } - /** - * Runs an EXPLAIN on the query. If it doesn't run successfully, errors will come through, - * which is what we want. - */ - lintQuery(connection, query, callback) { - connection.query(`EXPLAIN ${query}`, [], (error, results) => { - if (error) { - callback(error); - } - }); - } -} -exports.Database = Database; -//# sourceMappingURL=database.js.map \ No newline at end of file diff --git a/dist/src/database.js.map b/dist/src/database.js.map deleted file mode 100644 index 1e0956ca..00000000 --- a/dist/src/database.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/database.ts"],"names":[],"mappings":";;;AAAA,gCAAgC;AAEhC,MAAM,QAAQ;IAGZ,YACE,MAAc,EACd,IAAY,EACZ,IAAY,EACZ,QAAgB,EAChB,IAAa;QAEb,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,gBAAgB,CACtC,GAAG,MAAM,MAAM,IAAI,IAAI,QAAQ,IAAI,IAAI,IAAI,IAAI,EAAE,CAClD,CAAC;IACJ,CAAC;IAEM,YAAY,CAAC,UAAe,EAAE,QAAa;QAChD,UAAU,CAAC,KAAK,CAAC,gBAAgB,EAAE,CAAC,KAAa,EAAE,OAAiB,EAAE,EAAE;YACtE,IAAI,KAAK,EAAE;gBACT,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;aAC3B;YAED,QAAQ,CAAC,OAAO,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACI,SAAS,CACd,UAA4B,EAC5B,KAAa,EACb,QAAa;QAEb,UAAU,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YAC1D,IAAI,KAAK,EAAE;gBACT,QAAQ,CAAC,KAAK,CAAC,CAAC;aACjB;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAEQ,4BAAQ"} \ No newline at end of file diff --git a/dist/src/database/databaseFactory.js b/dist/src/database/databaseFactory.js new file mode 100644 index 00000000..6a78cc53 --- /dev/null +++ b/dist/src/database/databaseFactory.js @@ -0,0 +1,16 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const mySqlDatabase_1 = require("./mySqlDatabase"); +const postgresDatabase_1 = require("./postgresDatabase"); +function databaseFactory(driver, host, user, password, port) { + switch (driver) { + case 'mysql': + return new mySqlDatabase_1.default(host, user, password, port); + case 'postgres': + return new postgresDatabase_1.default(host, user, password, port); + default: + throw new Error(`${driver} driver is unsupported`); + } +} +exports.default = databaseFactory; +//# sourceMappingURL=databaseFactory.js.map \ No newline at end of file diff --git a/dist/src/database/databaseFactory.js.map b/dist/src/database/databaseFactory.js.map new file mode 100644 index 00000000..8feae0b2 --- /dev/null +++ b/dist/src/database/databaseFactory.js.map @@ -0,0 +1 @@ +{"version":3,"file":"databaseFactory.js","sourceRoot":"","sources":["../../../src/database/databaseFactory.ts"],"names":[],"mappings":";;AACA,mDAA4C;AAC5C,yDAAkD;AAElD,SAAwB,eAAe,CACrC,MAAc,EACd,IAAY,EACZ,IAAY,EACZ,QAAgB,EAChB,IAAa;IAEb,QAAQ,MAAM,EAAE;QACd,KAAK,OAAO;YACV,OAAO,IAAI,uBAAa,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QACvD,KAAK,UAAU;YACb,OAAO,IAAI,0BAAgB,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC1D;YACE,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM,wBAAwB,CAAC,CAAC;KACtD;AACH,CAAC;AAfD,kCAeC"} \ No newline at end of file diff --git a/dist/src/database/interface.js b/dist/src/database/interface.js new file mode 100644 index 00000000..d549d1f1 --- /dev/null +++ b/dist/src/database/interface.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=interface.js.map \ No newline at end of file diff --git a/dist/src/database/interface.js.map b/dist/src/database/interface.js.map new file mode 100644 index 00000000..b35cd0fb --- /dev/null +++ b/dist/src/database/interface.js.map @@ -0,0 +1 @@ +{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../../src/database/interface.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/src/database/mySqlDatabase.js b/dist/src/database/mySqlDatabase.js new file mode 100644 index 00000000..82720906 --- /dev/null +++ b/dist/src/database/mySqlDatabase.js @@ -0,0 +1,25 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const mysql = require("mysql2"); +class MySqlDatabase { + constructor(host, user, password, port) { + this.connection = mysql.createConnection({ + host, + user, + password, + port, + }); + } + lintQuery(query, callback) { + this.connection.query(`EXPLAIN ${query}`, err => { + if (err) { + callback(err); + } + }); + } + end() { + this.connection.end(); + } +} +exports.default = MySqlDatabase; +//# sourceMappingURL=mySqlDatabase.js.map \ No newline at end of file diff --git a/dist/src/database/mySqlDatabase.js.map b/dist/src/database/mySqlDatabase.js.map new file mode 100644 index 00000000..dc8e6e43 --- /dev/null +++ b/dist/src/database/mySqlDatabase.js.map @@ -0,0 +1 @@ +{"version":3,"file":"mySqlDatabase.js","sourceRoot":"","sources":["../../../src/database/mySqlDatabase.ts"],"names":[],"mappings":";;AAAA,gCAAgC;AAGhC,MAAqB,aAAa;IAGhC,YACE,IAAY,EACZ,IAAY,EACZ,QAAgB,EAChB,IAAa;QAEb,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACvC,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IAEM,SAAS,CAAC,KAAa,EAAE,QAAa;QAC3C,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,GAAG,CAAC,EAAE;YAC9C,IAAI,GAAG,EAAE;gBACP,QAAQ,CAAC,GAAG,CAAC,CAAC;aACf;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG;QACR,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;IACxB,CAAC;CACF;AA5BD,gCA4BC"} \ No newline at end of file diff --git a/dist/src/database/postgresDatabase.js b/dist/src/database/postgresDatabase.js new file mode 100644 index 00000000..fe12d40b --- /dev/null +++ b/dist/src/database/postgresDatabase.js @@ -0,0 +1,28 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const pg_1 = require("pg"); +class PostgresDatabase { + constructor(host, user, password, port) { + this.pool = new pg_1.Pool({ + host, + user, + password, + port, + }); + } + lintQuery(query, callback) { + this.pool.query(`EXPLAIN ${query}`, err => { + if (err) { + callback({ + code: err.name, + sqlMessage: err.message, + }); + } + }); + } + end() { + this.pool.end(); + } +} +exports.default = PostgresDatabase; +//# sourceMappingURL=postgresDatabase.js.map \ No newline at end of file diff --git a/dist/src/database/postgresDatabase.js.map b/dist/src/database/postgresDatabase.js.map new file mode 100644 index 00000000..b70f01da --- /dev/null +++ b/dist/src/database/postgresDatabase.js.map @@ -0,0 +1 @@ +{"version":3,"file":"postgresDatabase.js","sourceRoot":"","sources":["../../../src/database/postgresDatabase.ts"],"names":[],"mappings":";;AAAA,2BAA0B;AAG1B,MAAqB,gBAAgB;IAGnC,YACE,IAAY,EACZ,IAAY,EACZ,QAAgB,EAChB,IAAa;QAEb,IAAI,CAAC,IAAI,GAAG,IAAI,SAAI,CAAC;YACnB,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IAEM,SAAS,CAAC,KAAa,EAAE,QAAa;QAC3C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,GAAG,CAAC,EAAE;YACxC,IAAI,GAAG,EAAE;gBACP,QAAQ,CAAC;oBACP,IAAI,EAAE,GAAG,CAAC,IAAI;oBACd,UAAU,EAAE,GAAG,CAAC,OAAO;iBACxB,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG;QACR,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;IAClB,CAAC;CACF;AA/BD,mCA+BC"} \ No newline at end of file diff --git a/dist/src/main.js b/dist/src/main.js index a01b0f8e..2192455d 100755 --- a/dist/src/main.js +++ b/dist/src/main.js @@ -5,7 +5,7 @@ const program = require("commander"); const fs = require("fs"); const process = require("process"); const checkerRunner_1 = require("./checker/checkerRunner"); -const database_1 = require("./database"); +const databaseFactory_1 = require("./database/databaseFactory"); const formatterFactory_1 = require("./formatter/formatterFactory"); const printer_1 = require("./printer"); const config_1 = require("./config"); @@ -68,8 +68,9 @@ if (configuration === null) { } const driver = program.driver || (configuration === null || configuration === void 0 ? void 0 : configuration.driver) || "mysql"; if (program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host)) { - db = new database_1.Database(driver, program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host) || "localhost", program.user || (configuration === null || configuration === void 0 ? void 0 : configuration.user) || "root", // bad practice but unfortunately common, make it easier for the user - program.password || (configuration === null || configuration === void 0 ? void 0 : configuration.password), program.port || (configuration === null || configuration === void 0 ? void 0 : configuration.port) || "3306"); + db = databaseFactory_1.default(driver, program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host) || "localhost", program.user || (configuration === null || configuration === void 0 ? void 0 : configuration.user) || "root", // bad practice but unfortunately common, make it easier for the user + program.password || (configuration === null || configuration === void 0 ? void 0 : configuration.password), program.port || (configuration === null || configuration === void 0 ? void 0 : configuration.port) || undefined // let mysql2 or pg figure out the default port + ); } if (programFile) { if (fs.lstatSync(programFile).isDirectory()) { @@ -86,6 +87,6 @@ if (programFile) { } runner.run(queries, printer, prefix, omittedErrors, driver, db); if (program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host)) { - db.connection.end(); + db.end(); } //# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/dist/src/main.js.map b/dist/src/main.js.map index c3a49436..6ddee90a 100644 --- a/dist/src/main.js.map +++ b/dist/src/main.js.map @@ -1 +1 @@ -{"version":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AO,CAAC,IAAI,KAAI,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,IAAI,CAAA,EAAE;IACvC,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;CACrB"} \ No newline at end of file +{"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":";;;AAEA,qCAAqC;AACrC,yBAAyB;AACzB,mCAAmC;AAEnC,2DAAwD;AACxD,gEAAyD;AACzD,mEAAgE;AAChE,uCAAoC;AAEpC,qCAAkD;AAClD,4CAAqE;AACrE,kDAA0C;AAE1C,iCAAyC;AAEzC,SAAS,iBAAiB,CAAC,CAAM,EAAE,KAAU;IAC3C,OAAO,KAAK,GAAG,CAAC,CAAC;AACnB,CAAC;AAED,OAAO;KACJ,OAAO,CAAC,sBAAO,CAAC;KAChB,MAAM,CAAC,gBAAgB,EAAE,wBAAwB,CAAC;KAClD,MAAM,CACL,uBAAuB,EACvB,yDAAyD,CAC1D;KACA,MAAM,CACL,eAAe,EACf,2EAA2E,EAC3E,iBAAiB,EACjB,CAAC,CACF;KACA,MAAM,CACL,mBAAmB,EACnB,4DAA4D,EAC5D,QAAQ,CACT;KACA,MAAM,CAAC,iBAAiB,EAAE,6BAA6B,CAAC;KACxD,MAAM,CAAC,iBAAiB,EAAE,6BAA6B,CAAC;KACxD,MAAM,CAAC,qBAAqB,EAAE,iCAAiC,CAAC;KAChE,MAAM,CAAC,iBAAiB,EAAE,6BAA6B,CAAC;KACxD,MAAM,CAAC,mBAAmB,EAAE,oCAAoC,CAAC;KACjE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAEvB,IAAI,OAAO,GAAY,EAAE,CAAC;AAC1B,IAAI,MAAM,GAAW,EAAE,CAAC;AAExB,MAAM,gBAAgB,GAAG,IAAI,mCAAgB,EAAE,CAAC;AAChD,MAAM,MAAM,GAAG,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AACtD,MAAM,OAAO,G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AAM,GAAG,WAAW,CAAC;KACtB;CACF;AAED,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;AAEhE,IAAI,OAAO,CAAC,IAAI,KAAI,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,IAAI,CAAA,EAAE;IACvC,EAAE,CAAC,GAAG,EAAE,CAAC;CACV"} \ No newline at end of file diff --git a/package.json b/package.json index c8c287c7..baa20ff6 100644 --- a/package.json +++ b/package.json @@ -76,5 +76,5 @@ "test": "jest test --coverage --silent", "nearley": "nearleyc" }, - "version": "0.0.16" + "version": "0.0.17" } diff --git a/src/database/databaseFactory.ts b/src/database/databaseFactory.ts index 1e06a8f1..6c36956e 100644 --- a/src/database/databaseFactory.ts +++ b/src/database/databaseFactory.ts @@ -1,6 +1,6 @@ -import IDatabase from './interface'; -import MySqlDatabase from './mySqlDatabase'; -import PostgresDatabase from './postgresDatabase'; +import IDatabase from "./interface"; +import MySqlDatabase from "./mySqlDatabase"; +import PostgresDatabase from "./postgresDatabase"; export default function databaseFactory( driver: string, @@ -10,9 +10,9 @@ export default function databaseFactory( port?: number ): IDatabase { switch (driver) { - case 'mysql': + case "mysql": return new MySqlDatabase(host, user, password, port); - case 'postgres': + case "postgres": return new PostgresDatabase(host, user, password, port); default: throw new Error(`${driver} driver is unsupported`); diff --git a/src/database/mySqlDatabase.ts b/src/database/mySqlDatabase.ts index 1279169f..cfe12e03 100644 --- a/src/database/mySqlDatabase.ts +++ b/src/database/mySqlDatabase.ts @@ -1,15 +1,10 @@ -import * as mysql from 'mysql2'; -import IDatabase from './interface'; +import * as mysql from "mysql2"; +import IDatabase from "./interface"; export default class MySqlDatabase implements IDatabase { private connection: mysql.Connection; - constructor ( - host: string, - user: string, - password: string, - port?: number, - ) { + constructor(host: string, user: string, password: string, port?: number) { this.connection = mysql.createConnection({ host, user, @@ -19,7 +14,7 @@ export default class MySqlDatabase implements IDatabase { } public lintQuery(query: string, callback: any): void { - this.connection.query(`EXPLAIN ${query}`, err => { + this.connection.query(`EXPLAIN ${query}`, (err) => { if (err) { callback(err); } diff --git a/src/database/postgresDatabase.ts b/src/database/postgresDatabase.ts index 5a462512..7f52fbb5 100644 --- a/src/database/postgresDatabase.ts +++ b/src/database/postgresDatabase.ts @@ -1,15 +1,10 @@ -import { Pool } from 'pg'; -import IDatabase from './interface'; +import { Pool } from "pg"; +import IDatabase from "./interface"; export default class PostgresDatabase implements IDatabase { private pool: Pool; - constructor( - host: string, - user: string, - password: string, - port?: number, - ) { + constructor(host: string, user: string, password: string, port?: number) { this.pool = new Pool({ host, user, @@ -19,7 +14,7 @@ export default class PostgresDatabase implements IDatabase { } public lintQuery(query: string, callback: any): void { - this.pool.query(`EXPLAIN ${query}`, err => { + this.pool.query(`EXPLAIN ${query}`, (err) => { if (err) { callback({ code: err.name, diff --git a/src/main.ts b/src/main.ts index b7d7ea8e..51ce6716 100644 --- a/src/main.ts +++ b/src/main.ts @@ -96,7 +96,7 @@ const driver = program.driver || configuration?.driver || "mysql"; if (program.host || configuration?.host) { db = databaseFactory( - driver, + driver, program.host || configuration?.host || "localhost", program.user || configuration?.user || "root", // bad practice but unfortunately common, make it easier for the user program.password || configuration?.password, diff --git a/test/unit/database/databaseFactory.test.ts b/test/unit/database/databaseFactory.test.ts index c730f232..65a1d4af 100644 --- a/test/unit/database/databaseFactory.test.ts +++ b/test/unit/database/databaseFactory.test.ts @@ -22,18 +22,25 @@ test.each([ ["mysql", MySqlDatabase], ["postgres", PostgresDatabase], ])("it returns correct instance for driver", (driver, expected) => { - const database = databaseFactory(driver, "localhost", "user", "password", 3306); + const database = databaseFactory( + driver, + "localhost", + "user", + "password", + 3306 + ); expect(database).toBeInstanceOf(expected); }); test("it throws an exception if driver is not supported", () => { - const t = () => databaseFactory("mongodb", "localhost", "user", "password", 3306); + const t = () => + databaseFactory("mongodb", "localhost", "user", "password", 3306); expect(t).toThrow(Error); }); test("it does not call callback if there is no error", () => { const callback = jest.fn(() => true); - + jest.mock("mysql2", () => { const mock = { createConnection: () => mock, diff --git a/test/unit/database/mysqlDatabase.ts b/test/unit/database/mysqlDatabase.ts index fde5daea..29cb7fa5 100644 --- a/test/unit/database/mysqlDatabase.ts +++ b/test/unit/database/mysqlDatabase.ts @@ -27,9 +27,9 @@ test("it calls createConnection", () => { test("it calls callback if there is an error", () => { const db = new MySqlDatabase("localhost", "user", "password", 3306); - db.lintQuery("SELECT some_column FROM some_table WHERE id = 1", err => { + db.lintQuery("SELECT some_column FROM some_table WHERE id = 1", (err) => { expect(err.sqlMessage).toEqual("table does not exist"); - }) + }); }); test("it calls end on connection", () => { @@ -39,7 +39,7 @@ test("it calls end on connection", () => { test("it does not call callback if there is no error", () => { const callback = jest.fn(() => true); - + jest.mock("mysql2", () => { const mock = { createConnection: () => mock, diff --git a/test/unit/database/postgresDatabase.test.ts b/test/unit/database/postgresDatabase.test.ts index 0c7c9d61..0ba30e3c 100644 --- a/test/unit/database/postgresDatabase.test.ts +++ b/test/unit/database/postgresDatabase.test.ts @@ -28,10 +28,10 @@ test("it calls createConnection", () => { test("it calls callback if there is an error", () => { const db = new PostgresDatabase("localhost", "user", "password", 5432); - db.lintQuery("SELECT some_column FROM some_table WHERE id = 1", err => { + db.lintQuery("SELECT some_column FROM some_table WHERE id = 1", (err) => { expect(err.sqlMessage).toEqual("table does not exist"); expect(err.code).toEqual("name"); - }) + }); }); test("it calls end on connection", () => { @@ -41,7 +41,7 @@ test("it calls end on connection", () => { test("it does not call callback if there is no error", () => { const callback = jest.fn(() => true); - + jest.mock("pg", () => { const mock = { createConnection: () => mock,