You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

77 lines
1.5 KiB

package arango_db
import (
"context"
"log"
driver "github.com/arangodb/go-driver"
"github.com/pkg/errors"
)
type DataBase struct {
DB driver.Database
ctx context.Context
collections map[string]*Collection
}
func NewDataBase(ctx context.Context, client *Client, db_name string) *DataBase {
// Проверим существование базы данных
exists, ere := client.DatabaseExists(ctx, db_name)
if ere != nil {
log.Fatal(ere)
} else if exists {
db, eru := client.Database(ctx, db_name)
if eru != nil {
log.Fatal(eru)
}
//
return &DataBase{
DB: db,
ctx: ctx,
collections: make(map[string]*Collection),
}
}
//
db, eru := client.CreateDatabase(ctx, db_name, nil)
if eru != nil {
log.Fatal(eru)
}
//
return &DataBase{
DB: db,
ctx: ctx,
collections: make(map[string]*Collection),
}
}
// Получение коллекции
func (db *DataBase) GetCollection(name string) (*Collection, error) {
col, ok := db.collections[name]
if ok {
return col, nil
}
return nil, errors.Errorf("Collection %v not exists", name)
}
// Добавление коллекции
func (db *DataBase) AddCollection(name string) (*Collection, error) {
col, ok := db.collections[name]
if ok {
return col, nil
}
//
col, err := NewCollection(db.ctx, db.DB, name)
if err != nil {
return nil, err
}
// Зарегистрируем
db.collections[name] = col
return col, nil
}