Enable optimistic locking in Models


#1

I’m starting an API Rest with the Adonis-CLI template for APIs

How can I add a Version field for all my models?

I would like to control the version of the registry in case some user modified the registry.

The same makes the Sequelize

http://docs.sequelizejs.com/manual/tutorial/models-definition.html#configuration


#2

Can you share how it is supposed to work?


#3

It is quite simple, but widely used in business applications. so there is no problem with changes in the same registry.

// Enable optimistic locking. When enabled, sequelize will add a version count attribute
// to the model and throw an OptimisticLockingError error when stale instances are saved.
// Set to true or a string with the attribute name you want to use to enable.

version: true


#4

same as adding the dates fields for the time that was inserted and the record was modified. We could also have a default method to add a _version int field for this record modification management.

And even have a method to add a insert date and user insert and date of update and user of update and _version of the record. All fields of control of a record.


#5

I suspect the most efficient way to achieve this would be to extend the base lucid model class, and override the existing save/update/etc methods including the necessary additional logic to handle writing of version data. You would probably also need some new methods to provide for management of versioning – ie: setting currently active version, diffing between versions, etc.

You would also need to change how keys work, so that multiple items can have the same id with different version ids.


#6

yes! and how to do it? Would not it be interesting to have it in the ORM by default?