mirror of
https://github.com/typesense/typesense.git
synced 2025-05-21 14:12:27 +08:00
Support single document upsert.
This commit is contained in:
parent
bccc3ac362
commit
a692553909
@ -199,11 +199,12 @@ struct StringUtils {
|
||||
return (*p == 0) && val >= std::numeric_limits<int32_t>::min() && val <= std::numeric_limits<int32_t>::max();
|
||||
}
|
||||
|
||||
static bool is_bool(const std::string &s) {
|
||||
static bool is_bool(std::string &s) {
|
||||
if(s.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
StringUtils::tolowercase(s);
|
||||
return s == "true" || s == "false";
|
||||
}
|
||||
|
||||
|
@ -726,6 +726,23 @@ bool post_import_documents(http_req& req, http_res& res) {
|
||||
}
|
||||
|
||||
bool post_add_document(http_req & req, http_res & res) {
|
||||
const char *UPSERT = "upsert";
|
||||
if(req.params.count(UPSERT) == 0) {
|
||||
req.params[UPSERT] = "false";
|
||||
}
|
||||
|
||||
if(!StringUtils::is_bool(req.params[UPSERT])) {
|
||||
res.set_400("Parameter `" + std::string(UPSERT) + "` must be a boolean.");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string doc_id;
|
||||
if(req.params.count("id") != 0) {
|
||||
doc_id = req.params["id"];
|
||||
}
|
||||
|
||||
const bool upsert = (req.params[UPSERT] == "true");
|
||||
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
Collection* collection = collectionManager.get_collection(req.params["collection"]);
|
||||
|
||||
@ -734,7 +751,7 @@ bool post_add_document(http_req & req, http_res & res) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Option<nlohmann::json> inserted_doc_op = collection->add(req.body);
|
||||
Option<nlohmann::json> inserted_doc_op = collection->add(req.body, upsert, doc_id);
|
||||
|
||||
if(!inserted_doc_op.ok()) {
|
||||
res.set(inserted_doc_op.code(), inserted_doc_op.error());
|
||||
@ -746,7 +763,10 @@ bool post_add_document(http_req & req, http_res & res) {
|
||||
}
|
||||
|
||||
bool put_upsert_document(http_req & req, http_res & res) {
|
||||
std::string doc_id = req.params["id"];
|
||||
std::string doc_id;
|
||||
if(req.params.count("id") != 0) {
|
||||
doc_id = req.params["id"];
|
||||
}
|
||||
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
Collection* collection = collectionManager.get_collection(req.params["collection"]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user