Chapter 12 - Refactor your application using Domain-driven design (DDD)¶
Your application has grown quite a lot since you started your journey. You manage slideshows, database connection, application configuration with dotenv, an API, views, etc.
All these aspects are well defined in your head but the codebase doesn't exactly reflect those domains at the moment.
In this chapter, you'll refactor your application (= changing the code without adding new features) to use Domain-driven design to help structure your application with proven practices to extend it in the future.
This will allow to have distinct domains that you can extend or re-use in other projects, helping the understanding of the codebase and ensure the Separation of concerns.
In fact, in the previous chapter, you already created the Prisma domain to keep everything related to the database in one directory.
Move the Handlebars templates to the slideshows domain¶
Create the views directory for the slideshows domain and move the current Handlebars templates to it. The Handlebars template to display one slideshow is renamed [id].hbs now that it is moved to its own domain.
# Create the `slideshows` views directorymkdirviews/slideshows
# Move the Handlebars template to display all the slideshowsmvviews/index.hbsviews/slideshows/index.hbs
# Move and rename the Handlebars template to display one slideshowmvviews/slideshow.hbsviews/slideshows/[id].hbs
Split the current controller into an API and a Views controllers¶
The current AppController located in src/app.controller.ts has two aspects that seems too coupled and hard to maintain: the API endpoints and the Views rendering. You'll split these two aspects into two separate files.
Update the main module to load the slideshows module. As the slideshows module loads everything it needs, the app module only loads what needs to be loaded.
Congrats! Your application now persists all the slideshows in the database.
Prisma speeds up the development of the database thanks to its schema and the migrations.
The Prisma client generated from the schema ensures the types of your inputs are correct thanks to TypeScript.
When restarting the application, there is no more data loss.
SQLite helps to develop simple applications as only a file is required to store the database. For more robust applications, another kind of database is recommended, such as PostgreSQL. However, for development, SQLite is perfectly fine.