Update doc examples to new filter-APIs.

This commit is contained in:
Sebastian Jeltsch
2025-05-18 12:24:56 +02:00
parent e4cc6259ca
commit 8ed0a42502
12 changed files with 109 additions and 40 deletions

View File

@@ -293,6 +293,16 @@ pub struct Filter {
pub value: String,
}
impl Filter {
pub fn new(column: impl Into<String>, op: CompareOp, value: impl Into<String>) -> Self {
return Self {
column: column.into(),
op: Some(op),
value: value.into(),
};
}
}
impl From<Filter> for ValueOrFilterGroup {
fn from(value: Filter) -> Self {
return ValueOrFilterGroup::Filter(value);
@@ -306,6 +316,21 @@ pub enum ValueOrFilterGroup {
Or(Vec<ValueOrFilterGroup>),
}
impl<F> From<F> for ValueOrFilterGroup
where
F: Into<Vec<Filter>>,
{
fn from(filters: F) -> Self {
return ValueOrFilterGroup::And(
filters
.into()
.into_iter()
.map(ValueOrFilterGroup::Filter)
.collect(),
);
}
}
impl<'a> ListArguments<'a> {
pub fn new() -> Self {
return ListArguments::default();

View File

@@ -67,13 +67,13 @@ public enum RecordId: CustomStringConvertible {
}
private struct RecordIdResponse: Codable {
let ids: [String]
public let ids: [String]
}
public struct ListResponse<T: Decodable>: Decodable {
let cursor: String?
let total_count: Int64?
let records: [T]
public let cursor: String?
public let total_count: Int64?
public let records: [T]
}
public enum CompareOp {

View File

@@ -2,4 +2,4 @@ curl --globoff \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${AUTH_TOKEN}" \
--request GET \
'http://localhost:4000/api/records/v1/movies?limit=3&order=rank&watch_time[lt]=120&description[like]=%love%'
'http://localhost:4000/api/records/v1/movies?limit=3&order=rank&filter[watch_time][$lt]=120&filter[description][$like]=%love%'

View File

@@ -4,5 +4,16 @@ Future<ListResponse> list(Client client) async =>
await client.records('movies').list(
pagination: Pagination(limit: 3),
order: ['rank'],
filters: ['watch_time[lt]=120', 'description[like]=%love%'],
filters: [
Filter(
column: 'watch_time',
op: CompareOp.lessThan,
value: '120',
),
Filter(
column: 'description',
op: CompareOp.like,
value: '%love%',
),
],
);

View File

@@ -82,10 +82,10 @@ packages:
dependency: transitive
description:
name: dio
sha256: "5598aa796bbf4699afd5c67c0f5f6e2ed542afc956884b9cd58c306966efc260"
sha256: "253a18bbd4851fecba42f7343a1df3a9a4c1d31a2c1b37e221086b4fa8c8dbc9"
url: "https://pub.dev"
source: hosted
version: "5.7.0"
version: "5.8.0+1"
dio_web_adapter:
dependency: transitive
description:
@@ -364,7 +364,7 @@ packages:
path: "../../../client/trailbase-dart"
relative: true
source: path
version: "0.2.0"
version: "0.3.0"
typed_data:
dependency: transitive
description:

View File

@@ -6,5 +6,8 @@ public partial class Examples {
await client.Records("movies").List(
pagination: new Pagination(limit: 3),
order: ["rank"],
filters: ["watch_time[lt]=120", "description[like]=%love%"]);
filters: [
new Filter(column:"watch_time", op:CompareOp.LessThan, value:"120"),
new Filter(column:"description", op:CompareOp.Like, value:"%love%"),
]);
}

View File

@@ -1,4 +1,4 @@
use trailbase_client::{Client, ListArguments, ListResponse, Pagination};
use trailbase_client::{Client, CompareOp, Filter, ListArguments, ListResponse, Pagination};
pub async fn list(client: &Client) -> anyhow::Result<ListResponse<serde_json::Value>> {
Ok(
@@ -8,7 +8,10 @@ pub async fn list(client: &Client) -> anyhow::Result<ListResponse<serde_json::Va
ListArguments::new()
.with_pagination(Pagination::new().with_limit(3))
.with_order(["rank"])
.with_filters(["watch_time[lt]=120", "description[like]=%love%"]),
.with_filters([
Filter::new("watch_time", CompareOp::LessThan, "120"),
Filter::new("description", CompareOp::Like, "%love%"),
]),
)
.await?,
)

View File

@@ -1,12 +1,15 @@
import Foundation
import TrailBase
func list(client: Client) async throws -> ListResponse<SimpleStrict> {
try await client
.records("movies")
.list(
pagination: Pagination(limit: 3),
order: ["rank"],
filters: ["watch_time[lt]=120", "description[like]=%love%"]
)
func list(client: Client) async throws -> ListResponse<Movie> {
try await client
.records("movies")
.list(
pagination: Pagination(limit: 3),
order: ["rank"],
filters: [
.Filter(column: "watch_time", op: .LessThan, value: "120"),
.Filter(column: "description", op: .Like, value: "%love%"),
]
)
}

View File

@@ -1,5 +1,5 @@
import TrailBase
func read(client: Client, id: RecordId) async throws -> SimpleStrict {
try await client.records("simple_strict_table").read(recordId: id)
try await client.records("simple_strict_table").read(recordId: id)
}

View File

@@ -1,7 +1,11 @@
struct SimpleStrict: Codable, Equatable {
var id: String? = nil
var text_null: String? = nil
var text_default: String? = nil
let text_not_null: String
struct Movie: Codable, Equatable {
var name: String
}
struct SimpleStrict: Codable, Equatable {
var id: String? = nil
var text_null: String? = nil
var text_default: String? = nil
let text_not_null: String
}

View File

@@ -4,17 +4,26 @@ import TrailBase
@testable import RecordApiDocs
@Test func testDocs() async throws {
let client = try Client(site: URL(string: "http://localhost:4000")!)
let _ = try await client.login(email: "admin@localhost", password: "secret")
struct SimpleStrict: Codable, Equatable {
var id: String? = nil
let _ = try await list(client: client)
let id = try await create(client: client)
try await update(client: client, id: id)
let record = try await read(client: client, id: id)
assert(record.text_not_null == "updated")
try await delete(client: client, id: id)
var text_null: String? = nil
var text_default: String? = nil
let text_not_null: String
}
@Test func testDocs() async throws {
let client = try Client(site: URL(string: "http://localhost:4000")!)
let _ = try await client.login(email: "admin@localhost", password: "secret")
let movies = try await list(client: client)
#expect(movies.records.count == 3)
let id = try await create(client: client)
try await update(client: client, id: id)
let record = try await read(client: client, id: id)
#expect(record.text_not_null == "updated")
try await delete(client: client, id: id)
}

View File

@@ -6,5 +6,16 @@ export const list = async (client: Client): Promise<ListResponse<object>> =>
limit: 3,
},
order: ["rank"],
filters: ["watch_time[lt]=120", "description[like]=%love%"],
filters: [
{
column: "watch_time",
op: "$lt",
value: "120",
},
{
column: "description",
op: "$like",
value: "%love%",
},
],
});