-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nikolay Martyanov <[email protected]>
- Loading branch information
1 parent
45da532
commit 34dfc35
Showing
7 changed files
with
102 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DB_NAME= | ||
DB_USER= | ||
DB_PASSWORD= |
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,4 @@ | ||
# Gitignore for NodeJS project | ||
|
||
node_modules | ||
package-lock.json |
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,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; |
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 @@ | ||
{ | ||
"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" | ||
} | ||
} |
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,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}`); | ||
}); | ||
}); |
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,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); | ||
} | ||
}; |
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,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; |