OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to replace findOneById in TypeORM now it is deprecated

  • Thread starter Thread starter Nathan Dixon
  • Start date Start date
N

Nathan Dixon

Guest
I am writing generic functions for my database calls so don't know the ID field name. This was fine when we could use findOneById.

Is there a way now to do an equivalent findOneById where we don't know the primary key field name(s)?

This is basically how my code works now. But it will stop working when they remove the findOneById function and I've got to find another way to do it.

e.g.

Code:
async function softDeleteItem(
    itemId: number,
    entityClass: ObjectType<Entity> | EntitySchema<Entity> | string,
    deletedBy: string
): Promise<boolean> {
    const repo = db.getRepository(entityClass);

    const itemToDelete = await repo.findOneById(itemId);

    (itemToDelete as ISoftDeletable).deletedBy = deletedBy;

    await repo.softDelete(resourceEntityToDelete);
}

---- Update ---- The only way I have managed to achieve what I want is with queryBuilder and pass in the name of the ID property (this could be multiple ID properties, in which case loop through them and use andWhere() for each one):

Code:
async function softDeleteItem(
    itemId: number,
    itemIdKey: string,
    ...
): Promise<boolean> {
    ...

    const itemToDelete = await repo
        .createQueryBuilder('entity')
        .where(`entity.${itemIdKey as string} = :itemId`, { itemId })
        .getOneOrFail();

    ...
}

But I'm still wondering if there's a way to do it with just an ID value, as using findOneById was more readable and concise?

<p>I am writing generic functions for my database calls so don't know the ID field name. This was fine when we could use findOneById.</p>
<p>Is there a way now to do an equivalent findOneById where we don't know the primary key field name(s)?</p>
<p>This is basically how my code works now. But it will stop working when they remove the findOneById function and I've got to find another way to do it.</p>
<p>e.g.</p>
<pre><code>async function softDeleteItem(
itemId: number,
entityClass: ObjectType<Entity> | EntitySchema<Entity> | string,
deletedBy: string
): Promise<boolean> {
const repo = db.getRepository(entityClass);

const itemToDelete = await repo.findOneById(itemId);

(itemToDelete as ISoftDeletable).deletedBy = deletedBy;

await repo.softDelete(resourceEntityToDelete);
}
</code></pre>
<p>---- Update ----
The only way I have managed to achieve what I want is with queryBuilder and pass in the name of the ID property (this could be multiple ID properties, in which case loop through them and use andWhere() for each one):</p>
<pre><code>async function softDeleteItem(
itemId: number,
itemIdKey: string,
...
): Promise<boolean> {
...

const itemToDelete = await repo
.createQueryBuilder('entity')
.where(`entity.${itemIdKey as string} = :itemId`, { itemId })
.getOneOrFail();

...
}
</code></pre>
<p>But I'm still wondering if there's a way to do it with just an ID value, as using findOneById was more readable and concise?</p>
 

Latest posts

Top