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();
}
}
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';
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?