The purpose of this repository is to demonstrate how to generate typescript code depending on your project models using ts-morph.
This repository is built using a Nx mono-repository. It contains the next artifacts:
- A prisma generator that can generate very basic controllers for a Nestjs REST api. The code of the generator can be found in
libs/generator
. The generator takes in input a prisma schema that describes you database models. Then, it uses the schema to generate some basic rest controllers by using ts-morph. - A nestjs application that serves as a demonstration application to run the generated code. The code lives in
apps/api
. You can also find the prisma schema describing the project models inapps/api/prisma/schema.prisma
, and the generated code inapps/api/src/app/generated
.
Here are the steps to run the demonstration:
- Clone the repository and install the dependencies with
npm i
. - Generate the api code by running
npx nx run api:generate
. - You can now serve the
api
application withnpx nx run api:serve
. The api will be available athttp://localhost:3333/api
. You can test it by using the swagger available athttp://localhost:3333/docs
.
Now, you can add a model in the prisma schema, and re-run the generation to see the new model in the REST api:
- Uncomment the
model Tag
bloc inapps/api/prisma/schema.prisma
. - Re-run the generation:
npx nx run api:generate
. ATagController
class should have been added inapps/api/src/app/generated/tag/tag.controller.ts
. Import it and add it into the controllers declaration ofapps/api/src/app/app.module.ts
. - Restart the api server:
npx nx run api:serve
. You should now see theTag
resource in the swagger.
You could also play with the generator by editing it and run the generation again to see the modifications.
The next resources can be useful if you're looking on infos about ts-morph and/or prisma generators:
- The ts-morph documentation.
- Prisma generators documentation. It contains very interesting links to a cli tool to create a setup to write prisma-generators, and also a list of all the community generators available.
- An article about using ts-morph for code refactoring.
Other tools used in this demo:
- Nx.
- Nestjs.
- Nestjs swagger plugin.
- Marp to write slides decks with markdown.