dfuncd / repofuck

Fucking with the Repository Design Pattern
6 stars 2 forks source link

Implementation of `updateOrCreate()` function #107

Closed makoru-hikage closed 7 years ago

makoru-hikage commented 7 years ago

There will be cases where this function is needed.

For example, there are two tables that are related one to one: User and Employee. (Where Employee means the information of a user as an Employee). This Employee table refers to User by means of user_id.

The goal is to create a User that is referred by an Employee

Here are the key factors to scenario:

Steps to goal:

  1. Create a User using Repofuck's create() (an SQL INSERT shall be made, probably)
  2. Create and associate a new Employee to a User. (and do SQL INSERT again)

Problem:

  1. A sudden power outage or any untoward incident can interrupt Step no. 2 to the point that only a User is inserted; no Employee (that refers to that User) is made.
  2. Should the process be repeated again, this is where an updateOrCreate() or firstOrNew() must be made; it is not good to INSERT a new User while leaving that 'orphaned' User.

Must I make the necessary PR or you have a better idea? Or both?

rmrhz commented 7 years ago

It's a good addition. Target this for 0.2