Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Allow UPDATE by expression #141

Open
freshp86 opened this issue Feb 17, 2016 · 0 comments
Open

Allow UPDATE by expression #141

freshp86 opened this issue Feb 17, 2016 · 0 comments

Comments

@freshp86
Copy link
Contributor

Currently UPDATE queries can only accept literal values. For example

db.update(employee)
  .set(employee.salary, 2000)
  .where(employee.hireDate.lt(...));

There is no way to calculate the new value based on existing values. Proposing
the following syntax to allow update-by-expression.

/**
 * typedef {{
 *   vars: !Array<!lf.schema.BaseColumn>
 *   fn: !Function
 * }}
 */
lf.query.UpdateExpression;
  • Example1: updating some employees salary by 10%.
var updateExpression = {
  vars: [employee.salary],
  fn: function(x) { return x * 1.1; }
};
db.update(employee).
  set(employee.salary, updateExpression).
  where(employee.hireDate.lt(....));
  • Example2: Combining values from multiple colmuns. Converting the bonus to salary.
var updateExpression = {
  vars: [employee.salary, employee.bonus],
  fn: function(a, b) { return a + b; }
};
db.update(employee).
  set(employee.salary, updateExpression).
  set(employee.bonus, 0).
  where(employee.hireDate.lt(....));
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant