Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to set database in connection #213

Draft
wants to merge 2 commits into
base: typescript
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ sql-lint accepts an object using the following interface as its only argument
driver?: string
prefix?: string
password?: string
database?: string
verbosity?: number
}
```
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import databaseFactory from "./database/databaseFactory";
.option("--host <string>", "The host for the connection")
.option("--user <string>", "The user for the connection")
.option("--password <string>", "The password for the connection")
.option("--database <string>", "The database for the connection")
.option("--port <string>", "The port for the connection")
.option("--config <string>", "The path to the configuration file")
.parse(process.argv);
Expand Down Expand Up @@ -96,6 +97,7 @@ import databaseFactory from "./database/databaseFactory";
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,
program.database || configuration?.database,
program.port || configuration?.port || undefined // let mysql2 or pg figure out the default port
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/database/databaseFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ export default function databaseFactory(
host: string,
user: string,
password: string,
database: string,
port?: number
): IDatabase {
switch (driver) {
case "mysql":
return new MySqlDatabase(host, user, password, port);
return new MySqlDatabase(host, user, password, database, port);
case "postgres":
return new PostgresDatabase(host, user, password, port);
return new PostgresDatabase(host, user, password, database, port);
default:
throw new Error(`${driver} driver is unsupported`);
}
Expand Down
9 changes: 8 additions & 1 deletion src/database/mySqlDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import IDatabase, { sqlError } 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,
database: string,
port?: number
) {
this.connection = mysql.createConnection({
host,
user,
password,
database,
port,
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/database/postgresDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import IDatabase, { sqlError } 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,
database: string,
port?: number
) {
this.pool = new Pool({
host,
user,
password,
database,
port,
});
}
Expand Down
36 changes: 11 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,34 @@ interface Parameters {
driver?: string;
prefix?: string;
password?: string;
database?: string;
verbosity?: number;
}

export default async ({
sql,
host,
port,
user = '',
prefix = '',
password = '',
user = "",
prefix = "",
password = "",
database = "",
verbosity = 0,
driver = 'mysql',
driver = "mysql",
}: Parameters): Promise<IMessage[]> => {
const printer = new Printer(
verbosity,
new JsonFormat(),
);
const printer = new Printer(verbosity, new JsonFormat());

let db: IDatabase|undefined;
let db: IDatabase | undefined;
if (host) {
db = databaseFactory(
driver,
host,
user,
password,
port,
)
db = databaseFactory(driver, host, user, password, database, port);
}

const runner = new CheckerRunner();
await runner.run(
putContentIntoLines(sql),
printer,
prefix,
[],
driver,
db,
)
await runner.run(putContentIntoLines(sql), printer, prefix, [], driver, db);

if (db) {
db.end();
}

return printer.messages;
}
};
10 changes: 9 additions & 1 deletion test/unit/database/databaseFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ test.each([
"localhost",
"user",
"password",
"database",
3306
);
expect(database).toBeInstanceOf(expected);
});

test("it throws an exception if driver is not supported", () => {
const t = () =>
databaseFactory("mongodb", "localhost", "user", "password", 3306);
databaseFactory(
"mongodb",
"localhost",
"user",
"password",
"database",
3306
);
expect(t).toThrow(Error);
});

Expand Down
30 changes: 26 additions & 4 deletions test/unit/database/mysqlDatabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock("mysql2", () => {
host: "localhost",
user: "user",
password: "password",
database: "database",
port: 3306,
});
return mock;
Expand All @@ -22,17 +23,38 @@ jest.mock("mysql2", () => {
});

test("it calls createConnection", () => {
const db = new MySqlDatabase("localhost", "user", "password", 3306);
const db = new MySqlDatabase(
"localhost",
"user",
"password",
"database",
3306
);
});

test("it calls callback if there is an error", async () => {
const db = new MySqlDatabase("localhost", "user", "password", 3306);
const db = new MySqlDatabase(
"localhost",
"user",
"password",
"database",
3306
);
const sql = "SELECT some_column FROM some_table WHERE id = 1";
expect(await db.lintQuery(sql)).toHaveProperty("sqlMessage", "table does not exist");
expect(await db.lintQuery(sql)).toHaveProperty(
"sqlMessage",
"table does not exist"
);
});

test("it calls end on connection", () => {
const db = new MySqlDatabase("localhost", "user", "password", 3306);
const db = new MySqlDatabase(
"localhost",
"user",
"password",
"database",
3306
);
db.end();
});

Expand Down
25 changes: 22 additions & 3 deletions test/unit/database/postgresDatabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock("pg", () => {
host: "localhost",
user: "user",
password: "password",
database: "database",
port: 5432,
});
return mock;
Expand All @@ -23,11 +24,23 @@ jest.mock("pg", () => {
});

test("it calls createConnection", () => {
const db = new PostgresDatabase("localhost", "user", "password", 5432);
const db = new PostgresDatabase(
"localhost",
"user",
"password",
"database",
5432
);
});

test("it calls callback if there is an error", async () => {
const db = new PostgresDatabase("localhost", "user", "password", 5432);
const db = new PostgresDatabase(
"localhost",
"user",
"password",
"database",
5432
);
const sql = "SELECT some_column FROM some_table WHERE id = 1";

expect(await db.lintQuery(sql)).toMatchObject({
Expand All @@ -37,7 +50,13 @@ test("it calls callback if there is an error", async () => {
});

test("it calls end on connection", () => {
const db = new PostgresDatabase("localhost", "user", "password", 5432);
const db = new PostgresDatabase(
"localhost",
"user",
"password",
"database",
5432
);
db.end();
});

Expand Down
4 changes: 4 additions & 0 deletions test/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jest.mock("mysql2", () => {
user: "user",
host: "localhost",
password: "password",
database: "database",
});
return mock;
},
Expand All @@ -90,6 +91,7 @@ test("it uses db connection is provided", async () => {
user: "user",
host: "localhost",
password: "password",
database: "database",
sql: "SELECT some_column FROM my_database.some_table;",
};

Expand All @@ -108,6 +110,7 @@ jest.mock("pg", () => {
host: "localhost",
user: "user",
password: "password",
database: "database",
port: 5432,
});
return mock;
Expand All @@ -128,6 +131,7 @@ test("it uses correct driver when provided", async () => {
driver: "postgres",
host: "localhost",
password: "password",
database: "database",
port: 5432,
sql: "SELECT some_column FROM my_database.some_table;",
user: "user",
Expand Down