TypeORM: How to Select Random Rows
In TypeORM, you can select a single or multiple random rows by adding orderBy(‘RANDOM()’) to your query builder. Let’s see a couple of examples below for more clarity. Let’s say we have a User entity like…
TypeORM: Find Records by Relation Columns
In TypeORM, you can select rows in a table based on the relation columns (in the related table) by using a query builder like this: The code snippet above retrieves all posts that were created by…
Cascade Delete in TypeORM
When working with a database, the cascade delete feature ensures that deleting a parent record will also remove the child records. For instance, we have a table that stores information about users and another table that…
TypeORM: How to Connect to Multiple Database
In TypeORM, you can simply connect to multiple databases simultaneously just by creating multiple data sources. The Steps 1. Setup your data sources: 2. The next step is to initialize the connections (usually placed in the…
TypeORM: How to Execute Raw SQL Queries
In TypeORM, you can execute raw SQL queries by calling the query() method (you can access this method via your data source or the entity manager). You can do everything from CRUD data (create, read, update,…
TypeORM: ORDER BY Multiple Columns
In TypeORM, you can order your results by multiple columns. The 2 examples below show you how to do that. The first example uses the find() method whilst the second one uses a query builder. In…
How to Use Subqueries in TypeORM
This article is about using subqueries with query builders in TypeORM. Let’s say we have an entity named Product: The code snippet below finds products with a higher than average price by using a subquery (that…
TypeORM: Get Raw SQL Query from QueryBuilder
When communicating with databases through TypeORM, you can get raw SQL queries produced by query builders by using the getQuery() or getSql() methods (you can use them synchronously without the await keyword). Example: Output: If you…
TypeORM: How to Use Column Alias when Querying Data
This succinct tutorial shows you how to use column aliases to assign temporary names to columns when selecting data with TypeORM. The point here is the AS keyword. Let’s study a couple of examples for more…
TypeORM: Selecting DISTINCT Values
In real life, it’s possible that a column contains many duplicate values. However, there might be occasions when you only want to get different (unique) values. If you’ve worked with MySQL or PostgreSQL, you are very…
TypeORM: Sort Results by a Relation Column
When using TypeORM to interact with databases, there might be occasions when you want to take records and sort them based on a relational field. There are 2 solutions for this: using find options and using…
TypeORM: Counting Records in One-To-Many Relation
When communicating with the database through TypeORM, there will be many cases where you need to count records in a one-to-many relationship, such as: You want to list the users and the number of photos belonging…