A nodejs Framework for develop API in record time.
npm install hastejs-clinpx create-haste-app my-app
cd my-app
npm run develop
npx create-module moduleName
text
.
└── app
└── moduleName
├── controller.js
└── model.js
└── routes.json
`
$3
If you want to remove a module, just run below command on your project
root -
npx remove-module moduleName
Note: Don't delete a folder or module manually from app directory.
That may cause unnecessary error on your code.
$3
Routes refer to how Rest API's endpoints respond to client requests.
When you create a new module, haste by default create some REST convention endpoint for you.
Here, You can add new endpoints or update old endpoints as your need.
`json
{
"routes": [
{
"method": "GET",
"path": "/category",
"controller": "category.find",
"config": {
"middleware": []
}
},
{
"method": "GET",
"path": "/category/count",
"controller": "category.count",
"config": {
"middleware": []
}
},
{
"method": "GET",
"path": "/category/:id",
"controller": "category.findOne",
"config": {
"middleware": []
}
},
{
"method": "POST",
"path": "/category",
"controller": "category.create",
"config": {
"middleware": []
}
},
{
"method": "PUT",
"path": "/category/:id",
"controller": "category.update",
"config": {
"middleware": []
}
},
{
"method": "DELETE",
"path": "/category/:id",
"controller": "category.delete",
"config": {
"middleware": []
}
}
]
}
`
$3
Every route passes the request to the defined controller.
Controllers hold the business logic of your module.
Every route must define a controller. Controllers can communicate with the model and return data to the client or Error handlers.
`javascript
import {Model as Category} from '../../database/modelMapper.js'
const controller = {
async count(req, res, next){
try {
const response = await Category.count({});
res.json({total: response});
} catch (err) {
next(err);
}
},
}
export default controller;
`
The above code is responsible for the return count of the Category.
If you need to add a new function to your controller, you must add it to your routes.json file with the proper structure.
$3
HasteJs uses sequelize for managing database operations. For
updating your model you need to update your model.js file to structure your table.
For more about sequelize model visit here: https://sequelize.org/master/manual/model-basics.html
Example model:
`javascript
import {sequelize} from "../../database/index.js";
import DataTypes from 'sequelize';
export const Model = sequelize.define('Category', {
// Model attributes are defined here
// This are example attributes. please change as you want.
// visit https://sequelize.org/master/manual/model-basics.html for details.
title: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING
// allowNull defaults to true
}
}, {
// Other model options go here
});
`
$3
You can define all your relation/association here `database/relation.js` file.
Example
`javascript
import {Post, Category} from "./modelMapper.js";
const relation = ()=> {
Post.belongsTo(Category, {foreignKey: 'category_id'})
Category.hasMany(Post, {
foreignKey: 'category_id',
sourceKey: 'id'
})
}
export default relation();
``