Your ROOT_URL in app.ini is http://git.slaventius.ru/ but you are visiting http://37.143.12.169/test3k/authDB/blame/commit/6814a13a9bc51b9e9428d89cac16963b9ff580aa/internal/transport/arango/db.go You should set ROOT_URL correctly, otherwise the web may not work correctly.

78 lines
1.5 KiB

2 years ago
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
}