Skip to content

Commit

Permalink
Merge pull request #240 from ThisaruGuruge/connector-revamp
Browse files Browse the repository at this point in the history
Apply suggestions from code review
  • Loading branch information
ThisaruGuruge authored Apr 1, 2024
2 parents dcb920a + 9c1b4fc commit a89c1bf
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 174 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ To use the MongoDB connector, you need to have a MongoDB server running and acce

### Setup a MongoDB server locally

#### Step 1: Install and run the MongoDB server

1. Download and install the MongoDB server from the [MongoDB download center](https://www.mongodb.com/try/download/community).

2. Follow the installation instructions provided in the download center.
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.5"
distribution-version = "2201.8.6"

[[package]]
org = "ballerina"
Expand Down
2 changes: 0 additions & 2 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ To use the MongoDB connector, you need to have a MongoDB server running and acce

### Setup a MongoDB server locally

#### Step 1: Install and run the MongoDB server

1. Download and install the MongoDB server from the [MongoDB download center](https://www.mongodb.com/try/download/community).

2. Follow the installation instructions provided in the download center.
Expand Down
2 changes: 0 additions & 2 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ To use the MongoDB connector, you need to have a MongoDB server running and acce

### Setup a MongoDB server locally

#### Step 1: Install and run the MongoDB server

1. Download and install the MongoDB server from the [MongoDB download center](https://www.mongodb.com/try/download/community).

2. Follow the installation instructions provided in the download center.
Expand Down
2 changes: 2 additions & 0 deletions ballerina/client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public isolated client class Client {

# Closes the client.
#
# > **Note:** Use a single client instance for the lifetime of the application and close it when the application is done.
#
# + return - A `mongodb:Error` if the client is already closed or failed to close the client. `()` otherwise.
@display {label: "Close the Client"}
remote isolated function close() returns Error? = @java:Method {
Expand Down
12 changes: 10 additions & 2 deletions ballerina/collection.bal
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ public isolated client class Collection {

# Finds documents from the collection.
#
# > **Note:** Close the resulted stream once the operation is completed.
#
# + filter - The query filter to apply when retrieving documents
# + findOptions - The additional options to apply to the find operation
# + projection - The projection to apply to the find operation. If not provided, the projection will be generated
# based on the targetType
# + targetType - The type of the returned documents
# + return - A stream of documents which match the provided filter, or an error if the operation failed
isolated remote function find(map<json> filter = {}, FindOptions findOptions = {}, map<json>? projection = (),
typedesc<anydata> targetType = <>) returns stream<targetType, error?>|Error = @java:Method {
typedesc<record {|anydata...;|}> targetType = <>) returns stream<targetType, error?>|Error = @java:Method {
'class: "io.ballerina.lib.mongodb.Collection"
} external;

Expand All @@ -78,7 +80,7 @@ public isolated client class Collection {
# + targetType - The type of the returned document
# + return - The document which matches the provided filter, or an error if the operation failed
isolated remote function findOne(map<json> filter = {}, FindOptions findOptions = {}, map<json>? projection = (),
typedesc<anydata> targetType = <>) returns targetType|Error = @java:Method {
typedesc<record {|anydata...;|}> targetType = <>) returns targetType|Error? = @java:Method {
'class: "io.ballerina.lib.mongodb.Collection"
} external;

Expand All @@ -104,6 +106,8 @@ public isolated client class Collection {

# Lists the indexes of the collection.
#
# > **Note:** Close the resulted stream once the operation is completed.
#
# + return - A stream of indexes, or an error if the operation failed
isolated remote function listIndexes() returns stream<Index, error?>|Error =
@java:Method {
Expand Down Expand Up @@ -156,6 +160,8 @@ public isolated client class Collection {

# Retrieves the distinct values for a specified field across a collection.
#
# > **Note:** Close the resulted stream once the operation is completed.
#
# + fieldName - The field for which to return distinct values
# + filter - The query filter to apply when retrieving distinct values
# + targetType - The type of the returned distinct values
Expand Down Expand Up @@ -185,6 +191,8 @@ public isolated client class Collection {

# Aggregates documents according to the specified aggregation pipeline.
#
# > **Note:** Close the resulted stream once the operation is completed.
#
# + pipeline - The aggregation pipeline
# + targetType - The type of the returned documents
# + return - A stream of documents which match the provided pipeline, or an error if the operation failed
Expand Down
4 changes: 3 additions & 1 deletion ballerina/tests/03_collection_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ isolated function testInsertAndFind() returns error? {
isolated function testFindOne() returns error? {
Database database = check mongoClient->getDatabase("testFindOneDB");
Collection collection = check database->getCollection("Movies");
Movie? actualResult = check collection->findOne();
test:assertEquals(actualResult, (), "Expected an empty result");
Movie movie = {name: "Interstellar", year: 2014, rating: 9};
check collection->insertOne(movie);
Movie? actualResult = check collection->findOne();
actualResult = check collection->findOne();
test:assertEquals(actualResult, movie);
check collection->drop();
check database->drop();
Expand Down
Loading

0 comments on commit a89c1bf

Please sign in to comment.