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"
|
2023-12-03 13:31:29 +08:00
|
|
|
"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)
|
2023-12-03 13:31:29 +08:00
|
|
|
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
|
|
|
|
}
|