From b4d9e4c0fc6eeb7718f4b9d66c8d07ea0a63889d Mon Sep 17 00:00:00 2001 From: Matteo Stellato Date: Tue, 4 Oct 2022 13:29:12 +0200 Subject: [PATCH 1/7] Added pool feature for the postgres connector --- deno.json | 3 + deps.ts | 3 +- import_map.json | 13 +++ lib/connectors/connector.ts | 19 +++- lib/connectors/postgres-connector.ts | 95 ++++++++++++++----- tests/connection.ts | 26 ++++- .../connectors/postgres/connection.test.ts | 10 ++ 7 files changed, 138 insertions(+), 31 deletions(-) create mode 100644 deno.json create mode 100644 import_map.json create mode 100644 tests/units/connectors/postgres/connection.test.ts diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..67ba286 --- /dev/null +++ b/deno.json @@ -0,0 +1,3 @@ +{ + "importMap": "./import_map.json" +} \ No newline at end of file diff --git a/deps.ts b/deps.ts index dd99bc2..a128e28 100644 --- a/deps.ts +++ b/deps.ts @@ -13,7 +13,8 @@ export { } from "https://deno.land/x/mysql@v2.10.1/mod.ts"; export type { LoggerConfig } from "https://deno.land/x/mysql@v2.10.1/mod.ts"; -export { Client as PostgresClient } from "https://deno.land/x/postgres@v0.14.2/mod.ts"; +export { Client as PostgresClient,Pool as PostgresPool } from "https://deno.land/x/postgres@v0.16.1/mod.ts"; +export type { ClientOptions as PostgresClientOptions, ConnectionString as PostgresConnectionString } from "https://deno.land/x/postgres@v0.16.1/mod.ts"; export { DB as SQLiteClient } from "https://deno.land/x/sqlite@v3.1.3/mod.ts"; diff --git a/import_map.json b/import_map.json new file mode 100644 index 0000000..0bcc656 --- /dev/null +++ b/import_map.json @@ -0,0 +1,13 @@ +{ + "imports": { + "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/timers.js": "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.24/nodelibs/deno/timers.ts", + "https://dev.jspm.io/inherits@2.0": "https://ga.jspm.io/npm:inherits@2.0.4/inherits.js", + "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/url.js": "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.24/nodelibs/deno/url.ts", + "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/events.js": "https://ga.jspm.io/npm:@jspm/core@1/nodelibs/node/events.ts" + }, + "scopes": { + "https://ga.jspm.io/": { + "util": "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.24/nodelibs/deno/util.ts" + } + } +} diff --git a/lib/connectors/connector.ts b/lib/connectors/connector.ts index 59cb9e2..6a2a897 100644 --- a/lib/connectors/connector.ts +++ b/lib/connectors/connector.ts @@ -2,10 +2,16 @@ import type { QueryDescription } from "../query-builder.ts"; import { Translator } from "../translators/translator.ts"; /** Default connector options. */ -export interface ConnectorOptions {} +export interface ConnectorOptions { } + +/** Default pool options. */ +export interface ConnectorPoolOptions { } /** Default connector client. */ -export interface ConnectorClient {} +export interface ConnectorClient { } + +/** Default connector pool. */ +export interface ConnectionPool { } /** Connector interface for a database provider connection. */ export interface Connector { @@ -16,19 +22,22 @@ export interface Connector { _translator: Translator; /** Client that maintains an external database connection. */ - _client: ConnectorClient; + _client?: ConnectorClient | undefined; /** Options to connect to an external instance. */ _options: ConnectorOptions; /** Is the client connected to an external instance. */ - _connected: boolean; + _connected?: boolean | undefined; + + /** Is the optional pool for making connections to an external instance. */ + _pool?: ConnectionPool | undefined; /** Test connection. */ ping(): Promise; /** Connect to an external database instance. */ - _makeConnection(): void; + _makeConnection(): void | ConnectorClient | Promise | Promise; /** Execute a query on the external database instance. */ query(queryDescription: QueryDescription): Promise; diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index c1c838f..234bddc 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -1,5 +1,5 @@ -import { PostgresClient } from "../../deps.ts"; -import type { Connector, ConnectorOptions } from "./connector.ts"; +import { PostgresClient, PostgresPool } from "../../deps.ts"; +import type { Connector, ConnectorOptions, ConnectorPoolOptions } from "./connector.ts"; import { SQLTranslator } from "../translators/sql-translator.ts"; import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator.ts"; import type { QueryDescription } from "../query-builder.ts"; @@ -17,24 +17,53 @@ interface PostgresOptionsWithURI extends ConnectorOptions { uri: string; } +interface PostgresPoolOptions extends ConnectorPoolOptions { + connection_params: PostgresOptionsWithConfig | PostgresOptionsWithURI, + size: number, + lazy: boolean, +} + export type PostgresOptions = + PostgresPoolOptions | PostgresOptionsWithConfig | PostgresOptionsWithURI; + + export class PostgresConnector implements Connector { _dialect: SupportedSQLDatabaseDialect = "postgres"; - - _client: PostgresClient; + _pool: PostgresPool | undefined; + _client: PostgresClient | undefined; _options: PostgresOptions; _translator: SQLTranslator; - _connected = false; - - /** Create a PostgreSQL connection. */ + _connected: boolean | undefined; + + /** Create a PostgreSQL connection + * @example + * //Direct connection usage: + * const connection = new PostgresConnector({ + * host: '...', + * username: 'user', + * password: 'password', + * database: 'airlines', + * }); + * //Pool connection usage: + * const connection = new PostgresConnector({ + * connection_params: { + * host: '...', + * username: 'user', + * password: 'password', + * database: 'airlines', + * }, + * size: 5, + * lazy: false + * }); + */ constructor(options: PostgresOptions) { this._options = options; if ("uri" in options) { this._client = new PostgresClient(options.uri); - } else { + } else if ("database" in options) { this._client = new PostgresClient({ hostname: options.host, user: options.username, @@ -43,24 +72,40 @@ export class PostgresConnector implements Connector { port: options.port ?? 5432, }); } + else { + this._pool = new PostgresPool("uri" in options.connection_params ? options.connection_params.uri : { + ...options.connection_params + }, + options.size, + options.lazy + ); + } this._translator = new SQLTranslator(this._dialect); } async _makeConnection() { - if (this._connected) { - return; + if (this._client) { + if (this._connected) { + return; + } + + await this._client.connect(); + this._connected = true; + return this._client; + } else { + return await this._pool!.connect(); } - await this._client.connect(); - this._connected = true; } async ping() { - await this._makeConnection(); + return await this.tryConnection(await this._makeConnection()); + } + async tryConnection(client?: PostgresClient) { try { const [result] = ( - await this._client.queryObject("SELECT 1 + 1 as result") + await client!.queryObject("SELECT 1 + 1 as result") ).rows; return result === 2; } catch { @@ -70,32 +115,34 @@ export class PostgresConnector implements Connector { // deno-lint-ignore no-explicit-any async query(queryDescription: QueryDescription): Promise { - await this._makeConnection(); - + const client = await this._makeConnection() const query = this._translator.translateToQuery(queryDescription); - const response = await this._client.queryObject(query); + const response = await client!.queryObject(query); const results = response.rows as Values[]; if (queryDescription.type === "insert") { return results.length === 1 ? results[0] : results; } - return results; } async transaction(queries: () => Promise) { - const transaction = this._client.createTransaction("transaction"); + const client = await this._makeConnection() + const transaction = client!.createTransaction("transaction"); await transaction.begin(); await queries(); return transaction.commit(); } async close() { - if (!this._connected) { - return; + if (this._client) { + if (!this._connected) { + return; + } + await this._client.end(); + this._connected = false; + } else { + await this._pool?.end()! } - - await this._client.end(); - this._connected = false; } } diff --git a/tests/connection.ts b/tests/connection.ts index ba54c23..104c5a8 100644 --- a/tests/connection.ts +++ b/tests/connection.ts @@ -15,6 +15,18 @@ const defaultSQLiteOptions = { filepath: "test.sqlite", }; +const defaultPostgreSQLPoolOptions = { + connection_params: { + database: "test", + host: "127.0.0.1", + username: env.DB_USER, + password: env.DB_PASS, + port: Number(env.DB_PORT), + }, + size: 5, + lazy: false +}; + const getMySQLConnection = (options = {}, debug = true): Database => { const connection: Database = new Database( { dialect: "mysql", debug }, @@ -39,4 +51,16 @@ const getSQLiteConnection = (options = {}, debug = true): Database => { return connection; }; -export { getMySQLConnection, getSQLiteConnection }; +const getPostgreSQLPoolConnection = (options = {}, debug = true): Database => { + const connection: Database = new Database( + { dialect: "postgres", debug }, + { + ...defaultPostgreSQLPoolOptions, + ...options, + }, + ); + + return connection; +}; + +export { getMySQLConnection, getSQLiteConnection, getPostgreSQLPoolConnection }; diff --git a/tests/units/connectors/postgres/connection.test.ts b/tests/units/connectors/postgres/connection.test.ts new file mode 100644 index 0000000..b2586ea --- /dev/null +++ b/tests/units/connectors/postgres/connection.test.ts @@ -0,0 +1,10 @@ +import { getPostgreSQLPoolConnection } from "../../../connection.ts"; +import { assertEquals } from "../../../deps.ts"; + +Deno.test("PostgreSQL: Connection", async function () { + const connection = getPostgreSQLPoolConnection(); + const ping = await connection.ping(); + await connection.close(); + + assertEquals(ping, true); +}); From 432e7aa7868626bfdb8ab798c90b4ddfae99dc94 Mon Sep 17 00:00:00 2001 From: Matteo Stellato Date: Tue, 11 Oct 2022 13:54:55 +0200 Subject: [PATCH 2/7] added stable support for postgres pool, added tests --- import_map.json | 12 +++------ lib/connectors/connector.ts | 3 ++- lib/connectors/postgres-connector.ts | 11 +++++--- lib/database.ts | 5 ++++ tests/connection.ts | 26 ++++++++++++++----- .../connectors/postgres/connection.test.ts | 12 +++++++-- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/import_map.json b/import_map.json index 0bcc656..c119769 100644 --- a/import_map.json +++ b/import_map.json @@ -1,13 +1,7 @@ { "imports": { - "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/timers.js": "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.24/nodelibs/deno/timers.ts", - "https://dev.jspm.io/inherits@2.0": "https://ga.jspm.io/npm:inherits@2.0.4/inherits.js", - "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/url.js": "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.24/nodelibs/deno/url.ts", - "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/events.js": "https://ga.jspm.io/npm:@jspm/core@1/nodelibs/node/events.ts" - }, - "scopes": { - "https://ga.jspm.io/": { - "util": "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.24/nodelibs/deno/util.ts" - } + "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/timers.js": "https://deno.land/std@0.159.0/node/timers.ts", + "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/url.js": "https://deno.land/std@0.159.0/node/url.ts", + "https://dev.jspm.io/npm:@jspm/core@1/nodelibs/events.js": "https://deno.land/std@0.159.0/node/events.ts" } } diff --git a/lib/connectors/connector.ts b/lib/connectors/connector.ts index 6a2a897..0c0ac55 100644 --- a/lib/connectors/connector.ts +++ b/lib/connectors/connector.ts @@ -1,3 +1,4 @@ +import { PostgresPool } from "../../deps.ts"; import type { QueryDescription } from "../query-builder.ts"; import { Translator } from "../translators/translator.ts"; @@ -11,7 +12,7 @@ export interface ConnectorPoolOptions { } export interface ConnectorClient { } /** Default connector pool. */ -export interface ConnectionPool { } +export interface ConnectionPool extends PostgresPool{ } /** Connector interface for a database provider connection. */ export interface Connector { diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index 234bddc..b57ad07 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -74,7 +74,9 @@ export class PostgresConnector implements Connector { } else { this._pool = new PostgresPool("uri" in options.connection_params ? options.connection_params.uri : { - ...options.connection_params + hostname: options.connection_params.host, + user: options.connection_params.username, + ...options.connection_params, }, options.size, options.lazy @@ -105,8 +107,8 @@ export class PostgresConnector implements Connector { async tryConnection(client?: PostgresClient) { try { const [result] = ( - await client!.queryObject("SELECT 1 + 1 as result") - ).rows; + await client!.queryArray("SELECT 1 + 1 as result") + ).rows[0]; return result === 2; } catch { return false; @@ -140,9 +142,10 @@ export class PostgresConnector implements Connector { return; } await this._client.end(); - this._connected = false; } else { await this._pool?.end()! } + this._connected = false; } + } diff --git a/lib/database.ts b/lib/database.ts index 48e884e..cf2f772 100644 --- a/lib/database.ts +++ b/lib/database.ts @@ -189,6 +189,11 @@ export class Database { return this.getConnector()._client; } + /* Get the database pool if existent. */ + getPool?() { + return this.getConnector()._pool; + } + /** Create the given models in the current database. * * await db.sync({ drop: true }); diff --git a/tests/connection.ts b/tests/connection.ts index 104c5a8..23bc553 100644 --- a/tests/connection.ts +++ b/tests/connection.ts @@ -17,16 +17,16 @@ const defaultSQLiteOptions = { const defaultPostgreSQLPoolOptions = { connection_params: { - database: "test", - host: "127.0.0.1", - username: env.DB_USER, - password: env.DB_PASS, - port: Number(env.DB_PORT), + uri: "postgres://postgres:user@localhost:5432/test" }, - size: 5, + size: 2, lazy: false }; +const defaultPostgreSQLOptions = { + uri: "postgres://postgres:user@localhost:5432/test" +}; + const getMySQLConnection = (options = {}, debug = true): Database => { const connection: Database = new Database( { dialect: "mysql", debug }, @@ -63,4 +63,16 @@ const getPostgreSQLPoolConnection = (options = {}, debug = true): Database => { return connection; }; -export { getMySQLConnection, getSQLiteConnection, getPostgreSQLPoolConnection }; +const getPostgreSQLConnection = (options = {}, debug = true): Database => { + const connection: Database = new Database( + { dialect: "postgres", debug }, + { + ...defaultPostgreSQLOptions, + ...options, + }, + ); + + return connection; +}; + +export { getMySQLConnection, getSQLiteConnection, getPostgreSQLPoolConnection, getPostgreSQLConnection }; diff --git a/tests/units/connectors/postgres/connection.test.ts b/tests/units/connectors/postgres/connection.test.ts index b2586ea..eb00f3f 100644 --- a/tests/units/connectors/postgres/connection.test.ts +++ b/tests/units/connectors/postgres/connection.test.ts @@ -1,10 +1,18 @@ -import { getPostgreSQLPoolConnection } from "../../../connection.ts"; +import { getPostgreSQLPoolConnection, getPostgreSQLConnection } from "../../../connection.ts"; import { assertEquals } from "../../../deps.ts"; -Deno.test("PostgreSQL: Connection", async function () { +Deno.test({ name: "PostgreSQL: Connection", sanitizeResources: false}, async function () { const connection = getPostgreSQLPoolConnection(); const ping = await connection.ping(); await connection.close(); assertEquals(ping, true); }); + +Deno.test({ name: "PostgreSQL: Pool Connection", sanitizeResources: false}, async function () { + const connection = getPostgreSQLConnection(); + const ping = await connection.ping(); + await connection.close(); + + assertEquals(ping, true); +}); From 66ae1259527e32b48c9a54dfceaad5a44f2dc3ac Mon Sep 17 00:00:00 2001 From: Matteo Stellato Date: Fri, 14 Oct 2022 17:59:32 +0200 Subject: [PATCH 3/7] Update lib/connectors/connector.ts removed PostgresPool class as Connector is a base class Co-authored-by: Arnaud --- lib/connectors/connector.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connectors/connector.ts b/lib/connectors/connector.ts index 0c0ac55..e87891b 100644 --- a/lib/connectors/connector.ts +++ b/lib/connectors/connector.ts @@ -12,7 +12,7 @@ export interface ConnectorPoolOptions { } export interface ConnectorClient { } /** Default connector pool. */ -export interface ConnectionPool extends PostgresPool{ } +export interface ConnectionPool { } /** Connector interface for a database provider connection. */ export interface Connector { From 050d1c3d32c1de0b6b2fafbbbc079fdb0c83d6f1 Mon Sep 17 00:00:00 2001 From: Matteo Stellato Date: Fri, 14 Oct 2022 18:00:27 +0200 Subject: [PATCH 4/7] Update lib/connectors/connector.ts Co-authored-by: Arnaud --- lib/connectors/connector.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connectors/connector.ts b/lib/connectors/connector.ts index e87891b..a2d4fda 100644 --- a/lib/connectors/connector.ts +++ b/lib/connectors/connector.ts @@ -23,7 +23,7 @@ export interface Connector { _translator: Translator; /** Client that maintains an external database connection. */ - _client?: ConnectorClient | undefined; + _client?: ConnectorClient; /** Options to connect to an external instance. */ _options: ConnectorOptions; From 3aeaec84bf20c61ccbf9ba531a3624205cc79e38 Mon Sep 17 00:00:00 2001 From: Matteo Stellato Date: Sat, 15 Oct 2022 13:34:38 +0200 Subject: [PATCH 5/7] fixed stuff as previously mentioned in #363 --- lib/connectors/connector.ts | 22 ++-- lib/connectors/postgres-connector.ts | 118 ++++++++---------- tests/connection.ts | 6 +- .../connectors/postgres/connection.test.ts | 4 +- 4 files changed, 71 insertions(+), 79 deletions(-) diff --git a/lib/connectors/connector.ts b/lib/connectors/connector.ts index a2d4fda..8168429 100644 --- a/lib/connectors/connector.ts +++ b/lib/connectors/connector.ts @@ -1,4 +1,3 @@ -import { PostgresPool } from "../../deps.ts"; import type { QueryDescription } from "../query-builder.ts"; import { Translator } from "../translators/translator.ts"; @@ -12,7 +11,7 @@ export interface ConnectorPoolOptions { } export interface ConnectorClient { } /** Default connector pool. */ -export interface ConnectionPool { } +export interface ConnectorPool { } /** Connector interface for a database provider connection. */ export interface Connector { @@ -29,16 +28,25 @@ export interface Connector { _options: ConnectorOptions; /** Is the client connected to an external instance. */ - _connected?: boolean | undefined; + _connected?: boolean /** Is the optional pool for making connections to an external instance. */ - _pool?: ConnectionPool | undefined; + _pool?: ConnectorPool - /** Test connection. */ - ping(): Promise; + /** Gets the client or the pool connected to the database*/ + _getClientOrPool(): ConnectorPool | ConnectorClient /** Connect to an external database instance. */ - _makeConnection(): void | ConnectorClient | Promise | Promise; + _makeConnection(): void | Promise + + /** Gets the client connected to the database */ + getClient(): any + + /** Gets the pool connected to the database */ + getPool?(): any + + /** Test connection. */ + ping(): Promise; /** Execute a query on the external database instance. */ query(queryDescription: QueryDescription): Promise; diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index b57ad07..0709310 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -17,98 +17,86 @@ interface PostgresOptionsWithURI extends ConnectorOptions { uri: string; } -interface PostgresPoolOptions extends ConnectorPoolOptions { - connection_params: PostgresOptionsWithConfig | PostgresOptionsWithURI, - size: number, - lazy: boolean, +interface PostgresPoolOptions extends ConnectorPoolOptions, PostgresOptionsWithConfig, PostgresOptionsWithURI { + size: number; + lazy: boolean; } -export type PostgresOptions = - PostgresPoolOptions - | PostgresOptionsWithConfig - | PostgresOptionsWithURI; - - +export type PostgresOptions = PostgresPoolOptions; export class PostgresConnector implements Connector { _dialect: SupportedSQLDatabaseDialect = "postgres"; - _pool: PostgresPool | undefined; - _client: PostgresClient | undefined; + _pool?: PostgresPool; + _client?: PostgresClient; _options: PostgresOptions; _translator: SQLTranslator; - _connected: boolean | undefined; - - /** Create a PostgreSQL connection - * @example - * //Direct connection usage: - * const connection = new PostgresConnector({ - * host: '...', - * username: 'user', - * password: 'password', - * database: 'airlines', - * }); - * //Pool connection usage: - * const connection = new PostgresConnector({ - * connection_params: { - * host: '...', - * username: 'user', - * password: 'password', - * database: 'airlines', - * }, - * size: 5, - * lazy: false - * }); - */ + _connected = false; + constructor(options: PostgresOptions) { this._options = options; - if ("uri" in options) { - this._client = new PostgresClient(options.uri); - } else if ("database" in options) { - this._client = new PostgresClient({ + if (this._isPoolConnector()) { + this._pool = new PostgresPool("uri" in options ? options.uri : { hostname: options.host, user: options.username, - password: options.password, - database: options.database, - port: options.port ?? 5432, - }); - } - else { - this._pool = new PostgresPool("uri" in options.connection_params ? options.connection_params.uri : { - hostname: options.connection_params.host, - user: options.connection_params.username, - ...options.connection_params, + ...options, }, options.size, options.lazy ); - } + } else + if ("uri" in options) { + this._client = new PostgresClient(options.uri); + } else { + this._client = new PostgresClient({ + hostname: options.host, + user: options.username, + password: options.password, + database: options.database, + port: options.port ?? 5432, + }); + } this._translator = new SQLTranslator(this._dialect); } + _isPoolConnector() { + return "size" in this._options; + } + + _getClientOrPool() { + return this._isPoolConnector() ? this.getPool()! : this.getClient()!; + } + async _makeConnection() { - if (this._client) { + if (!this._isPoolConnector()) { if (this._connected) { - return; + return this._client!; } - - await this._client.connect(); - this._connected = true; - return this._client; + await this._client!.connect(); + return this._client!; + } else if (this._pool?.available || !this._pool?.available) { + return await this.getPool()?.connect() } else { - return await this._pool!.connect(); + throw new Error("no connections available") } + } + getClient() { + return this._client; } - async ping() { - return await this.tryConnection(await this._makeConnection()); + getPool() { + return this._pool; } - async tryConnection(client?: PostgresClient) { + async ping() { try { - const [result] = ( - await client!.queryArray("SELECT 1 + 1 as result") - ).rows[0]; + const connection = await this._makeConnection(); + console.log(connection) + const [result] = + (await connection!.queryArray("SELECT 1 + 1 as result") + ).rows[0]; + + console.log(result) return result === 2; } catch { return false; @@ -141,9 +129,7 @@ export class PostgresConnector implements Connector { if (!this._connected) { return; } - await this._client.end(); - } else { - await this._pool?.end()! + await this._getClientOrPool().end(); } this._connected = false; } diff --git a/tests/connection.ts b/tests/connection.ts index 23bc553..8107276 100644 --- a/tests/connection.ts +++ b/tests/connection.ts @@ -16,11 +16,9 @@ const defaultSQLiteOptions = { }; const defaultPostgreSQLPoolOptions = { - connection_params: { - uri: "postgres://postgres:user@localhost:5432/test" - }, + uri: "postgres://postgres:user@localhost:5432/test", size: 2, - lazy: false + lazy: true }; const defaultPostgreSQLOptions = { diff --git a/tests/units/connectors/postgres/connection.test.ts b/tests/units/connectors/postgres/connection.test.ts index eb00f3f..bc21c7a 100644 --- a/tests/units/connectors/postgres/connection.test.ts +++ b/tests/units/connectors/postgres/connection.test.ts @@ -2,7 +2,7 @@ import { getPostgreSQLPoolConnection, getPostgreSQLConnection } from "../../../c import { assertEquals } from "../../../deps.ts"; Deno.test({ name: "PostgreSQL: Connection", sanitizeResources: false}, async function () { - const connection = getPostgreSQLPoolConnection(); + const connection = getPostgreSQLConnection(); const ping = await connection.ping(); await connection.close(); @@ -10,7 +10,7 @@ Deno.test({ name: "PostgreSQL: Connection", sanitizeResources: false}, async fun }); Deno.test({ name: "PostgreSQL: Pool Connection", sanitizeResources: false}, async function () { - const connection = getPostgreSQLConnection(); + const connection = getPostgreSQLPoolConnection(); const ping = await connection.ping(); await connection.close(); From c38e2db11ffecd4751c1c18df5aa7d58f47866c3 Mon Sep 17 00:00:00 2001 From: Aiko-Suzuki <42787030+Aiko-Suzuki@users.noreply.github.com> Date: Sun, 23 Oct 2022 09:10:19 -0400 Subject: [PATCH 6/7] making getClient & _getClientOrPool optional --- lib/connectors/connector.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/connectors/connector.ts b/lib/connectors/connector.ts index 8168429..289a391 100644 --- a/lib/connectors/connector.ts +++ b/lib/connectors/connector.ts @@ -34,13 +34,13 @@ export interface Connector { _pool?: ConnectorPool /** Gets the client or the pool connected to the database*/ - _getClientOrPool(): ConnectorPool | ConnectorClient + _getClientOrPool?(): ConnectorPool | ConnectorClient /** Connect to an external database instance. */ _makeConnection(): void | Promise /** Gets the client connected to the database */ - getClient(): any + getClient?(): any /** Gets the pool connected to the database */ getPool?(): any From 3bd2b4700a8d78b3593a55439cc7ea9ae6e149c0 Mon Sep 17 00:00:00 2001 From: Aiko-Suzuki <42787030+Aiko-Suzuki@users.noreply.github.com> Date: Sun, 23 Oct 2022 09:18:35 -0400 Subject: [PATCH 7/7] update test --- .github/workflows/test-mysql.yml | 9 +++++++++ tests/connection.ts | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-mysql.yml b/.github/workflows/test-mysql.yml index 25abac4..c70eb85 100644 --- a/.github/workflows/test-mysql.yml +++ b/.github/workflows/test-mysql.yml @@ -22,6 +22,15 @@ jobs: ports: - 3306 options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=5s --health-retries=3 + postgres: + image: postgres:11 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: test + ports: + - 5432:5432 + options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 steps: - uses: actions/checkout@v2 diff --git a/tests/connection.ts b/tests/connection.ts index 1fba684..2f45e27 100644 --- a/tests/connection.ts +++ b/tests/connection.ts @@ -16,13 +16,13 @@ const defaultSQLiteOptions = { }; const defaultPostgreSQLPoolOptions = { - uri: "postgres://postgres:user@localhost:5432/test", + uri: "postgres://postgres:postgres@localhost:5432/test", size: 2, - lazy: true + lazy: true, }; const defaultPostgreSQLOptions = { - uri: "postgres://postgres:user@localhost:5432/test" + uri: "postgres://postgres:postgres@localhost:5432/test", }; const getMySQLConnection = (options = {}, debug = true): Database => {