-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* π₯ Remove any-db and unused packages * β Add mysql2 * π©Ή Switch db package to mysql2 to support mysql 8 * β Add postgres and types * π₯ Remove any-db types * β¨ Make database class work with mysql 8 * π¨ Let package figure out default port * π Make it work with postgres * β°οΈ Remove getDatabases * β»οΈ Refactor database module to use factory * β»οΈ Conform to existing design patterns and add tests * π₯ Remove old database class * π€‘ Mock mysql2 and pg in factory test * π·οΈ Add void return type to end methods * π¨ Add new line to end of files
- Loading branch information
1 parent
1b413d9
commit e10904f
Showing
12 changed files
with
265 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import IDatabase from './interface'; | ||
import MySqlDatabase from './mySqlDatabase'; | ||
import PostgresDatabase from './postgresDatabase'; | ||
|
||
export default function databaseFactory( | ||
driver: string, | ||
host: string, | ||
user: string, | ||
password: string, | ||
port?: number | ||
): IDatabase { | ||
switch (driver) { | ||
case 'mysql': | ||
return new MySqlDatabase(host, user, password, port); | ||
case 'postgres': | ||
return new PostgresDatabase(host, user, password, port); | ||
default: | ||
throw new Error(`${driver} driver is unsupported`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default interface IDatabase { | ||
/** | ||
* Runs an EXPLAIN on the query. If it doesn't run successfully, errors will come through, | ||
* which is what we want. | ||
*/ | ||
lintQuery(query: string, callback: any): void; | ||
end(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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, | ||
) { | ||
this.connection = mysql.createConnection({ | ||
host, | ||
user, | ||
password, | ||
port, | ||
}); | ||
} | ||
|
||
public lintQuery(query: string, callback: any): void { | ||
this.connection.query(`EXPLAIN ${query}`, err => { | ||
if (err) { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
|
||
public end(): void { | ||
this.connection.end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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, | ||
) { | ||
this.pool = new Pool({ | ||
host, | ||
user, | ||
password, | ||
port, | ||
}); | ||
} | ||
|
||
public lintQuery(query: string, callback: any): void { | ||
this.pool.query(`EXPLAIN ${query}`, err => { | ||
if (err) { | ||
callback({ | ||
code: err.name, | ||
sqlMessage: err.message, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
public end(): void { | ||
this.pool.end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import databaseFactory from "../../../src/database/databaseFactory"; | ||
import MySqlDatabase from "../../../src/database/mySqlDatabase"; | ||
import PostgresDatabase from "../../../src/database/postgresDatabase"; | ||
|
||
jest.mock("mysql2", () => { | ||
const mock = { | ||
createConnection: () => mock, | ||
}; | ||
return mock; | ||
}); | ||
|
||
jest.mock("pg", () => { | ||
const mock = { | ||
Pool: function () { | ||
return mock; | ||
}, | ||
}; | ||
return mock; | ||
}); | ||
|
||
test.each([ | ||
["mysql", MySqlDatabase], | ||
["postgres", PostgresDatabase], | ||
])("it returns correct instance for driver", (driver, expected) => { | ||
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); | ||
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, | ||
query: () => true, | ||
}; | ||
return mock; | ||
}); | ||
|
||
expect(callback.mock.calls.length).toBe(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import MySqlDatabase from "../../../src/database/mySqlDatabase"; | ||
|
||
jest.mock("mysql2", () => { | ||
const mock = { | ||
createConnection: (config) => { | ||
expect(config).toEqual({ | ||
host: "localhost", | ||
user: "user", | ||
password: "password", | ||
port: 3306, | ||
}); | ||
return mock; | ||
}, | ||
query: (query, callback) => { | ||
callback({ | ||
sqlMessage: "table does not exist", | ||
}); | ||
}, | ||
end: () => true, | ||
}; | ||
return mock; | ||
}); | ||
|
||
test("it calls createConnection", () => { | ||
const db = new MySqlDatabase("localhost", "user", "password", 3306); | ||
}); | ||
|
||
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 => { | ||
expect(err.sqlMessage).toEqual("table does not exist"); | ||
}) | ||
}); | ||
|
||
test("it calls end on connection", () => { | ||
const db = new MySqlDatabase("localhost", "user", "password", 3306); | ||
db.end(); | ||
}); | ||
|
||
test("it does not call callback if there is no error", () => { | ||
const callback = jest.fn(() => true); | ||
|
||
jest.mock("mysql2", () => { | ||
const mock = { | ||
createConnection: () => mock, | ||
query: () => true, | ||
}; | ||
return mock; | ||
}); | ||
|
||
expect(callback.mock.calls.length).toBe(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import PostgresDatabase from "../../../src/database/postgresDatabase"; | ||
|
||
jest.mock("pg", () => { | ||
const mock = { | ||
Pool: function (config) { | ||
expect(config).toEqual({ | ||
host: "localhost", | ||
user: "user", | ||
password: "password", | ||
port: 5432, | ||
}); | ||
return mock; | ||
}, | ||
query: (query, callback) => { | ||
callback({ | ||
name: "name", | ||
message: "table does not exist", | ||
}); | ||
}, | ||
end: () => true, | ||
}; | ||
return mock; | ||
}); | ||
|
||
test("it calls createConnection", () => { | ||
const db = new PostgresDatabase("localhost", "user", "password", 5432); | ||
}); | ||
|
||
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 => { | ||
expect(err.sqlMessage).toEqual("table does not exist"); | ||
expect(err.code).toEqual("name"); | ||
}) | ||
}); | ||
|
||
test("it calls end on connection", () => { | ||
const db = new PostgresDatabase("localhost", "user", "password", 5432); | ||
db.end(); | ||
}); | ||
|
||
test("it does not call callback if there is no error", () => { | ||
const callback = jest.fn(() => true); | ||
|
||
jest.mock("pg", () => { | ||
const mock = { | ||
createConnection: () => mock, | ||
query: () => true, | ||
}; | ||
return mock; | ||
}); | ||
|
||
expect(callback.mock.calls.length).toBe(0); | ||
}); |