Drizzle uses a syntax that closely resembles native SQL. In this chapter, we will compile some common uses of Drizzle to facilitate quick learning for those without a SQL background, while also discussing the best way to integrate Drizzle with Milkio.
BREAD
A guide to basic operations for adding, modifying, deleting, and querying tables.
Insert
Add a new row to a table.
Update
Modify a row based on conditions. Here, we are changing the name of all users named Amber to Kirsi.
Delete
Delete a row based on conditions. Here, we are deleting all users named Kirsi.
Get a Row
Retrieve a row of data, returning only the first result if there are multiple matches. Here, we are retrieving the user named Reynard.
Get All Rows and Sort
Retrieve all rows and sort by created_at. Note that for large tables, this may lead to performance issues.
Pagination
Retrieve data in pages instead of returning all data at once, similar to reading where each page contains a specified number of entries. This can effectively reduce server load.
Relationships
Database tables often have relationships with each other. For example, a blog post may have many comments, or an order belongs to a user who placed it.
One-to-One
Let’s assume we have a user table and a user info table. The user info table contains the user’s email address, and in the user info table, there is a userId field that records the id of the corresponding user table.
One-to-Many
Let’s assume we have an article table and a comment table, where one article can have multiple comments. In the comment table, there is an articleId field that records the id of the article it belongs to.
Many-to-Many
Let’s consider a scenario with user and organization tables, where a user can join multiple organizations, and an organization can have multiple users. In this case, we need an additional intermediate table to record the relationships between the two tables. It is a common practice to name the intermediate table by adding -to- between the names of the two tables.
Relationship Queries
In Drizzle, you can easily query associated data using with.
Type Safety
In the API section, we learned that Milkio can restrict API parameters based on TypeScript types.
By directly using table types, we can easily ensure that API parameters align with the database table structure. We no longer need to repeatedly define types in controllers as we did in the past.
Using the DBInsert helper type, we can convert tables into TypeScript types required for insertion.
Additionally, Milkio provides various helper types to better restrict params in APIs.
DBInsert
Type for inserting data, aligned with the type defined in your database table.
DBSelect
Type for selecting data, aligned with the type defined in your database table.
DBPartial
Makes all fields in the database table optional, typically used in update APIs that allow incremental submissions.
DBOmit
Removes fields from the database table, commonly used to prevent certain fields from being updated, such as id, createdAt, updatedAt, deletedAt, and to avoid updating permission-related fields like isAdmin.
Type Tags
In the API section, we learned about Typia’s type tags. Can we also add type tags to tables for stricter type constraints?
Drizzle allows us to define the actual type of a table using $type and add type tags. Let’s add type tags to the user table created in the previous section for more stringent type constraints.
JSON
Both MySQL and PostgreSQL support the JSON type, which means we can store more flexible data types in tables. Our user table can even be rewritten like this:
Then, we can use the data field to store user data.
JSON gives us great flexibility. When we are unsure about the fields a table may have, we can use json for storage. However, if you need to query the data, performance will be much lower compared to querying regular fields unless you establish multi-value indexes. So, please use it judiciously.
What’s Next?
Drizzle has many more useful features. Dive deeper by reading the Drizzle documentation!