Repository navigation
Closed
Labels
Description
Issue Description
UpdateResult.affected is always returning undefined when used with SQLite In-memory database.
While unit-testing service code, it worked fine with MySQL and returned the exact number of affected rows after executing UPDATE query.
But my unit test codes use SQLite instead of MySQL, and when using SQLite, UpdateResult is always returning undefined, although the columns on the table are actually updated.
Expected Behavior
UpdateResult#affected should return exact number of affected rows after executing UPDATE query.
Actual Behavior
Always returns undefined.
Steps to Reproduce
On this case, there is users table, containg user_id as PK, name, email, and password columns. UserUpdateDto is made to partially have name and email field.
The service method is as below.
async updateUserInfo(userId: number, dto: UserUpdateDto): Promise<BasicMessageDto> {
const result = await this.userRepository
.createQueryBuilder()
.update('users', { ...dto })
.where('user_id = :userId', { userId })
.execute();
console.log(result.affected);
if (result.affected !== 0) {
return new BasicMessageDto('Updated Successfully.');
} else throw new NotFoundException();
}- As I mentioned above, the above method works perfectly fine when used with MySQL.
But it doesn't work when I use SQLite database. Below is my ormconfig for creating connection
to SQLite memory database.
type Entity = Function | string | EntitySchema<any>;
export async function createMemoryDB(entities: Entity[]) {
return createConnection({
type: 'sqlite',
database: ':memory:',
entities,
logging: false,
synchronize: true,
});
}My Environment
| Dependency | Version |
|---|---|
| Operating System | MacOS Big Sur 11.2 |
| Node.js version | v14.15.4 |
| Typescript version | v4.0.5 |
| TypeORM version | v0.2.31 |
Additional Context
N/A
Relevant Database Driver(s)
-
sqlite
Are you willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time, and I know how to start.
- Yes, I have the time, but I don't know how to start. I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
sang-w0o commentedon Feb 14, 2021
I just confirmed that
DeleteResultis also having the same issue asUpdateResultwhen used with SQLite in-memory database.[-]UpdatedResult#affected always returning undefined in SQLite[/-][+]UpdateResult#affected always returning undefined in SQLite[/+]coyoteecd commentedon Feb 18, 2021
@sangwoo-98 If you're looking for a place to fix this in a PR, the code processing the raw query result is here:
typeorm/src/query-builder/UpdateQueryBuilder.ts
Lines 105 to 119 in fde9f07
You can look at the "raw" property and see if the SQLLite driver returns the number of affected rows there, and add an elseif statement to have it copied to the updateResult.affected, as it is done with other drivers. I see the DeleteQueryBuilder does have something, see
typeorm/src/query-builder/DeleteQueryBuilder.ts
Lines 88 to 90 in fde9f07
[I am not a maintainer, just thought to provide guidance so you can submit a PR]
sang-w0o commentedon Feb 18, 2021
@coyoteecd Great! I'll try to take a look at it as soon as I have some extra time :)
sang-w0o commentedon Feb 18, 2021
Well I spent 4 hours into it, couldn't fix it :(
For the research I did about better-sqlite3, it seemed quite better than sqlite, and using better-sqlite3 instead of sqlite did fix this issue.
For people who are viewing this in the future, I think the only way is to use better-sqlite3. (Until someone fixes this issue..)