dimanche 24 octobre 2021

Should id field be optional or not?

For example, I have entity Person which can be obtained from DB, or inserted into DB.

class Person {
    id: number;
    name: string;
    age: number;
}

id field is autogenerated column in DB, so in situation when model is obtained from DB it will be always filled.

But when creating model, we are to set name and age fields.

So, in case, when we getting model from DB - id field is not optional and is always set. In case when we creating new model to insert into DB id field should be unset, and therefore in model definition should be optional.

Are there any best practices or common approaches to handle this problem?

Aucun commentaire:

Enregistrer un commentaire