dendrite/internal/cosmosdbapi/client.go
alexfca acf63daf79
Add CT and UT to all documents and refactor (#17)
* - Create CosmosDocument as a base class
- Add CT and UT
- Refactor all tables to use the CosmosDocument

* - Add UpsertDocument method to perform updates in a generic way
- Add SetUpdateTime() to update the UT for updates
- Refactor it all

Co-authored-by: alexf@example.com <alexf@example.com>
2021-09-20 17:41:04 +10:00

88 lines
1.7 KiB
Go

package cosmosdbapi
import (
"context"
"time"
cosmosapi "github.com/vippsas/go-cosmosdb/cosmosapi"
)
type CosmosConnection struct {
Url string
Key string
}
func GetCosmosConnection(accountEndpoint string, accountKey string) CosmosConnection {
return CosmosConnection{
Url: accountEndpoint,
Key: accountKey,
}
}
func GetClient(conn CosmosConnection) *cosmosapi.Client {
cfg := cosmosapi.Config{
MasterKey: conn.Key,
}
return cosmosapi.New(conn.Url, cfg, nil, nil)
}
func UpsertDocument(ctx context.Context,
conn CosmosConnection,
databaseName string,
containerName string,
partitonKey string,
dbData interface{}) error {
var options = getUpsertDocumentOptions(partitonKey)
_, _, err := GetClient(conn).CreateDocument(
ctx,
databaseName,
containerName,
&dbData,
options)
return err
}
func (doc *CosmosDocument) SetUpdateTime() {
now := time.Now().UTC()
doc.Ut = now.Format(time.RFC3339)
}
func GenerateDocument(
collection string,
tenantName string,
partitionKey string,
docId string,
) CosmosDocument {
doc := CosmosDocument{}
now := time.Now().UTC()
doc.Timestamp = now.Unix()
doc.Ct = now.Format(time.RFC3339)
doc.Ut = now.Format(time.RFC3339)
doc.Cn = collection
doc.Tn = tenantName
doc.Pk = partitionKey
doc.Id = docId
return doc
}
func GetDocumentOrNil(connection CosmosConnection, config CosmosConfig, ctx context.Context, partitionKey string, cosmosDocId string, dbData interface{}) error {
var _, err = GetClient(connection).GetDocument(
ctx,
config.DatabaseName,
config.ContainerName,
cosmosDocId,
GetGetDocumentOptions(partitionKey),
&dbData,
)
if err != nil {
if err.Error() == "Resource that no longer exists" {
dbData = nil
return nil
}
return err
}
return nil
}