senecajs / seneca-entity

Entity plugin for seneca
MIT License
13 stars 15 forks source link

Request for a method to update entity after save #3

Open NejcHorvat opened 8 years ago

NejcHorvat commented 8 years ago

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

Three use cases:

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 commented 8 years ago

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

NejcHorvat commented 8 years ago

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

mcdonnelldean commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

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

indr commented 8 years ago

@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 commented 5 years ago

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 https://github.com/senecajs/seneca-store-test/issues/48