|
| 1 | +--- |
| 2 | +id: pg-cluster |
| 3 | +title: Postgres Node.js Cluster Connection |
| 4 | +sidebar_label: Cluster |
| 5 | +--- |
| 6 | + |
| 7 | +The `Cluster` object represents group of physical connections or connection pools to the underlying database in a primary & replicas setup. |
| 8 | + |
| 9 | +### `Cluster.query(SQLQuery): Promise<any[]>` |
| 10 | + |
| 11 | +Run an SQL Query and get a promise for an array of results. If your query contains multiple statements, only the results of the final statement are returned. |
| 12 | + |
| 13 | +Write queries are executed in the primary connection, and read-only queries are executed in the replica connections. |
| 14 | + |
| 15 | +```ts |
| 16 | +// query executed on a secondary connection |
| 17 | +const result = await cluster.query(sql`SELECT 1 + 1 AS a`); |
| 18 | +result[0].a; |
| 19 | +// => 2 |
| 20 | + |
| 21 | +// query executed on the primary connection |
| 22 | +await cluster.query(sql`UPDATE users SET active = true`); |
| 23 | +``` |
| 24 | + |
| 25 | +### `Cluster.query(SQLQuery[]): Promise<any[]>` |
| 26 | + |
| 27 | +If you pass an array of SQLQueries, you will get an array in response where each element of the array is the results of one of the queries. |
| 28 | + |
| 29 | +If there is at least one write query in the argument list, then the queries are executed in the primary connection, otherwise in the replica connections. |
| 30 | + |
| 31 | +```ts |
| 32 | +// query executed on a secondary connection |
| 33 | +const [resultA, resultB] = await cluster.query([ |
| 34 | + sql`SELECT 1 + 1 AS a`, |
| 35 | + sql`SELECT 1 + 1 AS b`, |
| 36 | +]); |
| 37 | +resultA[0].a + resultB[0].b; |
| 38 | +// => 4 |
| 39 | +``` |
| 40 | + |
| 41 | +### `Cluster.queryStream(SQLQuery): AsyncIterable<any>` |
| 42 | + |
| 43 | +Run an SQL Query and get an async iterable of the results. e.g. |
| 44 | + |
| 45 | +```js |
| 46 | +for await (const record of cluster.queryStream( |
| 47 | + sql`SELECT * FROM massive_table`, |
| 48 | +)) { |
| 49 | + console.log(result); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Write queries are executed in the primary connection, and read-only queries are executed in the replica connections. |
| 54 | + |
| 55 | +### `Cluster.queryNodeStream(SQLQuery): ReadableStream` |
| 56 | + |
| 57 | +Run an SQL Query and get a node.js readable stream of the results. e.g. |
| 58 | + |
| 59 | +```js |
| 60 | +const Stringifier = require('newline-json').Stringifier; |
| 61 | + |
| 62 | +cluster |
| 63 | + .queryNodeStream(sql`SELECT * FROM massive_table`) |
| 64 | + .pipe(new Stringifier()) |
| 65 | + .pipe(process.stdout); |
| 66 | +``` |
| 67 | + |
| 68 | +Write queries are executed in the primary connection, and read-only queries are executed in the replica connections. |
| 69 | + |
| 70 | +### `Cluster.tx<T>(fn: (tx: Transaction) => Promise<T>, options?): Promise<T>` |
| 71 | + |
| 72 | +Executes the callback `fn` within a transaction on the primary connection or replica connections (depending on `options.readOnly`). |
| 73 | + |
| 74 | +A transaction wraps a regular task with 3 additional queries: |
| 75 | + |
| 76 | +1. it executes `BEGIN` just before invoking the callback function |
| 77 | +2. it executes `ROLLBACK`, if the callback throws an error or returns a rejected promise |
| 78 | +3. it executes `COMMIT`, if the callback does throw any error and does not return a rejected promise |
| 79 | + |
| 80 | +```ts |
| 81 | +// transaction executed on a replica connection |
| 82 | +const result = await cluster.tx( |
| 83 | + async (tx) => { |
| 84 | + const resultA = await tx.query(sql`SELECT 1 + 1 AS a`); |
| 85 | + const resultB = await tx.query(sql`SELECT 1 + 1 AS b`); |
| 86 | + return resultA[0].a + resultB[0].b; |
| 87 | + }, |
| 88 | + {readOnly: true}, |
| 89 | +); |
| 90 | + |
| 91 | +// transaction executed on the primary connection |
| 92 | +const resultPrimary = await cluster.tx(async (tx) => { |
| 93 | + const resultA = await tx.query(sql`SELECT 1 + 1 AS a`); |
| 94 | + const resultB = await tx.query(sql`SELECT 1 + 1 AS b`); |
| 95 | + return resultA[0].a + resultB[0].b; |
| 96 | +}); |
| 97 | +// => 4 |
| 98 | +``` |
| 99 | + |
| 100 | +### `Cluster.task(fn): Promise<T>` |
| 101 | + |
| 102 | +This method exists to mimic the API in `ConnectionPool.task`. It executes the `task` method on the primary connection. |
0 commit comments