diff --git a/package.json b/package.json index c13cfaa..531cc8d 100644 --- a/package.json +++ b/package.json @@ -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/" }, diff --git a/server/app.ts b/server/app.ts index dc8fd73..f434177 100644 --- a/server/app.ts +++ b/server/app.ts @@ -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(); @@ -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) => { diff --git a/server/database/config/build.ts b/server/database/config/build.ts index 6c4fbe1..5c295df 100644 --- a/server/database/config/build.ts +++ b/server/database/config/build.ts @@ -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 } \ No newline at end of file + // 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(); diff --git a/server/database/config/connection.ts b/server/database/config/connection.ts index 4648beb..a3e7c71 100644 --- a/server/database/config/connection.ts +++ b/server/database/config/connection.ts @@ -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; diff --git a/server/database/config/fakeData.ts b/server/database/config/fakeData.ts index 87fa72d..76b0e13 100644 --- a/server/database/config/fakeData.ts +++ b/server/database/config/fakeData.ts @@ -45,4 +45,5 @@ const Advertisements = [ }, ]; + export { Announcements, Advertisements }; diff --git a/server/index.ts b/server/index.ts index 75433f1..4b88008 100644 --- a/server/index.ts +++ b/server/index.ts @@ -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 diff --git a/server/models/Advertisement.ts b/server/models/Advertisement.ts new file mode 100644 index 0000000..0b961c5 --- /dev/null +++ b/server/models/Advertisement.ts @@ -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; diff --git a/server/models/Advertisements.ts b/server/models/Advertisements.ts deleted file mode 100644 index 03afac8..0000000 --- a/server/models/Advertisements.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { - InferAttributes, - InferCreationAttributes, - DataTypes, - Model, - CreationOptional, -} from 'sequelize'; -import sequelize from '../database/config/connection'; - -interface AdvertisementsModel - extends Model< - InferAttributes, - InferCreationAttributes - > { - id: CreationOptional - title: string - description: string - start_date: Date - end_date: Date - image: string -} - -const Advertisements = sequelize.define('Advertisements', { - id: { - primaryKey: true, - autoIncrement: true, - type: DataTypes.NUMBER, - }, - title: { - type: DataTypes.STRING, - allowNull: false, - }, - description: { - type: DataTypes.STRING, - allowNull: false, - }, - start_date: { - type: DataTypes.DATE, - allowNull: false, - }, - end_date: { - type: DataTypes.DATE, - allowNull: false, - }, - image: { - type: DataTypes.STRING, - allowNull: false, - }, -}); - -export default Advertisements; diff --git a/server/models/Announcement.ts b/server/models/Announcement.ts new file mode 100644 index 0000000..c090afc --- /dev/null +++ b/server/models/Announcement.ts @@ -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; diff --git a/server/models/Announcements.ts b/server/models/Announcements.ts deleted file mode 100644 index 2f83469..0000000 --- a/server/models/Announcements.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { - InferAttributes, - InferCreationAttributes, - DataTypes, - Model, - CreationOptional, -} from 'sequelize'; -import sequelize from '../database/config/connection'; - -interface AnnouncementsModel - extends Model< - InferAttributes, - InferCreationAttributes - > { - id: CreationOptional - title: string - start_date: Date - end_date: Date -} - -const Announcements = sequelize.define('Announcements', { - id: { - primaryKey: true, - autoIncrement: true, - type: DataTypes.NUMBER, - }, - title: { - type: DataTypes.STRING, - allowNull: false, - }, - start_date: { - type: DataTypes.DATE, - allowNull: false, - }, - end_date: { - type: DataTypes.DATE, - allowNull: false, - }, -}); - -export default Announcements; diff --git a/server/models/index.ts b/server/models/index.ts index e69de29..bef65c1 100644 --- a/server/models/index.ts +++ b/server/models/index.ts @@ -0,0 +1,6 @@ +import Announcement from './Announcement'; +import Advertisement from './Advertisement'; + +export { + Announcement, Advertisement, +};