Small API that communicates with a MongoDB database. Used in the NanoERP project.
It bases itself on the concept of generating code through specification.
First, you must create a Mongoose Model from a Schema that an endpoint can use. Example:
const SampleSchema = new Schema({
name: {
required: true,
type: String,
},
});
const SampleModel = model('Sample', SampleSchema);You can find more information about it here.
With a Model, you can generate an Express router based on an array of endpoints. Each endpoint must be defined according to the EndpointType interface, and endpoint queries can ben passed through the uri string.
const router_config: Array<RouterConfig> = [
{
type: "GET",
uri: "/:id",
middleware: ControllerFactory.create("GET_BY_ID", SampleModel),
},
{
type: "POST",
uri: "/",
middleware: ControllerFactory.create("POST_ONE", SampleModel),
},
]
const sample_router = RouterFactory.createRoute(config);Lastly, youd just have to use the created route in your Express API.
const app = express();
app.use(sample_router);src
├── data
│ ├── models
│ └── schema
├── generators
│ ├── Actions
│ ├── CommonTypes
│ ├── Controllers
│ │ ├── ControllerMiddleware
│ ├── Factories
├── middleware
│ └── auth
└── routes
For Models and Schema.
For API route generation.
For non-generator middleware, such as Auth.
For defining and loading generated routes.
Endpoint(or Router) Generator
- Each
Endpointshould use aEndpointConfigspecification. - Each
Endpointshould be test-able with aTestGenerator.
Inquire(when a guest is preparing to make a reservation).
- Each
Inquireshould have a creation date.
Accommodation
- Each
Accommodationmust have a unique name. - Each
Accommodationmust have aLocation.
Location
- Each
Locationmust have a uniquelocation_name.
Room
- Each
Roommust have a unique name. - Each
Roommust belong to anAccommodation.
Company
- Each
Companymust have a name.
User
- Each
Usermust belong to aCompany. - Each
Userhas a set ofRoles. - The
Userthat signs up a company automatically gains theAdminrole.
Role
- Each
Rolegives a set of permissions to aUser. - A
Rolecan be customized by aUserwith theAdminrole. - The
Adminrole can only be removed by aUserwithAdminrole. - The
Adminrole can't be removed by aUserthat is currently using it.