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

Request for a method to update entity after save #3

Open
NejcHorvat opened this issue Apr 8, 2016 · 8 comments
Open

Request for a method to update entity after save #3

NejcHorvat opened this issue Apr 8, 2016 · 8 comments
Assignees

Comments

@NejcHorvat
Copy link

Would it be possible to introduce a mechanism for updating the entity with a result from the store that saved it?

Three use cases:

  • You save an item, the database has defaults set on certain fields. Your entity does not reflect what is currently saved in the database.
  • You don't want to use the UUID way of generating ids in a store and would rather use the autoincrement functionality of your database. The database returns the id of a newly saved item which would have to propagate back to the entity
  • You have triggers set on a database that mutates the saved entity. You wish to propagate those changed back.

Postgres example would be just adding RETURN * at the end of a save / update. The returned item could be appended to the result and applied on the entity.

What would be the preferred approach for this? I am willing to create a pull for this but would like to hear your opinion on this and maybe get a few pointers or thoughts.

@mcdonnelldean
Copy link
Contributor

@NejcHorvat could you explain more? When you save is the new object to returned in the callback for save?

@NejcHorvat
Copy link
Author

@mcdonnelldean yes. When you save the store would return the saved object so that the entity could be updated with any mutated values.

@mcdonnelldean
Copy link
Contributor

@NejcHorvat I'm not understanding the requirement. Could you explain it more. Are you asking to have an event or way to update an entity you have locally in cases where something externally (service or database) have updated it?

@indr
Copy link

indr commented Jul 30, 2016

@mcdonnelldean what @NejcHorvat means is this:

table SUPPLIER (
  ID long not null auto_increment(1,1)
  FLAG bit not null default(true)
  NAME string
  TAX_NUMBER string
  TAX_CODE string
)

before insert trigger on SUPPLIER (
  if (new.TAX_NUMBER != NULL and new.TAX_CODE == null) {
    new.TAX_CODE = 'A'
  }
)
seneca.make('supplier').data$({name: 'Company', taxNumber: '123-ABC'})
  .save$(function (err, entity) {
    assert(entity.id > 0)
    assert.strictEqual(entity.flag, true)
    assert.equals(entity.taxCode, 'A')
  });

@NejcHorvat
Copy link
Author

I'm sorry for not replying to you @mcdonnelldean, had a few super busy months and the project I had this requirement for had to go on the back burner. What @indr said is correct. This is what I had in mind.

@mcdonnelldean
Copy link
Contributor

@indr @NejcHorvat Is this not what it does already?

@indr
Copy link

indr commented Aug 2, 2016

@mcdonnelldean I don't know. Didn't look at the source or test it. It also isn't the seneca-entitys responsibility to read from a data source. The save$ method seams to send the pattern role:entity,cmd:save,ent:{} and passes the same callback (https://github.com/senecajs/seneca-entity/blob/master/lib/make_entity.js#L155). I guess this will be handled by the stores?

@NejcHorvat What store did you use?

@rjrodger
Copy link
Contributor

the returned entity (via callback) should reflect whatever is in the database - this is store implementation dependent however - adding an issue to the store test suite
senecajs/seneca-store-test#48

@rjrodger rjrodger self-assigned this Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants