Improving our models
Even though we are now reading from and writing to the database through our entity, we only wrote a basic, initial implementation; we should review our code to see what can be improved.
Let’s now go back to the entity file, src/entries/entry.entity.ts
, and figure out what kind of improvements we can do there.
Auto-generated IDs
All of the database entries need to have a unique ID. At this point, we are simply relying on the ID sent by the client when creating the entity (when sending the POST request,) but this is less than desirable.
Any server-side application will be connected to multiple clients, and all of those clients have no way of knowing which ID’s are already in use, so it would be impossible for them to generate and send a unique ID with each POST request.
TypeORM provides a couple of ways of generating unique ID’s for entities. The first one is using the @PrimaryGeneratedColumn()
decorator. By using it, you no longer need to include...