dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

Stored procedure and custom sql statement support for MySql #839

Closed maneyputha closed 5 years ago

maneyputha commented 5 years ago

Does this this thing supports stored procedures and custom sql queries ? I have been trying to figure it out for while now. If it does not directly support, at least does this thing has some workaround to make it work ? Thanks.

maneyputha commented 5 years ago

Found it ! It's documented under the raw queries section.

db.driver.execQuery("SELECT id, email FROM user", function (err, data) { ... })

// You can escape identifiers and values.
// For identifier substitution use: ??
// For value substitution use: ?
db.driver.execQuery(
  "SELECT user.??, user.?? FROM user WHERE user.?? LIKE ? AND user.?? > ?",
  ['id', 'name', 'name', 'john', 'id', 55],
  function (err, data) { ... }
)

// Identifiers don't need to be scaped most of the time
db.driver.execQuery(
  "SELECT user.id, user.name FROM user WHERE user.name LIKE ? AND user.id > ?",
  ['john', 55],
  function (err, data) { ... }
)