Kgn-web
Kgn-web

Reputation: 7555

How to implement object mapper in node.js

I have MongoDb as my db . data in this instance is populated on a daily basis through a csv file import.

a document in this collection/csv looks as

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

This data is fetched via node.js api as below

    //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;

Being a newbie to node.js on googling I get a number of packages

now I am not sure which is the most common/popular/recommended package & how do I use it

How can I map my DB object to the app object?

Thanks!

Upvotes: 0

Views: 1383

Answers (1)

NeNaD
NeNaD

Reputation: 20304

Since you are using MongoDB and Mongoose, you don't have to do anything. Mongoose will automatically return objects or array of objects for each query.

Upvotes: 1

Related Questions