dendrite/internal/cosmosdbapi/document.go
alexfca 3ca96b13b3
- Implement the SycAPI to use CosmosDB (#8)
- Update the Config to use Cosmos for the sync API
- Ensure Cosmos DocId does not contain escape chars
- Create a shared Cosmos PartitionOffet table and refactor to use it
- Hardcode the "nafka" Connstring to use the "file:naffka.db"
- Create seq documents for each of the nextXXXID methods
2021-05-27 18:45:53 +10:00

49 lines
1.2 KiB
Go

package cosmosdbapi
import (
"context"
"fmt"
"strings"
)
func removeSpecialChars(docId string) string {
// The following characters are restricted and cannot be used in the Id property: '/', '\', '?', '#'
invalidChars := [4]string{"/", "\\", "?", "#"}
replaceChar := ","
result := docId
for _, invalidChar := range invalidChars {
result = strings.ReplaceAll(result, invalidChar, replaceChar)
}
return result
}
func GetDocumentId(tenantName string, collectionName string, id string) string {
safeId := removeSpecialChars(id)
return fmt.Sprintf("%s,%s,%s", collectionName, tenantName, safeId)
}
func GetPartitionKey(tenantName string, collectionName string) string {
return fmt.Sprintf("%s,%s", collectionName, tenantName)
}
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
}