memos/store/db/db.go

33 lines
779 B
Go
Raw Normal View History

2023-10-05 23:11:29 +08:00
package db
import (
"github.com/pkg/errors"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
"github.com/usememos/memos/store/db/mysql"
"github.com/usememos/memos/store/db/postgres"
2023-10-05 23:11:29 +08:00
"github.com/usememos/memos/store/db/sqlite"
)
// NewDBDriver creates new db driver based on profile.
func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
var driver store.Driver
var err error
2023-10-05 23:19:52 +08:00
2023-10-06 00:34:06 +08:00
switch profile.Driver {
case "sqlite":
2023-10-05 23:11:29 +08:00
driver, err = sqlite.NewDB(profile)
2023-10-06 00:34:06 +08:00
case "mysql":
driver, err = mysql.NewDB(profile)
case "postgres":
driver, err = postgres.NewDB(profile)
2023-10-06 00:34:06 +08:00
default:
return nil, errors.New("unknown db driver")
2023-10-05 23:11:29 +08:00
}
if err != nil {
return nil, errors.Wrap(err, "failed to create db driver")
}
return driver, nil
}