这是我的架构,我想将角色设置为枚举
@Prop({ 必需: true }) 名称: 字符串;
@Prop({ 必需: true }) 电子邮件: 字符串;
@Prop({ 必需: true }) 密码: 字符串;
@Prop() 作用:字符串;
这就是我以前在猫鼬中所做的
role: {
type: String,
enum: roles,
default: 'user',
},
Run Code Online (Sandbox Code Playgroud)
const 角色 = ['用户', '管理员'];
我开始使用 NestJS,从旧的express/mongoose 项目迁移,并立即撞上了栅栏,只是遵循 NestJS 文档中的 MongoDB/序列化章节。我准备了以下架构
/////// schema
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { Exclude, Expose } from 'class-transformer';
export type UserDocument = User & mongoose.Document;
@Schema()
export class User {
@Prop()
@Exclude()
_id: String
@Expose()
get id(): String { return this._id ? `${this._id}` : undefined }
@Prop()
name: string
@Prop({ unique: true })
login: string
@Exclude()
@Prop()
password: string
}
export const UserSchema = SchemaFactory.createForClass(User);
Run Code Online (Sandbox Code Playgroud)
将其注册到app.module中
MongooseModule.forRoot('mongodb://localhost/old_project'),
MongooseModule.forFeature([ { …Run Code Online (Sandbox Code Playgroud) In our codebase we've been using T.lean() or T.toObject() and our return types would be LeanDocument<T>. Mongoose 7 no longer exports LeanDocument, and the existing migration guide suggests using the following setup:
// Do this instead, no `extends Document`
interface ITest {
name?: string;
}
const Test = model<ITest>('Test', schema);
// If you need to access the hydrated document type, use the following code
type TestDocument = ReturnType<(typeof Test)['hydrate']>;
Run Code Online (Sandbox Code Playgroud)
But this gives me HydratedDocument that I can get …
mongoose typescript typescript-generics nestjs nestjs-mongoose
当我在 prop 装饰器中使用嵌套的对象数组时:
@Schema()
export class Child {
@Prop()
name: string;
}
@Schema()
export class Parent {
@Prop({type: [Child], _id: false}) // don't need `_id` for nested objects
children: Child[];
}
export const ParentSchema = SchemaFactory.createForClass(Parent);
Run Code Online (Sandbox Code Playgroud)
我收到错误:
TypeError: Invalid schema configuration: `Child` is not a valid type within the array `children`.
Run Code Online (Sandbox Code Playgroud)
如果我需要使用@Prop({_id: false})(以保持嵌套模式独立),我该如何解决这个问题?
如果我们更改 prop 装饰器,@Prop([Child])它就可以工作,但是我们需要禁用_id嵌套对象:
@Schema({_id: false})
export class Child {
@Prop()
name: string;
}
@Schema()
export class Parent {
@Prop([Child])
children: Child[]; …Run Code Online (Sandbox Code Playgroud) javascript mongoose typescript-decorator nestjs nestjs-mongoose
我有一个 Nestjs 和 MongoDB 应用程序。
auth.module.ts-
@Module({
imports: [
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
],
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)
auth.service.ts-
@Injectable()
export class AuthService {
// Inject User model into AuthService
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}
getUser(username: string) {
const user = this.userModel.find({ name: username });
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
@nestjs/mongoose我使用和创建了一个 UserSchema mongoose。
根据文档,当我导入MongooseModule模块中使用的架构时,该架构只能在该特定模块中使用。
如果我想访问我的模块和服务中的多个模型怎么办?有办法吗?
如何将多个模型注入到服务中?