简体   繁体   English

在 Mongoose Schema 中使用 Snowflake 作为类型

[英]Using Snowflake as Type inside Mongoose Schema

Background : Discord.js identifies many items using Snowflakes (messages, members, guilds, etc.).背景:Discord.js 使用雪花识别许多项目(消息、成员、公会等)。 In my scenario, I am using snowflakes as unique identifiers for members inside the guild and as the primary key inside my MongoDB database.在我的场景中,我使用雪花作为公会内部成员的唯一标识符和我的 MongoDB 数据库中的主键。

Problem : I am wanting to use Discord's Snowflake as a type inside my Mongoose schema:问题:我想在 Mongoose 模式中使用 Discord 的 Snowflake 作为类型:

import { Snowflake } from 'discord.js';
import { Schema } from 'mongoose';

const MembershipSchema: Schema = new Schema({
    discordID: { type: Snowflake, required: true, unique: true },
});

However, I get an error from VSCode's Intellisense telling me that 'Snowflake' only refers to a type, but is being used as a value here.但是,我从 VSCode 的 Intellisense 收到一个错误,告诉我'Snowflake' only refers to a type, but is being used as a value here. I am confused because my interpretation from the Discord.js documentation was that Snowflake was a type (essentially just a string ).我很困惑,因为我对 Discord.js 文档的解释是Snowflake是一种类型(本质上只是一个string )。

I can definitely simply use string instead but I am wondering if there is any way to make it work similar to how I have shown above.我绝对可以简单地使用string代替,但我想知道是否有任何方法可以使它的工作方式类似于我上面显示的方式。 I want it to be really explicit that the value going inside the discordID field must be a Snowflake , not any old string.我希望它非常明确, discordID字段中的值必须是Snowflake ,而不是任何旧字符串。

I found the most appropriate method for enforcing the types partially includes the answers provided by @MrMythical and @Toasty.我发现强制执行类型的最合适方法部分包括@MrMythical 和@Toasty 提供的答案。 However, their answers are incomplete and should not be followed completely to achieve my goal here.但是,他们的答案是不完整的,不应完全遵循以实现我在这里的目标。

Essentially, the solution I found after some more hours of research was to simply add a typing interface to the model when it is created.从本质上讲,经过几个小时的研究,我找到的解决方案是在创建模型时简单地向模型添加一个输入界面。 At least in VSCode, Intellisense will recognize this and will enforce the types on the defined schema parameters based on what was in the interface.至少在 VSCode 中,Intellisense 会认识到这一点,并将根据接口中的内容强制定义模式参数的类型。

An example is shown below:一个例子如下所示:

MembershipModel.ts会员模型.ts

import { model, Schema } from 'mongoose';
import Membership from '../interfaces/MembershipStorage';

const MembershipSchema: Schema = new Schema(
    {
        discordID: { type: String, required: true, unique: true },
        ieeeID: { type: String, required: true, unique: true },
    },
    {
        collection: 'members',
        minimize: false,
        strictQuery: true,
    }
);

export default model<Membership>('Member', MembershipSchema);

MembershipStorage.ts MembershipStorage.ts

import { Snowflake } from 'discord.js';

export default interface Membership {
    discordID: Snowflake;
    ieeeID: string;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM