使用定义类型的类型在打字稿中编写Mongoose的类型化模型和模式的类和接口

时间:2021-10-04 20:12:22

how can I use classes and interfaces to write typed Models and schemas in Typescript using definitelytyped .

如何使用类和接口使用定义类型在打字稿中编写类型化模型和模式。

import mongoose = require("mongoose");

 //how can I use a class for the schema and model so I can new up
export interface IUser extends mongoose.Document {
name: String;
}

export class UserSchema{
name: String;
}




var userSchema = new mongoose.Schema({
name: String
});
export var User = mongoose.model<IUser>('user', userSchema);

1 个解决方案

#1


18  

This is how I do this:

我就是这样做的:

  1. Define TypeScript class which will define our logic.
  2. 定义TypeScript类,它将定义我们的逻辑。
  3. Define the interface (which I name Document): that's the type mongoose will interact with
  4. 定义接口(我命名为Document):这是mongoose将与之交互的类型
  5. Define the model (we'll be able to find, insert, update...)
  6. 定义模型(我们将能够查找、插入、更新…)

In code:

在代码:

import { Document, Schema, model } from 'mongoose'

// 1) CLASS
export class User {
  name: string
  mail: string

  constructor(data: {
    mail: string
    pass: string
  }) {
    this.mail = data.mail
    this.name = data.name
  }

  /* any method would be defined here*/
  foo(): string {
     return this.name.uppercase() // whatever
  }
}

// no necessary to export the schema (keep it private to the module)
var schema = new Schema({
  mail: { required: true, type: String },
  name: { required: false, type: String }
})
// register each method at schema
schema.method('foo', User.prototype.foo)

// 2) Document
export interface UserDocument extends User, Document { }

// 3) MODEL
export const Users = model<UserDocument>('User', schema)

How would I use this? let's imagine that code is stored in user.ts, now you'd be able to do the following:

我怎么用这个呢?让我们假设代码存储在用户中。ts,现在你可以做到以下几点:

import { User, UserDocument, Users } from 'user'

let myUser = new User({ name: 'a', mail: 'aaa@aaa.com' })
Users.create(myUser, (err: any, doc: UserDocument) => {
   if (err) { ... }
   console.log(doc._id) // id at DB
   console.log(doc.name) // a
   doc.foo() // works :)
})

#1


18  

This is how I do this:

我就是这样做的:

  1. Define TypeScript class which will define our logic.
  2. 定义TypeScript类,它将定义我们的逻辑。
  3. Define the interface (which I name Document): that's the type mongoose will interact with
  4. 定义接口(我命名为Document):这是mongoose将与之交互的类型
  5. Define the model (we'll be able to find, insert, update...)
  6. 定义模型(我们将能够查找、插入、更新…)

In code:

在代码:

import { Document, Schema, model } from 'mongoose'

// 1) CLASS
export class User {
  name: string
  mail: string

  constructor(data: {
    mail: string
    pass: string
  }) {
    this.mail = data.mail
    this.name = data.name
  }

  /* any method would be defined here*/
  foo(): string {
     return this.name.uppercase() // whatever
  }
}

// no necessary to export the schema (keep it private to the module)
var schema = new Schema({
  mail: { required: true, type: String },
  name: { required: false, type: String }
})
// register each method at schema
schema.method('foo', User.prototype.foo)

// 2) Document
export interface UserDocument extends User, Document { }

// 3) MODEL
export const Users = model<UserDocument>('User', schema)

How would I use this? let's imagine that code is stored in user.ts, now you'd be able to do the following:

我怎么用这个呢?让我们假设代码存储在用户中。ts,现在你可以做到以下几点:

import { User, UserDocument, Users } from 'user'

let myUser = new User({ name: 'a', mail: 'aaa@aaa.com' })
Users.create(myUser, (err: any, doc: UserDocument) => {
   if (err) { ... }
   console.log(doc._id) // id at DB
   console.log(doc.name) // a
   doc.foo() // works :)
})