Here is the rewritten article:
Synchronizing Database Types with Your Codebase: A Key to Secure and Maintainable Software
When building software, synchronizing types between your database and codebase is crucial for security and maintainability. However, this process can be time-consuming and error-prone, especially when dealing with large databases. A mismatch can lead to unexpected runtime errors and inconsistencies in the application, particularly in dynamic languages like JavaScript that lack static type checking.
The Role of TypeScript and Kanel
TypeScript can help address these issues by catching type errors at compile time rather than runtime. However, TypeScript won’t automatically generate type definitions from a database out of the box. That’s where tools like Kanel come into play. Kanel allows developers to generate TypeScript types from PostgreSQL databases, ensuring type safety and eliminating manual type declarations.
What is Kanel?
Kanel is a tool that generates TypeScript types based on the structure of your PostgreSQL database. By connecting to your Postgres database and analyzing its schema, Kanel can generate TypeScript types that accurately represent the tables, columns, and relationships within your database. This saves you time and reduces the chances of human error.
How Kanel Differs from an ORM
Unlike ORMs, Kanel focuses solely on generating TypeScript types based on the Postgres database schema. ORMs help you connect and query your databases; Kanel does only a subset of what an ORM can do by generating the types. You cannot query or modify your database using Kanel, and it is more suitable for projects that follow database-driven development approaches where the database schema is the source of truth for the application.
Database-Driven Development
In database-driven development, the database schema is considered the primary source of truth for the application. You don’t need to define anything else other than the types that Kanel will generate to interact with the database. This approach is particularly useful when accessing a predefined database schema that multiple applications use.
Getting Started with Kanel
Before you begin, ensure that you have the following prerequisites installed: Node.js, pnpm, a live PostgreSQL database, and Docker and Docker Compose (optional). Then, install Kanel and create a .kanelrc.js
file in the root directory of your project to configure the connection to your Postgres database.
Generating TypeScript Types
Once you’ve configured Kanel, you can generate TypeScript types from your PostgreSQL database by running a simple command. This will trigger Kanel to generate the types in the output directory specified in your .kanelrc.js
file.
Creating a Basic App with Kanel and Kysely
Let’s create a to-do list app using a NestJS backend and a Vue 3 frontend. We’ll focus on integrating Kanel and Kysely into a backend API to interact with the Postgres database. We’ll use a monorepo architecture, where the backend and frontend are housed in the same repository.
Copying the Template Repository
Start by copying the monorepo template from the companion GitHub repository. This will allow you to follow along more easily.
Configuring a Shared Kanel and Kysely Package
Create a shared Kanel and Kysely package to contain the generated TypeScript types from your Postgres database. This package will be used in the backend and can be reused later in the frontend app.
Generating the PostgreSQL Schema
After configuring Kanel, create a PostgreSQL instance and run an SQL query to create the necessary tables and schema in your database. Then, generate the TypeScript schema using Kanel.
Creating the Backend API
Import the generated TypeScript types from your todo-shared project into your backend API. Create a new database.ts file to initiate your Kysely connection to the PostgreSQL database. Then, create controllers and services for the user, project, and to-do endpoints.
Testing the App
Finally, test your app by running the backend and frontend separately. You can then access the frontend app on your local machine by opening a web browser and navigating to http://localhost:8080.
Conclusion
Kanel is a powerful tool for generating TypeScript types from PostgreSQL databases. By leveraging Kanel, you can ensure type safety and eliminate manual type declarations, simplifying the development process and reducing the risk of type-related errors.