如何在 TypeScript 中使用 mongoose-sequence 项目? [英] How do I use the mongoose-sequence project in TypeScript?

查看:74
本文介绍了如何在 TypeScript 中使用 mongoose-sequence 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 猫鼬序列项目.(这个项目似乎是最流行和最受支持的项目.)

I am trying to build a REST API server with an auto-incrementing field with Mongoose, Express, and TypeScript using the mongoose-sequence project. (This project seems like the most popular and supported of the ones out there.)

我已经运行 npm install --save @types\mongoose-sequence 来导入 TypeScript 的类型.

I have run npm install --save @types\mongoose-sequence to import the types for TypeScript.

但是,我似乎无法弄清楚如何正确使用它.

However, I can't seem to figure out how to correctly make use of it.

README 中的代码说要执行以下操作:

The code in the README says to do the following:

const AutoIncrement = require('mongoose-sequence')(mongoose);

这如何转化为 TypeScript?

How does that translate into TypeScript?

有人可以解释一下让我滚动的基础知识吗?

Can someone explain the basics to get me rolling?

推荐答案

这段代码在类型级别和值级别都对我有用.

This code works for me both on a type-level and a value level.

import Inc from "mongoose-sequence";
import { userSchema } from "../SOME-MONGOOSE-SCHEMA.ts";
const AutoIncrement = Inc(userSchema);

请注意,这与文档相矛盾,文档说您应该向它传递一个 mongoose 实例,但类型说它需要一个 mongoose 模式.

Note that this contradicts the documentation, which says you should pass it a mongoose instance but the typings say it takes a mongoose schema.

这篇关于如何在 TypeScript 中使用 mongoose-sequence 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆