Node.js PassportLocalDocument和PaginateModel是否在同一界面上?
我对typescript和NestJS框架比较陌生。目前,我想为我的应用程序中的所有模型实现分页机制。在当前的api项目中,我将NestJS与mongoose一起使用 我的用户schemma如下Node.js PassportLocalDocument和PaginateModel是否在同一界面上?,node.js,mongodb,typescript,mongoose,nestjs,Node.js,Mongodb,Typescript,Mongoose,Nestjs,我对typescript和NestJS框架比较陌生。目前,我想为我的应用程序中的所有模型实现分页机制。在当前的api项目中,我将NestJS与mongoose一起使用 我的用户schemma如下 export const UserSchema = new mongoose.Schema({ firstName: String, lastName: String, email: String, phone: String, password: { type: Strin
export const UserSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
phone: String,
password: {
type: String
}
});
UserSchema.plugin(mongoosePaginate);
UserSchema.plugin(passportLocalMongoose, {
usernameField: 'email',
});
我的用户界面如下所示:
export interface IUser extends PassportLocalDocument {
readonly firstName: string;
readonly lastName: string;
readonly email: string;
readonly phone: string;
readonly password: string;
}
我的用户服务如下所示:
@Injectable()
export class UsersService implements IUsersService {
constructor(@InjectModel('User') private readonly userModel: PassportLocalModel<IUser>) {
}
async findAll(): Promise<IUser[]> {
return await this.userModel.find().exec();
}
@Injectable()
导出类UsersService实现IUsersService{
构造函数(@InjectModel('User')私有只读用户模型:PassportLocalModel){
}
异步findAll():承诺{
return等待this.userModel.find().exec();
}
我想通过IUser界面添加mongoose paginate功能,这样我就可以通过this.userModel.paginate
在服务中访问它
我提到我安装了:“@types/mongoose paginate”:“^5.0.6”
和“mongoose paginate”:“^5.0.3”
,我可以从mongoose
导入PaginateModel
我想,IUser
界面应该是这样的:
导出接口IUser扩展了PaginateModel{}
,但我不确定在注入服务时如何实例化它
等待您的回复,伙计们,谢谢!:D我处理了另一个问题。我创建了两个接口,一个用于注册/身份验证,另一个用于数据操作 导入分页模型后,必须使用
文档
扩展界面
导出接口IUser扩展文档
之后,当您将其注入服务时:
@InjectModel('User')私有只读用户模型:PaginateModel
最后,在服务接口和服务实现中,更改返回类型,如下所示:
async findAll(yourParams:yourparamsdo):Promise
这是一段代码,适用于将插件与nestjs一起使用的用户。您还可以安装以获得打字支持从'@nestjs/common'导入{Injectable};
从'@nestjs/mongoose'导入{InjectModel};
从“猫鼬”导入{PaginateModel};
从“./interfaces/Message.interface”导入{Message};
@可注射()
导出类消息服务{
建造师(
//“分页模型”将提供必要的分页方法
@InjectModel(“Message”)专用只读messageModel:PaginateModel,
) {}
/**
*查找频道中的所有邮件
*
*@param{string}channelId
*@param{number}[page=1]
*@param{number}[limit=10]
*@返回
*@memberof messages服务
*/
异步FindAllByChannelIdAginated(channelId:字符串,页码:number=1,限制:number=10){
常量选项={
填充:[
//要填充的外键字段
],
页码:编号(第页),
限制:数量(限制),
};
//从数据库中获取数据
return wait this.messageModel.paginate({channel:channelId},options);
}
}
@Totoro这与您的tsconfig有关,因为此功能用于早期版本的应用程序。我想,tsconfig配置的最新更新会导致此问题。
import { Schema } from 'mongoose';
import * as mongoosePaginate from 'mongoose-paginate';
export const MessageSchema = new Schema({
// Your schema definitions here
});
// Register plugin with the schema
MessageSchema.plugin(mongoosePaginate);
export interface Message extends Document {
// Your schema fields here
}
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { PaginateModel } from 'mongoose';
import { Message } from './interfaces/message.interface';
@Injectable()
export class MessagesService {
constructor(
// The 'PaginateModel' will provide the necessary pagination methods
@InjectModel('Message') private readonly messageModel: PaginateModel<Message>,
) {}
/**
* Find all messages in a channel
*
* @param {string} channelId
* @param {number} [page=1]
* @param {number} [limit=10]
* @returns
* @memberof MessagesService
*/
async findAllByChannelIdPaginated(channelId: string, page: number = 1, limit: number = 10) {
const options = {
populate: [
// Your foreign key fields to populate
],
page: Number(page),
limit: Number(limit),
};
// Get the data from database
return await this.messageModel.paginate({ channel: channelId }, options);
}
}