mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-21 01:40:49 +00:00
Added Documents CURD
This commit is contained in:
66
internal/constants/responses.go
Normal file
66
internal/constants/responses.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package constants
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
var ServerInfoResponse = gin.H{
|
||||
"_self": "",
|
||||
"id": config.Config.DatabaseAccount,
|
||||
"_rid": fmt.Sprintf("%s.%s", config.Config.DatabaseAccount, config.Config.DatabaseDomain),
|
||||
"media": "//media/",
|
||||
"addresses": "//addresses/",
|
||||
"_dbs": "//dbs/",
|
||||
"writableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"readableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
"userReplicationPolicy": map[string]interface{}{
|
||||
"asyncReplication": false,
|
||||
"minReplicaSetSize": 1,
|
||||
"maxReplicasetSize": 4,
|
||||
},
|
||||
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
|
||||
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
|
||||
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
|
||||
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
|
||||
}
|
||||
|
||||
var QueryPlanResponse = gin.H{
|
||||
"partitionedQueryExecutionInfoVersion": 2,
|
||||
"queryInfo": map[string]interface{}{
|
||||
"distinctType": "None",
|
||||
"top": nil,
|
||||
"offset": nil,
|
||||
"limit": nil,
|
||||
"orderBy": []interface{}{},
|
||||
"orderByExpressions": []interface{}{},
|
||||
"groupByExpressions": []interface{}{},
|
||||
"groupByAliases": []interface{}{},
|
||||
"aggregates": []interface{}{},
|
||||
"groupByAliasToAggregateType": map[string]interface{}{},
|
||||
"rewrittenQuery": "",
|
||||
"hasSelectValue": false,
|
||||
"dCountInfo": nil,
|
||||
},
|
||||
"queryRanges": []interface{}{
|
||||
map[string]interface{}{
|
||||
"min": "",
|
||||
"max": "FF",
|
||||
"isMinInclusive": true,
|
||||
"isMaxInclusive": false,
|
||||
},
|
||||
},
|
||||
}
|
||||
90
internal/repositories/documents.go
Normal file
90
internal/repositories/documents.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package repositories
|
||||
|
||||
import "strings"
|
||||
|
||||
var documents = []Document{}
|
||||
|
||||
func GetAllDocuments(databaseId string, collectionId string) ([]Document, RepositoryStatus) {
|
||||
filteredDocuments := make([]Document, 0)
|
||||
|
||||
for _, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
docCollId := doc["_internal"].(map[string]interface{})["collectionId"]
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId {
|
||||
doc["_partitionKeyValue"] = doc["_internal"].(map[string]interface{})["partitionKeyValue"]
|
||||
filteredDocuments = append(filteredDocuments, doc)
|
||||
}
|
||||
}
|
||||
|
||||
return filteredDocuments, StatusOk
|
||||
}
|
||||
|
||||
func GetDocument(databaseId string, collectionId string, documentId string) (Document, RepositoryStatus) {
|
||||
for _, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
docCollId := doc["_internal"].(map[string]interface{})["collectionId"]
|
||||
docId := doc["id"]
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId && docId == documentId {
|
||||
doc["_partitionKeyValue"] = doc["_internal"].(map[string]interface{})["partitionKeyValue"]
|
||||
return doc, StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return Document{}, StatusNotFound
|
||||
}
|
||||
|
||||
func DeleteDocument(databaseId string, collectionId string, documentId string) RepositoryStatus {
|
||||
for index, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
docCollId := doc["_internal"].(map[string]interface{})["collectionId"]
|
||||
docId := doc["id"]
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId && docId == documentId {
|
||||
documents = append(documents[:index], documents[index+1:]...)
|
||||
return StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return StatusNotFound
|
||||
}
|
||||
|
||||
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) RepositoryStatus {
|
||||
if document["id"] == "" {
|
||||
return BadRequest
|
||||
}
|
||||
|
||||
collection, status := GetCollection(databaseId, collectionId)
|
||||
if status != StatusOk {
|
||||
return StatusNotFound
|
||||
}
|
||||
|
||||
for _, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
docCollId := doc["_internal"].(map[string]interface{})["collectionId"]
|
||||
docId := doc["id"]
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId && docId == document["id"] {
|
||||
return Conflict
|
||||
}
|
||||
}
|
||||
|
||||
partitionKeyValue := make([]string, 0)
|
||||
for _, path := range collection.PartitionKey.Paths {
|
||||
var val interface{}
|
||||
for _, part := range strings.Split(path, "/") {
|
||||
val = document[part]
|
||||
}
|
||||
partitionKeyValue = append(partitionKeyValue, val.(string))
|
||||
}
|
||||
|
||||
document["_internal"] = map[string]interface{}{
|
||||
"databaseId": databaseId,
|
||||
"collectionId": collectionId,
|
||||
"partitionKeyValue": partitionKeyValue,
|
||||
}
|
||||
documents = append(documents, document)
|
||||
|
||||
return StatusOk
|
||||
}
|
||||
@@ -10,6 +10,7 @@ const (
|
||||
StatusOk = 1
|
||||
StatusNotFound = 2
|
||||
Conflict = 3
|
||||
BadRequest = 4
|
||||
)
|
||||
|
||||
type Collection struct {
|
||||
@@ -86,3 +87,20 @@ type Trigger struct {
|
||||
collectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type Document map[string]interface{}
|
||||
|
||||
type PartitionKeyRange struct {
|
||||
Rid string `json:"_rid"`
|
||||
ID string `json:"id"`
|
||||
Etag string `json:"_etag"`
|
||||
MinInclusive string `json:"minInclusive"`
|
||||
MaxExclusive string `json:"maxExclusive"`
|
||||
RidPrefix int `json:"ridPrefix"`
|
||||
Self string `json:"_self"`
|
||||
ThroughputFraction int `json:"throughputFraction"`
|
||||
Status string `json:"status"`
|
||||
Parents []any `json:"parents"`
|
||||
Ts int `json:"_ts"`
|
||||
Lsn int `json:"lsn"`
|
||||
}
|
||||
|
||||
21
internal/repositories/partition_key_ranges.go
Normal file
21
internal/repositories/partition_key_ranges.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]PartitionKeyRange, RepositoryStatus) {
|
||||
// I have no idea what this is tbh
|
||||
return []PartitionKeyRange{
|
||||
{
|
||||
Rid: "ZxlyAP7rKwACAAAAAAAAUA==",
|
||||
ID: "0",
|
||||
Etag: "\"00005504-0000-0100-0000-65c555490000\"",
|
||||
MinInclusive: "",
|
||||
MaxExclusive: "FF",
|
||||
RidPrefix: 0,
|
||||
Self: "dbs/ZxlyAA==/colls/ZxlyAP7rKwA=/pkranges/ZxlyAP7rKwACAAAAAAAAUA==/",
|
||||
ThroughputFraction: 1,
|
||||
Status: "online",
|
||||
Parents: []interface{}{},
|
||||
Ts: 1707431241,
|
||||
Lsn: 17,
|
||||
},
|
||||
}, StatusOk
|
||||
}
|
||||
@@ -3,13 +3,13 @@ package repositories
|
||||
var triggers = []Trigger{}
|
||||
|
||||
func GetAllTriggers(databaseId string, collectionId string) ([]Trigger, RepositoryStatus) {
|
||||
sps := make([]Trigger, 0)
|
||||
filteredTriggers := make([]Trigger, 0)
|
||||
|
||||
for _, coll := range triggers {
|
||||
if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId {
|
||||
sps = append(sps, coll)
|
||||
filteredTriggers = append(filteredTriggers, coll)
|
||||
}
|
||||
}
|
||||
|
||||
return sps, StatusOk
|
||||
return filteredTriggers, StatusOk
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user