db.connect((err, client) => { if (err) throw err; console.log('Connected to database', db.database); db.end(); });
handle(notification) { const database = this.client.database; const {channel, payload: str} = notification; logger.info(msg.RECEIVED_NOTIFICATION, green(database + ':' + channel), dim(str || '(empty)')); try { const payload = this.parsePayload(str); // Invoke all the handlers registered on the channel this.invokeHandlers(channel, payload); } catch (e) { error(e.message); } }
listenTo(channel) { const database = this.client.database; this.client.query(`LISTEN ${channel}`).then(() => { logger.info(msg.STARTED_LISTENING, green(database + ':' + channel)); // Warn if handlers are not registered for the channels being listened to if (!Array.isArray(this.handlers[channel])) { logger.warn(msg.WARN_NO_HANDLERS_FOUND, channel); } }); }
db.connect((err, client) => { if (err) throw err; console.log('Connected to database', db.database); db.query(` SELECT * FROM snippets ORDER BY id `, (err, result) => { if (err) throw err; console.log(result.rows); db.end(); }); });
db.connect((err, client) => { if (err) throw err; console.log('Connected to database', db.database); const body = 'hello world'; db.query(` INSERT INTO snippets (body) VALUES ( '${body}' ) RETURNING id `, (err, result) => { if (err) throw err; const id = result.rows[0].id; console.log('Inserted row with id %s', id); db.query(` INSERT INTO snippets (body) VALUES ( '${body}' ) RETURNING id `, () => { if (err) throw err; const id = result.rows[0].id; console.log('Inserted row with id %s', id); db.end(); }); }); });
db.connect((err, client) => { if (err) throw err; console.log('Connected to database', db.database); const body = 'hello world'; db.query(` INSERT INTO snippets (body) VALUES ( '${body}' ) RETURNING id `, (err, result) => { if (err) throw err; const id = result.rows[0].id; const updatedBody = 'greetings, world'; console.log('Inserted row with id %s', id); db.query(` UPDATE snippets SET (body) = ( '${body}' ) WHERE id=${id}; `, (err, result) => { if (err) throw err; console.log('Updated %s rows.', result.rowCount); db.end(); }); }); });
db.connect((err, client) => { if (err) throw err; console.log('Connected to database', db.database); db.query(` CREATE TABLE IF NOT EXISTS snippets ( id SERIAL, PRIMARY KEY(id), body text ); `, (err, result) => { if (err) throw err; console.log('Created table "snippets"'); db.end(); }); });