|
@@ -179,6 +179,27 @@ public:
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* A single query filter with operator.
|
|
* A single query filter with operator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Construction options, in increasing order of readability:
|
|
|
|
|
+ *
|
|
|
|
|
+ * Filter{"age", FilterOp::GT, 18} // raw 3-arg
|
|
|
|
|
+ * Filter::gt("age", 18) // named factory (recommended)
|
|
|
|
|
+ *
|
|
|
|
|
+ * The named factories cover every operator; pick the one that matches
|
|
|
|
|
+ * the predicate's intent so callsites read like the question they're
|
|
|
|
|
+ * asking:
|
|
|
|
|
+ *
|
|
|
|
|
+ * Filter::eq("status", "active")
|
|
|
|
|
+ * Filter::ne("deleted", true)
|
|
|
|
|
+ * Filter::gt("age", 18) // age > 18
|
|
|
|
|
+ * Filter::gte("score", 0.8) // score ≥ 0.8
|
|
|
|
|
+ * Filter::lt("retry_count", 5) // retry_count < 5
|
|
|
|
|
+ * Filter::lte("priority", 3) // priority ≤ 3
|
|
|
|
|
+ * Filter::in("role", {"admin", "owner"}) // role IN (...)
|
|
|
|
|
+ * Filter::contains("tags", "urgent") // tags contains "urgent"
|
|
|
|
|
+ * Filter::exists("email", true) // email is set
|
|
|
|
|
+ * Filter::regex("name", "^Alice") // name matches /^Alice/
|
|
|
|
|
+ * Filter::search("description", "needle") // case-insensitive substr
|
|
|
*/
|
|
*/
|
|
|
struct Filter {
|
|
struct Filter {
|
|
|
std::string field;
|
|
std::string field;
|
|
@@ -188,9 +209,41 @@ public:
|
|
|
Filter() = default;
|
|
Filter() = default;
|
|
|
Filter(std::string f, FilterOp o, nlohmann::json v)
|
|
Filter(std::string f, FilterOp o, nlohmann::json v)
|
|
|
: field(std::move(f)), op(o), value(std::move(v)) {}
|
|
: field(std::move(f)), op(o), value(std::move(v)) {}
|
|
|
- // Convenience: equality filter
|
|
|
|
|
- Filter(std::string f, nlohmann::json v)
|
|
|
|
|
- : field(std::move(f)), op(FilterOp::EQ), value(std::move(v)) {}
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Named factories — preferred at callsites.
|
|
|
|
|
+ static Filter eq(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::EQ, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter ne(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::NE, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter gt(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::GT, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter gte(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::GTE, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter lt(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::LT, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter lte(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::LTE, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter in(std::string f, nlohmann::json array_value) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::IN, std::move(array_value)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter contains(std::string f, nlohmann::json v) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::CONTAINS, std::move(v)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter exists(std::string f, bool present = true) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::EXISTS, present};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter regex(std::string f, std::string pattern) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::REGEX, std::move(pattern)};
|
|
|
|
|
+ }
|
|
|
|
|
+ static Filter search(std::string f, std::string term) {
|
|
|
|
|
+ return Filter{std::move(f), FilterOp::SEARCH, std::move(term)};
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct QueryOptions {
|
|
struct QueryOptions {
|
|
@@ -219,15 +272,14 @@ public:
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Find documents matching a query.
|
|
* Find documents matching a query.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Pass `QueryOptions{}` for "no filters, default page size" — the
|
|
|
|
|
+ * no-options overload was removed in v2.1 because it silently scanned
|
|
|
|
|
+ * the whole collection and the named call site reads clearer.
|
|
|
*/
|
|
*/
|
|
|
[[nodiscard]] std::vector<nlohmann::json> find(const std::string& collection,
|
|
[[nodiscard]] std::vector<nlohmann::json> find(const std::string& collection,
|
|
|
const QueryOptions& options);
|
|
const QueryOptions& options);
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Find all documents in a collection (no filters).
|
|
|
|
|
- */
|
|
|
|
|
- [[nodiscard]] std::vector<nlohmann::json> find(const std::string& collection);
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* Find documents with full metrics (including WAL fallback info).
|
|
* Find documents with full metrics (including WAL fallback info).
|
|
|
*/
|
|
*/
|