From 4c2bc08f1c008ad93aba57f5800ad5c6e2468d21 Mon Sep 17 00:00:00 2001 From: Mitar Date: Wed, 28 Apr 2021 21:36:13 -0700 Subject: [PATCH] Allow client instance instead of configuration. --- docs/configuration.md | 10 ++++++---- lib/index.js | 15 ++++++++++++--- types/index.d.ts | 4 +++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 1d8a158..0d2270a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2,11 +2,13 @@ ### connectionConfig -- Type: `object` +- Type: `object | Client` - Default: `null` - Required: `true` -Connection configuration object for [node-postgres](https://node-postgres.com/features/connecting#programmatic) +Connection configuration object for [node-postgres](https://node-postgres.com/features/connecting#programmatic). + +It can be an existing `pg.Client` instance, too. In this case the instance already has to be connected. ### defaultSchema @@ -14,7 +16,7 @@ Connection configuration object for [node-postgres](https://node-postgres.com/fe - Default: `'public'` - Required: `false` -It will be used in objects whose names do not contain a schema name +It will be used in objects whose names do not contain a schema name. ### logging @@ -22,4 +24,4 @@ It will be used in objects whose names do not contain a schema name - Default: `console.info` - Required: `false` -Option to enable logging in the console or callback of the format `function(...messages) {}` for displaying a message about changes +Option to enable logging in the console or callback of the format `function(...messages) {}` for displaying a message about changes. diff --git a/lib/index.js b/lib/index.js index 660bb68..465403c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,8 @@ const errors = require('./errors'); const Metalize = require('metalize'); +const { Client } = require('pg'); + const Logger = require('./logger'); const ConnectionManager = require('./connection-manager'); @@ -194,11 +196,16 @@ class Differ { let error; let preparedChanges; + let client; this._logger.info(chalk.green('Sync started')); - const client = ConnectionManager.getClient(this._connectionConfig); - await client.connect(); + if (this._connectionConfig instanceof Client) { + client = this._connectionConfig; + } else { + client = ConnectionManager.getClient(this._connectionConfig); + await client.connect(); + } try { await client.query('set search_path to public'); @@ -220,7 +227,9 @@ class Differ { error = e; } - await client.end(); + if (!(this._connectionConfig instanceof Client)) { + await client.end(); + } if (error) throw error; diff --git a/types/index.d.ts b/types/index.d.ts index 21d3782..99bce5b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import { Client } from 'pg'; + interface ReferenceOptions { table: string; columns: string[]; @@ -127,7 +129,7 @@ interface ImportOptions { } interface DifferOptions { - connectionConfig: object; + connectionConfig: object | Client; defaultSchema?: string; logging?: boolean | Function; }