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
Endpoint
should use aEndpointConfig
specification. - Each
Endpoint
should be test-able with aTestGenerator
.
Inquire(when a guest is preparing to make a reservation).
- Each
Inquire
should have a creation date.
Accommodation
- Each
Accommodation
must have a unique name. - Each
Accommodation
must have aLocation
.
Location
- Each
Location
must have a uniquelocation_name
.
Room
- Each
Room
must have a unique name. - Each
Room
must belong to anAccommodation
.
Company
- Each
Company
must have a name.
User
- Each
User
must belong to aCompany
. - Each
User
has a set ofRole
s. - The
User
that signs up a company automatically gains theAdmin
role.
Role
- Each
Role
gives a set of permissions to aUser
. - A
Role
can be customized by aUser
with theAdmin
role. - The
Admin
role can only be removed by aUser
withAdmin
role. - The
Admin
role can't be removed by aUser
that is currently using it.