nodejs

typeorm repository 뿌수기

펀치맨 2022. 2. 15. 22:19

👓 IN array

import { In } from 'typeorm';

repository.find({where: { id : In(idArr)}});

https://github.com/typeorm/typeorm/issues/1239#issuecomment-367207538

👓 LIKE

repository.find({
    where : { name : Like(`%${name}%`)}
});

repository.find({
    where : { name : Like(`%${escapeLikeString(htmlentities(name))}%`)}
});

// 특수 문자 처리
export const htmlentities = (value: string) =>
  value.replace(/[\u00A0-\u9999<>\&]/g, function (i) {
    return '&#' + i.charCodeAt(0) + ';';
  });
  
// % _ 처리  
export const escapeLikeString = (raw: string, escapeChar = '\\') => {
  return raw.replace(/[\\%_]/g, (match) => escapeChar + match);
};

 

 

👓 > , >= , < , <=

repository.find({
    where : { date1 : MoreThan('2022-01-01')}
});

repository.find({
    where : { date1 : MoreThanOrEqual('2022-01-01')}
});

repository.find({
    where : { date1 : LessThan('2022-01-01')}
});

repository.find({
    where : { date1 : LessThanOrEqual('2022-01-01')}
});

 

👓 offset

repository.find({
    skip: 5,
})

👓 limit

repository.find({
    take: 10,
})

 

 

https://github.com/typeorm/typeorm/blob/master/docs/find-options.md

반응형