Compare commits
2 Commits
7d9350c3df
...
cbbb0ed4c4
| Author | SHA1 | Date | |
|---|---|---|---|
| cbbb0ed4c4 | |||
| 9113ba4c74 |
@@ -0,0 +1,74 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
class ControllerBuilder {
|
||||
static build() {
|
||||
const config = require("./configuration.json");
|
||||
const controllerDir = path.join(__dirname, "release/controllers");
|
||||
|
||||
// Create release/controllers directory
|
||||
if (!fs.existsSync(controllerDir))
|
||||
fs.mkdirSync(controllerDir, { recursive: true });
|
||||
|
||||
config.model.forEach((model) => {
|
||||
const controllerCode = `const express = require('express');
|
||||
const router = express.Router();
|
||||
const model = require('../models/${model.name}.model.js');
|
||||
|
||||
// CREATE
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.create(req.body);
|
||||
res.status(201).json(data);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// READ ALL
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.findAll();
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const updated = await model.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated[0] === 0) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(await model.findByPk(req.params.id));
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await model.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (!deleted) return res.status(404).json({ error: 'Not found' });
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;`;
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(controllerDir, `${model.name}.controller.js`),
|
||||
controllerCode
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ControllerBuilder;
|
||||
+34
-9
@@ -1,13 +1,38 @@
|
||||
let fs = require('fs');
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function Model_builder() {
|
||||
let config = fs.readFileSync('configuration.json');
|
||||
class ModelBuilder {
|
||||
static build() {
|
||||
const config = require("./configuration.json");
|
||||
const modelDir = path.join(__dirname, "release/models");
|
||||
|
||||
this.build = function () {
|
||||
//generate files and put it into release folder
|
||||
//Copy initialize files into release folder
|
||||
//TODO
|
||||
// Create release/models directory
|
||||
if (!fs.existsSync(modelDir)) fs.mkdirSync(modelDir, { recursive: true });
|
||||
|
||||
config.model.forEach((model) => {
|
||||
const modelCode = `const { DataTypes } = require('sequelize');
|
||||
module.exports = (sequelize) => {
|
||||
return sequelize.define('${model.name}', {
|
||||
${model.field
|
||||
.map(
|
||||
(field) => `
|
||||
${field[0]}: {
|
||||
type: DataTypes.${field[1].toUpperCase()},
|
||||
allowNull: ${field[3] === "required" ? "false" : "true"}
|
||||
}`
|
||||
)
|
||||
.join(",")}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
};`;
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(modelDir, `${model.name}.model.js`),
|
||||
modelCode
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
module.exports = ModelBuilder;
|
||||
|
||||
@@ -2,35 +2,35 @@
|
||||
"model": [
|
||||
{
|
||||
"name": "location",
|
||||
"field: [
|
||||
"field": [
|
||||
["id", "integer", "ID", "required"],
|
||||
["name", "string", "Name", "required"],
|
||||
["status", "integer", "Status", "required"],
|
||||
["status", "integer", "Status", "required"]
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "email",
|
||||
"field: [
|
||||
"field": [
|
||||
["id", "integer", "ID", "required"],
|
||||
["email", "string", "Email", "required"],
|
||||
["status", "integer", "Status", "required"],
|
||||
["status", "integer", "Status", "required"]
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "sms",
|
||||
"field: [
|
||||
"field": [
|
||||
["id", "integer", "ID", "required"],
|
||||
["phone", "string", "Phone", "required"],
|
||||
["status", "integer", "Status", "required"],
|
||||
["status", "integer", "Status", "required"]
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "user",
|
||||
"field: [
|
||||
"field": [
|
||||
["id", "integer", "ID", "required"],
|
||||
["name", "string", "Name", "required"],
|
||||
["email", "string", "Email", "required"],
|
||||
["status", "integer", "Status", "required"],
|
||||
["status", "integer", "Status", "required"]
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
const Model_builder = require("./Model_builder");
|
||||
const Controller_builder = require("./Controller_builder");
|
||||
|
||||
Model_builder.build();
|
||||
Controller_builder.build();
|
||||
|
||||
console.log("Model and controller files generated in /release");
|
||||
@@ -0,0 +1,51 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const model = require('../models/email.model.js');
|
||||
|
||||
// CREATE
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.create(req.body);
|
||||
res.status(201).json(data);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// READ ALL
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.findAll();
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const updated = await model.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated[0] === 0) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(await model.findByPk(req.params.id));
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await model.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (!deleted) return res.status(404).json({ error: 'Not found' });
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,51 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const model = require('../models/location.model.js');
|
||||
|
||||
// CREATE
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.create(req.body);
|
||||
res.status(201).json(data);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// READ ALL
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.findAll();
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const updated = await model.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated[0] === 0) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(await model.findByPk(req.params.id));
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await model.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (!deleted) return res.status(404).json({ error: 'Not found' });
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,51 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const model = require('../models/sms.model.js');
|
||||
|
||||
// CREATE
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.create(req.body);
|
||||
res.status(201).json(data);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// READ ALL
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.findAll();
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const updated = await model.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated[0] === 0) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(await model.findByPk(req.params.id));
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await model.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (!deleted) return res.status(404).json({ error: 'Not found' });
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,51 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const model = require('../models/user.model.js');
|
||||
|
||||
// CREATE
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.create(req.body);
|
||||
res.status(201).json(data);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// READ ALL
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const data = await model.findAll();
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const updated = await model.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated[0] === 0) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(await model.findByPk(req.params.id));
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await model.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (!deleted) return res.status(404).json({ error: 'Not found' });
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,20 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
module.exports = (sequelize) => {
|
||||
return sequelize.define('email', {
|
||||
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
module.exports = (sequelize) => {
|
||||
return sequelize.define('location', {
|
||||
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
module.exports = (sequelize) => {
|
||||
return sequelize.define('sms', {
|
||||
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
phone: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
module.exports = (sequelize) => {
|
||||
return sequelize.define('user', {
|
||||
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
};
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
require("dotenv").config();
|
||||
const express = require("express");
|
||||
const bodyParser = require("body-parser");
|
||||
const { User, Post, sequelize } = require("./models");
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// Helper to get model by resource name
|
||||
const models = { users: User, posts: Post };
|
||||
|
||||
// Sync DB
|
||||
sequelize.sync();
|
||||
|
||||
// TreeQL-style dynamic GET
|
||||
app.get("/:resource/:id?/:subresource?", async (req, res) => {
|
||||
const { resource, id, subresource } = req.params;
|
||||
const Model = models[resource];
|
||||
if (!Model) return res.status(404).json({ error: "Resource not found" });
|
||||
|
||||
try {
|
||||
if (id) {
|
||||
const instance = await Model.findByPk(
|
||||
id,
|
||||
subresource ? { include: subresource } : {}
|
||||
);
|
||||
if (!instance) return res.status(404).json({ error: "Not found" });
|
||||
if (subresource && instance[subresource]) {
|
||||
return res.json(instance[subresource]);
|
||||
}
|
||||
return res.json(instance);
|
||||
} else {
|
||||
const all = await Model.findAll();
|
||||
return res.json(all);
|
||||
}
|
||||
} catch (e) {
|
||||
return res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// TreeQL-style POST (create resource or subresource)
|
||||
app.post("/:resource/:id?/:subresource?", async (req, res) => {
|
||||
const { resource, id, subresource } = req.params;
|
||||
const Model = models[resource];
|
||||
if (!Model) return res.status(404).json({ error: "Resource not found" });
|
||||
|
||||
try {
|
||||
if (id && subresource) {
|
||||
// e.g. POST /users/1/posts
|
||||
const parent = await models[resource].findByPk(id);
|
||||
if (!parent) return res.status(404).json({ error: "Parent not found" });
|
||||
const childModel = models[subresource];
|
||||
if (!childModel)
|
||||
return res.status(404).json({ error: "Subresource not found" });
|
||||
const child = await childModel.create({ ...req.body, UserId: id });
|
||||
return res.status(201).json(child);
|
||||
} else {
|
||||
// e.g. POST /users
|
||||
const instance = await Model.create(req.body);
|
||||
return res.status(201).json(instance);
|
||||
}
|
||||
} catch (e) {
|
||||
return res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// TreeQL-style PUT (update resource)
|
||||
app.put("/:resource/:id", async (req, res) => {
|
||||
const { resource, id } = req.params;
|
||||
const Model = models[resource];
|
||||
if (!Model) return res.status(404).json({ error: "Resource not found" });
|
||||
|
||||
try {
|
||||
const instance = await Model.findByPk(id);
|
||||
if (!instance) return res.status(404).json({ error: "Not found" });
|
||||
await instance.update(req.body);
|
||||
return res.json(instance);
|
||||
} catch (e) {
|
||||
return res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// TreeQL-style DELETE (delete resource)
|
||||
app.delete("/:resource/:id", async (req, res) => {
|
||||
const { resource, id } = req.params;
|
||||
const Model = models[resource];
|
||||
if (!Model) return res.status(404).json({ error: "Resource not found" });
|
||||
|
||||
try {
|
||||
const instance = await Model.findByPk(id);
|
||||
if (!instance) return res.status(404).json({ error: "Not found" });
|
||||
await instance.destroy();
|
||||
return res.json({ success: true });
|
||||
} catch (e) {
|
||||
return res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => console.log(`TreeQL API running on port ${PORT}`));
|
||||
@@ -0,0 +1,9 @@
|
||||
require("dotenv").config();
|
||||
const { Sequelize } = require("sequelize");
|
||||
|
||||
const sequelize = new Sequelize("day_15", "root", process.env.DB_PASSWORD, {
|
||||
host: "localhost",
|
||||
dialect: "mysql",
|
||||
});
|
||||
|
||||
module.exports = sequelize;
|
||||
@@ -0,0 +1,15 @@
|
||||
const { DataTypes } = require("sequelize");
|
||||
const sequelize = require("./db");
|
||||
|
||||
const User = sequelize.define("User", {
|
||||
name: DataTypes.STRING,
|
||||
});
|
||||
|
||||
const Post = sequelize.define("Post", {
|
||||
title: DataTypes.STRING,
|
||||
});
|
||||
|
||||
User.hasMany(Post, { as: "posts" });
|
||||
Post.belongsTo(User);
|
||||
|
||||
module.exports = { User, Post, sequelize };
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "day15",
|
||||
"version": "1.0.0",
|
||||
"description": "- setup project\r - implement https://www.treeql.org/ manually",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^2.2.0",
|
||||
"dotenv": "^17.2.0",
|
||||
"express": "^5.1.0",
|
||||
"mysql2": "^3.14.2",
|
||||
"sequelize": "^6.37.7"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user