Automtically add the partition key and a value to the partition key to a JSON file when Posting it into Postman

Viewed 94

So I have many records (around 1000) with me that I want to insert into the cosmos DB. The partition key is not attached to them. Is there any way I can attach the partition key using the .forEach(...) method? in this case the partition key name is firstname.

Can we add the partition key and value to every record while posting it in Postman?

Is there anything I can code using .forEach to the attach the firstname to every record before it is stored into the cosmos db?

How would I modify the insertCustomer code to achieve the results?

Expected output:

{
    "shipment": [
        {
            "firstname": "abc",
            "id": "12345",
            "age": 23
        },
        {  
            "firstname": "bca",
            "id": "321",
            "age": 22
        }
    ]
}

Currently the data I have is like this:

{
    "shipment": [
        {
            "id": "12345",
            "age" :23
        },
        {  
            "id" : "321",
            "age" : 22
        }
    ]
}

app.js

var Connection = require('tedious').Connection;
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');

const employee = require('./router');
var app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(employee);


//CORS Middleware
app.use(function (req, res, next) {
    //Enabling CORS
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
    next();
});


var ser = app.listen(3000, function () {
    console.log('Server is running on port 3000..');
});

controller.js

const config = require("./config");
const CosmosClient = require("@azure/cosmos").CosmosClient;

const { endpoint, key, databaseId, containerId } = config;

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const client = new CosmosClient({ endpoint, key });

const database = client.database(databaseId);
const container = database.container(containerId);

class MainController {
    async fetchCustomers (req,res) {
        // <CreateClientObjectDatabaseContainer>
        const {endpoint, key, databaseId, containerId} = config;
        const client = new CosmosClient({endpoint, key});
        const database = client.database(databaseId);
        const container = database.container(containerId);


        try {
            // <QueryItems>
            console.log(`Querying container: Items`);
            const queryIterator = container.items.query(
            // query to return all items
                {
                    query: ' SELECT * from c '
                },

            { enableScanInQuery: true }
        );
            const { resources: items, requestCharge } = await queryIterator.fetchNext();
            const itemDef = items[0];
            console.log("Item '" + itemDef.id + "' found, request charge: " + requestCharge);
            res.json({resources: items, requestCharge});
        } catch (err) {
            console.log(err.message);
        }
    }
    async insertCustomer(req , res){
        var params  = req.body;
        const { resource: createdItem } = await container.items.create(params);

        console.log(`\r\nCreated new item: ${createdItem.id} - ${createdItem.pk}\r\n`);
        res.json({ resource: createdItem })

    }
}

const controller = new MainController();
module.exports = controller;

router.js

const express =  require('express');
const controller = require("./controller");
const router = express.Router();

router.get('/api/fetchCustomers', controller.fetchCustomers);
router.post('/api/insertCustomer' , controller.insertCustomer);

module.exports = router;
0 Answers
Related