Skip to main content

Connecting to the database

To connect to the database, import the default database module and provide the necessary options.

import CollabDatabase from '@pdftron/collab-db-postgresql';
const db = new CollabDatabase({
host: '127.0.0.1',
port: 5432,
dbName: 'mydb',
username: 'postgres',
password: 'pdftron'
});

The options are as follows:

  • host (string) the host of your database
  • port (number) the port to connect on
  • dbName (string) the name of your postgres database
  • username (string) the user to connect as
  • password (string) the users password
  • connectionString (string) the connection string URI
  • ssl (object) passed directly to node.TLSSocket, supports all tls.connect options
  • statementTimeout (number) number of milliseconds before a statement in query will time out, default is no timeout
  • queryTimeout (number) number of milliseconds before a query call will timeout, default is no timeout
  • idleInTransactionSessionTimeout (number) number of milliseconds before terminating any session with an open idle transaction, default is no timeout

SSL#

The package supports TLS/SSL connections to your PostgreSQL server as long as the server is configured to support it. When instantiating a pool or a client you can provide an ssl property on the config object and it will be passed to the constructor for the node TLSSocket. node TLSSocket.

import CollabDatabase from '@pdftron/collab-db-postgresql';
const db = new CollabDatabase({
...options,
ssl: {
rejectUnauthorized: false,
key: fs.readFileSync('/path/to/private-keys/postgresql.key').toString(),
cert: fs.readFileSync('/path/to/certificates/postgresql.crt').toString()
}
});

Connect Database#

After you create the instance, call connectDB() to make the connection. Make sure the promise resolves before using the connection any further.

import CollabDatabase from '@pdftron/collab-db-postgresql';
const db = new CollabDatabase({
...options
});
await db.connectDB();

Next steps#