How to use @nestjs/mongoose - 10 common examples

To help you get started, we’ve selected a few @nestjs/mongoose examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jmcdo29 / testing-nestjs / apps / mongo-sample / src / cat / cat.service.spec.ts View on Github external
beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        CatService,
        {
          provide: getModelToken('Cat'),
          // notice that only the functions we call from the model are mocked
          useValue: {
            new: jest.fn().mockResolvedValue(mockCat()),
            constructor: jest.fn().mockResolvedValue(mockCat()),
            find: jest.fn(),
            findOne: jest.fn(),
            update: jest.fn(),
            create: jest.fn(),
            remove: jest.fn(),
            exec: jest.fn(),
          },
        },
      ],
    }).compile();

    service = module.get(CatService);
github marcomelilli / nestjs-email-authentication / src / auth / auth.module.ts View on Github external
Module, MiddlewareConsumer, NestModule
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { JwtStrategy } from './passport/jwt.strategy';
import { AuthController } from './auth.controller';
import { UserSchema } from '../users/schemas/user.schema';
import { EmailVerificationSchema } from '../auth/schemas/emailverification.schema';
import { ForgottenPasswordSchema } from './schemas/forgottenpassword.schema';
import { ConsentRegistrySchema } from './schemas/consentregistry.schema';
import { UsersService } from '../users/users.service';
import { JWTService } from './jwt.service';
import { MongooseModule } from '@nestjs/mongoose';
import { LoggerMiddleware } from '../common/middlewares/logger.middleware';

@Module({
  imports: [MongooseModule.forFeature([
    { name: 'User', schema: UserSchema },
    { name: 'EmailVerification', schema: EmailVerificationSchema },
    { name: 'ForgottenPassword', schema: ForgottenPasswordSchema },
    { name: 'ConsentRegistry', schema: ConsentRegistrySchema }
  ])],
  controllers: [AuthController],
  providers: [AuthService, UsersService, JWTService, JwtStrategy],
})
export class AuthModule implements NestModule {
  public configure(consumer: MiddlewareConsumer) {
     consumer
      .apply(LoggerMiddleware)
      // .exclude(
      //   { path: 'example', method: RequestMethod.GET },
      // )
      .forRoutes(AuthController);
github djjorik / angular-chat / server / app.module.ts View on Github external
import { RoomsModule } from './rooms/rooms.module';
import { MongooseModule } from '@nestjs/mongoose';
import { CorsMiddleware } from './middlewares/cors.middleware';
import { AuthModule } from './auth/auth.module';


const getMongoUrl = () => {
  if (process.env.MONGOUSER && process.env.MONGOPASSWORD) {
    return `mongodb://${process.env.MONGOUSER}:${process.env.MONGOPASSWORD}@ds249839.mlab.com:49839/angular-nest-chat-app`;
  } else {
    return 'mongodb://localhost:27017/nest';
  }
};

@Module({
  imports: [MongooseModule.forRoot(getMongoUrl()),
    UsersModule,
    RoomsModule,
    AuthModule
  ],
  components: []
})
export class ApplicationModule {
  // configure(consumer: MiddlewaresConsumer): void {
  //   consumer.apply(CorsMiddleware).forRoutes('*');
  // }
}
github jkchao / blog-service / src / module / articles / __test__ / article.resolver.spec.ts View on Github external
beforeAll(async () => {
      const module = await Test.createTestingModule({
        imports: [
          MongooseModule.forRoot(config.MONGO_URL),
          ArticlesModule,
          GraphQLModule.forRoot({
            typePaths: ['./**/*.graphql'],
            path: '/api/v2',
            context: ({ req, res }: { req: Request; res: Response }) => ({
              request: req
            })
          })
        ]
      })
        .overrideProvider(ArticlesSercice)
        .useValue(articleService)
        .compile();

      app = await module.createNestApplication().init();
    });
github EricKit / nest-user-auth / test / app.e2e-spec.ts View on Github external
beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
      providers: [
        UsersService,
        {
          provide: getModelToken('User'),
          useValue: UserModel,
        },
      ],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });
github jkchao / blog-service / src / module / heros / __test__ / heros.resolver.spec.ts View on Github external
beforeAll(async () => {
      const module = await Test.createTestingModule({
        imports: [
          MongooseModule.forRoot(config.MONGO_URL),
          HerosModule,
          GraphQLModule.forRoot({
            typePaths: ['./**/*.graphql'],
            path: '/api/v2',
            context: ({ req, res }: { req: Request; res: Response }) => ({
              request: req
            })
          })
        ]
      })
        .overrideProvider(HerosService)
        .useValue(heroService)
        .overrideProvider(EmailService)
        .useValue(emailService)
        .compile();
github jkchao / blog-service / src / module / comments / __test__ / comments.resolver.spec.ts View on Github external
beforeAll(async () => {
      const module = await Test.createTestingModule({
        imports: [
          MongooseModule.forRoot(config.MONGO_URL),
          CommentsModule,
          GraphQLModule.forRoot({
            typePaths: ['./**/*.graphql'],
            path: '/api/v2',
            context: ({ req, res }: { req: Request; res: Response }) => ({
              request: req
            })
          })
        ]
      })
        .overrideProvider(CommentsService)
        .useValue(commentsService)
        .overrideProvider(ArticlesSercice)
        .useValue({
          findOneArticle() {
            return { _id: '123' };
github jkchao / blog-service / src / module / links / __test__ / link.service.spec.ts View on Github external
beforeAll(async () => {
    const module = await Test.createTestingModule({
      imports: [LinksModule]
    })
      .overrideProvider(getModelToken('Links'))
      .useValue(mockRepository)
      .compile();

    linksService = module.get(LinksService);
  });
github jkchao / blog-service / src / module / tags / __test__ / tags.service.spec.ts View on Github external
beforeAll(async () => {
    const module = await Test.createTestingModule({
      imports: [TagsModule]
    })
      .overrideProvider(getModelToken('Tags'))
      .useValue(mockRepository)
      .overrideProvider(getModelToken('Articles'))
      .useValue({})
      .overrideProvider(ArticlesSercice)
      .useValue({
        aggregate() {
          return [
            {
              _id: '123'
            }
          ];
        }
      })
      .compile();

    tagsService = module.get(TagsService);
  });
github jkchao / blog-service / src / module / options / __test__ / options.service.spec.ts View on Github external
beforeAll(async () => {
      const module = await Test.createTestingModule({
        imports: [OptionsModule]
      })
        .overrideProvider(getModelToken('Options'))
        .useValue(mockRepository)
        .compile();

      optionsService = module.get(OptionsService);
    });