nestjs

nestjs / TypeOrm database transaction

让人想犯罪 __ 提交于 2019-12-01 21:09:35
Assuming we have 2 services, A and B. Service A has a function doing the following: Validate the data Call a service B function, that makes changes to the database Do some more stuff Do changes to the database Now, let's assume that one of the following, steps 3 or 4 failed. Since service B made changes in the database, those changes are still there. Is there any way of rolling the database back in this case? I though about database transactions, but I couldn't find any way to do that in nest js, although it is supported by TypeOrm, it doesn't look natural to nest. If not, I am now "stuck"

Inject nestjs service from another module

谁说胖子不能爱 提交于 2019-12-01 02:26:38
I've got a PlayersModule and an ItemsModule . I want to use the ItemsService in the PlayersService . When I add it by injection: import { Injectable } from '@nestjs/common'; import { InjectModel } from 'nestjs-typegoose'; import { ModelType, Ref } from 'typegoose'; import { Player } from './player.model'; import { Item } from '../items/item.model'; import { ItemsService } from '../items/items.service'; @Injectable() export class PlayersService { constructor( @InjectModel(Player) private readonly playerModel: ModelType<Player>, private readonly itemsService: ItemsService){} I get this nest

Importing TypeScript modules with dependency injecetion in NestJS

好久不见. 提交于 2019-11-29 12:58:23
In my NestJS application - I have TypeScript classes that have other classes and values injected into them. The only thing is that I'm importing the TypeScript classes with import statements, and also using the DI system to inject them. Is there some way to remove the import statements and just let the DI system handle it? TL;DR import -> class reference DI -> class instantiation Matching by string token is possible, but class reference is preferred. Encapsulation The dependency injection system mainly handles the instantiation of the classes. This is great, because you do not need to care

NestJs TypeORM configuration using env files

回眸只為那壹抹淺笑 提交于 2019-11-29 12:04:20
I have two .env files like dev.env and staging.env . I am using typeorm as my database ORM. I would like to know how to let typeorm read either of the config file whenever I run the application. Error: No connection options were found in any of configurations file from typeormmodule. You can create a ConfigService that reads in the file corresponding to the environment variable NODE_ENV : 1) Set the NODE_ENV variable in your start scripts: "start:dev": "cross-env NODE_ENV=dev ts-node -r tsconfig-paths/register src/main.ts", "start:staging": "cross-env NODE_ENV=staging node dist/src/main.js", 2

Importing TypeScript modules with dependency injecetion in NestJS

夙愿已清 提交于 2019-11-28 06:51:37
问题 In my NestJS application - I have TypeScript classes that have other classes and values injected into them. The only thing is that I'm importing the TypeScript classes with import statements, and also using the DI system to inject them. Is there some way to remove the import statements and just let the DI system handle it? 回答1: TL;DR import -> class reference DI -> class instantiation Matching by string token is possible, but class reference is preferred. Encapsulation The dependency

NestJs TypeORM configuration using env files

心不动则不痛 提交于 2019-11-28 06:20:56
问题 I have two .env files like dev.env and staging.env . I am using typeorm as my database ORM. I would like to know how to let typeorm read either of the config file whenever I run the application. Error: No connection options were found in any of configurations file from typeormmodule. 回答1: You can create a ConfigService that reads in the file corresponding to the environment variable NODE_ENV : 1) Set the NODE_ENV variable in your start scripts: "start:dev": "cross-env NODE_ENV=dev ts-node -r

Inject nestjs service from another module

本秂侑毒 提交于 2019-11-28 02:49:14
问题 I've got a PlayersModule and an ItemsModule . I want to use the ItemsService in the PlayersService . When I add it by injection: import { Injectable } from '@nestjs/common'; import { InjectModel } from 'nestjs-typegoose'; import { ModelType, Ref } from 'typegoose'; import { Player } from './player.model'; import { Item } from '../items/item.model'; import { ItemsService } from '../items/items.service'; @Injectable() export class PlayersService { constructor( @InjectModel(Player) private

Nest JS two instances of the same provider

你。 提交于 2019-11-28 02:26:01
问题 Hi on a test suite it appears to me that I have 2 living instances of a same provider, one for the implementation and another one for the real implementation. I base my conclusion in a fact that on my test I tried replace a method by a jest.fn call but still, on the service I am testing the method still points to the original implementation. What makes it even more odd is that I was able to mock another service performing exactly the same procedure, as if, depending on how those services were