@startuml
' hide the spot
hide circle

' avoid problems with angled crows feet
skinparam linetype ortho

entity "Note" {
  *id : uuid <<generated>>
  --
  *shortid : text
  *alias : text
  *viewcount : number
  *ownerId : uuid <<FK User>>
}

entity "User" {
  *id : uuid <<generated>>
  --
  *userName : text
  *displayName : text
  *createdAt : date
  *updatedAt : date
  photo : text
  email : text
}

entity "AuthToken" as authToken{
  *id : number <<generated>>
  --
  *userId : uuid
  *accessToken : text
}

entity "Identity" {
  *id : number
  --
  *userId : uuid <<FK User>>
  ' Identifies the external login provider and is set in the config
  *providerName : text
  *syncSource : boolean
  *createdAt : date
  *updatedAt : date
  ' The unique identifier of a user from the login provider
  providerUserId : text
  ' Token used to access the OAuth provider in the users name. Can be NULL
  oauthAccessToken : text
  ' Password hash. Can be NULL
  passwordHash : text
}

entity "Session" as seesion {
  *id : text
  --
  *expiredAt : number
  *json : text
}


entity "Revision" {
  *id : uuid <<generated>>
  --
  *noteId : uuid <<FK Note>>
  *content : text
  *patch : text
  *createdAt : date
  *length : number
}

entity "Authorship" {
  *id : uuid <<generated>>
  --
  *userId : uuid <FK User>>
  *startPos : number
  *endPos : number
  *createdAt : date
  *updatedAt : date
}

entity "RevisionAuthorship" {
  *revisionId : uuid <<FK Revision>>
  *authorshipId : uuid <<FK Authorship>>
}

entity "AuthorColors" {
  *noteId : uuid <<FK Note>>
  *userId : uuid <<FK User>>
  --
  *color : text
}


entity "NoteUserPermission" {
  *userId : uuid <<FK User>>
  *noteId : uuid <<FK Note>>
  --
  *canEdit : boolean
}

entity "Group" {
  *id : number <<generated>>
  --
  *name : text <<unique>>
  *displayName : text
  ' Is set to denote a special group
  ' Special groups are used to map the old share settings like "everyone can edit"
  ' or "logged in users can view" to the group permission system
  *special : boolean
  }

 entity "NoteGroupPermission" {
  *groupId : number <<FK Group>>
  *noteId : uuid <<FK Note>>
  --
  *canEdit : boolean
}

Note "1" - "1..*" Revision
Revision "0..*" - "0..*" Authorship
(Revision, Authorship) .. RevisionAuthorship
Authorship "0..*" -- "1" User
Note "0..*" -- "1" User : owner
Note "1" -- "0..*" NoteUserPermission
NoteUserPermission "1" -- "1" User
Note "1" -- "0..*" NoteGroupPermission
NoteGroupPermission "0..*" -- "1" Group
Identity "1..*" -- "1" User
authToken "1..*" -- "1" User
seesion "1..*" -- "1" User
Note "0..*" -- "0..*" User : color
(Note, User) .. AuthorColors
@enduml
