0

I try to make SQL requests from the server using Typeform and getManager().query(), passing to query string with SQL.

  1. Working example from MySqlWorkbench screenshot https://prnt.sc/uad3tu and code snippet. If I copy and paste it to the code, it works just fine
    SELECT `Event`.*, `Repeats`.`start_event_at`, `Repeats`.`end_event_at`
    FROM `events` `Event` 
    LEFT JOIN `event_repeats` `Repeats` on `Repeats`.`eventId` = `Event`.`id`
    WHERE `Event`.`category` = 'fitness' 
    AND `Repeats`.`end_event_at` >= '2020-09-02T14:59:00.000Z'
  1. But if I use variables in an interpolated string it throws an error, screenshot https://prnt.sc/uad5zy and
    SELECT `Event`.*, `Repeats`.`start_event_at`, `Repeats`.`end_event_at`
    FROM `events` `Event` 
    LEFT JOIN `event_repeats` `Repeats` on `Repeats`.`eventId` = `Event`.`id`
    WHERE `Event`.`category` = ${category} 
    AND `Repeats`.`end_event_at` >= '2020-09-02T14:59:00.000Z'

Thrown error: https://prnt.sc/uad6zs

What I do wrong and how to manage it?

3 Answers 3

2

When using query method use underlying driver escaping mechanism. For mysql:

await getEntityManager().query('SELECT * FROM tbl_1 WHERE category = ?', [ 'fitness' ])
Sign up to request clarification or add additional context in comments.

1 Comment

@АлександрНаумкин Thanks for you feedback, glad to help you
1

Adding to other responses, for future reference - each driver has different ways of doing that replace of variables

  • Oracle: query('SELECT * FROM table WHERE name = :name', [ { val: 'something' } ])
  • MySQL: query('SELECT * FROM table WHERE name = ?', [ 'something' ])
  • MSSQL: query('SELECT * FROM table WHERE name = @0', [ 'something' ])
  • Postgres: query('SELECT * FROM table WHERE name = $1', [ 'something' ])

Source: https://github.com/typeorm/typeorm/issues/881#issuecomment-1576624514

Comments

0

The final code looks like follows:

async getAllEvents(search_options: any, end_at, offFilters): Promise<Event[]> {

const { isOnline, isMobility, impairment, category } = search_options;

let events = null;
if (offFilters) {
  events = await getManager().query(
      ' SELECT Event.*, Repeats.start_event_at, Repeats.end_event_at ' +
            ' FROM events Event ' +
            ' LEFT JOIN event_repeats Repeats ON Repeats.eventId = Event.id ' +
            ' WHERE Event.category = ? ' +
            ' AND Repeats.end_event_at >= ?', [category, end_at])
} else {
  const online = isOnline ? 1 : 0
  const mobility = isMobility ? 1 : 0

  events = await getManager().query(
            ' SELECT Event.*, Repeats.start_event_at, Repeats.end_event_at ' +
            ' FROM events Event ' +
            ' LEFT JOIN event_repeats Repeats ON Repeats.eventId = Event.id ' +
            ' WHERE Event.category = ? ' +
            ' AND Event.isOnline = ? ' +
            ' AND Event.isMobility = ? ' +
            ' AND Event.impairment = ? ' +
            ' AND Repeats.end_event_at >= ?', [category, online, mobility, impairment, end_at])
}
return events
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.