-
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 <ohmspectator@gmail.com>
- Loading branch information
1 parent
b0783b7
commit 6ac877b
Showing
9 changed files
with
116 additions
and
18 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# Gitignore for NodeJS project | ||
|
||
node_modules | ||
package-lock.json | ||
package-lock.json | ||
|
||
*.log |
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,14 @@ | ||
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', | ||
}); | ||
|
||
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
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 |
---|---|---|
@@ -1,21 +1,19 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Check if .env file exists | ||
if [ ! -f .env ]; then | ||
echo "Please create .env file in the root directory" | ||
exit 1 | ||
fi | ||
OUTPUT_DIR="./src/models/generated" | ||
|
||
# Load environment variables | ||
source .env | ||
# Create output directory if it doesn't exist | ||
if [ ! -d "$OUTPUT_DIR" ]; then | ||
mkdir -p "$OUTPUT_DIR" | ||
fi | ||
|
||
# Check the required environment variables | ||
REQUIRED_VARIABLES=(DB_NAME DB_USER DB_PASSWORD) | ||
for VARIABLE in "${REQUIRED_VARIABLES[@]}"; do | ||
if [ -z "${!VARIABLE}" ]; then | ||
echo "Please set $VARIABLE in .env file" | ||
exit 1 | ||
fi | ||
done | ||
sequelize-auto -o "$OUTPUT_DIR" --schema gadm > model-generation.log | ||
|
||
sequelize-auto -o './src/models' -d "$DB_NAME" -h localhost -u "$DB_USER" -x "$DB_PASSWORD" -p 5432 -e postgres --schema gadm | ||
#Check if the generation was successful | ||
if [ $? -eq 0 ]; then | ||
echo "Models generated successfully" | ||
rm model-generation.log | ||
else | ||
echo "Models generation failed" | ||
exit 1 | ||
fi |
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: 'Region', | ||
key: 'id', | ||
}, | ||
}, | ||
hasSubregions: { | ||
type: DataTypes.BOOLEAN, | ||
allowNull: false, | ||
}, | ||
}); | ||
|
||
module.exports = Region; |
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,9 @@ | ||
const express = require('express'); | ||
|
||
const regionRoutes = require('./regionRoutes'); | ||
|
||
const router = express.Router(); | ||
|
||
router.use('/region', regionRoutes); | ||
|
||
module.exports = router; |
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,8 @@ | ||
const express = require('express'); | ||
const regionController = require('../controllers/regionController'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/:regionId', regionController.getRegionById); | ||
|
||
module.exports = router; |