From 98fdfa83c17929ef396648e82c40d0b679812f0e Mon Sep 17 00:00:00 2001 From: Muhammad Abdulhadi Date: Thu, 28 Oct 2021 16:20:23 +0300 Subject: [PATCH] Create build:db script --- .gitignore | 2 +- .vscode/settings.json | 2 +- README.md | 2 +- package.json | 2 +- server/database/config/build.js | 5 +++-- server/database/{ => config}/connection.js | 3 +++ server/database/config/databaseConfig.sql | 7 +++++++ server/database/config/index.js | 13 +++++++++++++ server/database/quieres/getAllUsersQuery.js | 2 +- server/database/quieres/userEstatesQuiery.js | 2 +- server/index.js | 1 - server/test/index.test.js | 4 ++-- 12 files changed, 34 insertions(+), 11 deletions(-) rename server/database/{ => config}/connection.js (95%) create mode 100644 server/database/config/databaseConfig.sql create mode 100644 server/database/config/index.js diff --git a/.gitignore b/.gitignore index 6fe20f0..5693cd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules .env -package-lock.json \ No newline at end of file +package-lock.json diff --git a/.vscode/settings.json b/.vscode/settings.json index c3a7563..6681eb0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,4 +4,4 @@ "editor.codeActionsOnSave": { "source.fixAll.eslint": true } - } \ No newline at end of file +} \ No newline at end of file diff --git a/README.md b/README.md index b0579fa..4a8a767 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Real Estate is an App that facilitates to access and rent or buy available homes ## Team Members :busts_in_silhouette::- 1. Mohammed haroon 2. MOhammed salah -3. ameera +3. Ameera Abed Alnaser Khalil 4. Elham Fadel diff --git a/package.json b/package.json index c3dc9d5..84ccd3e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "cross-env NODE_ENV=production node server", "dev": "cross-env NODE_ENV=development nodemon server", "test": "cross-env NODE_ENV=test jest", - "build:db": "NODE_ENV=development node server/database/config/build" + "build:db": "cross-env NODE_ENV=development node server/database/config" }, "repository": { "type": "git", diff --git a/server/database/config/build.js b/server/database/config/build.js index 93646ef..a35d117 100644 --- a/server/database/config/build.js +++ b/server/database/config/build.js @@ -1,11 +1,12 @@ const { readFileSync } = require('fs'); const { join } = require('path'); -const connection = require('../connection'); +const connection = require('./connection'); const dbBuild = () => { let sql = readFileSync(join(__dirname, 'build.sql')).toString(); sql += readFileSync(join(__dirname, 'fakeData.sql')).toString(); return connection.query(sql); }; -module.exports = { dbBuild }; + +module.exports = dbBuild; diff --git a/server/database/connection.js b/server/database/config/connection.js similarity index 95% rename from server/database/connection.js rename to server/database/config/connection.js index e2c8673..572fd03 100644 --- a/server/database/connection.js +++ b/server/database/config/connection.js @@ -1,3 +1,4 @@ +require('env2')('.env'); const { Pool } = require('pg'); const { @@ -17,8 +18,10 @@ switch (NODE_ENV) { default: throw new Error('There\'s no environment'); } + const options = { connectionString: dbUrl, ssl: { rejectUnauthorized: false }, }; + module.exports = new Pool(options); diff --git a/server/database/config/databaseConfig.sql b/server/database/config/databaseConfig.sql new file mode 100644 index 0000000..7e74e32 --- /dev/null +++ b/server/database/config/databaseConfig.sql @@ -0,0 +1,7 @@ +CREATE DATABASE housydev; +CREATE USER housyuser WITH superuser password '123456'; +ALTER DATABASE housydev OWNER TO housyuser; +-- test db +CREATE DATABASE housytest; +CREATE USER housyuser2 WITH superuser password '123456'; +ALTER DATABASE housytest OWNER TO housyuser2; \ No newline at end of file diff --git a/server/database/config/index.js b/server/database/config/index.js new file mode 100644 index 0000000..b975dc6 --- /dev/null +++ b/server/database/config/index.js @@ -0,0 +1,13 @@ +/* eslint-disable no-console */ +const connection = require('./connection'); +const dbBuild = require('./build'); + +(async () => { + try { + await dbBuild(); + console.log('Build Successfully'); + connection.end(); + } catch (e) { + console.log('Build Failed', e); + } +})(); diff --git a/server/database/quieres/getAllUsersQuery.js b/server/database/quieres/getAllUsersQuery.js index 24231b0..680e88c 100644 --- a/server/database/quieres/getAllUsersQuery.js +++ b/server/database/quieres/getAllUsersQuery.js @@ -1,4 +1,4 @@ -const connection = require('../connection'); +const connection = require('../config/connection'); const getAllUsersQuery = () => connection.query('SELECT * FROM agents'); diff --git a/server/database/quieres/userEstatesQuiery.js b/server/database/quieres/userEstatesQuiery.js index 811caa3..f1b86ed 100644 --- a/server/database/quieres/userEstatesQuiery.js +++ b/server/database/quieres/userEstatesQuiery.js @@ -1,3 +1,3 @@ -const connection = require('../connection'); +const connection = require('../config/connection'); module.exports = (userId) => connection.query('SELECT * FROM estates WHERE agent_id = $1', [userId]); diff --git a/server/index.js b/server/index.js index 77625cc..991b1f9 100644 --- a/server/index.js +++ b/server/index.js @@ -1,4 +1,3 @@ -require('env2')('.env'); const app = require('./app'); const port = app.get('port'); diff --git a/server/test/index.test.js b/server/test/index.test.js index d5dd994..fb575b4 100644 --- a/server/test/index.test.js +++ b/server/test/index.test.js @@ -1,8 +1,8 @@ /* eslint-disable no-undef */ const supertest = require('supertest'); const app = require('../app'); -const { dbBuild } = require('../database/config/build'); -const connection = require('../database/connection'); +const dbBuild = require('../database/config/build'); +const connection = require('../database/config/connection'); beforeEach(() => dbBuild()); afterAll(() => connection.end());