Nodejs how make connection pool?

Hello,

how can I use connection pool with the nodejs sdk ? Is that possible ? If not is there a tutorial how can I implement this or do I dont need this with couchbase ?

The SDKs maintain connection pools internally. See the documentation for configuration.

Thanks!

In the docs I saw this:

Connection Lifecycle

We recommend creating a single Cluster instance when your application starts up, and sharing this instance throughout your application. Each of the respective sub-instances (Bucket, Collection, etc…​) of the Cluster class can be stored and re-used, or created in an on-demand fashion whenever needed.

Before your application stops, gracefully shut down the client by calling the close() method of each Cluster you created.

So I have init the connection like this that I dont call every time connect ?

index:

const app = express();

export const db = couchbase_db();

app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(auth(config));


app.get('/test', async (req, res) => {
  try {
    const d = (await db).scope('travel')

    const a = await d.query('SELECT * FROM products;');

    console.log(a.rows);
    res.send('');
  } catch(e) {
    console.log(e);
    res.send(e);
  }
});

app.listen(3000, () => {
  console.log('Application started on port 3000!');
});

db.ts

import {
  Bucket,
  Cluster,
  Collection,
  connect,
  GetResult,
} from 'couchbase';
import * as Sentry from '@sentry/node';

export const cbdb = async () => {
  try {
    const cluster: Cluster = await connect('couchbases://xxxx', {
      username: 'xxxx',
      password: 'xxxx',
configProfile: "wanDevelopment",
    });

    const bucket: Bucket = cluster.bucket('db1');

    return bucket;
  } catch(e) {
    console.log(e);
    Sentry.captureException(`${e}`);
  }
};

Do I make it right ?

That looks good. db = couchbase_db(); The method that gets your connection is cbdb(), right?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.