Skip to content

Commit

Permalink
Update database configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mu7ammadAbed committed Oct 13, 2022
1 parent 2e906d9 commit 51d67ff
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 97 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "cross-env NODE_ENV=test jest -i",
"build": "tsc -p .",
"heroku-postbuild": "npm run build && cd client/ && npm i && npm run build",
"db:seed": "cross-env NODE_ENV=development ts-node server/database/config/build.ts",
"eslint:fix": "eslint --fix",
"lint": "eslint --ext .ts server/ && eslint --ext .tsx client/src/"
},
Expand Down
11 changes: 11 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dotenv from 'dotenv';
import cookieParser from 'cookie-parser';
import { join } from 'path';
import morgan from 'morgan';
import { Announcement, Advertisement } from './models';

dotenv.config();

Expand All @@ -24,6 +25,16 @@ if (NODE_ENV === 'development') {

// app.use('/api/v1', router);

app.get('/announcements', async (req, res) => {
const data = await Announcement.findAll();
res.json({ message: 'Announcements', data });
});

app.get('/advertisements', async (req, res) => {
const data = await Advertisement.findAll();
res.json({ message: 'Advertisements', data });
});

if (NODE_ENV === 'production') {
app.use(express.static(join(__dirname, '..', 'client', 'build')));
app.get('*', (req, res) => {
Expand Down
23 changes: 20 additions & 3 deletions server/database/config/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import sequelize from "./connection";
import sequelize from './connection';
import {
Announcements, Advertisements,
} from './fakeData';

import {
Announcement, Advertisement,
} from '../../models/index';

const dbConnect = () => sequelize.sync();
const insertDB = async () => {
try {
await sequelize.sync({ force: true });
await Announcement.bulkCreate(Announcements);
await Advertisement.bulkCreate(Advertisements);

export { dbConnect }
// eslint-disable-next-line no-console
console.log('Build Database Successfully');
} catch (err) {
// eslint-disable-next-line no-console
console.log('Build Database Failed', err);
}
};
insertDB();
2 changes: 1 addition & 1 deletion server/database/config/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ switch (NODE_ENV) {
}
if (!url) throw new Error('The database url is invalid!');

const sequelizeConfig = new Sequelize(url);
const sequelizeConfig = new Sequelize(url, { logging: false, dialect: 'postgres' });
export default sequelizeConfig;
1 change: 1 addition & 0 deletions server/database/config/fakeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ const Advertisements = [
},

];

export { Announcements, Advertisements };
2 changes: 1 addition & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sequelize from './database/config/connection';

const port = app.get('port') as number;

sequelize.sync({ force: true }) // TODO: To be reomved later when DB is 100% ready
sequelize.sync()
.then(() => {
app.listen(port, () => {
// eslint-disable-next-line no-console
Expand Down
32 changes: 32 additions & 0 deletions server/models/Advertisement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database/config/connection';

const Advertisement = sequelize.define('Advertisements', {
id: {
primaryKey: true,
autoIncrement: true,
type: DataTypes.INTEGER,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.STRING,
allowNull: false,
},
start_date: {
type: DataTypes.STRING,
allowNull: false,
},
end_date: {
type: DataTypes.STRING,
allowNull: false,
},
image: {
type: DataTypes.STRING,
allowNull: false,
},
});

export default Advertisement;
51 changes: 0 additions & 51 deletions server/models/Advertisements.ts

This file was deleted.

24 changes: 24 additions & 0 deletions server/models/Announcement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database/config/connection';

const Announcement = sequelize.define('Announcements', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
start_date: {
type: DataTypes.STRING,
allowNull: false,
},
end_date: {
type: DataTypes.STRING,
allowNull: false,
},
});

export default Announcement;
41 changes: 0 additions & 41 deletions server/models/Announcements.ts

This file was deleted.

6 changes: 6 additions & 0 deletions server/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Announcement from './Announcement';
import Advertisement from './Advertisement';

export {
Announcement, Advertisement,
};

0 comments on commit 51d67ff

Please sign in to comment.