Skip to content

Entity generation

Steve Lebleu edited this page Apr 24, 2021 · 5 revisions

The boilerplate provides rsgen, a small cli tool which can be work as entity generator. The tool is installed by default in your node runtime.

> Why ?

Obviously to gain a little bit of time with repetitive and boring tasks. 🕐 😎

So, you can generate:

  • Controller
  • Routes
  • Repository
  • Model
  • Validations
  • Subscriber
  • Query string interface
  • Request interface
  • Business service and data-layer service (resource only)
  • E2e tests
  • Fixtures

> How to ?

Open your terminal and go to the root of your project:

$ cd path-to/the-root/of-your-project/

Run rsgen command:

$ rsgen

Step 1: choose type of generation

Prompt ask first what you want to generate: a core component (in this cas don't hesitate to send a PR 🙏) or a resource as module.

According to this choice, the files will be generated into the core or the resources directory. If you wish generate a business module, keep the default choice and generate a resource.

Choose type of generation

Step 2: enter the name of your entity

Next, enter the name of the entity to generate. You can provide one or many words separated by spaces, generator will use hyphens for filename, and PascalCase for entity name.

Define entity name

Step 3: choose allowed roles

Finaly, indicates who can access to the generated resource by choosing roles in the list.

Choose role

Enjoy

And Voilà ! 🎺 🎺 🎺

Done

Your module has been generated, according to the previous example you can find the following files:

  • /src/api/resources/car/
    • /src/api/resources/car/car.route.ts
    • /src/api/resources/car/car.controler.ts
    • /src/api/resources/car/car.model.ts
    • /src/api/resources/car/car.repository.ts
    • /src/api/resources/car/car.validation.ts
  • /test/e2e/0[xxx]-car.e2e.test.js
  • /test/utils/fixtures/car.js

Generated files

> And after that ?

Generated files contains only basic features and some parts must be filled by yourself :

  • Model is filled with a primary auto-incremented id, and date system columns. Fill it with your columns and relations.
  • Validation rules are created but empty by default. Fill it with your rules.
  • Tests e2e will be improved with your use cases.
  • Fixture should be filled according to your model.