Skip to content

Commit

Permalink
Add connect to PG task
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Fallon committed May 17, 2024
1 parent edd9638 commit 98667d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Connect to PostgreSQL
# Runs a script that creates a PostgreSQL client, populates client with data, and retrieves data
run: node client_pg.js
# Environment variable used by the `client_pg.js` script to create a new PostgreSQL client.
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
- name: RSPEC tests
run: |
RAILS_ENV=test bin/rails db:create
Expand Down
29 changes: 29 additions & 0 deletions client_pg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { Client } = require('pg');

const pgclient = new Client({
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
user: 'postgres',
password: 'postgres',
database: 'postgres'
});

pgclient.connect();

const table = 'CREATE TABLE student(id SERIAL PRIMARY KEY, firstName VARCHAR(40) NOT NULL, lastName VARCHAR(40) NOT NULL, age INT, address VARCHAR(80), email VARCHAR(40))'
const text = 'INSERT INTO student(firstname, lastname, age, address, email) VALUES($1, $2, $3, $4, $5) RETURNING *'
const values = ['Mona the', 'Octocat', 9, '88 Colin P Kelly Jr St, San Francisco, CA 94107, United States', 'octocat@github.com']

pgclient.query(table, (err, res) => {
if (err) throw err
});

pgclient.query(text, values, (err, res) => {
if (err) throw err
});

pgclient.query('SELECT * FROM student', (err, res) => {
if (err) throw err
console.log(err, res.rows) // Print the data in student table
pgclient.end()
});

0 comments on commit 98667d7

Please sign in to comment.