You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide functions for relational handling in Typeorm.
5
+
<palign="center"> <b>typeorm-helper</b> enhances TypeORM integration in your NestJS projects, streamlining database interactions and management. It offers utilities for easier setup and configuration of TypeORM, simplifies common database operations, and promotes best practices for managing entities and migrations. You can efficiently manage your data layer, ensuring robust and maintainable data access within your application. </p>
4
6
5
-
## How to use?
7
+
## Installation 🤖
6
8
7
-
Extend BaseEntity from typeorm-helper
9
+
Install the `typeorm-helper` package with:
8
10
9
-
```typescript
10
-
exportclassPostextendsBaseEntity {
11
-
}
11
+
```bash
12
+
npm install @hodfords/typeorm-helper --save
12
13
```
13
14
14
-
Extend BaseRepository from typeorm-helper
15
+
## Usage 🚀
16
+
17
+
### Defining custom repositories and entities
18
+
19
+
When managing different entities, you can define custom repositories and entities. Below is an example for the Category entity and its corresponding repository.
20
+
21
+
#### Entity
22
+
23
+
The `Category` table in the database is modeled by the `CategoryEntity`, `typeorm` decorators should be used to define this entity.
The `CategoryRepository` is a custom repository that handles all database operations related to the `CategoryEntity`. By using the `@CustomRepository` decorator and extending `BaseRepository`, you ensure that your repository has both common CRUD functionality and can be easily customized with entity-specific methods.
25
46
26
47
```typescript
27
-
let user =awaitUser.createQueryBuilder().getOne();
Lazy relations allow you to load related entities only when they are needed. This can significantly improve performance by preventing the fetching of unnecessary data upfront.
57
+
58
+
This functionality supports handling single entity, collection of entities, and paginated collection. Below is an example of how to load a list of posts associated with a specific category.
>For queries that are complex, need to be reused, or contain a lot of logic. We should use a class to store it.
175
+
### Query Builder
176
+
177
+
For complex and reusable queries, it's helpful to put the logic inside a class. This makes it easier to manage and reuse the query, resulting in cleaner and more maintainable code.
> We create a class, which wraps the migration of typeorm, allowing for simpler and more readable. For the update command, let's use pure queries for the time being.
199
+
### Migrations
200
+
201
+
We develop a class that abstracts the typeorm migration, making it easier to understand. For the update command, let's use pure queries for the time being.
@@ -231,6 +251,9 @@ export class CreateUserTable1626749239046 extends BaseMigration {
231
251
```
232
252
233
253
### Column method
254
+
255
+
The BaseColumn class provides methods that define and configure properties for a database column, including length, nullability, uniqueness, indexing, default values, and foreign key relationships.
256
+
234
257
```typescript
235
258
length(length: number): this;
236
259
nullable(): this;
@@ -239,3 +262,7 @@ export class CreateUserTable1626749239046 extends BaseMigration {
0 commit comments