From 38df0d9e78804994b1874ffd443efd4b7d22d0a5 Mon Sep 17 00:00:00 2001 From: YousefQwasmeh <151037@ppu.edu.ps> Date: Sun, 17 Nov 2019 11:41:39 +0200 Subject: [PATCH 1/5] database --- .gitignore | 2 ++ server/database/dbBuild.js | 18 ++++++++++++++++++ server/database/dbBuild.sql | 30 ++++++++++++++++++++++++++++++ server/database/dbConnection.js | 13 +++++++++++++ server/database/initValue.sql | 18 ++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 server/database/dbBuild.js create mode 100644 server/database/dbBuild.sql create mode 100644 server/database/dbConnection.js create mode 100644 server/database/initValue.sql diff --git a/.gitignore b/.gitignore index ad572e6..ddd3b40 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,5 @@ buck-out/ # CocoaPods /ios/Pods/ + +.env \ No newline at end of file diff --git a/server/database/dbBuild.js b/server/database/dbBuild.js new file mode 100644 index 0000000..e626d2c --- /dev/null +++ b/server/database/dbBuild.js @@ -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 + } +}) diff --git a/server/database/dbBuild.sql b/server/database/dbBuild.sql new file mode 100644 index 0000000..c6cac6e --- /dev/null +++ b/server/database/dbBuild.sql @@ -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; \ No newline at end of file diff --git a/server/database/dbConnection.js b/server/database/dbConnection.js new file mode 100644 index 0000000..1eca293 --- /dev/null +++ b/server/database/dbConnection.js @@ -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") +}) diff --git a/server/database/initValue.sql b/server/database/initValue.sql new file mode 100644 index 0000000..6374e72 --- /dev/null +++ b/server/database/initValue.sql @@ -0,0 +1,18 @@ +BEGIN; + + INSERT INTO users + (name, email,password,color,img) + VALUES + ('yousef qwasmeh', '151037@ppu.edu.ps', '0', '#ccc', ''), + ('munir', '121050@ppu.edu.ps', '123456', '#ff0', 'my address'); + + + INSERT INTO messages + (user_id,description) + VALUES + (1, '0'), + (2, '1'), + (1, '0000000') + ; + + COMMIT; \ No newline at end of file From 6acdd43575414ff065a20f7388ee5ac4245f183e Mon Sep 17 00:00:00 2001 From: YousefQwasmeh <151037@ppu.edu.ps> Date: Sun, 17 Nov 2019 11:54:16 +0200 Subject: [PATCH 2/5] try --- config.env | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 config.env diff --git a/config.env b/config.env new file mode 100644 index 0000000..e69de29 From 2a9152b80d9f670c408c48e908f02a19c94edead Mon Sep 17 00:00:00 2001 From: YousefQwasmeh <151037@ppu.edu.ps> Date: Sun, 17 Nov 2019 14:01:25 +0200 Subject: [PATCH 3/5] 2 --- .gitignore | 1 - config.env | 0 2 files changed, 1 deletion(-) delete mode 100644 config.env diff --git a/.gitignore b/.gitignore index ddd3b40..c7635ad 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,3 @@ buck-out/ # CocoaPods /ios/Pods/ -.env \ No newline at end of file diff --git a/config.env b/config.env deleted file mode 100644 index e69de29..0000000 From 339db85d41eea87db5f4f0a117f2f08a21bffcc5 Mon Sep 17 00:00:00 2001 From: YousefQwasmeh <151037@ppu.edu.ps> Date: Sun, 17 Nov 2019 14:02:30 +0200 Subject: [PATCH 4/5] 2 --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c7635ad..fe2e827 100644 --- a/.gitignore +++ b/.gitignore @@ -56,5 +56,4 @@ buck-out/ *.jsbundle # CocoaPods -/ios/Pods/ - +/ios/Pods/ \ No newline at end of file From 886a5a755e390ad97cda0eec569d687932081711 Mon Sep 17 00:00:00 2001 From: YousefQwasmeh <151037@ppu.edu.ps> Date: Sun, 17 Nov 2019 14:03:00 +0200 Subject: [PATCH 5/5] 2 --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fe2e827..ad572e6 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,4 @@ buck-out/ *.jsbundle # CocoaPods -/ios/Pods/ \ No newline at end of file +/ios/Pods/