Skip to content

Commit

Permalink
added schema primarykey while creating collection
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed Jul 30, 2023
1 parent 47240b6 commit 8ee3530
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
52 changes: 48 additions & 4 deletions internal/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package collections
import (
"github.com/TanmoySG/wunderDB/internal/metadata"
"github.com/TanmoySG/wunderDB/model"
wdbErrors "github.com/TanmoySG/wunderDB/pkg/wdb/errors"
)

const (
collectionExists = true
collectionDoesNotExist = false

schemaFieldRequired = "required"
schemaFieldProperties = "properties"

defaultPrimaryKeyField = "recordId"
)

type Collections map[model.Identifier]*model.Collection
Expand All @@ -25,12 +31,21 @@ func (c Collections) CheckIfExists(collectionID model.Identifier) (bool, *model.
}
}

func (c Collections) CreateCollection(collectionID model.Identifier, schema model.Schema) {
func (c Collections) CreateCollection(collectionID model.Identifier, schema model.Schema, primaryKey *model.Identifier) *wdbErrors.WdbError {
// get primary key field value
primaryKeyField, err := c.getPrimaryKey(primaryKey, schema)
if err != nil {
return err
}

c[collectionID] = &model.Collection{
Data: map[model.Identifier]*model.Datum{},
Schema: schema,
Metadata: metadata.New().BasicChangeMetadata(),
Data: map[model.Identifier]*model.Datum{},
Schema: schema,
Metadata: metadata.New().BasicChangeMetadata(),
PrimaryKey: &primaryKeyField,
}

return nil
}

func (c Collections) GetCollection(collectionID model.Identifier) *model.Collection {
Expand All @@ -44,3 +59,32 @@ func (c Collections) DeleteCollection(collectionID model.Identifier) {
func (c Collections) UpdateMetadata(collectionID model.Identifier) {
c[collectionID].Metadata = metadata.Use(c[collectionID].Metadata).BasicChangeMetadata()
}

func (Collections) getPrimaryKey(primaryKey *model.Identifier, schema model.Schema) (model.Identifier, *wdbErrors.WdbError) {
// default primary key field is "recordId"
var primaryKeyField model.Identifier = model.Identifier(defaultPrimaryKeyField)

if primaryKey != nil && schema[schemaFieldProperties] != nil && schema[schemaFieldRequired] != nil {
// check if primary key is in "properties" field of JSON Schema
_, pkeyExistsInProperties := schema[schemaFieldProperties].(map[string]interface{})[primaryKey.String()]

// check if primary key is in "required" field of JSON Schema
pkeyIsRequired := false
for _, requiredField := range schema[schemaFieldRequired].([]interface{}) {
if primaryKey.String() == requiredField.(string) {
pkeyIsRequired = true
}
}

// if primary key not in "properties" or "required"
// then return primaryKey schema mismatch error
if !pkeyExistsInProperties || !pkeyIsRequired {
return "", &wdbErrors.PrimaryKeyNotInSchemaError
}

// set primaryKeyField as primary key mentioned
primaryKeyField = model.Identifier(primaryKey.String())
}

return primaryKeyField, nil
}
2 changes: 1 addition & 1 deletion internal/collections/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_CreateCollection(t *testing.T) {
database := model.Database{Collections: testCollection}

collections := UseDatabase(&database)
collections.CreateCollection(testCollection2Name, model.Schema{})
collections.CreateCollection(testCollection2Name, model.Schema{}, nil)

expectedCollectionsMap := testCollection
expectedCollectionsMap[testCollection2Name] = testCollection2
Expand Down

0 comments on commit 8ee3530

Please sign in to comment.