zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[Feature Request] Add model name metadata that could be used in the Abstract model

Open jiashengguo opened this issue 2 years ago • 0 comments

Use something like $TYPENAME to represent the actual model name. There are two known scenarios for now:

  • Be able to disambiguate relations in the abstract model.
abstract model Base {
    createdBy User @relation(name: 'author_$TYPENAME', fields: [createdById], references: [id])
    updatedBy User @relation(name: 'updated_$TYPENAME', fields: [updatedById], references: [id])
    createdById Int
    updatedById Int
}

model User {
    createdPosts Base[] @relation(name: 'author_Post')
    updatedPosts Base[] @relation(name: 'updated_Post')
    ...
}
  • Be able to write more general policy rules like below

model User {
  id         Int     @unique @default(autoincrement())
  role    Role @relation(fields: [role_id], references: [id])
  role_id Int  @db.Integer()

}

model Role {
  id         Int       @unique @default(autoincrement())
  Permission Permission[]
  User       User[]
}

model Permission {
  id         Int     @unique @default(autoincrement())
  role       Role    @relation(fields: [role_id], references: [id])
  role_id    Int     @db.Integer()
  action     String  @db.VarChar()
  subject    String  @db.VarChar()
}

abstract model Asset{
    id Int @unique @default(autoincrement())

    @@allow('read', auth().role.permissions?[subject == $TYPENAME&& action =='READ'] )
    @@allow('update', auth().role.permissions?[subject ==$TYPENAME&& action =='UPDATE'] )
}

See more detail in the discord thread: https://discord.com/channels/1035538056146595961/1173699621537718392/1173708969567592499

jiashengguo avatar Nov 14 '23 17:11 jiashengguo