What I'm trying to achieve is the ability to access tables within two databases by using database qualified table names (without specifically setting the default database with an SQL USE statement). My JavaScript application running on Node.js v10.15.3 is connecting to MySql with the NPM mysql 2.17.1 library as follows:
const mysql = require('mysql');
...
let configuration = {
host: "localhost",
port: 3306,
user: "user",
password: "password"
};
let connection = mysql.createConnection(configuration);
The connection is made but when I query using database qualified table names
let select = 'SELECT COUNT(*) AS count FROM application.table;';
connection.query(select);
I get:
(node:20332) UnhandledPromiseRejectionWarning: Error: ER_NO_DB_ERROR: No database selected
My guess is that this error was the mysql NPM library detecting that I hadn't specified a database to use, so I tried adding the following before the SELECT statement:
connection.query(`USE application;`);
...and now I get:
(node:10412) UnhandledPromiseRejectionWarning: Error: ER_NO_SUCH_TABLE: Table 'application.application.table' doesn't exist
So, the Node mysql library appears to be prepending the database name everywhere I reference any database object...
I'm actually storing the database qualified table names as JavaScript strings of the form 'application.table' or 'metadata.table' and would prefer to keep these in this format for simplicity of management (e.g. if the design required a table were to move from one database to the other we only need to change the string that references the table in one place).
Is there a Node mysql library configuration option to assure it that all database object references will be database qualified?