Skip to content

Instantly share code, notes, and snippets.

@adsonrocha
Last active October 23, 2019 00:18
Show Gist options
  • Save adsonrocha/b7239958c37166d75f685c943ce2574f to your computer and use it in GitHub Desktop.
Save adsonrocha/b7239958c37166d75f685c943ce2574f to your computer and use it in GitHub Desktop.
User service with mongoose model injected
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User } from './user.model';
import { Model } from 'mongoose';
@Injectable()
export class UserService {
constructor(
@InjectModel('User') private readonly userModel: Model<User>,
) {
}
async create(doc: User) {
const result = await new this.userModel(doc).save();
return result.id;
}
async findById(id: number) {
// ...
}
async update(user: User) {
// ...
}
async remove(user: User) {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment