How to select record where column is hashed in TypeOrm

92 Views Asked by At

My database has a table like this :

id rt
1 value1 (hashed by argon)
2 value2 (hashed by argon)

Which I want a record that match some value with hashed rt. somting like this :

SELECT * FROM TABLE WHERE (argon.verify(rt,"some value"))

How can I get something like this in TypeORM ?

2

There are 2 best solutions below

0
Majva On BEST ANSWER

It seems that there is no solution at the database level. The problem can be solved as follows :

  async findOneByUserAndRefreshToken(
  userId: number,
  refreshToken: string,
  ): Promise<Device | undefined> {
  const devices = await this.repo.find({
  where: {
    rtExpiresIn: MoreThan(new Date()),
    user: { id: userId },
  },
  });
  const device = devices.find((device) =>
  argon.verify(device.refreshToken, refreshToken),
  );
  return device;
  } 
1
Hai Alison On

you can use the query function to execute raw SQL query like this:

if you are using Typeorm > 0.3.0

constructor(
    @InjectDataSource()
    private dataSource: DataSource,
  ) {}
this.dataSource.query(`SELECT * FROM TABLE WHERE (argon.verify(rt,"some value"))`)

if you are using Entity extend BaseEntity, you can use it like this:

YourEntity.query(`SELECT * FROM TABLE WHERE (argon.verify(rt,"some value"))`)