backstopmedia / nest-book-example

70 stars 35 forks source link

MongoDb async/await version 6 Services Differences #28

Open datajango opened 5 years ago

datajango commented 5 years ago

Version 6 of Nest has a few differences in the way services are handled. The NestJS web sites, the PDF book, and the code here are all out-of-sync with each other? The book on page 129 show book aync/await calls:

from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { EntrySchema } from './entry.schema'; import { Entry } from './entry.interface'; @Injectable() export class EntriesService { constructor( @InjectModel(EntrySchema) private readonly entryModel: Model ) {} // this method retrieves all entries findAll() { return this.entryModel.find().exec(); } // this method retrieves only one entry, by entry ID findById(id: string) { return this.entryModel.findById(id).exec(); } // this method saves an entry in the database create(entry) { entry._id = new Types.ObjectId(); const createdEntry = new this.entryModel(entry); return createdEntry.save(); } }

the nest web site does: https://docs.nestjs.com/techniques/mongodb

import { Model } from 'mongoose'; import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Cat } from './interfaces/cat.interface'; import { CreateCatDto } from './dto/create-cat.dto';

@Injectable() export class CatsService { constructor(@InjectModel('Cat') private readonly catModel: Model) {}

async create(createCatDto: CreateCatDto): Promise { const createdCat = new this.catModel(createCatDto); return await createdCat.save(); }

async findAll(): Promise<Cat[]> { return await this.catModel.find().exec(); } }

Will this project be upgraded to version 6 with examples of best practices?