@startuml
hide circle
skinparam nodesep 60

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

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" {
  *id : text
  --
  *expiredAt : number
  *json : text
}


entity "Revision" {
  *id : number <<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 : number <<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
}

entity "Tag" {
  *id: number <<generated>>
  *name: text
}

entity "MediaUpload" {
  *id : text <<unique>>
  --
  *noteId : uuid <<FK Note>>
  *userId : uuid <<FK User>>
  *backendType: text
  backendData: text
  *createdAt : date
}

User "1" -- "0..*" Note: owner
User "1" -u- "1..*" Identity
User "1" - "1..*" authToken
User "1" -l- "1..*" Session
User "1" - "0..*" MediaUpload
User "0..*" -- "0..*" Note
User "1" - "0..*" Authorship

(User, Note) . AuthorColors

Revision "0..*" - "0..*" Authorship
(Revision, Authorship) .. RevisionAuthorship

MediaUpload "0..*"  -- "1" Note
Note "1" - "1..*" Revision
Note "0..*" -l- "0..*" Tag
Note "0..*" -- "0..*" Group

User "0..*" -- "0..*" Note
(User, Note) . NoteUserPermission
(Note, Group) . NoteGroupPermission

@enduml
