Skip to content

Commit

Permalink
feat: Support user specified collection names
Browse files Browse the repository at this point in the history
This allows to support multiple collections using the same model type.
  • Loading branch information
efirs committed May 17, 2023
1 parent 36103f0 commit b2a3ff3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
44 changes: 38 additions & 6 deletions tigris/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import (
// top level GetTxCollection(ctx, tx) function should be used
// instead of method of Tx interface.
type Database struct {
name string
driver driver.Driver
name string
}

func newDatabase(name string, driver driver.Driver) *Database {
func newDatabase(name string, drv driver.Driver) *Database {
return &Database{
name: name,
driver: driver,
driver: drv,
}
}

Expand All @@ -55,10 +55,34 @@ func (db *Database) CreateCollections(ctx context.Context, model schema.Model, m
return db.createCollectionsFromSchemas(ctx, schemas)
}

// CreateCollection creates collection in the Database using provided collection model and optional name.
// This method is only needed if collection need to be created dynamically,
// all static collections are created by OpenDatabase.
func (db *Database) CreateCollection(ctx context.Context, model schema.Model, name ...string) error {
schemas, err := schema.FromCollectionModels(SchemaVersion, schema.Documents, model)
if err != nil {
return fmt.Errorf("error parsing model schema: %w", err)
}

if len(name) > 1 {
return fmt.Errorf("only one name parameter allowed")
}

if len(name) == 1 {
// there only one schema
for _, v := range schemas {
v.Name = name[0]
}
}

return db.createCollectionsFromSchemas(ctx, schemas)
}

func (db *Database) createCollectionsFromSchemasLow(ctx context.Context, tx driver.Tx, inSchemas map[string]*schema.Schema) error {
schemas := make([]driver.Schema, len(inSchemas))

var i int

for _, v := range inSchemas {
sch, err := schema.Build(v)
if err != nil {
Expand All @@ -70,11 +94,13 @@ func (db *Database) createCollectionsFromSchemasLow(ctx context.Context, tx driv
}

var err error

if tx != nil {
_, err = tx.CreateOrUpdateCollections(ctx, schemas)
} else {
_, err = db.driver.UseDatabase(db.name).CreateOrUpdateCollections(ctx, schemas)
}

if err != nil {
return err
}
Expand Down Expand Up @@ -171,10 +197,16 @@ func MustOpenDatabase(ctx context.Context, cfg *Config, models ...schema.Model,
}

// GetCollection returns collection object corresponding to collection model T.
func GetCollection[T schema.Model](db *Database) *Collection[T] {
func GetCollection[T schema.Model](db *Database, name ...string) *Collection[T] {
var m T
name := schema.ModelName(&m)
return getNamedCollection[T](db, name)

nm := schema.ModelName(&m)

if len(name) > 0 {
nm = name[0]
}

return getNamedCollection[T](db, nm)
}

func getNamedCollection[T schema.Model](db *Database, name string) *Collection[T] {
Expand Down
36 changes: 36 additions & 0 deletions tigris/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,40 @@ func TestDatabase(t *testing.T) {
require.Nil(t, resp)
require.Equal(t, &driver.Error{TigrisError: &api.TigrisError{Code: api.Code_NOT_FOUND, Message: "branch does not exist"}}, err1)
})

t.Run("create collection", func(t *testing.T) {
mc.EXPECT().CreateOrUpdateCollections(gomock.Any(),
pm(&api.CreateOrUpdateCollectionsRequest{
Project: "db1",
Branch: "staging",
Schemas: [][]byte{[]byte(`{"title":"coll_1","properties":{"Key1":{"type":"string"}},"primary_key":["Key1"],"collection_type":"documents"}`)},
Options: &api.CollectionOptions{},
})).Do(func(ctx context.Context, r *api.CreateOrUpdateCollectionsRequest) {
}).Return(&api.CreateOrUpdateCollectionsResponse{}, nil)

type Coll1 struct {
Key1 string `tigris:"primary_key:1"`
}

err1 := client.GetDatabase().CreateCollection(ctx, &Coll1{})
require.NoError(t, err1)

mc.EXPECT().CreateOrUpdateCollections(gomock.Any(),
pm(&api.CreateOrUpdateCollectionsRequest{
Project: "db1",
Branch: "staging",
Schemas: [][]byte{[]byte(`{"title":"other_coll_1","properties":{"Key1":{"type":"string"}},"primary_key":["Key1"],"collection_type":"documents"}`)},
Options: &api.CollectionOptions{},
})).Do(func(ctx context.Context, r *api.CreateOrUpdateCollectionsRequest) {
}).Return(&api.CreateOrUpdateCollectionsResponse{}, nil)

err1 = client.GetDatabase().CreateCollection(ctx, &Coll1{}, "other_coll_1")
require.NoError(t, err1)

err1 = client.GetDatabase().CreateCollection(ctx, &Coll1{}, "other_coll_1", "multi_parameter_not_allowed")
require.Error(t, err1)

coll := GetCollection[Coll1](client.GetDatabase(), "other_coll_1")
require.Equal(t, "other_coll_1", coll.name)
})
}

0 comments on commit b2a3ff3

Please sign in to comment.