nestjs

TypeORM Entity in NESTJS - Cannot use import statement outside a module

扶醉桌前 提交于 2020-01-23 08:01:06
问题 Started new project with 'nest new' command. Works fine until I add entity file to it. Got following error: import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; ^^^^^^ SyntaxError: Cannot use import statement outside a module What do I miss? Adding Entity to Module: import { Module } from '@nestjs/common'; import { BooksController } from './books.controller'; import { BooksService } from './books.service'; import { BookEntity } from './book.entity'; import { TypeOrmModule } from

While creating a dynamic module in Nest.js should i use registerAsync or forRootAsync?

两盒软妹~` 提交于 2020-01-21 18:39:24
问题 While creating a dynamic module some of the nestjs modules are using registerAsync() some use forRootAsync() . which is the recommended method or is there any difference between these two? PassportModule.registerAsync({ imports: [ConfigModule], useExisting: PassportConfigService, }), TypeOrmModule.forRootAsync({ imports: [ConfigModule], useExisting: TypeormConfigService, }), 回答1: The names are just conventions and do not influence your application's behavior. Nevertheless, it is important to

Test NestJS Service against Actual Database

萝らか妹 提交于 2020-01-21 12:43:27
问题 I would like to be able to test my Nest service against an actual database. I understand that most unit tests should use a mock object, but it also, at times, makes sense to test against the database itself. I have searched through SO and the GH issues for Nest, and am starting to reach the transitive closure of all answers. :-) I am trying to work from https://github.com/nestjs/nest/issues/363#issuecomment-360105413. Following is my Unit test, which uses a custom provider to pass the

Logging request/response in Nest.js

十年热恋 提交于 2020-01-21 05:44:09
问题 New to Nest.js, I am trying to implement a simple logger for tracing HTTP requests like : :method :url :status :res[content-length] - :response-time ms From my understanding the best place for that would be interceptors. But I Also use Guards and as mentionned, Guards are triggered after middlewares but before interceptors. Meaning, my forrbidden accesses are not logged. I could write the logging part in two different places but rather not. Any idea? Thanks! My Interceptor code: import {

Auth module with deferred User module

此生再无相见时 提交于 2020-01-16 17:34:28
问题 I have an AuthModule in a generalized library which requires interacting with a UserModule similar to the docs. What I want to do is define an Interface that the UserService must adhere to instead of an actual implementation. This will leave the implementation details to the users of the library. I've tried a few different approaches such as having a string token APP_USER_SERVICE initially null then be overridden by the implementor, but this seemed to run into trouble injecting a null value

Unit Testing Nestjs Mongoose services

旧城冷巷雨未停 提交于 2020-01-16 08:24:45
问题 I've been trying to figure out how to unit test services NestJS. So I wrote a spec file which tests these NestJS services which use mongoose using jest. The spec file is as follows: import { Test, TestingModule } from '@nestjs/testing'; import { EventsService } from './events.service'; import { getModelToken } from '@nestjs/mongoose'; import { CreateEventDto } from './dto/create-event.dto'; const event = { _id: '53d53d2s', name: 'Event Name', description: 'Description of the event.', min_team

Nest.JS deploy to Heroku

耗尽温柔 提交于 2020-01-13 11:38:29
问题 I'm currently trying to deploy the basic starter template generated by the Nest.JS CLI (as of version 5.3.0) and seem to be getting a timeout on app startup. I'm wondering if anyone has managed to deploy to Heroku? I'm not sure whether or not I need to include some kind of Procfile? Also, there doesn't seem to be much info around deploying the Nest.JS The Heroku logs when I try to deploy. 2018-10-16T06:52:09.602465+00:00 heroku[web.1]: Starting process with command `npm start` 2018-10-16T06

NestJS - Validating body conditionally, based on one property

匆匆过客 提交于 2020-01-13 04:43:49
问题 I'm trying to find a nice way to validate a body using DTO (using the brilliant class-validator and class-transformer libraries). It works really well, even for nested structures but in my case I'd like to have the body property based on some conditions. Example that will probably help to understand: Let's imagine my body should always have selectedCategory . Based on that field, the content could either be from category 1, which contains prop1 OR from category 2, which contains prop2 . I do

How to get user's information in Nest.js?

萝らか妹 提交于 2020-01-11 11:51:10
问题 I am using Angular+Nest to develop a website. I have created a service(Angular) so that client can get user's information from server when project start up(the same as fresh). Some actions don't need to login, so the login is optional. What I want is if user has logined, then client should send a request to get user's information. Server code as below: export const RequestUser = createParamDecorator((data, req): RequestUserDTO => { return req.user; }); @Controller('auth') export class

Modify or save data in request object in fastify

醉酒当歌 提交于 2020-01-06 08:36:07
问题 I use nestjs to build a REST API. I have a middleware which loads data from redis cache and should save it in the request object to access it in the controller function. If i use express as engine it works, but with fastify it doesn't work. The data is undefined in the controller function. The code looks like: function mymiddleware(req, res, next) => { req.data = {...}; next(); }; 回答1: this is a simple working example: const fastify = require('fastify')({ logger: true }) fastify.use(function