【问题标题】:How to implement object mapper in node.js如何在 node.js 中实现对象映射器
【发布时间】:2021-10-23 18:17:27
【问题描述】:

我有 MongoDb 作为我的数据库。此实例中的数据每天通过 csv 文件导入填充。

此集合/csv 中的文档看起来像

    {
    "_id": {
        "$oid": "611e455ddd7c222661065037"
    },
     "country": "India",
     "country_code": 91,
     "country_iso": "IND",
     "origination_prefix": "",
     "voice_rate_in_usd": 0.3148,
     "voice_unit_in_seconds": 60
    }

此数据是通过node.js api 获取的,如下所示

    //some code emitted for brevity
    import { connect, Schema, model } from "mongoose";
    import express from "express";

    const router = express.Router();

    const schema = new Schema<Pricing>({
    country: { type: String, required: true },
    countryCode: { type: String, required: false },
    countryIso: { type: String, required: true },
    destinationPrefix: { type: [Number] },
    voiceRate: { type: Number },
    voiceUnit: { type: Number },
    });

    const pricesModel = model<Pricing>("call_rate", schema);

    router.get("", async (req, res) => {
     const prices = await pricesModel.find().limit(10);
     res.send(prices);
    });

    export default router;

作为node.js 在谷歌上搜索的新手,我得到了很多包

现在我不确定哪个是最常见/流行/推荐的软件包以及如何使用它

如何将我的 DB 对象映射到应用程序对象?

谢谢!

【问题讨论】:

    标签: javascript node.js typescript mongodb mongoose


    【解决方案1】:

    由于您使用的是MongoDBMongoose,因此您无需执行任何操作。 Mongoose 将自动为每个查询返回对象或对象数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多