-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from GSG-G10/4-setup-database
setup database
- Loading branch information
Showing
4 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |