Skip to content

Commit

Permalink
Merge pull request #11 from fack2/data-base-#10
Browse files Browse the repository at this point in the history
database#9
  • Loading branch information
Nadeen-Sh authored Nov 17, 2019
2 parents c1b9951 + 886a5a7 commit 2069157
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server/database/dbBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require("fs")
const dbConnection = require("./dbConnection")

const createDataBase = fs.readFileSync(`${__dirname}/dbBuild.sql`).toString()
const initDate = fs.readFileSync(`${__dirname}/initValue.sql`).toString()

dbConnection.query(createDataBase, (err, res) => {
console.log("database is connected successfully")
dbConnection.query(initDate, (err2, res2) => {
console.log("data inserted successfully")
if (err2) {
throw err2
}
})
if (err) {
throw err
}
})
30 changes: 30 additions & 0 deletions server/database/dbBuild.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BEGIN;

drop table if exists users,messages
CASCADE;

create table users
(
user_id serial primary key,
name varchar(100) not null,
email text not null,
password text not null,
img text,
color text

);

create table messages
(
message_id serial primary key,
time timestamp not null DEFAULT CURRENT_TIMESTAMP,
description text not null,
user_id INTEGER,
FOREIGN KEY
(user_id) REFERENCES users (user_id)

);



COMMIT;
13 changes: 13 additions & 0 deletions server/database/dbConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { Pool } = require("pg")
require("env2")("./config.env")

const connectionString = process.env.DATABASE_URL

if (!connectionString) {
throw new Error("set DATABASE_URL")
}

module.exports = new Pool({
connectionString,
ssl: !connectionString.includes("localhost")
})
18 changes: 18 additions & 0 deletions server/database/initValue.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BEGIN;

INSERT INTO users
(name, email,password,color,img)
VALUES
('yousef qwasmeh', '[email protected]', '0', '#ccc', ''),
('munir', '[email protected]', '123456', '#ff0', 'my address');


INSERT INTO messages
(user_id,description)
VALUES
(1, '0'),
(2, '1'),
(1, '0000000')
;

COMMIT;

0 comments on commit 2069157

Please sign in to comment.