Skip to content

Instantly share code, notes, and snippets.

@proclaim
Last active June 7, 2021 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proclaim/63c2dedf4d1cf40dc4f5460a7d74c2d2 to your computer and use it in GitHub Desktop.
Save proclaim/63c2dedf4d1cf40dc4f5460a7d74c2d2 to your computer and use it in GitHub Desktop.
typeorm delete multiple records
// assume entityIds is an array
connection.createQueryBuilder()
.delete()
.from(entity)
.where('entity.id IN (:...ids)', { ids: entityIds })
.execute()
@noomerzx
Copy link

This "...ids" is not "SQL" right ? it's seem like JS expression, So it's allowed to put expression there ?

@jameshmread
Copy link

This way works.

This "...ids" is not "SQL" right ? it's seem like JS expression, So it's allowed to put expression there ?
I agree, not sure you can run the spread operator in that string since it will get translated to sql

getConnection()
      .createQueryBuilder()
      .delete()
      .from(entity)
      .where('id In(:id)', {
        id: ['1','2'],
      })
      .execute();

@proclaim
Copy link
Author

proclaim commented Jun 7, 2021

This way works.

This "...ids" is not "SQL" right ? it's seem like JS expression, So it's allowed to put expression there ?
I agree, not sure you can run the spread operator in that string since it will get translated to sql

getConnection()
      .createQueryBuilder()
      .delete()
      .from(entity)
      .where('id In(:id)', {
        id: ['1','2'],
      })
      .execute();

that'll do! the code that I'm using will be compiled so it worked on my end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment