Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you connect to multiple databases using knex.js?

Tags:

knex.js

There is a process that takes data from one database and replicates it to another. They are on different db platforms. It is intended that knex.js be the middleware. This works under 0.10.0

var first = require("knex")(...);
var second = require("knex").initialize(...);

but complains that initialize is deprecated. Can someone give an example of how to do this in the current version of knex.js?

like image 210
P Hemans Avatar asked Jun 21 '17 00:06

P Hemans


People also ask

Is it possible to connect to multiple databases?

Enabling Multiple Database ConnectionsPerformance can degrade if you allow too many database connections for the number of processors that are available. Performance might also degrade if you increase the number of database connections to process small amounts of data.

How do I link multiple databases in node JS?

Can we connect multiple database in node JS? According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose. createConnection('mongodb://localhost/testA'); var conn2 = mongoose.

What is KNEX NPM?

Knex is a SQL query builder, mainly used for Node. js applications with built in model schema creation, table migrations, connection pooling and seeding.


1 Answers

Why don't you use same syntax as for first? I guess that .initialize is just outdated(deprecated) version of just function call.

var first = require("knex")(firstConfig);
var second = require("knex")(secondConfig);

first.select('*').from('users');
second.select('*').from('table');

And you have 2 different builders (each with different config).

like image 57
coockoo Avatar answered Sep 22 '22 21:09

coockoo