Skip to content

Commit

Permalink
Create build:db script
Browse files Browse the repository at this point in the history
  • Loading branch information
Mu7ammadAbed committed Oct 28, 2021
1 parent 5f2fd2f commit 98fdfa8
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
.env
package-lock.json
package-lock.json
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions server/database/config/build.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('env2')('.env');
const { Pool } = require('pg');

const {
Expand All @@ -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);
7 changes: 7 additions & 0 deletions server/database/config/databaseConfig.sql
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions server/database/config/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
})();
2 changes: 1 addition & 1 deletion server/database/quieres/getAllUsersQuery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const connection = require('../connection');
const connection = require('../config/connection');

const getAllUsersQuery = () => connection.query('SELECT * FROM agents');

Expand Down
2 changes: 1 addition & 1 deletion server/database/quieres/userEstatesQuiery.js
Original file line number Diff line number Diff line change
@@ -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]);
1 change: 0 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('env2')('.env');
const app = require('./app');

const port = app.get('port');
Expand Down
4 changes: 2 additions & 2 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
@@ -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());
Expand Down

0 comments on commit 98fdfa8

Please sign in to comment.