@@ -55,13 +55,14 @@ import { User } from '../entities/user.entity';
5555
5656@Controller (' /api/users' )
5757export class UserController {
58+ constructor (@DSource () private dataSource : DataSource ) {}
5859 @Get () // => GET /api/users
59- public async getAll(@ DSource () dataSource : DataSource ): Promise <User []> {
60- return dataSource .manager .find (User );
60+ public async getAll(): Promise <User []> {
61+ return this . dataSource .manager .find (User );
6162 }
6263 @Get (' /:id' ) // => GET /api/users/:id
63- public async getById(@Param (' id' ) id : string , @ DSource () dataSource : DataSource ): Promise <User > {
64- return dataSource .manager .findOne (User , id );
64+ public async getById(@Param (' id' ) id : string ): Promise <User > {
65+ return this . dataSource .manager .findOne (User , id );
6566 }
6667 @Post (' /:id' ) // => POST /api/users/:id
6768 public async modify(@Body () body : { name: string ; }): Promise <{ name: string ; }> {
@@ -420,6 +421,26 @@ export class UserController {
420421
421422** ` @DSource() ` ** decorator injects you a [ DataSource] ( https://typeorm.io/data-source-api ) object.
422423
424+ Support constructor ** ` @DSource() ` ** decorator
425+
426+ ``` typescript
427+ import { Controller , Get , DSource , DataSource } from ' typenexus' ;
428+ import { Response , Request }from ' express' ;
429+ import { User } from ' ../entity/User.js' ;
430+
431+ @Controller (' /users' )
432+ export class UserController {
433+ constructor (@DSource () private dataSource : DataSource ) {}
434+ @ContentType (' application/json' )
435+ @Get () // => GET /users
436+ public async getUsers(): Promise <User []> {
437+ return this .dataSource .manager .find (User );
438+ }
439+ }
440+ ```
441+
442+ Support parameter ** ` @DSource() ` ** decorator
443+
423444``` typescript
424445import { Controller , Get , DSource , DataSource } from ' typenexus' ;
425446import { Response , Request }from ' express' ;
@@ -719,10 +740,11 @@ import { User } from '../entity/User.js';
719740
720741@Controller ()
721742export class UserController {
743+ constructor (@DSource () private dataSource : DataSource ) {}
722744 @Delete (" /users/:id" )
723745 @OnUndefined (204 )
724- async remove(@Param (" id" ) id : string , @ DSource () dataSource : DataSource ): Promise <void > {
725- return dataSource .manager .findOneBy (User , { id });
746+ async remove(@Param (" id" ) id : string ): Promise <void > {
747+ return this . dataSource .manager .findOneBy (User , { id });
726748 }
727749}
728750```
@@ -735,10 +757,11 @@ import { User } from '../entity/User.js';
735757
736758@Controller ()
737759export class UserController {
760+ constructor (@DSource () private dataSource : DataSource ) {}
738761 @Delete (" /users/:id" )
739762 @OnUndefined (404 )
740- async remove(@Param (" id" ) id : string , @ DSource () dataSource : DataSource ): Promise <void > {
741- return dataSource .manager .findOneBy (User , { id });
763+ async remove(@Param (" id" ) id : string ): Promise <void > {
764+ return this . dataSource .manager .findOneBy (User , { id });
742765 }
743766}
744767```
@@ -761,10 +784,11 @@ import { User } from '../entity/User.js';
761784
762785@Controller ()
763786export class UserController {
787+ constructor (@DSource () private dataSource : DataSource ) {}
764788 @Get (" /users/:id" )
765789 @OnUndefined (UserNotFoundError )
766- async remove(@Param (" id" ) id : string , @ DSource () dataSource : DataSource ): Promise <void > {
767- return dataSource .manager .findOneBy (User , { id });
790+ async remove(@Param (" id" ) id : string ): Promise <void > {
791+ return this . dataSource .manager .findOneBy (User , { id });
768792 }
769793}
770794```
0 commit comments