-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
51 lines (43 loc) · 1002 Bytes
/
init.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
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"log"
"os"
"path"
"strings"
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func NonExistant(path string) bool {
_, err := os.Stat(path)
return os.IsNotExist(err)
}
func initialize() *gorm.DB {
filePath := path.Join(os.TempDir(), "Todos.sqlite")
exists := NonExistant(filePath)
if exists {
TempFile, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
TempFile.Close()
}
db, err := gorm.Open(sqlite.Open(filePath), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
NoLowerCase: true,
NameReplacer: strings.NewReplacer("CID", "Cid"),
},
NowFunc: func() time.Time {
return time.Now().Local()
},
PrepareStmt: true,
TranslateError: true,
})
if err != nil {
log.Fatal(err)
}
db.Exec("CREATE TABLE IF NOT EXISTS Todos (ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR(50), Description TEXT, Priority VARCHAR(6), CreatedDate DATE);")
return db
}