mikro-orm icon indicating copy to clipboard operation
mikro-orm copied to clipboard

Support optimistic locking in mongodb

Open kaionesyan opened this issue 3 years ago • 4 comments

Describe the bug Inserting version: true inside the @Property decorator makes the field not get persisted in the database when running the MongoDB driver.

To Reproduce Steps to reproduce the behavior:

  1. Create an entity:
@Entity({ collection: 'users' })
export class User {
  @PrimaryKey({ name: '_id' })
  id: string;

  @Property({ unique: true, nullable: true })
  email?: string;

  @Property({ nullable: true })
  dateOfBirth?: Date;

  @Property({ unique: true, nullable: true })
  phoneNumber?: string;

  @Property({ version: true })
  version: number;

  constructor(data: CreateUserDto) {
    this.id = v4();
    this.email = data.email;
    this.dateOfBirth = data.dateOfBirth;
    this.phoneNumber = data.phoneNumber;
    this.version = 1;
  }
}
  1. Call either new User(data) or through em.getRepository(User).create({...data, version: 1})

  2. You'll see that the version field does not get persisted

Expected behavior Version field should go to the database in order for optimistic concurrency error handling to happen

Versions Running ^5.4.2" of both @mikro-orm/core and @mikro-orm/mongodb

kaionesyan avatar Oct 19 '22 05:10 kaionesyan

Versions are managed automatically, you can't set the value yourself, it is ignored when computing changesets, and moreover, this is SQL only feature, it's not implemented in mongo driver at all.

B4nan avatar Oct 19 '22 07:10 B4nan

Versions are managed automatically, you can't set the value yourself, it is ignored when computing changesets, and moreover, this is SQL only feature, it's not implemented in mongo driver at all.

Ah, I see. I saw that the reference documentation actually says this is SQL only. Maybe an entry could be added to the "Transactions and Concurrency" tab as well? The Starting v3.4, transactions are also supported in MongoDB driver. is kind of misleading :stuck_out_tongue:

kaionesyan avatar Oct 19 '22 11:10 kaionesyan

which part is misleading? transactions are supported in mongo, this is purely about locking. pessimistic locking is SQL feature (it literally maps to SQL keywords), but for optimistic locks I am sure we can find a way to support it in mongo rather easily

B4nan avatar Oct 19 '22 12:10 B4nan

There are places in the documentation that clearly specify that functionality X is not available on nosql drivers and such. Transactions and Concurrency page does not specify under "Optimistic Locking" that is does not support mongo. I was reading the version explanation and didn't notice it was not available for MongoDB and kept trying different things thinking I was doing something wrong (which I was, using that when I should not be using since I'm using MongoDB haha)

Maybe just a *currently only supported in SQL drivers below the Optimistic Locking text would be enough! What do you think?

kaionesyan avatar Oct 19 '22 17:10 kaionesyan