Skip to content
Open
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
10 changes: 6 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

### 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

- Type: `string`
- 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

- Type: `boolean | function`
- 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.
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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');
Expand All @@ -220,7 +227,9 @@ class Differ {
error = e;
}

await client.end();
if (!(this._connectionConfig instanceof Client)) {
await client.end();
}

if (error) throw error;

Expand Down
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import { Client } from 'pg';

interface ReferenceOptions {
table: string;
columns: string[];
Expand Down Expand Up @@ -127,7 +129,7 @@ interface ImportOptions {
}

interface DifferOptions {
connectionConfig: object;
connectionConfig: object | Client;
defaultSchema?: string;
logging?: boolean | Function;
}
Expand Down