Typegoose

Typegoose -- 使用 TypeScript 类定义 Mongoose 模型。「Typegoose - Define Mongoose models using TypeScript classes.」

Github星跟蹤圖

This Repository got moved

Please use hasezoey's fork to be up-to-date
Please dont create new issues & pull request anymore, thanks

Typegoose

Build Status
Coverage Status
npm

Define Mongoose models using TypeScript classes.

Basic usage

import { prop, Typegoose, ModelType, InstanceType } from 'typegoose';
import * as mongoose from 'mongoose';

mongoose.connect('mongodb://localhost:27017/test');

class User extends Typegoose {
  @prop()
  name?: string;
}

const UserModel = new User().getModelForClass(User);

// UserModel is a regular Mongoose Model with correct types
(async () => {
  const u = await UserModel.create({ name: 'JohnDoe' });
  const user = await UserModel.findOne();

  // prints { _id: 59218f686409d670a97e53e0, name: 'JohnDoe', __v: 0 }
  console.log(user);
})();

Motivation

A common problem when using Mongoose with TypeScript is that you have to define both the Mongoose model and the TypeScript interface. If the model changes, you also have to keep the TypeScript interface file in sync or the TypeScript interface would not represent the real data structure of the model.

Typegoose aims to solve this problem by defining only a TypeScript interface (class) which need to be enhanced with special Typegoose decorators.

Under the hood it uses the reflect-metadata API to retrieve the types of the properties, so redundancy can be significantly reduced.

Instead of:

interface Car {
  model?: string;
}

interface Job {
  title?: string;
  position?: string;
}

interface User {
  name?: string;
  age: number;
  job?: Job;
  car: Car

主要指標

概覽
名稱與所有者szokodiakos/typegoose
主編程語言TypeScript
編程語言TypeScript (語言數: 1)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2017-03-17 21:50:35
推送於2021-05-10 22:53:43
最后一次提交2019-09-06 11:07:59
發布數30
最新版本名稱v5.9.0 (發布於 )
第一版名稱v2.2.0 (發布於 2017-07-14 17:56:43)
用户参与
星數1.2k
關注者數23
派生數133
提交數285
已啟用問題?
問題數284
打開的問題數8
拉請求數79
打開的拉請求數10
關閉的拉請求數51
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?