Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Week1/databases/connectDatabases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pkg from "pg";
const { Client } = pkg;

export async function connectDB(database = "world") {
const client = new Client({
user: "hyfuser",
host: "localhost",
database: database,
password: "hyfpassword",
port: 5432,
});

try {
await client.connect();
console.log(`Connected to database: ${client.database}`);
return client;
} catch (error) {
console.error("Connection error:", error);
throw error;
}
}
64 changes: 64 additions & 0 deletions Week1/databases/exercise2_world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { connectDB } from "./connectDatabase.js";

async function runQueries() {
const client = await connectDB("world");

try {
const q1 = await client.query(
`SELECT name FROM country WHERE population > 8000000;`
);
console.log("1. Countries > 8M:", q1.rows);

const q2 = await client.query(
`SELECT name FROM country WHERE name ILIKE '%land%';`
);
console.log("2. Countries with 'land':", q2.rows);

const q3 = await client.query(
`SELECT name FROM city WHERE population BETWEEN 500000 AND 1000000;`
);
console.log("3. Cities 500k–1M:", q3.rows);

const q4 = await client.query(
`SELECT name FROM country WHERE continent = 'Europe';`
);
console.log("4. European countries:", q4.rows);

const q5 = await client.query(
`SELECT name FROM country ORDER BY surfacearea DESC;`
);
console.log("5. Countries by surface area DESC:", q5.rows);

const q6 = await client.query(
`SELECT name FROM city WHERE countrycode = 'NLD';`
);
console.log("6. Cities in Netherlands:", q6.rows);

const q7 = await client.query(
`SELECT population FROM city WHERE name = 'Rotterdam';`
);
console.log("7. Population of Rotterdam:", q7.rows);

const q8 = await client.query(`
SELECT name FROM country ORDER BY surfacearea DESC LIMIT 10;
`);
console.log("8. Top 10 countries by surface area:", q8.rows);

const q9 = await client.query(`
SELECT name FROM city ORDER BY population DESC LIMIT 10;
`);
console.log("9. Top 10 most populated cities:", q9.rows);

const q10 = await client.query(
`SELECT SUM(population) AS world_population FROM country;`
);
console.log("10. World population:", q10.rows[0].world_population);
} catch (err) {
console.error("Query error:", err);
} finally {
await client.end();
console.log("Connection closed.");
}
}

runQueries();
97 changes: 97 additions & 0 deletions Week1/databases/setupMeetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import pkg from "pg";
const { Client } = pkg;

const defaultClient = new Client({
user: "hyfuser",
host: "localhost",
database: "postgres",
password: "hyfpassword",
port: 5432,
});

async function setupDatabase() {
let client;
try {
await defaultClient.connect();

await defaultClient.query(`DROP DATABASE IF EXISTS meetup;`);
await defaultClient.query(`CREATE DATABASE meetup;`);
console.log('Database "meetup" created.');

await defaultClient.end();

client = new Client({
user: "hyfuser",
host: "localhost",
database: "meetup",
password: "hyfpassword",
port: 5432,
});
await client.connect();

await client.query(`
CREATE TABLE Invitee (
invitee_no INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
invitee_name VARCHAR(100),
invited_by VARCHAR(100)
);
`);

await client.query(`
CREATE TABLE Room (
room_no INT PRIMARY KEY,
room_name VARCHAR(64),
floor_number SMALLINT
);
`);

await client.query(`
CREATE TABLE Meeting (
meeting_no INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
meeting_title VARCHAR(64),
starting_time TIMESTAMP,
ending_time TIMESTAMP,
room_no INT REFERENCES Room(room_no)
);
`);

console.log("Tables created.");

await client.query(`
INSERT INTO Invitee (invitee_name, invited_by) VALUES
('Alice Johnson', 'Bob Smith'),
('Bob Smith', 'Carol White'),
('Carol White', 'David Lee'),
('David Lee', 'Alice Johnson'),
('Eve Brown', 'Bob Smith');
`);

await client.query(`
INSERT INTO Room (room_name, floor_number) VALUES
('Blue Room', 1),
('Green Room', 1),
('Yellow Room', 2),
('Red Room', 2),
('Conference Hall', 3);
`);

await client.query(`
INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES
('Project Kickoff', '2025-09-01 09:00:00', '2025-09-01 10:00:00', 1),
('Design Review', '2025-09-02 11:00:00', '2025-09-02 12:30:00', 2),
('Team Standup', '2025-09-03 09:30:00', '2025-09-03 10:00:00', 3),
('Client Presentation', '2025-09-04 14:00:00', '2025-09-04 15:30:00', 4),
('Retrospective', '2025-09-05 16:00:00', '2025-09-05 17:00:00', 5);
`);

console.log("Sample data inserted.");
} catch (err) {
console.error("Error:", err);
} finally {
if (client) await client.end().catch(() => {});
await defaultClient.end().catch(() => {});
console.log("Setup complete (connections closed).");
}
}

setupDatabase();
45 changes: 45 additions & 0 deletions Week1/recipes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
CREATE TABLE recipes (
recipe_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL
);

CREATE TABLE categories (
category_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE
);

CREATE TABLE ingredients (
ingredient_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE
);

CREATE TABLE steps (
step_id INT PRIMARY KEY AUTO_INCREMENT,
description TEXT NOT NULL
);

CREATE TABLE recipe_category (
recipe_id INT,
category_id INT,
PRIMARY KEY (recipe_id, category_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE
);

CREATE TABLE recipe_ingredient (
recipe_id INT,
ingredient_id INT,
quantity VARCHAR(50),
PRIMARY KEY (recipe_id, ingredient_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id) ON DELETE CASCADE,
FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id) ON DELETE CASCADE
);

CREATE TABLE recipe_step (
recipe_id INT,
step_id INT,
step_order INT NOT NULL,
PRIMARY KEY (recipe_id, step_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id) ON DELETE CASCADE,
FOREIGN KEY (step_id) REFERENCES steps(step_id) ON DELETE CASCADE
);
22 changes: 22 additions & 0 deletions Week2/databases/connectDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pkg from 'pg';
const { Client } = pkg;


export async function connectDB(database = 'postgres') {
const client = new Client({
user: 'hyfuser',
host: 'localhost',
database,
password: 'hyfpassword',
port: 5432,
});

try {
await client.connect();
console.log(`Connected to database: ${database}`);
return client;
} catch (error) {
console.error('Connection error:', error);
throw error;
}
}
52 changes: 52 additions & 0 deletions Week2/databases/exercise 1 Keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pkg from "pg";
const { Client } = pkg;

const client = new Client({
user: "postgres",
host: "localhost",
database: "hyf_db",
password: "your_password",
port: 5432,
});

async function main() {
try {
await client.connect();
console.log("Connected to the database");

await client.query(`
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'gender_type') THEN
CREATE TYPE gender_type AS ENUM ('Male', 'Female', 'Other');
END IF;
END $$;
`);

await client.query(`
CREATE TABLE IF NOT EXISTS authors (
author_id SERIAL PRIMARY KEY,
author_name VARCHAR(100) NOT NULL,
university VARCHAR(100),
date_of_birth DATE,
h_index INT,
gender gender_type,
mentor INT,
CONSTRAINT fk_mentor FOREIGN KEY (mentor) REFERENCES authors(author_id) ON DELETE SET NULL
);
`);

console.log(
"Authors table created with self-referencing mentor key and gender ENUM"
);
} catch (error) {
console.error("Error occurred:", error.message);
} finally {
await client.end();
console.log("Database connection closed");
}
}

main().catch((error) => {
console.error("Main function error:", error.message);
});
73 changes: 73 additions & 0 deletions Week2/databases/exercise 2 Relationships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pkg from "pg";
const { Client } = pkg;

const client = new Client({
user: "postgres",
host: "localhost",
database: "hyf_db",
password: "your_password",
port: 5432,
});

async function main() {
try {
await client.connect();
console.log("Connected to the database");

await client.query(`
CREATE TABLE IF NOT EXISTS research_papers (
paper_id SERIAL PRIMARY KEY,
paper_title VARCHAR(255) NOT NULL,
conference VARCHAR(100),
publish_date DATE
);
`);

await client.query(`
CREATE TABLE IF NOT EXISTS author_papers (
author_id INT REFERENCES authors(author_id),
paper_id INT REFERENCES research_papers(paper_id),
PRIMARY KEY (author_id, paper_id)
);
`);

console.log("Research papers and author_papers tables created");

await client.query(`
INSERT INTO authors (author_name, university, date_of_birth, h_index, gender)
VALUES
('Alice Smith', 'MIT', '1980-05-10', 42, 'Female'),
('Bob Johnson', 'Stanford', '1975-09-20', 55, 'Male'),
('Carol Lee', 'Harvard', '1985-01-15', 38, 'Female')
ON CONFLICT DO NOTHING;
`);

await client.query(`
INSERT INTO research_papers (paper_title, conference, publish_date)
VALUES
('AI in Healthcare', 'NeurIPS', '2020-12-01'),
('Quantum Computing Advances', 'QCon', '2021-06-15'),
('Deep Learning Optimization', 'ICML', '2019-07-07')
ON CONFLICT DO NOTHING;
`);

await client.query(`
INSERT INTO author_papers (author_id, paper_id)
VALUES (1,1), (2,1), (1,2), (3,3)
ON CONFLICT DO NOTHING;
`);

console.log("Sample authors and papers inserted");
} catch (error) {
console.error("An error occurred:", error);
throw error; // Re-throw to allow caller to handle if needed
} finally {
await client.end();
console.log("Database connection closed");
}
}

main().catch((error) => {
console.error("Failed to execute main function:", error);
process.exit(1); // Exit with error code
});
Loading