nestjs

How to stub EntityManager and Connection in TypeORM with Jest

喜你入骨 提交于 2020-06-17 13:26:22
问题 I got an app on NestJS in Typescript using TypeORM and unit-tests written with Jest. I have a function that uses transactions like this: async createMany(users: User[]) { await this.connection.transaction(async manager => { await manager.save(users[0]); await manager.save(users[1]); }); } That's an example from NestJS docs. I do it roughly in the same way via this.connection.transaction but the business-logic is different. The thing is I want to make a unit-test to test this service function.

During a function call how can I use rest parameters in Typescript?

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-17 13:08:52
问题 I am working on nestjs and I want to retrieve data from DB on the basis of name property, which I am passing in a method of services code. The issue is when am passing {"name": "item1"} in postman, am getting item1 related data, but when am passing {"name":"item1","name":"item2"} in postman, I am getting data related to item2 only.I want to retrieve data of both item1, item2, and so on. To overcome this issue I thought rest parameters would be a good choice, but I don't know how to pass rest

How use profiles from nartc/automapper into a nestjs application

橙三吉。 提交于 2020-06-16 17:16:08
问题 I'm trying to use AutoMapper for nodejs from nartc/automapper lib inside a NestJS project, but I'm having troubles when trying to use Profiles functionality. Here is my configuration: App.module @Module({ imports: [ AutomapperModule.withMapper(), ], controllers: [], providers: [], }) export class AppModule implements NestModule {} Profile @Profile() export class RoleProfile extends ProfileBase { constructor(@InjectMapper() mapper: AutoMapper) { super(); mapper .createMap(Role,

How use profiles from nartc/automapper into a nestjs application

妖精的绣舞 提交于 2020-06-16 17:15:08
问题 I'm trying to use AutoMapper for nodejs from nartc/automapper lib inside a NestJS project, but I'm having troubles when trying to use Profiles functionality. Here is my configuration: App.module @Module({ imports: [ AutomapperModule.withMapper(), ], controllers: [], providers: [], }) export class AppModule implements NestModule {} Profile @Profile() export class RoleProfile extends ProfileBase { constructor(@InjectMapper() mapper: AutoMapper) { super(); mapper .createMap(Role,

Saving and working with a self-referencing entity in typeorm

折月煮酒 提交于 2020-06-16 17:14:10
问题 I will try to be clear when explaining the issue I'm currently facing. A piece of code from the CategoryEntity class. category.entity.ts @ManyToMany(type => EntryEntity, entry => entry.categories) entries: EntryEntity[]; @ManyToOne(type => UserEntity, user => user.categories, {onDelete: 'CASCADE', cascade: true}) user: UserEntity; @ManyToOne(type => CategoryEntity, category => category.subcategories) category: CategoryEntity; @OneToMany(type => CategoryEntity, category => category.category)

How to use query parameters in Nest.js?

跟風遠走 提交于 2020-06-14 04:17:04
问题 I am a freshman in Nest.js. And my code as below @Get('findByFilter/:params') async findByFilter(@Query() query): Promise<Article[]> { } I have used postman to test this router http://localhost:3000/article/findByFilter/bug?google=1&baidu=2 Actually, I can get the query result { google: '1', baidu: '2' } . But I'm not clear why the url has a string 'bug' ? If I delete that word just like http://localhost:3000/article/findByFilter?google=1&baidu=2 then the postman will shows statusCode 404 .

How to use query parameters in Nest.js?

会有一股神秘感。 提交于 2020-06-14 04:16:29
问题 I am a freshman in Nest.js. And my code as below @Get('findByFilter/:params') async findByFilter(@Query() query): Promise<Article[]> { } I have used postman to test this router http://localhost:3000/article/findByFilter/bug?google=1&baidu=2 Actually, I can get the query result { google: '1', baidu: '2' } . But I'm not clear why the url has a string 'bug' ? If I delete that word just like http://localhost:3000/article/findByFilter?google=1&baidu=2 then the postman will shows statusCode 404 .

How to access websocket from controller or another component/services?

。_饼干妹妹 提交于 2020-06-13 10:54:53
问题 I have a REST API, I want to send event to the client via websocket. How to inject websocket instance in controller or another component? 回答1: Better solution is to create global module . You can then emit events from any other module/controller. A. Afir approach will create multiple instances of Gateway if you try to use it in other modules. Note : This is just simplest solution Create socket.module.ts import { Module, Global } from '@nestjs/common'; import { SocketService } from './socket

Best way to architecture server side i18n

时光毁灭记忆、已成空白 提交于 2020-06-12 06:40:11
问题 I'm using nestjs to create my REST API, and I got the requirement to support i18n for all the messages that the API returns (exception messages, hints, and so on) and I'm wondering what is the better way to do it with nestjs framework. With plain express, I can get the user language from the request headers, and that can be translated to a Nestjs Middleware in order to put the language code into someware that lives in the request execution context and then using from my i18n service (I do not

Using 'nestjs/jwt' signing with dynamic/user-related secret

三世轮回 提交于 2020-06-12 05:31:08
问题 I'm trying to create a user token based on the secret of the user trying to log in. However instead of using a secret from the environment I want to use a secret assigned to a user object inside the database. import { Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { UserService } from '@src/modules/user/services'; @Injectable() export class AuthService { public constructor(private readonly jwtService: JwtService, private readonly userService: UserService)