Skip to content

Commit beae4f9

Browse files
committed
feat:create and list indexes
1 parent afdc3e7 commit beae4f9

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ updateIPaddressAttribute: $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/
111111
updateStringAttribute: $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/updateStringAttribute.cpp
112112
$(CXX) $(CXXFLAGS) -o tests/attribute/updateStringAttribute $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/updateStringAttribute.cpp $(LDFLAGS)
113113

114+
# Collection-Indexes
115+
listIndexes: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/listIndexes.cpp
116+
$(CXX) $(CXXFLAGS) -o tests/indexes/listIndexes $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/listIndexes.cpp $(LDFLAGS)
117+
createIndex: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/createIndex.cpp
118+
$(CXX) $(CXXFLAGS) -o tests/indexes/createIndex $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/createIndex.cpp $(LDFLAGS)
119+
120+
114121

115122
# Storage
116123
createBucket: $(SRCS) $(EXAMPLES_DIR)/storage/createBucket.cpp
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
8+
Appwrite appwrite(projectId);
9+
Databases& database = appwrite.getDatabases();
10+
11+
std::string databaseId = "database123";
12+
std::string collectionId = "test1234";
13+
std::string key = "index_from_cpp";
14+
15+
// we need to use type as "key" for arrayed attributes
16+
std::string type = "key";
17+
std::vector<std::string> attributes = {"new_enum_attribute"};
18+
19+
database.setup(apiKey, projectId);
20+
21+
try {
22+
std::string response = database.createIndex(
23+
databaseId, collectionId,key, type, attributes
24+
);
25+
std::cout << "Index created successfully! \nResponse: " << response << std::endl;
26+
} catch (const AppwriteException& ex) {
27+
std::cerr << "Exception: " << ex.what() << std::endl;
28+
}
29+
30+
return 0;
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
std::string databaseId = "database123";
8+
std::string collectionId = "test1234";
9+
10+
Appwrite appwrite(projectId);
11+
Databases& databases = appwrite.getDatabases();
12+
13+
databases.setup(apiKey, projectId);
14+
15+
try {
16+
std::string response = databases.listIndexes(databaseId, collectionId);
17+
std::cout << "Indexes listed successfully! \nResponse: " << response << std::endl;
18+
} catch (const AppwriteException& ex) {
19+
std::cerr << "Exception: " << ex.what() << std::endl;
20+
}
21+
22+
return 0;
23+
}

include/classes/Databases.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class Databases {
5050
std::string deleteDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId);
5151
std::string getDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId);
5252

53+
//indexes
54+
std::string listIndexes(const std::string& databaseId, const std::string& collectionId);
55+
std::string createIndex(const std::string& databaseId, const std::string& collectionId, const std::string& key, const std::string& type, const std::vector<std::string> &attributes);
56+
57+
5358
private:
5459
std::string apiKey;
5560
std::string projectId;

include/enums/HttpStatus.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enum HttpStatus {
55
OK = 200,
66
CREATED = 201,
77
ATTRIBUTE_CREATED = 202,
8+
INDEX_CREATED = 202,
89
DELETED = 204,
910
BAD_REQUEST = 400,
1011
UNAUTHORIZED = 401,

src/services/Databases.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,55 @@ std::string Databases::getDocument(const std::string& databaseId, const std::str
700700
throw AppwriteException("Error fetching document. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
701701
}
702702

703+
}
704+
705+
std::string Databases::listIndexes(const std::string& databaseId, const std::string& collectionId){
706+
Validator::validateDatabaseParams(databaseId, collectionId);
707+
708+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/indexes";
709+
710+
std::vector<std::string> headers = Config::getHeaders(projectId);
711+
headers.push_back("X-Appwrite-Key: " + apiKey);
712+
713+
std::string response;
714+
int statusCode = Utils::getRequest(url, headers, response);
715+
716+
if (statusCode == HttpStatus::OK) {
717+
return response;
718+
} else {
719+
throw AppwriteException("Error fetching indexes. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
720+
}
721+
}
722+
723+
std::string Databases::createIndex(const std::string& databaseId, const std::string& collectionId, const std::string& key, const std::string& type, const std::vector<std::string> &attributes){
724+
if (databaseId.empty()) {
725+
throw AppwriteException("Missing required parameter: 'databaseId'");
726+
}
727+
if (collectionId.empty()) {
728+
throw AppwriteException("Missing required parameter: 'collectionId'");
729+
}
730+
731+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/indexes";
732+
733+
json payloadJson = {
734+
{"databaseId", databaseId},
735+
{"collectionId", collectionId},
736+
{"key", key},
737+
{"type", type},
738+
{"attributes", attributes}
739+
};
740+
741+
std::string payload = payloadJson.dump();
742+
std::vector<std::string> headers = Config::getHeaders(projectId);
743+
headers.push_back("X-Appwrite-Key: " + apiKey);
744+
745+
std::string response;
746+
747+
int statusCode = Utils::postRequest(url, payload, headers, response);
748+
749+
if (statusCode == HttpStatus::INDEX_CREATED) {
750+
return response;
751+
} else {
752+
throw AppwriteException("Error creating index. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
753+
}
703754
}

tests/indexes/createIndex

1.27 MB
Binary file not shown.

tests/indexes/listIndexes

1.25 MB
Binary file not shown.

0 commit comments

Comments
 (0)