Search code examples
sql-servertypeormnode.js-typeorm

How to convert query ' WHERE IN' of strings with TypeORM Query Builder?


Getting errors in following TypeORM query:

async exportUsers(stateId: string, zipcodes: string) 
{
  //zipcodes = '60563', '54656', '94087';//
  const query = await this.userRepository
  .createQueryBuilder('user')
  .select('user.email', 'email')
  .where('left(user.Zip,5) in :zip', { zip: zipcodes }); 
}

How to pass string containing 'array of strings' to TypeORM query using IN.


Solution

  • This worked:

    zipcodes = '60563', '54656', '94087';
    const ziplist: string[] = zipcodes.replace("'", "").split(",");
    

    Changed query to:

    .where('left(s.ShipToZip,5) in (:...zip)', { zip: ziplist });