|
|
|
@ -1,4 +1,4 @@ |
|
|
|
|
package tempdb |
|
|
|
|
package database |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
@ -12,24 +12,24 @@ import ( |
|
|
|
|
"github.com/mainnika/mongox-go-driver/v2/mongox/database" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// TempDB is a temporary database connection that will be destroyed after close
|
|
|
|
|
type TempDB struct { |
|
|
|
|
// EphemeralDatabase is a temporary database connection that will be destroyed after close
|
|
|
|
|
type EphemeralDatabase struct { |
|
|
|
|
mongox.Database |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewTempDB creates new mongo connection
|
|
|
|
|
func NewTempDB(URI string) (tempdb *TempDB, err error) { |
|
|
|
|
// NewEphemeral creates new mongo connection
|
|
|
|
|
func NewEphemeral(URI string) (db *EphemeralDatabase, err error) { |
|
|
|
|
|
|
|
|
|
name := strconv.Itoa(rand.Int()) |
|
|
|
|
opts := options.Client().ApplyURI(URI) |
|
|
|
|
client, err := mongo.Connect(context.Background(), opts) |
|
|
|
|
|
|
|
|
|
tempdb = &TempDB{Database: database.NewDatabase(client, name)} |
|
|
|
|
db = &EphemeralDatabase{Database: database.NewDatabase(client, name)} |
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Close the connection and drop database
|
|
|
|
|
func (tdb *TempDB) Close() { |
|
|
|
|
_ = tdb.Client().Database(tdb.Name()).Drop(tdb.Context()) |
|
|
|
|
func (e *EphemeralDatabase) Close() error { |
|
|
|
|
return e.Client().Database(e.Name()).Drop(e.Context()) |
|
|
|
|
} |