Node

Node Global Timer Simple Web Server Module Postgres DB
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

Node : Postgres DB

2022-02-10

Simple Test

const { Client } = require('pg');
const connectionString = 'postgres://postgres@127.0.0.1:5432/node1';
const client = new Client({
    connectionString: connectionString
});
client.connect();
client.query('SELECT * FROM table1', function (err, result) {
    console.log(err ? err.stack : result.rows) 
    client.end();
});

Module

index.js

const db = require('./database');
db.query('SELECT * FROM table1', function (err, result) {
    console.log(err ? err.stack : result.rows) 
});

database.js

const { Client } = require('pg');
const connectionString = 'postgres://postgres@127.0.0.1:5432/node1';
const client = new Client({
    connectionString: connectionString
});
client.connect();
module.exports = client;

DB as Global var

global.db = require('./database');
db.query('SELECT * FROM table1', function (err, result) {
    console.log(err ? err.stack : result.rows) 
});
let username = "test";
let password = "test";
db.query("SELECT * FROM users WHERE username = '?' AND password = '?'", [username, password], function(err, result, fields) {
    console.log(err ? err.stack : result.rows) 
});