-
Notifications
You must be signed in to change notification settings - Fork 16
/
init.go
68 lines (60 loc) · 1.52 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package sqlite3
import (
"github.com/fiatjaf/eventstore"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/mattn/go-sqlite3"
)
const (
queryLimit = 100
queryIDsLimit = 500
queryAuthorsLimit = 500
queryKindsLimit = 10
queryTagsLimit = 10
)
var _ eventstore.Store = (*SQLite3Backend)(nil)
var ddls = []string{
`CREATE TABLE IF NOT EXISTS event (
id text NOT NULL,
pubkey text NOT NULL,
created_at integer NOT NULL,
kind integer NOT NULL,
tags jsonb NOT NULL,
content text NOT NULL,
sig text NOT NULL);`,
`CREATE UNIQUE INDEX IF NOT EXISTS ididx ON event(id)`,
`CREATE INDEX IF NOT EXISTS pubkeyprefix ON event(pubkey)`,
`CREATE INDEX IF NOT EXISTS timeidx ON event(created_at DESC)`,
`CREATE INDEX IF NOT EXISTS kindidx ON event(kind)`,
`CREATE INDEX IF NOT EXISTS kindtimeidx ON event(kind,created_at DESC)`,
}
func (b *SQLite3Backend) Init() error {
db, err := sqlx.Connect("sqlite3", b.DatabaseURL)
if err != nil {
return err
}
db.Mapper = reflectx.NewMapperFunc("json", sqlx.NameMapper)
b.DB = db
for _, ddl := range ddls {
_, err = b.DB.Exec(ddl)
if err != nil {
return err
}
}
if b.QueryLimit == 0 {
b.QueryLimit = queryLimit
}
if b.QueryIDsLimit == 0 {
b.QueryIDsLimit = queryIDsLimit
}
if b.QueryAuthorsLimit == 0 {
b.QueryAuthorsLimit = queryAuthorsLimit
}
if b.QueryKindsLimit == 0 {
b.QueryKindsLimit = queryKindsLimit
}
if b.QueryTagsLimit == 0 {
b.QueryTagsLimit = queryTagsLimit
}
return nil
}