db connect

This commit is contained in:
Sebastian Frank 2022-02-09 21:06:31 +01:00
parent 6b367dbd7a
commit 754c6dd624
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57

View File

@ -10,6 +10,23 @@ var (
ErrNotFound = errors.New("not found")
)
type Connection struct {
connection *mgo.Session
}
func NewConnection(dial string) (*Connection, error) {
connection, err := mgo.Dial(dial)
if err != nil {
return nil, err
}
connection.SetMode(mgo.Monotonic, true)
return &Connection{connection: connection}, nil
}
func (c *Connection) Close() {
c.connection.Close()
}
type Session struct {
session *mgo.Session
}
@ -18,17 +35,8 @@ func (s *Session) Close() {
s.session.Close()
}
func (s *Session) Copy() *Session {
return &Session{session: s.session.Copy()}
}
func NewSession(dial string) (*Session, error) {
session, err := mgo.Dial(dial)
if err != nil {
return nil, err
}
session.SetMode(mgo.Monotonic, true)
return &Session{session: session}, nil
func (c *Connection) NewSession() (*Session, error) {
return &Session{session: c.connection.Copy()}, nil
}
type Database struct {