Skip to content

Commit 0a0ae07

Browse files
adding jmoiron and db.go
1 parent fdbdd4d commit 0a0ae07

File tree

7 files changed

+45
-0
lines changed

7 files changed

+45
-0
lines changed

db/db.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package db
2+
3+
import (
4+
"fmt"
5+
"github.com/jmoiron/sqlx"
6+
)
7+
8+
9+
10+
type Database struct {
11+
db *sqlx.DB
12+
}
13+
14+
15+
16+
func NewDatabase() (*Database,error) {
17+
db, err := sqlx.Open("mysql","root:password@tcp(localhost:3306)/ecomm?parseTime=true")
18+
19+
if err !=nil {
20+
return nil, fmt.Error("Error opening database, %w",err)
21+
}
22+
23+
return &Database{db:db},nil
24+
}
25+
26+
27+
28+
func (d *Database) Close() error {
29+
return db.db.Close()
30+
}
31+
32+
33+
func (d *Database) GetDB() *sqlx.DB {
34+
return d.db
35+
}
36+
37+

db/migrations/20250523220200_init_schema.down.sql

100644100755
File mode changed.

db/migrations/20250523220200_init_schema.up.sql

100644100755
File mode changed.

db/migrations/20250523231739_add_users_table.down.sql

100644100755
File mode changed.

db/migrations/20250523231739_add_users_table.up.sql

100644100755
File mode changed.

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/bloodgroup-cplusplus/ecomm
22

33
go 1.23.3
4+
5+
require github.com/jmoiron/sqlx v1.4.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
2+
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
3+
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
4+
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
5+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
6+
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=

0 commit comments

Comments
 (0)