db.go 779 B

1234567891011121314151617181920212223242526272829303132
  1. package db
  2. import (
  3. "github.com/pkg/errors"
  4. "github.com/usememos/memos/server/profile"
  5. "github.com/usememos/memos/store"
  6. "github.com/usememos/memos/store/db/mysql"
  7. "github.com/usememos/memos/store/db/postgres"
  8. "github.com/usememos/memos/store/db/sqlite"
  9. )
  10. // NewDBDriver creates new db driver based on profile.
  11. func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
  12. var driver store.Driver
  13. var err error
  14. switch profile.Driver {
  15. case "sqlite":
  16. driver, err = sqlite.NewDB(profile)
  17. case "mysql":
  18. driver, err = mysql.NewDB(profile)
  19. case "postgres":
  20. driver, err = postgres.NewDB(profile)
  21. default:
  22. return nil, errors.New("unknown db driver")
  23. }
  24. if err != nil {
  25. return nil, errors.Wrap(err, "failed to create db driver")
  26. }
  27. return driver, nil
  28. }