简体   繁体   English

Mongoose Schema 类型作为来自 TypeScript 的自定义接口?

[英]Mongoose Schema type as a custom interface from TypeScript?

在此处输入图像描述

I´d like to store a custom object that has the StationRating interface attributes, can somebody help me?我想存储一个具有 StationRating 接口属性的自定义 object,有人可以帮我吗?

It's possible, but it requires some boilerplate.这是可能的,但它需要一些样板文件。 Issues are:问题是:

  • Interfaces and types in TS do not exist in emitted code TS 中的接口和类型在发出的代码中不存在

  • While you could go in the other direction - create a schema object and make an interface/type out of it - schema object values must be constructors , eg Number , which is not the same thing as something with a number type.虽然您可以在另一个方向上使用 go - 创建一个模式 object 并从中创建一个接口/类型 - 模式 object 值必须是构造函数,例如Number ,这不是number的东西。

But you can create a type that maps the constructor types to their primitive types (eg Number to number ), then use that to turn the schema object into the type you want:但是您可以创建一个将构造函数类型映射到其原始类型(例如Numbernumber )的类型,然后使用它将模式 object 转换为您想要的类型:

type ConstructorMapping<T> =
    T extends NumberConstructor ? number :
    T extends StringConstructor ? string : never; // etc: continue as needed

const schemaObj = {
  score: Number,
  user_id: String,
  station_id: String,
  description: String,
};

type SchemaObj = typeof schemaObj;
type StationRating = {
    [prop in keyof SchemaObj]: ConstructorMapping<SchemaObj[prop]>
};

Then use schemaObj when calling new Schema , and you'll also have the following usable type:然后在调用new Schema时使用schemaObj ,您还将拥有以下可用类型:

type StationRating = {
    score: number;
    user_id: string;
    station_id: string;
    description: string;
}

Is it worth it?这值得么? I'm not sure.我不确定。 For smaller objects, maybe not.对于较小的物体,也许不是。 For larger objects, maybe so.对于较大的物体,也许是这样。 You might prefer just to write out the type and the schema object.您可能更喜欢只写出类型和架构 object。

Is there a specific reason you want to store it in an object?是否有特定原因要将其存储在 object 中? You could use a virtual method to create the "Rating".您可以使用虚拟方法来创建“评级”。 I'm not exactly sure without seeing more, but if all you want to do is reference the score or create some calculations off it, then it seems like an easy enough solution.如果没有看到更多内容,我不确定,但如果您只想参考分数或根据分数创建一些计算,那么这似乎是一个足够简单的解决方案。 https://mongoosejs.com/docs/tutorials/virtuals.html https://mongoosejs.com/docs/tutorials/virtuals.html

const userSchema = mongoose.Schema({
  firstName: String,
  lastName: String
});
// Create a virtual property `fullName` with a getter and setter.
userSchema.virtual('fullName').
  get(function() { return `${this.firstName} ${this.lastName}`; }).
  set(function(v) {
    // `v` is the value being set, so use the value to set
    // `firstName` and `lastName`.
    const firstName = v.substring(0, v.indexOf(' '));
    const lastName = v.substring(v.indexOf(' ') + 1);
    this.set({ firstName, lastName });
  });
const User = mongoose.model('User', userSchema);

const doc = new User();
// Vanilla JavaScript assignment triggers the setter
doc.fullName = 'Jean-Luc Picard';

doc.fullName; // 'Jean-Luc Picard'
doc.firstName; // 'Jean-Luc'
doc.lastName; // 'Picard'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM