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

API for pluggable create/update ReQL #227

Open
robertjpayne opened this issue Aug 30, 2016 · 4 comments
Open

API for pluggable create/update ReQL #227

robertjpayne opened this issue Aug 30, 2016 · 4 comments
Labels
feature New functionalities

Comments

@robertjpayne
Copy link

I have quite a unique use case but I think it may be nice to have official support for rather than monkey patching.

Our application requires sync capabilities and to handle conflicts during writes I've devised a plan to atomically check a field while updating it like (sorry if syntax is not 100% correct):

update do |row|
  r.branch(
    row["timestamp"] < updateAttrs["timestamp"],
    updateAttrs,
    r.error("Invalid Update, timestamp is older than db")
  )
end

This would be pretty easy to if there was pluggable access to how the RQL query is generated for inserts/updates on a per-model-class basis.

@nviennot
Copy link
Collaborator

This is a good idea. I like it.

Any suggestions for the API?

@robertjpayne
Copy link
Author

@nviennot I don't think it needs to be particularly crazy, just looking at code there is:

  def _create(options={})
    attrs = self.class.persistable_attributes(@_attributes)
    result = NoBrainer.run(self.class.rql_table.insert(attrs))
    self.pk_value ||= result['generated_keys'].to_a.first
    @new_record = false
    unlock_unique_fields # just an optimization for the uniquness validation
    true
  end

  def _update(attrs)
    rql = ->(doc){ self.class.persistable_attributes(attrs, :rql_doc => doc) }
    NoBrainer.run { selector.update(&rql) }
  end

Maybe all we need is a class method that is generate_insert_rql(attrs) and generate_update_rql(selector, attrs) which both return a block that is passed directly into NoBrainer.run?

Thoughts? Might not be the cleanest and I'm not 100% certain on functionality of the self.class.persistable_attributes.

@robertjpayne
Copy link
Author

Closing this as it's just a bit too hefty to do so…

@nviennot
Copy link
Collaborator

Reopening because this usecase is common, and this needs to be addressed in some ways.

@zedtux zedtux added the feature New functionalities label Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionalities
Projects
None yet
Development

No branches or pull requests

3 participants