Skip to content
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

setup database #22

Merged
merged 6 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node server",
"dev": "nodemon server",
"test": "jest"
"start": "cross-env NODE_ENV=production node server",
"dev": "cross-env NODE_ENV=development nodemon server",
"test": "cross-env NODE_ENV=test jest"
},
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions server/database/config/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { readFileSync } = require('fs');
const { join } = require('path');

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;
43 changes: 43 additions & 0 deletions server/database/config/bulid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
BEGIN;
DROP TABLE IF EXISTS admins,estates,agents,images;
CREATE TABLE admins(
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE agents(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL UNIQUE,
avater TEXT

);
CREATE TABLE estates(
id SERIAL PRIMARY KEY,
agent_id INTEGER REFERENCES agents(id) ON DELETE CASCADE,
title VARCHAR(255) NOT NULL,
price DECIMAL NOT NULL,
description TEXT NOT NULL,
type VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL,
street VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
region VARCHAR(255) NOT NULL,
bathrooms INTEGER DEFAULT 0,
bedrooms INTEGER DEFAULT 0,
rooms INTEGER DEFAULT 0,
space DECIMAL NOT NULL ,
approved BOOLEAN DEFAULT FALSE,
rate INTEGER DEFAULT 0,
available BOOLEAN DEFAULT TRUE
);

CREATE TABLE images(
id SERIAL PRIMARY KEY,
estate_id INTEGER REFERENCES estates(id) ON DELETE CASCADE,
image TEXT
);
COMMIT;
25 changes: 25 additions & 0 deletions server/database/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require('env2')('.env');
const {Pool} = require('pg');

const {
NODE_ENV, DATABASE_URL_DEV, DATABASE_URL, DATABASE_URL_TEST,
} = process.env;
let dbUrl = '';
switch (NODE_ENV) {
case 'development':
dbUrl = DATABASE_URL_DEV;
break;
case 'production':
dbUrl = DATABASE_URL;
break;
case 'test':
dbUrl = DATABASE_URL_TEST;
break;
default:
throw new Error('There\'s no environment');
}
const options = {
connectionString: dbUrl,
ssl: { rejectUnauthorized: false },
};
module.exports = new Pool(options);