hiddentao / squel

:office: SQL query string builder for Javascript
https://hiddentao.github.io/squel
MIT License
1.56k stars 231 forks source link

Use a normal SQL string #376

Open basickarl opened 5 years ago

basickarl commented 5 years ago

I have functions which require a squel object. I have however written the SQL string myself without using this library.

Is there a way to insert the raw SQL string statement into a squel object?

thomashamlin commented 5 years ago

There isn't; squel is simply an SQL string builder.

To execute raw SQL, send it directly to your DB connection. E.g. if you're using https://github.com/mysqljs/mysql, something like this works:

const mysql = require('mysql')
const connection = mysql.createConnection({ ... }).connect()
connection.query('SELECT * FROM tbl', (error, results, fields) => {
  ...
})

If your function expect squel objects, you could try to reverse engineer the method(s) they end up calling and pass an object that implements those. Perhaps simply implementing toString() (returns your SQL string) would do the trick?

victornpb commented 5 years ago

If it doesn't further compose your query you can fake it by doing something like this:

const fakeSquel = { toString(){ return 'SELECT * FROM table'; }};