猫鼬和Angular之间的共享架构

时间:2020-06-13 20:36:31

标签: node.js angular mongodb express mongoose

我目前正在编写一个使用Node / Express / Mongoose后端API的Angular应用。

如果我在这里问一个愚蠢的问题,我只能道歉,但是我似乎有很多代码“重复”。

例如,在我的后端,我有一个猫鼬模型,如下所示:

const mongoose = require("mongoose");

const aicraftSchema = mongoose.Schema({
    creator: {
        type: mongoose.Types.ObjectId,
        required: true,
    },
    isSimulator: {
        type: Boolean,
        default: false,
    },
    registration: {
        type: String,
        required: true,
        index: true,
    },
    manufacturer: {
        type: String,
        index: true,
        required: true,
    },
    type: {
        type: String,
        index: true,
        required: true,
    },
   //Removed for Brevity
}

但是后来我不得不在Angular应用中手动创建一个接口,如下所示:

export interface Aircraft {
  _id: string;
  creator?: string;
  isSimulator: boolean;
  registration: string;
  manufacturer: string;
  type: string;
  //Removed for brevity
}

在/ api / aircraft上执行GET会返回与Angular接口匹配的对象数组。

这是最佳做法吗?还有另一种解决方法吗?我看着用TypeScript编写后端,但这似乎获得的支持很少。

我问的原因是,我今早大部分时间都在调试由Mongoose模型和Angular接口之间的微小错字引起的问题。我知道这是否只是我需要注意的事情,但我想知道是否有更好的方法。

欢呼

0 个答案:

没有答案
相关问题