Skip to content

Commit f1b8dfc

Browse files
committed
docs: Add YugabyteDB smart driver features to Sequelize documentation
- Added subsection: Using the YugabyteDB Smart Driver - Explains how to use smart driver with sequelize-yugabytedb - Links to smart-drivers documentation - Shows package.json configuration - Simplified load balance properties section - Environment variables examples - Connection parameters examples - Removed detailed property table for brevity
1 parent f529c35 commit f1b8dfc

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

docs/content/stable/develop/drivers-orms/nodejs/sequelize.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ The following code creates an Employees model to store and retrieve employee inf
8888
- The actual table is created by calling the `Employee.sync()` API in the `createTableAndInsert()` function. This also inserts the data for three employees into the table using the `Employee.create()` API.
8989
- Finally, you can retrieve the information of all employees using `Employee.findAll()`.
9090
91+
#### Using the YugabyteDB Smart Driver
92+
93+
You can also use the YugabyteDB smart driver (`@yugabytedb/pg`) which provides connection load balancing feature that spreads connections uniformly across the nodes in the entire cluster or in specific placements (zones or regions).
94+
95+
Read more on the smart driver features [here](../../smart-drivers/#using-yugabytedb-smart-drivers).
96+
97+
To use the YugabyteDB smart driver instead of the vanilla PG driver, add the following to your application's `package.json` dependencies:
98+
99+
```json
100+
{
101+
"dependencies": {
102+
"sequelize-yugabytedb": "^1.0.5",
103+
"pg": "npm:@yugabytedb/pg@8.7.3-yb-10"
104+
}
105+
}
106+
```
107+
108+
This overrides the default PostgreSQL driver with the YugabyteDB smart driver.
109+
110+
To provide the load-balance specific properties, use either the environment variables or connection parameters. See the examples below for more details.
111+
91112
Add the code in the `example.js` file.
92113
93114
```js
@@ -189,6 +210,59 @@ Employees Details:
189210
]
190211
```
191212
213+
#### Specifying load balance properties
214+
215+
You can enable these features by specifying the properties either as the environment variables or via connection parameters.
216+
217+
##### Environment variables
218+
219+
```js
220+
const { Sequelize, DataTypes } = require('sequelize-yugabytedb')
221+
222+
// Enable load balancing across nodes in the cluster
223+
process.env.PGLOADBALANCE = 'any'; // Valid values: any, prefer-primary, prefer-rr, only-primary, only-rr
224+
// Specify the region(s)/zone(s) to target nodes from (Optional)
225+
process.env.PGTOPOLOGYKEYS = 'aws.us-east-2.us-east-2a';
226+
// Set the minimum time interval for refreshing the cluster topology information (Optional)
227+
process.env.PGYBSERVERSREFRESHINTERVAL = '5';
228+
229+
const sequelize = new Sequelize('yugabyte', 'yugabyte', 'yugabyte', {
230+
host: 'localhost',
231+
port: '5433',
232+
dialect: 'postgres'
233+
})
234+
```
235+
236+
You can also set these in your shell or in a `.env` file:
237+
238+
```sh
239+
export PGLOADBALANCE=any
240+
export PGTOPOLOGYKEYS=aws.us-east-2.us-east-2a
241+
export PGYBSERVERSREFRESHINTERVAL=5
242+
```
243+
244+
##### Connection parameters
245+
246+
Alternatively, you can specify load balancing properties directly in the connection string:
247+
248+
```js
249+
const connectionString =
250+
'postgres://yugabyte:yugabyte@localhost:5433/yugabyte' +
251+
'?loadBalance=any' +
252+
'&topologyKeys=aws.us-east-2.us-east-2a' +
253+
'&ybServersRefreshInterval=5';
254+
255+
const sequelize = new Sequelize(connectionString, {
256+
dialect: 'postgres',
257+
pool: {
258+
max: 10,
259+
min: 2,
260+
idle: 10000,
261+
acquire: 30000
262+
}
263+
});
264+
```
265+
192266
## Specifying SSL configuration
193267
194268
This configuration can be used while connecting to a YugabyteDB Aeon cluster or a local YB cluster with SSL enabled.

0 commit comments

Comments
 (0)