forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 9
Alaa_Nasher-w2-databases #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alaa2019-ml
wants to merge
3
commits into
HackYourAssignment:main
Choose a base branch
from
Alaa2019-ml:Alaa_Nahser-w2-databases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { Client } from "pg"; | ||
| import { authors, researches, researchAuthors } from "./data.js"; | ||
| import { dbName } from "./database.js"; | ||
|
|
||
| const config = { | ||
| host: "localhost", | ||
| port: 5432, | ||
| user: "hyfuser", | ||
| password: "hyfpassword", | ||
| database: dbName, | ||
| }; | ||
|
|
||
| const client = new Client(config); | ||
|
|
||
| async function seedDatabase() { | ||
| const CREATE_AUTHORS_TABLE = ` | ||
| CREATE TABLE IF NOT EXISTS authors ( | ||
| author_id SERIAL PRIMARY KEY, | ||
| author_name VARCHAR(100), | ||
| university VARCHAR(100), | ||
| date_of_birth DATE, | ||
| h_index INTEGER, | ||
| gender VARCHAR(1) CHECK (gender IN ('m', 'f')) | ||
| ); | ||
| `; | ||
|
|
||
| const ADD_MENTOR_COLUMN = ` | ||
| ALTER TABLE authors | ||
| ADD COLUMN mentor INTEGER; | ||
| ALTER TABLE authors | ||
| ADD CONSTRAINT fk_mentor | ||
| FOREIGN KEY (mentor) REFERENCES authors(author_id); | ||
| `; | ||
|
|
||
| const CREATE_RESEARCH_PAPERS = ` | ||
| CREATE TABLE IF NOT EXISTS research_papers ( | ||
| paper_id SERIAL PRIMARY KEY, | ||
| paper_title VARCHAR(300), | ||
| conference VARCHAR(300), | ||
| publish_date DATE, | ||
| published BOOLEAN DEFAULT false | ||
| ); | ||
| `; | ||
|
|
||
| const CREATE_RESEARCH_AUTHORS = ` | ||
| CREATE TABLE IF NOT EXISTS research_authors ( | ||
| author_id INTEGER, | ||
| paper_id INTEGER, | ||
| PRIMARY KEY (author_id, paper_id), | ||
| FOREIGN KEY (author_id) REFERENCES authors(author_id), | ||
| FOREIGN KEY (paper_id) REFERENCES research_papers(paper_id) | ||
| ); | ||
| `; | ||
|
|
||
| try { | ||
| await client.connect(); | ||
| console.log("Connected to PostgreSQL database!"); | ||
|
|
||
| await client.query("DROP TABLE IF EXISTS research_authors CASCADE;"); | ||
|
|
||
| await client.query("DROP TABLE IF EXISTS research_papers CASCADE;"); | ||
|
|
||
| await client.query("DROP TABLE IF EXISTS authors CASCADE;"); | ||
|
|
||
| await client.query(CREATE_AUTHORS_TABLE); | ||
| console.log("Authors table created successfully"); | ||
|
|
||
| await client.query(ADD_MENTOR_COLUMN); | ||
| console.log("Mentor column added to authors table"); | ||
| console.log("Authors table altered successfully"); | ||
|
|
||
| await client.query(CREATE_RESEARCH_PAPERS); | ||
| console.log("Research Papers table created successfully"); | ||
|
|
||
| await client.query(CREATE_RESEARCH_AUTHORS); | ||
| console.log("Research Authors table created successfully"); | ||
|
|
||
| for (const author of authors) { | ||
| const insertQuery = ` | ||
| INSERT INTO authors(author_name, university, date_of_birth, h_index, gender, mentor) | ||
| VALUES ($1, $2, $3, $4, $5, NULL) | ||
| `; | ||
| const values = [ | ||
| author.author_name, | ||
| author.university, | ||
| author.date_of_birth, | ||
| author.h_index, | ||
| author.gender, | ||
| ]; | ||
| await client.query(insertQuery, values); | ||
| console.log(`Inserted author: ${author.author_name}`); | ||
|
|
||
| //if author has mentor then update the table and add it | ||
| if (author.mentor !== null) { | ||
| const updateQuery = ` | ||
| UPDATE authors | ||
| SET mentor = $1 | ||
| WHERE author_name = $2 | ||
| `; | ||
| await client.query(updateQuery, [author.mentor, author.author_name]); | ||
| console.log(`Updated mentor for: ${author.author_name}`); | ||
| } | ||
| } | ||
|
|
||
| for (const research of researches) { | ||
| const insertQuery = ` | ||
| INSERT INTO research_papers(paper_title, conference, publish_date, published) | ||
| VALUES ($1, $2, $3, $4) | ||
| `; | ||
| const values = [ | ||
| research.paper_title, | ||
| research.conference, | ||
| research.publish_date, | ||
| research.published, | ||
| ]; | ||
| await client.query(insertQuery, values); | ||
| console.log(`Inserted research paper: ${research.paper_title}`); | ||
| } | ||
|
|
||
| for (const ele of researchAuthors) { | ||
| const insertQuery = ` | ||
| INSERT INTO research_authors(author_id, paper_id) | ||
| VALUES ($1, $2) | ||
| `; | ||
| const values = [ele.author_id, ele.paper_id]; | ||
| await client.query(insertQuery, values); | ||
| console.log(`Linked author ${ele.author_id} to paper ${ele.paper_id}.`); | ||
| } | ||
|
|
||
| console.log("Database seeded successfully!"); | ||
| } catch (error) { | ||
| console.error("Error seeding database:", error); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| seedDatabase(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You loop through
authorsto insert data twice. Could you do it just once?