Skip to content

Commit

Permalink
[WIP] back: init models
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator committed Oct 4, 2023
1 parent 45da532 commit 34dfc35
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DB_NAME=
DB_USER=
DB_PASSWORD=
4 changes: 4 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Gitignore for NodeJS project

node_modules
package-lock.json
15 changes: 15 additions & 0 deletions backend/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require('dotenv').config();

const dbName = process.env.DB_NAME;
const dbUser = process.env.DB_USER;
const dbPassword = process.env.DB_PASSWORD;

const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(dbName, dbUser, dbPassword, {
host: 'localhost',
dialect: 'postgres',
schema: 'public',
});

module.exports = sequelize;
25 changes: 25 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "track-your-regions-backend",
"version": "1.0.0",
"description": "Backend for Track Your Regions",
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"pg": "^8.11.3",
"sequelize": "^6.33.0"
},
"devDependencies": {
"dotenv-cli": "^7.3.0",
"jest": "^27.0.6",
"jtest": "^0.0.1",
"nodemon": "^2.0.22",
"sequelize-auto": "^0.8.8",
"supertest": "^6.1.6"
},
"scripts": {
"start": "node src/app.js",
"dev": "nodemon src/app.js",
"test": "jest",
"generate-models": "dotenv -- sequelize-auto -o './src/models' -d tyr_db -h localhost -u $DB_USER -x $DB_PASSWORD -p 5432 -e postgres --schema gadm"
}
}
16 changes: 16 additions & 0 deletions backend/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
const routes = require('./routes');
const sequelize = require('../config/database');

const app = express();

app.use(express.json());
app.use('/', routes);

const PORT = 3000;

sequelize.sync().then(() => {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
});
12 changes: 12 additions & 0 deletions backend/src/controllers/regionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Region = require('../models/region');

exports.getRegionById = async (req, res) => {
const { regionId } = req.params;
try {
const region = await Region.findByPk(regionId);
if (!region) return res.status(404).send('Region not found');
res.json(region);
} catch (err) {
res.status(500).send(err.message);
}
};
27 changes: 27 additions & 0 deletions backend/src/models/region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../../config/database');

const Region = sequelize.define('Region', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
parentRegionId: {
type: DataTypes.INTEGER,
references: {
model: 'regions',
key: 'id',
},
},
hasSubregions: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
});

module.exports = Region;

0 comments on commit 34dfc35

Please sign in to comment.