summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/database/connection/connection.go')
-rw-r--r--pkg/database/connection/connection.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/pkg/database/connection/connection.go b/pkg/database/connection/connection.go
new file mode 100644
index 0000000..733aee2
--- /dev/null
+++ b/pkg/database/connection/connection.go
@@ -0,0 +1,42 @@
+// Contains utility functions around the database
+
+package connection
+
+import (
+ "glsamaker/pkg/config"
+ "glsamaker/pkg/logger"
+ "context"
+ "github.com/go-pg/pg/v9"
+)
+
+// DBCon is the connection handle
+// for the database
+var (
+ DB *pg.DB
+)
+
+type dbLogger struct{}
+
+func (d dbLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
+ return c, nil
+}
+
+// AfterQuery is used to log SQL queries
+func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error {
+ logger.Debug.Println(q.FormattedQuery())
+ return nil
+}
+
+// Connect is used to connect to the database
+// and turn on logging if desired
+func Connect() {
+ DB = pg.Connect(&pg.Options{
+ User: config.PostgresUser(),
+ Password: config.PostgresPass(),
+ Database: config.PostgresDb(),
+ Addr: config.PostgresHost() + ":" + config.PostgresPort(),
+ })
+
+ DB.AddQueryHook(dbLogger{})
+
+}