# Integrating Our Databse --- ## Why do we want to integrate our database with our code? As engineers we need access to our data with in the code we write. The opposite is that we need a way to populate our data lakes with new data. We need to be able to do this *DYNAMICALLY* --- ## PG-Promise --- # Install ``` npm install pg-promise ``` --- ## Setup ``` const pgPromise = require('pg-promise')() // Lets talk about this. const db = pgPromise({host: '?', port: '?', database: '?' }) // -> ? ``` --- ## Usages ``` /**most popular usages*/ db.any(arg_1*, arg_2?); // read from DB db.one(arg_1*, arg_2?); // read from DB db.none(arg_1*, arg_2?); // write to DB ``` --- ## db.any() When we are expecting multiple results. ``` db.any(` SELECT * FROM table_name `).then(data => { console.log(data); }) ``` --- ## db.one() When we are expecting only one result. ``` db.one(` SELECT name FROM table_name WHERE id = 5 `).then(data => { console.log(data); }) ``` --- ## db.none() When we are not expecting results but we want to write to the DB ``` db.none(` INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2') `).then(data => { console.log(data); }) ``` --- ## The second argument Let's just look at the syntax --- ``` db.any(` SELECT name FROM table_name WHERE id = $1 `, [5]).then(data => { console.log(data); }) ``` ``` db.none(` INSERT INTO table_name (column1, column2) VALUES ('$1', '$2') `, ['value1', 'value2']).then(data => { console.log(data); }) ``` --- # Let's look at some code. </>
{"metaMigratedAt":"2023-06-17T02:59:22.295Z","metaMigratedFrom":"Content","title":"Integrating Our Databse","breaks":true,"contributors":"[{\"id\":\"5e29e175-4809-4add-a41e-e8982dab52a9\",\"add\":1776,\"del\":165}]"}
    147 views