Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support optimistic locking in mongodb #3612

Open
kaionesyan opened this issue Oct 19, 2022 · 4 comments
Open

Support optimistic locking in mongodb #3612

kaionesyan opened this issue Oct 19, 2022 · 4 comments
Labels
enhancement New feature or request mongodb

Comments

@kaionesyan
Copy link
Contributor

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

@B4nan
Copy link
Member

B4nan commented Oct 19, 2022

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 B4nan changed the title Version option inside @Property decorator makes the field not get persisted in the database (MongoDB) Support optimistic locking in mongodb Oct 19, 2022
@B4nan B4nan added enhancement New feature or request mongodb labels Oct 19, 2022
@kaionesyan
Copy link
Contributor Author

kaionesyan commented Oct 19, 2022

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 😛

@B4nan
Copy link
Member

B4nan commented Oct 19, 2022

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

@kaionesyan
Copy link
Contributor Author

kaionesyan commented Oct 19, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mongodb
Projects
None yet
Development

No branches or pull requests

2 participants