generated from koddr/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
38 lines (31 loc) · 871 Bytes
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package database
import (
"embed"
"os"
"path/filepath"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/secretium/secretium/internal/config"
"github.com/secretium/secretium/internal/constants"
)
//go:embed sql_queries/*
var sqlQueries embed.FS
// Database contains DB connection and other dependencies for application.
type Database struct {
Connection *sqlx.DB
SQLQueries embed.FS
}
// New returns a new instance of DB connection.
func New(c *config.Config) (*Database, error) {
// Create a folder for the SQLite DB.
_ = os.Mkdir(constants.ConstConfigSQLitePath, os.ModePerm)
// Connect to the SQLite DB.
connection, err := sqlx.Connect("sqlite3", filepath.Join(constants.ConstConfigSQLitePath, "db.sqlite3"))
if err != nil {
return nil, err
}
return &Database{
Connection: connection,
SQLQueries: sqlQueries,
}, nil
}