TypeScript 支援

Mongoose 在 v5.11.0 中引入了官方支援的 TypeScript 綁定。Mongoose 的 index.d.ts 檔案支援多種語法,並盡可能與 @types/mongoose 相容。本指南將描述 Mongoose 建議的在 TypeScript 中使用 Mongoose 的方法。

建立您的第一個文檔

若要在 TypeScript 中開始使用 Mongoose,您需要:

  1. 建立一個介面來表示 MongoDB 中的文檔。
  2. 建立一個與文檔介面相對應的Schema(結構描述)
  3. 建立一個 Model(模型)。
  4. 連線到 MongoDB.
import { Schema, model, connect } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface IUser {
  name: string;
  email: string;
  avatar?: string;
}

// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: String
});

// 3. Create a Model.
const User = model<IUser>('User', userSchema);

run().catch(err => console.log(err));

async function run() {
  // 4. Connect to MongoDB
  await connect('mongodb://127.0.0.1:27017/test');

  const user = new User({
    name: 'Bill',
    email: 'bill@initech.com',
    avatar: 'https://i.imgur.com/dM7Thhn.png'
  });
  await user.save();

  console.log(user.email); // 'bill@initech.com'
}

您身為開發人員,有責任確保您的文檔介面與您的 Mongoose 結構描述一致。例如,如果 email 在您的 Mongoose 結構描述中是 required,但在您的文檔介面中是選填的,Mongoose 不會回報錯誤。

User() 建構函式會傳回 HydratedDocument<IUser> 的實例。 IUser 是一個文檔介面,它表示 IUser 物件在 MongoDB 中的原始物件結構。 HydratedDocument<IUser> 表示一個已填充的 Mongoose 文檔,具有方法、虛擬屬性和其他 Mongoose 特有的功能。

import { HydratedDocument } from 'mongoose';

const user: HydratedDocument<IUser> = new User({
  name: 'Bill',
  email: 'bill@initech.com',
  avatar: 'https://i.imgur.com/dM7Thhn.png'
});

ObjectIds 和其他 Mongoose 類型

若要定義類型為 ObjectId 的屬性,您應該在 TypeScript 文檔介面中使用 Types.ObjectId。您應該在您的結構描述定義中使用 'ObjectId'Schema.Types.ObjectId

import { Schema, Types } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface IUser {
  name: string;
  email: string;
  // Use `Types.ObjectId` in document interface...
  organization: Types.ObjectId;
}

// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true },
  // And `Schema.Types.ObjectId` in the schema definition.
  organization: { type: Schema.Types.ObjectId, ref: 'Organization' }
});

那是因為 Schema.Types.ObjectId 是一個繼承自 SchemaType 的類別並非您用來建立新的 MongoDB ObjectId 的類別。

使用自訂綁定

如果 Mongoose 的內建 index.d.ts 檔案不適用於您,您可以在 package.json 中的 postinstall 腳本中移除它,如下所示。但是,在您這麼做之前,請在 Mongoose 的 GitHub 頁面上開啟一個 issue 並描述您遇到的問題。

{
  "postinstall": "rm ./node_modules/mongoose/index.d.ts"
}

下一步

現在您已經了解如何在 TypeScript 中使用 Mongoose 的基礎知識,讓我們來看看 TypeScript 中的靜態方法