Skip to content

UpdateResult#affected always returning undefined in SQLite #7374

Closed

Description

@sang-w0o

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

  1. sang-w0o commented on Feb 14, 2021

    @sang-w0o
    Author

    I just confirmed that DeleteResult is also having the same issue as UpdateResult when used with SQLite in-memory database.

  2. changed the title [-]UpdatedResult#affected always returning undefined in SQLite[/-] [+]UpdateResult#affected always returning undefined in SQLite[/+] on Feb 14, 2021
  3. coyoteecd commented on Feb 18, 2021

    @coyoteecd
    Contributor

    @sangwoo-98 If you're looking for a place to fix this in a PR, the code processing the raw query result is here:

    if (this.connection.driver instanceof PostgresDriver) {
    updateResult.raw = result[0];
    updateResult.affected = result[1];
    }
    else if (this.connection.driver instanceof MysqlDriver) {
    updateResult.raw = result;
    updateResult.affected = result.affectedRows;
    }
    else if (this.connection.driver instanceof BetterSqlite3Driver) { // only works for better-sqlite3
    updateResult.raw = result;
    updateResult.affected = result.changes;
    }
    else {
    updateResult.raw = result;
    }

    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

    } else if (driver instanceof BetterSqlite3Driver) { // only works for better-sqlite3
    deleteResult.raw = result;
    deleteResult.affected = result.changes;
    .

    [I am not a maintainer, just thought to provide guidance so you can submit a PR]

  4. sang-w0o commented on Feb 18, 2021

    @sang-w0o
    Author

    @coyoteecd Great! I'll try to take a look at it as soon as I have some extra time :)

  5. sang-w0o commented on Feb 18, 2021

    @sang-w0o
    Author

    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..)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions