Sequelize.fn : Constructing the update argument outside the call

Viewed 147

Issue Description

I'm currently trying to make updates on passwords using sequelize.fn. I've seen numerous sites, including the Sequelize's manual, yet I found too few articles that approaches my case, and none of them seems to solve it.

Moreover, the function to encrypt the passwords need nested function, so I need to gradually construct the nested function by putting the successive arguments into variables. I have not found anything on constructing the functions, thus using sequelize.fn outside the arguments of the functions find and updates.

Additional context

In my case, " crypt('password', gen_salt('bf', 4)) " (in Postgres) needs to become sequelize.fn('crypt', 'password', sequelize.fn('gen_salt', 'bf', 4)) and to be stored into a variable When I inject it into the update function, an Fn Object is injected, and the result in the database seems to be a memory address instead of the result of the crypt function.

I may post a code example so you can have a better understanding of what I'm trying to do and I think should be added into the documentation I did not made this doc issue to resolve my issue, only to point out what I think is lacking in the documentation I will ask StackOverflow for help regarding my issue.

//Take a format and a value, and applies the functions inside the format onto the value
function recursSequelizeFn (format, value) {
  const sequelize = db.sequelize;
  let nextId = 0;
  let matches;
  let finalArgs = {};

  //The RegEx finds the function calls of which parameters aren't functions themselves
  //As the format string is modified on each loop, I need to initialize the RegEx on each loop
  while ((matches = (/(\w+)\(([^(,)]+)?(?:,([^(,)]+))*\)/g).exec(format)) !== null) {
    //matches[0] is the whole expression
    //matches[1] is the function's name
    //matches[>=2] are the arguments
    let func = matches[1];
    let args = matches.slice(2);

    
    for (let argNo in args) {
      //Transforms DB's columns into Sequelize-interpreted columns
      //I do not check the column that is found, because I do not have others columns than the one that is being treated
      args[argNo] = args[argNo].replace(/seqCol_(\w+)/g, (correspondance, p1, decalage, chaine)=>{return value;});
      //If the parameter was previously saved, we replace its reference by its value
      args[argNo] = args[argNo].replace(/(@\d+)/g, (correspondance, p1, decalage, chaine)=>{return finalArgs[p1];});
    }
    //finally, we save the value of the function and replace the whole expression by a reference
    finalArgs['@'+nextId]=sequelize.fn(func, ...args);
    format = format.replace(matches[0], '@'+nextId);
    nextId++;
  }
  //When the treatment is finished, we return the last value saved into the array
  //Or we return the original value if we have not made any change (the array will be empty)
  return finalArgs['@'+(nextId-1)]||value;
}

Object.keys(req.message.attributes).map((key)=>{
  //req.message.attributes[key] contains the formatting needed to be put onto the value
  //e.g. crypt(password, gen_salt('bf', 4))
  //req.body[key] contains the value
  //e.g. '1234'
  let seqValue = util.recursSequelizeFn(req.message.attributes[key], req.body[key]);
  req.body[key] = seqValue;
});

//mainModel is a sequelize model
mainModel.update(req.body, {where: {id:req.params.id}, logging:console.log})
.then() //...
0 Answers
Related