How to run node postgres inside Electron? - javascript

I'm trying to use postgres (pg) inside electron.
When the window opens, the connection and query works normally, but when the electron window is reloaded, pg connect and queries does not return anything.
<script>
const { Client } = require('pg')
async function query() {
const client = new Client({
host: '192.168.99.100',
port: 5433,
user: 'user',
password: 'password',
database: 'database',
})
console.log(await client.connect())
const res = await client.query('SELECT * from users')
console.log(res)
await client.end()
}
query()
</script>
PS: If i try to use Knex for connect and query the database, it returns this error:
Uncaught (in promise) KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
at Client_PG.acquireConnection

Related

How to fix in KnexJs the error destroy of undefined

In my Node application, I'm running a script to generate a DB that uses the KnexJs query builder and the PostgreSQL
"node": "~14.17.6"
"knex": "^0.95.15"
"pg": "^8.7.1"
The error I'm getting
Cannot read property 'destroy' of undefined
TypeError: Cannot read property 'destroy' of undefined
at process.value (/app/node_modules/knex/lib/knex-builder/make-knex.js:91:26)
at process.emit (events.js:315:20)
at process.exit (internal/process/per_thread.js:169:15)
at success (/app/node_modules/knex/bin/utils/cli-config-utils.js:76:11)
at Command.<anonymous> (/app/node_modules/knex/bin/cli.js:236:9)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
The script I'm running is as follow
const conn = {
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_USER,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
charset: 'utf8',
};
const databaseName = process.env.POSTGRES_DB;
const knex = require('knex')({ client: 'pg', connection: conn });
knex
.raw('CREATE DATABASE ??', databaseName)
.then(() => console.info('Successfully created db: ' + databaseName))
.catch((err) =>
console.warn('Warning: Unable to create db. Probably already exists.', err)
)
.finally(() => knex.destroy())
.then(() => {
const connection2 = knex({
client: 'pg',
connection: { ...conn, database: databaseName },
});
return connection2
.raw('CREATE EXTENSION IF NOT EXISTS citext')
.then(() => console.info('Successfully created extension citext'))
.catch((err) => console.error('Unable to create extension citext.', err))
.finally(() => connection2.destroy());
})
.then(() => process.exit(0));
I'm unable to understand what causes the issue and why
I think the issue probably is due to misunderstanding about what is the knex instance and what is the connection instance.
In this part:
const knex = require('knex')({ client: 'pg', connection: conn });
You defined a connection using the knex module. So, when you invoke "knex" to create a second connection below:
.then(() => {
const connection2 = knex({
client: 'pg',
connection: { ...conn, database: databaseName },
});
connection2 is not a connection because knex in this context is the desired connection. You could use the previously knex connection object.
To avoid these issues, I recommend you to separate things and rename variables to make it more explainable:
const knex = require('knex') // this is the knex module
const connection = knex({ client: 'pg', connection: conn }) // This is a connection using knex
// So you should use `connection` instead `knex`, as you are using the connection instance to perform queries
connection
.raw('CREATE DATABASE ??', databaseName)
// ...
.then(() => {
// Now this should work and return a connection
const connection2 = knex({
client: 'pg',
connection: { ...conn, database: databaseName },
});
// But as connection2 is actually the same of connection, it could be
// const connection2 = connection;
return connection2 // or return connection
.raw('CREATE EXTENSION IF NOT EXISTS citext')
.then(() => console.info('Successfully created extension citext'))
.catch((err) => console.error('Unable to create extension citext.', err))
.finally(() => connection2.destroy());
})
Another point to talk about is that if is really necessary to ends the connection and to create another one just after first destroy() call. As you can have an instance to the connection, so you can use it while you are performing queries and just destroy it once if it is not necessary anymore.

Unable to reach the proper tables in Postgres from Node using node-postgres

I'm trying to connect to postgres from Node using node-postgres, but even though the authentication is granted, I'm unable to search reach the tables. For example, when I perform:
const { Client } = require('pg')
const client = new Client({
user: 'username',
password: 'somepassword',
host: 'hostinfo',
port: '5432',
database: 'databaseInfo',
ssl: true,
})
const execute = async() => {
try {
await client.connect()
await client.query("BEGIN")
const results = await client.query("select * from User")
await client.query("COMMIT")
console.log(results.rows)
} catch(err) {
throw new Error(err)
} finally {
await client.end()
console.log("Finished")
}
}
execute()
it returns the User of the database, which is the 'username' that I used to access the database, not the content of the User table.
If I were to search for any other tables in the database, say the Review table, the error message shows
UnhandledPromiseRejectionWarning: Error: error: relation "review" does
not exist
If your table is really named "User" (with a capital "U"), you should be quoting its name when querying, like this:
const results = await client.query('select * from "User"')
When unquoted, PostgreSQL will think you're calling the function alias user, which is the same as current_user.
References:
https://www.postgresql.org/docs/current/functions-info.html
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Cannot connect to MSSQL server using node-mssql

const sql = require("mssql");
var sqlconfig = {
server:"192.168.200.5",
database:"DATA",
user: "labo",
password: "*********",
connectionTimeout: 30000,
port: 1433,
dialect: "mssql",
dialectOptions: {
"instanceName": "SQLEXPRESS"
},
};
new sql.ConnectionPool(sqlconfig).connect().then(pool => {
console.log(pool); return pool.query('select * from data where id = 2')
}).then(result=> {
console.dir(result)
}).catch(err => {
console.log(err);
})
Problem: Return Login failed for user 'Labo' ELOGIN when trying to login to Microsoft SQL Server using node.js.
What I have tried: Grab the example codes on npm to find the avoid extra problems. Error still persisted.
What I suspect: The username I inserted was "labo" but the one returned from err was 'Labo' (with capitalized 'L'). Is this a bug?
This is my code

Node.js MySQL connection not being released properly

I think my connections aren't being released properly. Sometimes I get an error stating that my pool has reached its limit. Also, sometimes accessing the db randomly takes 15+ seconds. Whenever I check how many connections are in use using pool._allConnections.length, it never returns anything above 60. Here is my code:
const mysql = require('mysql');
const config = require('./config.json');
const pool = mysql.createPool({
connectionLimit : 999,
host: config.host,
user: config.user,
password: config.password,
database: config.database
});
const db = (() => {
_query = (query, params, callback) => {
pool.getConnection((err, connection) => {
if (err) {
callback(null, err);
} else {
connection.query(query, params, (err, rows) => {
connection.release();
if (!err) {
callback(rows);
} else {
callback(null, err);
}
});
}
});
};
return {
query: _query
};
})();
module.exports = db;
i've faced same issue and https://github.com/mysqljs/mysql/issues/1518 help me. Notice line
Yeah, that was the issue. I was calling mysql.createPool on each
query.
Actually you are importing db from query.js (let say your post code) to fire a query. every time you fire a query it create a new pool.to solve this issue you can put createPool code block to app.js and can share it global or can use in query.js via any other code style.
Referring official doc https://github.com/mysqljs/mysql#pooling-connections find line
Since the pool.query method is a short-hand for the pool.getConnection
-> connection.query -> connection.release() flow, calling pool.end() before all the queries added via pool.query have completed,
later i used this to stop headache of release connection

Unable to connect MySQL DB, getting ECONNREFUSED

I tried to connect the DB via MySQL Java Script as below:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "******",
user: "******",
password: "******"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
And I'm using Node to execute, While I execute the above program getting the below error message:
if (err) throw err;
^
Error: connect ECONNREFUSED XX.XXX.XX.XXX:3306
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
Also I have pinged my ip address (ping XX.XXX.XX.XXX) and can able to get the response. Can help me to connect the DB.
I had the same issue and the error resolved by adding the socketPath parameter
var connection = mysql.createConnection({
user: 'user',
password: 'pass',
socketPath: '/var/run/mysqld/mysqld.sock',
database: 'dbname'
});
I highly suggest creating a connectionPool to save your resources. Here is how I did it:
//import settings.js
const db_config = {
hostname : "localhost",
user : settings.user, //username
password : settings.password, //password
database : settings.database //db
}
//create db connection pool
const con = mysql.createPool(db_config);
So the next step is to use that connection pool! Don't be scared about the async/bluebird nature of this code, it's just how I built it.
async function query(sql, params) {
const connection = await con.getConnectionAsync();
return connection.queryAsync(sql,params)
.then(rows => rows)
.finally(() => connection.release());
}
This is grabbing the connection getConnection method (Async is from bluebird promisifying the method), and using the query method (async'd), and then finally releasing the connection back into the pool. This helps save you from memory leaks caused by unclosed TCP connections.
Make sure that you also specify the port if you have changed it from the default MySQL port as well.

Categories

Resources