PhilWaldmann / openrecord

Make ORMs great again!
https://openrecord.js.org
MIT License
486 stars 38 forks source link

Not returning complete record on create #108

Closed incompletude closed 4 years ago

incompletude commented 4 years ago

I have a simple model:

import Store from 'openrecord/store/postgres'

class Person extends Store.BaseModel {
  static definition() {
    this.validatesFormatOf('email', 'email')
  }
}

export default Person

Then I'm using:

let person = await Person.create({ email: 'valid@email.com', password: 'somepass' })

The postgres table denifition is:

create table person (
  id_person serial,
  slug uuid not null default uuid_generate_v4(),
  email text not null,
  password text not null,
  is_active boolean not null default false,
  primary key (id_person),
  unique (email)
);

The result is:

Person {
  relations: {},
  attributes: {
    id_person: 36,
    slug: null,
    email: 'assfffw@ww1c.om',
    password: '$2b$10$x5fVpQpq3pv5Nvmp13CmQuJyT6L.CfaALZAuehYwyfbeJaoeABV.u',
    is_active: false
  },
  changes: {},
  object_changes: {},
  isNewRecord: [Getter],
  context: [Getter],
  errors: [ValidationError] { errors: {} }
}

The slug field is null, when it is created on the database. If I query the database again, the result is right:

person = await Person.find(person.id_person)
PhilWaldmann commented 4 years ago

Hi,

openrecord currently only supports inserts the autogenerated primary key via SQL returning.

You could use the afterCreate or afterSave hook to reload fields.

PhilWaldmann commented 4 years ago

If you are interested, please open a PR for this. i think a new boolean attribute option (e.g. readAfterWrite) would be nice.