meta | projects | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
This tutorial provides two ways to create connection:
- The first one uses @tsed/typeorm module to create multiple connections with @@ServerSettings@@,
- The second uses the new async provider feature introduced in v5.27 to create connection.
Additionally, this topic shows you how you can use Entity from Typeorm with Ts.ED controller (on the bottom of this page).
Currently, @tsed/typeorm
allows you to:
- Configure one or more TypeORM connections via the
@ServerSettings
configuration. All databases will be initialized when the server starts during the server'sOnInit
phase. - Use the Entity TypeORM as Model for Controllers, AJV Validation and Swagger.
- Declare a connection with asyncProvider or automatically by server configuration.
To begin, install the TypeORM module for TS.ED:
npm install --save @tsed/typeorm
npm install --save typeorm
Then import @tsed/typeorm
in your Server:
<<< @/docs/tutorials/snippets/typeorm/typeorm-configuration.ts
TypeORMService lets you retrieve an instance of TypeORM Connection.
<<< @/docs/tutorials/snippets/typeorm/typeorm-usage.ts
For more information about TypeORM, look its documentation here;
It is also possible to create your connection with the useAsyncFactory
feature (See custom providers)
This approach allows you to inject your connection as a Service to another one.
To create a new connection, declare your custom provider as follows:
<<< @/docs/tutorials/snippets/typeorm/typeorm-async-provider.ts
Then inject your connection to another service or provide like this:
<<< @/docs/tutorials/snippets/typeorm/typeorm-injection-async-provider.ts
We need to define an Entity TypeORM like this and use Ts.ED Decorator to define the JSON Schema.
<<< @/docs/tutorials/snippets/typeorm/typeorm-entity.ts
Now, the model is correctly defined and can be used with a Controller, AJV validation, Swagger and TypeORM.
We can use this model with a Controller like that:
<<< @/docs/tutorials/snippets/typeorm/typeorm-entity-controller.ts
You can create a custom repository which should contain methods to work with your database.
Usually custom repositories are created for a single entity and contain their specific queries.
For example, let's say we want to have a method called findByName(firstName: string, lastName: string)
which will search for users by a given first and last names.
The best place for this method is in Repository, so we could call it like userRepository.findByName(...)
.
You can achieve this using custom repositories.
@tsed/typeorm
plugin configures the DI so that repositories declared for TypeORM can be injected into a Ts.ED controller or service.
The first way to create a custom repository is to extend Repository. Example:
<<< @/docs/tutorials/snippets/typeorm/typeorm-entity-repository.ts
Then inject your repository to another service:
<<< @/docs/tutorials/snippets/typeorm/typeorm-injection-entity-repository.ts
::: tip Use @@UseConnection@@ decorator to select which database connection the injected repository should be used (require Ts.ED v5.58.0+). :::