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?
已存档?
是复刻?
已锁定?
是镜像?
是私有?