mirror of
https://github.com/zoriya/tide.git
synced 2026-08-15 18:43:19 +00:00
Setup postgresql
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"tide/api/models"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
Connection *sql.DB
|
||||
}
|
||||
|
||||
func NewDatabase() (*Database, error) {
|
||||
d := new(Database)
|
||||
con := fmt.Sprintf(
|
||||
"postgresql://%v:%v@%v:%v/%v",
|
||||
os.Getenv("POSTGRES_USER"),
|
||||
os.Getenv("POSTGRES_PASSWORD"),
|
||||
os.Getenv("POSTGRES_SERVER"),
|
||||
os.Getenv("POSTGRES_PORT"),
|
||||
os.Getenv("POSTGRES_DB"),
|
||||
)
|
||||
db, err := sql.Open("posgres", con)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.Connection = db
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Database) AddItem(item *models.Item) (*models.Item, error) {
|
||||
_, err := d.Connection.Exec("INSERT INTO items (id) VALUES (?)", item.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
Reference in New Issue
Block a user