Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
feat(products): create crud for products
Browse files Browse the repository at this point in the history
- NestJS CRUD (https://github.com/nestjsx/crud) to generate the
  RESTful CRUD for the object.
- Swagger (https://docs.nestjs.com/recipes/swagger) to generate API
  documentation, listening at `/api/docs`.
- Typegroose (https://github.com/typegoose/typegoose) to map
  Typescript objects to MongoDB/Mongoose.
- https://www.npmjs.com/package/nest-crud-mongoose

Took example from https://github.com/9173860/nest-crud-mongoose-demo.
  • Loading branch information
felipecrs committed Mar 12, 2020
1 parent 93a0044 commit 75596a9
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 12 deletions.
7 changes: 6 additions & 1 deletion apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ProductsModule } from './products/products.module';
import { TypegooseModule } from 'nestjs-typegoose';

@Module({
imports: [],
imports: [
ProductsModule,
TypegooseModule.forRoot('mongodb://localhost:27017/price-search')
],
controllers: [AppController],
providers: [AppService]
})
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/app/products/product.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { prop } from '@typegoose/typegoose';
import { ApiProperty } from '@nestjs/swagger';

export class Product {
@ApiProperty()
@prop({ required: true })
name: string;
}
18 changes: 18 additions & 0 deletions apps/api/src/app/products/products.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductsController } from './products.controller';

describe('Products Controller', () => {
let controller: ProductsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ProductsController],
}).compile();

controller = module.get<ProductsController>(ProductsController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
16 changes: 16 additions & 0 deletions apps/api/src/app/products/products.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller } from '@nestjs/common';
import { Crud } from '@nestjsx/crud';
import { ApiTags } from '@nestjs/swagger';
import { Product } from './product.model';
import { ProductsService } from './products.service';

@Crud({
model: {
type: Product
}
})
@ApiTags(Product.name)
@Controller('products')
export class ProductsController {
constructor(private readonly service: ProductsService) {}
}
13 changes: 13 additions & 0 deletions apps/api/src/app/products/products.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { ProductsController } from './products.controller';
import { ProductsService } from './products.service';
import { TypegooseModule } from 'nestjs-typegoose';
import { Product } from './product.model';

@Module({
imports: [TypegooseModule.forFeature([Product])],
controllers: [ProductsController],
exports: [ProductsService],
providers: [ProductsService]
})
export class ProductsModule {}
18 changes: 18 additions & 0 deletions apps/api/src/app/products/products.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductsService } from './products.service';

describe('ProductsService', () => {
let service: ProductsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ProductsService],
}).compile();

service = module.get<ProductsService>(ProductsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
14 changes: 14 additions & 0 deletions apps/api/src/app/products/products.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Product } from './product.model';
import { ModelType, DocumentType } from '@typegoose/typegoose/lib/types';
import { MongooseCrudService } from 'nest-crud-mongoose';
import { InjectModel } from 'nestjs-typegoose';

@Injectable()
export class ProductsService extends MongooseCrudService<
DocumentType<Product>
> {
constructor(@InjectModel(Product) public repo: ModelType<Product>) {
super(repo);
}
}
18 changes: 17 additions & 1 deletion apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app/app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { mongoose, setLogLevel, LogLevels } from '@typegoose/typegoose';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);

const options = new DocumentBuilder()
.setTitle('Price Search')
.setDescription('The Price Search API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup(`${globalPrefix}/docs`, app, document);

mongoose.set('debug', true);
setLogLevel(LogLevels.DEBUG);

const port = process.env.port || 3333;
await app.listen(port, () => {
console.log('Listening at http://localhost:' + port + '/' + globalPrefix);
const baseUrl = `http://localhost:${port}/${globalPrefix}`;
console.log(`Listening at ${baseUrl}`);
console.log(`Swagger UI listening at ${baseUrl}/docs`);
});
}

Expand Down
Loading

0 comments on commit 75596a9

Please sign in to comment.