ehmpathy / sql-dao-generator

Generate data-access-objects from your domain-objects
MIT License
0 stars 0 forks source link

restrict referencing entities through nesting and prefer referencing entities by uuid instead #2

Closed uladkasach closed 3 years ago

uladkasach commented 3 years ago

specifically,

  1. forbid nesting an entity inside of a domain-object because it causes a ton of confusion on the backend side (since we have to consider persisting data)
    
    -- forbid this
    interface Profile {
    user: User
    picture: Image
    }

-- prefer this interface Profile { userUuid: string; picture: Image; }


^- this has been shown time and time again to prevent errors and simplify code on the backend. so we should enforce that.

however, we also want to support foreign keys in the tables, when the referenced entity is in the same database

i.e., in both cases, we want
```ts
    export const profile = new Entity({
      name: 'profile',
      properties: {
        user_id: prop.REFERENCES(user),
        image_id: prop.REFERENCES(image),
      },
      ...
    });

which means that somehow we have to tell the dao generator that in case 2, userUuid is actually a reference to User

one way we can do that is by defining a static property called references (in contrast to nested)

so... references would look something like this

class Profile extends DomainEntity<Profile> implements Profile {
  public static references = { userUuid: User } 
}

so note how we dont have to specify the references that can be derived impicitly (i.e., we can see that Image references image)

but we do have to specify the references that can not be derived implicitly

uladkasach commented 3 years ago

alternatively, maybe we can detect that it is a reference on our own by seeing that there's another domain object having a dao generated for it that has the same name (minus the 'uuid') part

i.e., we see that we're generating dao for

interface User {
...
}

interface Profile {
  userUuid: string,
  ...
}

which we can use to tell us

userUuid => references user.id
uladkasach commented 3 years ago

we can also allow specifying this references explicitly in the schema-dao-generator config, to keep the domain object definition clean. e.g., we add metadata for the dao generator in that config file

uladkasach commented 3 years ago

done through implicit uuid reference