dnote/pkg/server/database/models.go
Sung 6314749263
Improve empty server sync when multiple clients exist (#706)
* Fix dbPath

* Require full sync when after another client uploads to an empty server

* Avoid orphan notes with empty sync
2025-10-26 17:54:24 -07:00

88 lines
2.7 KiB
Go

/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
*
* This file is part of Dnote.
*
* Dnote is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dnote is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
*/
package database
import (
"time"
)
// Model is the base model definition
type Model struct {
ID int `gorm:"primaryKey" json:"-"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
// Book is a model for a book
type Book struct {
Model
UUID string `json:"uuid" gorm:"uniqueIndex;type:text"`
UserID int `json:"user_id" gorm:"index"`
Label string `json:"label" gorm:"index"`
Notes []Note `json:"notes" gorm:"foreignKey:BookUUID;references:UUID"`
AddedOn int64 `json:"added_on"`
EditedOn int64 `json:"edited_on"`
USN int `json:"-" gorm:"index"`
Deleted bool `json:"-" gorm:"default:false"`
}
// Note is a model for a note
type Note struct {
Model
UUID string `json:"uuid" gorm:"index;type:text"`
Book Book `json:"book" gorm:"foreignKey:BookUUID;references:UUID"`
User User `json:"user"`
UserID int `json:"user_id" gorm:"index"`
BookUUID string `json:"book_uuid" gorm:"index;type:text"`
Body string `json:"content"`
AddedOn int64 `json:"added_on"`
EditedOn int64 `json:"edited_on"`
USN int `json:"-" gorm:"index"`
Deleted bool `json:"-" gorm:"default:false"`
Client string `gorm:"index"`
}
// User is a model for a user
type User struct {
Model
UUID string `json:"uuid" gorm:"type:text;index"`
Email NullString `gorm:"index"`
Password NullString `json:"-"`
LastLoginAt *time.Time `json:"-"`
MaxUSN int `json:"-" gorm:"default:0"`
FullSyncBefore int64 `json:"-" gorm:"default:0"`
}
// Token is a model for a token
type Token struct {
Model
UserID int `gorm:"index"`
Value string `gorm:"index"`
Type string
UsedAt *time.Time
}
// Session represents a user session
type Session struct {
Model
UserID int `gorm:"index"`
Key string `gorm:"index"`
LastUsedAt time.Time
ExpiresAt time.Time
}