|
@@ -3,11 +3,53 @@
|
|
|
#include "filters.hpp"
|
|
#include "filters.hpp"
|
|
|
#include "json_http.hpp"
|
|
#include "json_http.hpp"
|
|
|
#include "server.hpp"
|
|
#include "server.hpp"
|
|
|
|
|
+#include <string>
|
|
|
|
|
+#include <vector>
|
|
|
#include <cstdlib>
|
|
#include <cstdlib>
|
|
|
#include <limits>
|
|
#include <limits>
|
|
|
#include <optional>
|
|
#include <optional>
|
|
|
namespace svapi {
|
|
namespace svapi {
|
|
|
namespace {
|
|
namespace {
|
|
|
|
|
+
|
|
|
|
|
+/// The document carried by a write request, accepting both shapes:
|
|
|
|
|
+///
|
|
|
|
|
+/// {"data": {...}} a wrapper, unwrapped here
|
|
|
|
|
+/// {...} the document itself
|
|
|
|
|
+///
|
|
|
|
|
+/// `id` is never a field: POST takes the document id from the body, PUT and
|
|
|
|
|
+/// PATCH from the URL, so it is removed either way.
|
|
|
|
|
+///
|
|
|
|
|
+/// A `data` key next to any OTHER top-level key is refused. Taking body["data"]
|
|
|
|
|
+/// and discarding the siblings loses them silently, which is how a caller ends
|
|
|
|
|
+/// up missing fields with nothing in the response to say so.
|
|
|
|
|
+///
|
|
|
|
|
+/// A document whose own top-level field is named `data` is still writable by
|
|
|
|
|
+/// nesting it once: {"data": {"data": ...}}.
|
|
|
|
|
+nlohmann::json documentBody(const nlohmann::json& body) {
|
|
|
|
|
+ if (!body.is_object())
|
|
|
|
|
+ throw ApiError(ErrCode::Unprocessable, "validation", "body must be a JSON object");
|
|
|
|
|
+
|
|
|
|
|
+ if (body.contains("data")) {
|
|
|
|
|
+ std::vector<std::string> siblings;
|
|
|
|
|
+ for (auto it = body.begin(); it != body.end(); ++it)
|
|
|
|
|
+ if (it.key() != "data" && it.key() != "id") siblings.push_back(it.key());
|
|
|
|
|
+ if (!siblings.empty()) {
|
|
|
|
|
+ std::string list;
|
|
|
|
|
+ for (const auto& k : siblings) list += (list.empty() ? "" : ", ") + k;
|
|
|
|
|
+ throw ApiError(ErrCode::Unprocessable, "ambiguous_body",
|
|
|
|
|
+ "'data' is treated as a wrapper and must be the only top-level field, "
|
|
|
|
|
+ "but found alongside: " + list +
|
|
|
|
|
+ ". Send the document on its own, or nest it as {\"data\":{\"data\":...}}.");
|
|
|
|
|
+ }
|
|
|
|
|
+ nlohmann::json data = body["data"];
|
|
|
|
|
+ if (data.is_object() && data.contains("id")) data.erase("id");
|
|
|
|
|
+ return data;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ nlohmann::json doc = body;
|
|
|
|
|
+ if (doc.contains("id")) doc.erase("id");
|
|
|
|
|
+ return doc;
|
|
|
|
|
+}
|
|
|
// Authorize + verify the collection exists; returns the qualified collection name.
|
|
// Authorize + verify the collection exists; returns the qualified collection name.
|
|
|
std::string scoped(ServerDeps* d, const httplib::Request& req, int projIdx, int collIdx, KeyOp op) {
|
|
std::string scoped(ServerDeps* d, const httplib::Request& req, int projIdx, int collIdx, KeyOp op) {
|
|
|
ApiKey k = requireKey(*d, req);
|
|
ApiKey k = requireKey(*d, req);
|
|
@@ -37,8 +79,7 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Insert);
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Insert);
|
|
|
auto body = bodyJson(req);
|
|
auto body = bodyJson(req);
|
|
|
std::string id = body.value("id", "");
|
|
std::string id = body.value("id", "");
|
|
|
- nlohmann::json data = body.contains("data") ? body["data"] : body;
|
|
|
|
|
- if (data.is_object() && data.contains("id")) data.erase("id");
|
|
|
|
|
|
|
+ nlohmann::json data = documentBody(body);
|
|
|
const uint32_t ttl = ttlParam(req).value_or(0);
|
|
const uint32_t ttl = ttlParam(req).value_or(0);
|
|
|
std::string newId = d->db.client().insert(c, data, id, ttl);
|
|
std::string newId = d->db.client().insert(c, data, id, ttl);
|
|
|
if (newId.empty()) throw ApiError(ErrCode::Unavailable, "db_error", "insert failed");
|
|
if (newId.empty()) throw ApiError(ErrCode::Unavailable, "db_error", "insert failed");
|
|
@@ -72,7 +113,7 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
svr.Patch(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
svr.Patch(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Update); std::string id = req.matches[3];
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Update); std::string id = req.matches[3];
|
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
|
- nlohmann::json patchBody = bodyJson(req);
|
|
|
|
|
|
|
+ nlohmann::json patchBody = documentBody(bodyJson(req));
|
|
|
const auto ttl = ttlParam(req);
|
|
const auto ttl = ttlParam(req);
|
|
|
uint64_t v = ttl ? d->db.client().patchWithTtl(c, id, patchBody, *ttl)
|
|
uint64_t v = ttl ? d->db.client().patchWithTtl(c, id, patchBody, *ttl)
|
|
|
: d->db.client().patch(c, id, patchBody);
|
|
: d->db.client().patch(c, id, patchBody);
|
|
@@ -83,7 +124,8 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
svr.Put(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
svr.Put(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Update); std::string id = req.matches[3];
|
|
std::string c = scoped(d, req, 1, 2, KeyOp::Update); std::string id = req.matches[3];
|
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
if (!d->db.client().exists(c, id)) throw ApiError(ErrCode::NotFound, "not_found", "no such document");
|
|
|
- if (!d->db.client().update(c, id, bodyJson(req))) throw ApiError(ErrCode::Unavailable, "db_error", "update failed");
|
|
|
|
|
|
|
+ if (!d->db.client().update(c, id, documentBody(bodyJson(req))))
|
|
|
|
|
+ throw ApiError(ErrCode::Unavailable, "db_error", "update failed");
|
|
|
sendJson(res, 200, {{"id", id}});
|
|
sendJson(res, 200, {{"id", id}});
|
|
|
});
|
|
});
|
|
|
|
|
|