Skip to content

Commit

Permalink
Merge pull request #93 from sachinira/bugfix-replace
Browse files Browse the repository at this point in the history
Add support to replace id field in a document
  • Loading branch information
sachinira authored May 31, 2023
2 parents 104c811 + 78e220f commit 30b5420
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
28 changes: 16 additions & 12 deletions cosmosdb/client_endpoint.bal
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public isolated client class DataPlaneClient {
#
# + databaseId - ID of the database to which the container belongs to
# + containerId - ID of the container which contains the existing document
# + documemtId - ID of the document
# + documentId - Current ID of the document
# + document - A JSON document which will replace the existing document
# + partitionKey - The specific value related to the partition key field of the container
# + requestOptions - The `cosmos_db:RequestOptions` which can be used to add additional capabilities that can
Expand All @@ -72,14 +72,18 @@ public isolated client class DataPlaneClient {
@display {label: "Replace Document"}
remote isolated function replaceDocument(@display {label: "Database ID"} string databaseId,
@display {label: "Container ID"} string containerId,
@display {label: "Document ID"} string documemtId,
@display {label: "Document ID"} string documentId,
@display {label: "New Document"} map<json> document,
@display {label: "Partition Key"} int|float|decimal|string partitionKey,
@display {label: "Optional Header Parameters"} RequestOptions?
requestOptions = ()) returns DocumentResponse|error {
json jsonDocument = check document.cloneWithType(json);
json updatedDocument = check jsonDocument.mergeJson({"id": documemtId});
return replaceDocument(databaseId, containerId, documemtId, <map<json>>updatedDocument, partitionKey,
json updatedDocument = check document.cloneWithType(json);
// If the doument doesn't contain an id, the document will not replace the id in the database.
// If the document contains an id, it will replace the existing id in the database.
if !document.hasKey("id") {
updatedDocument = check updatedDocument.mergeJson({"id": documentId});
}
return replaceDocument(databaseId, containerId, documentId, <map<json>>updatedDocument, partitionKey,
requestOptions);
}

Expand Down Expand Up @@ -156,7 +160,7 @@ public isolated client class DataPlaneClient {
@display {label: "Container ID"} string containerId,
@display {label: "SQL Query"} string sqlQuery,
@display {label: "Optional Header Parameters"} QueryOptions? queryOptions
= (), typedesc<record {}> returnType = <>)
= (), typedesc<record {}> returnType = <>)
returns stream<returnType, error?>|error = @java:Method {
'class: "io.ballerinax.cosmosdb.DataplaneClient"
} external;
Expand All @@ -168,13 +172,13 @@ public isolated client class DataPlaneClient {
# + storedProcedureId - A unique ID for the newly created stored procedure
# + storedProcedure - A JavaScript function represented as a string
# + options - The `cosmos_db:CosmosStoredProcedureRequestOptions` which can be used to add additional capabilities
# that can override client configuration provided in the inilization
# that can override client configuration provided in the inilization
# + return - If successful, returns a `cosmos_db:StoredProcedure`. Else returns error.
@display {label: "Create Stored Procedure"}
remote isolated function createStoredProcedure(@display {label: "Database ID"} string databaseId,
@display {label: "Container ID"} string containerId,
@display {label: "Stored Procedure ID"} string storedProcedureId,
@display {label: "Stored Procedure Function"} string
@display {label: "Stored Procedure Function"} string
storedProcedure, CosmosStoredProcedureRequestOptions? options = ())
returns StoredProcedureResponse|error {
return createStoredProcedure(databaseId, containerId, storedProcedureId, storedProcedure, options);
Expand Down Expand Up @@ -220,7 +224,7 @@ public isolated client class DataPlaneClient {
remote isolated function executeStoredProcedure(@display {label: "Database ID"} string databaseId,
@display {label: "Container ID"} string containerId,
@display {label: "Stored Procedure ID"} string storedProcedureId,
@display {label: "Partition Key"} int|float|decimal|string
@display {label: "Partition Key"} int|float|decimal|string
patitionKey, @display {label: "Execution Options"}
StoredProcedureExecuteOptions? storedProcedureExecuteOptions = ())
returns StoredProcedureResponse|error = @java:Method {
Expand All @@ -239,21 +243,21 @@ customConfig = ()) returns error? = @java:Method {
} external;

isolated function createDocument(string databaseId, string containerId,
map<json> document, int|float|decimal|string partitionKey, RequestOptions?
map<json> document, int|float|decimal|string partitionKey, RequestOptions?
documentCreateOptions = ()) returns DocumentResponse|error = @java:Method {
'class: "io.ballerinax.cosmosdb.DataplaneClient"
} external;

isolated function replaceDocument(string databaseId, string containerId, string id,
map<json> document, int|float|decimal|string partitionKey,
RequestOptions? documentCreateOptions = ()) returns DocumentResponse|error =
RequestOptions? documentCreateOptions = ()) returns DocumentResponse|error =
@java:Method {
'class: "io.ballerinax.cosmosdb.DataplaneClient"
} external;

isolated function createStoredProcedure(string databaseId, string containerId,
string storedProcedureId, string storedProcedure,
CosmosStoredProcedureRequestOptions? options = ()) returns
CosmosStoredProcedureRequestOptions? options = ()) returns
StoredProcedureResponse|error = @java:Method {
'class: "io.ballerinax.cosmosdb.DataplaneClient"
} external;
64 changes: 58 additions & 6 deletions cosmosdb/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ function testCreateDocument() returns error? {
testGetOneDocumentWithRequestOptions,
testQueryDocuments,
testGetDocumentList,
testGetDocumentListWithRequestOptions]
testGetDocumentListWithRequestOptions
]
}
function testReplaceDocument() returns error? {
log:printInfo("ACTION : replaceDocument()");
Expand All @@ -413,6 +414,58 @@ function testReplaceDocument() returns error? {
test:assertEquals(response.statusCode, 200);
}

@test:Config {
groups: ["document"],
dependsOn: [testCreateContainer]
}
function testReplaceDocumentID() returns error? {
log:printInfo("ACTION : replaceDocumentID()");

int valueOfPartitionKey = 1234;
string documentId = string `document_${randomString.toString()}`;
map<json> documentBody = {
"LastName": "Einstein",
"Parents": [
{
"FamilyName": null,
"FirstName": "Hermann"
},
{
"FamilyName": null,
"FirstName": "Pauline"
}
],
"Children": [
{
"FamilyName": null,
"FirstName": "Hans",
"Gender": "male",
"Grade": 5,
"Pets": [{"GivenName": "Pumpkin"}]
}
],
"Address": {
"State": "WA",
"Country": "King",
"City": "Seattle"
},
"IsRegistered": true,
"AccountNumber": 1234
};

DocumentResponse createResponse = check azureCosmosClient->createDocument(databaseId, containerId, documentId, documentBody, valueOfPartitionKey);
test:assertEquals(createResponse.statusCode, 201);

map<json> updatedDocumentBody = {
"LastName": "Antony",
"AccountNumber": 1234,
"id": "new_document_123"
};

DocumentResponse updateResponse = check azureCosmosClient->replaceDocument(databaseId, containerId, documentId, updatedDocumentBody, valueOfPartitionKey);
test:assertEquals(updateResponse.statusCode, 200);
}

@test:Config {
groups: ["document"],
dependsOn: [testCreateContainer]
Expand Down Expand Up @@ -478,15 +531,14 @@ public type Pet record {
string GivenName;
};


@test:Config {
groups: ["document"],
dependsOn: [testCreateDocument, testCreateDocumentWithRequestOptions]
}
function testGetDocumentList() returns error? {
log:printInfo("ACTION : getDocumentList()");
int valueOfPartitionKey = 1234;
stream<Person, error?> result = check azureCosmosClient->getDocumentList(databaseId, containerId,
stream<Person, error?> result = check azureCosmosClient->getDocumentList(databaseId, containerId,
valueOfPartitionKey);
check result.forEach(isolated function(Person queryResult) {
test:assertEquals(1234, queryResult.AccountNumber);
Expand All @@ -503,7 +555,7 @@ function testGetDocumentListWithRequestOptions() returns error? {
QueryOptions options = {
consistencyLevel: EVENTUAL
};
stream<Person, error?> result = check azureCosmosClient->getDocumentList(databaseId, containerId,
stream<Person, error?> result = check azureCosmosClient->getDocumentList(databaseId, containerId,
valueOfPartitionKey, options);
check result.forEach(isolated function(Person queryResult) {
test:assertEquals(1234, queryResult.AccountNumber);
Expand Down Expand Up @@ -609,7 +661,7 @@ function testCreateStoredProcedure() returns error? {
response.setBody("Hello, World");
}`;

StoredProcedureResponse response = check azureCosmosClient->createStoredProcedure(databaseId, containerId, sprocId,
StoredProcedureResponse response = check azureCosmosClient->createStoredProcedure(databaseId, containerId, sprocId,
createSprocBody);
test:assertEquals(response.statusCode, 201);
}
Expand All @@ -627,7 +679,7 @@ function testExecuteOneStoredProcedure() returns error? {
int partitionKey = 1234;

_ = check azureCosmosClient->executeStoredProcedure(databaseId, containerId, sprocId, partitionKey, options);

}

@test:Config {
Expand Down

0 comments on commit 30b5420

Please sign in to comment.