It is possible. See the following code. Before using it, you must upgrade Node.js to 7.6.0 or higher. You can use Postgresql by calling only the main(yourQuery) function. Found it on Google.
const pg = require('pg') // create a config to configure both pooling behavior // and client options // note: all config is optional and the environment variables // will be read if the config is not present var config = { user: 'username', // env var: PGUSER database: 'databaseName', // env var: PGDATABASE password: 'Password', // env var: PGPASSWORD host: 'localhost', // Server hosting the postgres database port: 35432, // env var: PGPORT max: 10, // max number of clients in the pool idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed } const pool = new pg.Pool(config) async function query (q) { const client = await pool.connect() let res try { await client.query('BEGIN') try { res = await client.query(q) await client.query('COMMIT') } catch (err) { await client.query('ROLLBACK') throw err } } finally { client.release() } return res } async function main (queryStr) { try { const { rows } = await query(queryStr); console.log(JSON.stringify(rows)); } catch (err) { console.log('Database ' + err) } } main('SELECT * FROM user where user = \'123\'')
Jasurbek nabijonov
source share