chore: rename store to db
This commit is contained in:
50
internal/db/account.go
Normal file
50
internal/db/account.go
Normal file
@ -0,0 +1,50 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// why don't need `cache` for account?
|
||||
// because all account store in `operations.accountsMap`
|
||||
// the most of the read operation is from `operations.accountsMap`
|
||||
// just for persistence in database
|
||||
|
||||
// CreateAccount just insert account to database
|
||||
func CreateAccount(account *model.Account) error {
|
||||
return errors.WithStack(db.Create(account).Error)
|
||||
}
|
||||
|
||||
// UpdateAccount just update account in database
|
||||
func UpdateAccount(account *model.Account) error {
|
||||
return errors.WithStack(db.Save(account).Error)
|
||||
}
|
||||
|
||||
// DeleteAccountById just delete account from database by id
|
||||
func DeleteAccountById(id uint) error {
|
||||
return errors.WithStack(db.Delete(&model.Account{}, id).Error)
|
||||
}
|
||||
|
||||
// GetAccounts Get all accounts from database order by index
|
||||
func GetAccounts(pageIndex, pageSize int) ([]model.Account, int64, error) {
|
||||
accountDB := db.Model(&model.Account{})
|
||||
var count int64
|
||||
if err := accountDB.Count(&count).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get accounts count")
|
||||
}
|
||||
var accounts []model.Account
|
||||
if err := accountDB.Order(columnName("index")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&accounts).Error; err != nil {
|
||||
return nil, 0, errors.WithStack(err)
|
||||
}
|
||||
return accounts, count, nil
|
||||
}
|
||||
|
||||
// GetAccountById Get Account by id, used to update account usually
|
||||
func GetAccountById(id uint) (*model.Account, error) {
|
||||
var account model.Account
|
||||
account.ID = id
|
||||
if err := db.First(&account).Error; err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
return &account, nil
|
||||
}
|
91
internal/db/meta.go
Normal file
91
internal/db/meta.go
Normal file
@ -0,0 +1,91 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/go-cache"
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/pkg/singleflight"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
stdpath "path"
|
||||
)
|
||||
|
||||
var metaCache = cache.NewMemCache(cache.WithShards[*model.Meta](2))
|
||||
|
||||
// metaG maybe not needed
|
||||
var metaG singleflight.Group[*model.Meta]
|
||||
|
||||
func GetNearestMeta(path string) (*model.Meta, error) {
|
||||
path = utils.StandardizePath(path)
|
||||
meta, err := GetMetaByPath(path)
|
||||
if err == nil {
|
||||
return meta, nil
|
||||
}
|
||||
if errors.Cause(err) != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
}
|
||||
if path == "/" {
|
||||
return nil, errors.WithStack(errs.MetaNotFound)
|
||||
}
|
||||
return GetNearestMeta(stdpath.Dir(path))
|
||||
}
|
||||
|
||||
func GetMetaByPath(path string) (*model.Meta, error) {
|
||||
meta, ok := metaCache.Get(path)
|
||||
if ok {
|
||||
return meta, nil
|
||||
}
|
||||
meta, err, _ := metaG.Do(path, func() (*model.Meta, error) {
|
||||
meta := model.Meta{Path: path}
|
||||
if err := db.Where(meta).First(&meta).Error; err != nil {
|
||||
return nil, errors.Wrapf(err, "failed select meta")
|
||||
}
|
||||
metaCache.Set(path, &meta)
|
||||
return &meta, nil
|
||||
})
|
||||
return meta, err
|
||||
}
|
||||
|
||||
func GetMetaById(id uint) (*model.Meta, error) {
|
||||
var u model.Meta
|
||||
if err := db.First(&u, id).Error; err != nil {
|
||||
return nil, errors.Wrapf(err, "failed get old meta")
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func CreateMeta(u *model.Meta) error {
|
||||
return errors.WithStack(db.Create(u).Error)
|
||||
}
|
||||
|
||||
func UpdateMeta(u *model.Meta) error {
|
||||
old, err := GetMetaById(u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metaCache.Del(old.Path)
|
||||
return errors.WithStack(db.Save(u).Error)
|
||||
}
|
||||
|
||||
func GetMetas(pageIndex, pageSize int) ([]model.Meta, int64, error) {
|
||||
metaDB := db.Model(&model.Meta{})
|
||||
var count int64
|
||||
if err := metaDB.Count(&count).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get metas count")
|
||||
}
|
||||
var metas []model.Meta
|
||||
if err := metaDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get find metas")
|
||||
}
|
||||
return metas, count, nil
|
||||
}
|
||||
|
||||
func DeleteMetaById(id uint) error {
|
||||
old, err := GetMetaById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metaCache.Del(old.Path)
|
||||
return errors.WithStack(db.Delete(&model.Meta{}, id).Error)
|
||||
}
|
58
internal/db/meta_test.go
Normal file
58
internal/db/meta_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
Init(db)
|
||||
}
|
||||
|
||||
func TestCreateMeta(t *testing.T) {
|
||||
metas := []model.Meta{
|
||||
{Path: "/"},
|
||||
{Path: "/a"},
|
||||
{Path: "/a/b"},
|
||||
{Path: "/a/b/c"},
|
||||
}
|
||||
for _, meta := range metas {
|
||||
err := CreateMeta(&meta)
|
||||
if err != nil {
|
||||
t.Errorf("failed to create meta: %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateMeta(t *testing.T) {
|
||||
meta := model.Meta{ID: 1, Path: "/b"}
|
||||
err := UpdateMeta(&meta)
|
||||
if err != nil {
|
||||
t.Errorf("failed to update meta: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNearestMeta1(t *testing.T) {
|
||||
meta, err := GetNearestMeta("/b/c/d")
|
||||
if err != nil {
|
||||
t.Errorf("failed to get nearest meta: %+v", err)
|
||||
}
|
||||
if meta.Path != "/b" {
|
||||
t.Errorf("unexpected meta: %+v", meta)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNearestMeta2(t *testing.T) {
|
||||
meta, err := GetNearestMeta("/c/d/e")
|
||||
if errors.Cause(err) != ErrMetaNotFound {
|
||||
t.Errorf("unexpected error: %+v", err)
|
||||
t.Errorf("unexpected meta: %+v", meta)
|
||||
}
|
||||
}
|
17
internal/db/store.go
Normal file
17
internal/db/store.go
Normal file
@ -0,0 +1,17 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
)
|
||||
|
||||
var db gorm.DB
|
||||
|
||||
func Init(d *gorm.DB) {
|
||||
db = *d
|
||||
err := db.AutoMigrate(new(model.Account), new(model.User), new(model.Meta))
|
||||
if err != nil {
|
||||
log.Fatalf("failed migrate database: %s", err.Error())
|
||||
}
|
||||
}
|
78
internal/db/user.go
Normal file
78
internal/db/user.go
Normal file
@ -0,0 +1,78 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/go-cache"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/pkg/singleflight"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var userCache = cache.NewMemCache(cache.WithShards[*model.User](2))
|
||||
var userG singleflight.Group[*model.User]
|
||||
|
||||
func ExistAdmin() bool {
|
||||
return db.Take(&model.User{Role: model.ADMIN}).Error != nil
|
||||
}
|
||||
|
||||
func ExistGuest() bool {
|
||||
return db.Take(&model.User{Role: model.GUEST}).Error != nil
|
||||
}
|
||||
|
||||
func GetUserByName(username string) (*model.User, error) {
|
||||
user, ok := userCache.Get(username)
|
||||
if ok {
|
||||
return user, nil
|
||||
}
|
||||
user, err, _ := userG.Do(username, func() (*model.User, error) {
|
||||
user := model.User{Username: username}
|
||||
if err := db.Where(user).First(&user).Error; err != nil {
|
||||
return nil, errors.Wrapf(err, "failed find user")
|
||||
}
|
||||
userCache.Set(username, &user)
|
||||
return &user, nil
|
||||
})
|
||||
return user, err
|
||||
}
|
||||
|
||||
func GetUserById(id uint) (*model.User, error) {
|
||||
var u model.User
|
||||
if err := db.First(&u, id).Error; err != nil {
|
||||
return nil, errors.Wrapf(err, "failed get old user")
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func CreateUser(u *model.User) error {
|
||||
return errors.WithStack(db.Create(u).Error)
|
||||
}
|
||||
|
||||
func UpdateUser(u *model.User) error {
|
||||
old, err := GetUserById(u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userCache.Del(old.Username)
|
||||
return errors.WithStack(db.Save(u).Error)
|
||||
}
|
||||
|
||||
func GetUsers(pageIndex, pageSize int) ([]model.User, int64, error) {
|
||||
userDB := db.Model(&model.User{})
|
||||
var count int64
|
||||
if err := userDB.Count(&count).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get users count")
|
||||
}
|
||||
var users []model.User
|
||||
if err := userDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get find users")
|
||||
}
|
||||
return users, count, nil
|
||||
}
|
||||
|
||||
func DeleteUserById(id uint) error {
|
||||
old, err := GetUserById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userCache.Del(old.Username)
|
||||
return errors.WithStack(db.Delete(&model.User{}, id).Error)
|
||||
}
|
13
internal/db/util.go
Normal file
13
internal/db/util.go
Normal file
@ -0,0 +1,13 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
)
|
||||
|
||||
func columnName(name string) string {
|
||||
if conf.Conf.Database.Type == "postgres" {
|
||||
return fmt.Sprintf(`"%s"`, name)
|
||||
}
|
||||
return fmt.Sprintf("`%s`", name)
|
||||
}
|
Reference in New Issue
Block a user