In my NodeJS project, I have a line like this:
const { fieldA, fieldB, fieldC, fieldD, fieldE, fieldF, ...rest } = originalObject;
fieldB, fieldC, fieldD, fieldE & fieldF are never used again in code, but as we know, the way the ES-6 deconstruction works, the rest object will be a new object without fieldsA-F. Therefore, later somewhere there is a dB operation, in a way so that fieldsA-F aren't upserted.
const updatedItem = await model.findOneAndUpdate({ fieldA }, rest, { upsert: true });
However, I want to conditionally include/ exclude fieldF in rest. I know it can be done on the reading side, but how do it in the assignment side?
You would need to create a new object and pass it in the db assignment phase
const {param1, param2, param3} = require('./config.json')
const reqParam1 = true
const data = {}
if(reqParam1) data[key] = param1 // define key
updateDb(data)
or even better require everything and use the delete operator to remove the non required keys
const config = {"param1": "hi", "param2": "hello"} //require("./config.json")
const reqParam1 = false
if(!reqParam1) delete config.param1
console.log(config)