-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.exs
More file actions
106 lines (81 loc) · 3.85 KB
/
query.exs
File metadata and controls
106 lines (81 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env elixir
# Query builder example
Mix.install([
{:flowfull, path: ".."}
])
defmodule QueryExample do
alias Flowfull.Operators
def run do
client = Flowfull.new("https://api.example.com")
IO.puts("=== Example 1: Simple filter ===")
{:ok, response} =
Flowfull.query(client, "/users")
|> Flowfull.Query.where("status", Operators.eq("active"))
|> Flowfull.Query.execute()
IO.inspect(response, label: "Response")
IO.puts("\n=== Example 2: Multiple filters ===")
{:ok, response} =
Flowfull.query(client, "/users")
|> Flowfull.Query.where("age", Operators.gte(18))
|> Flowfull.Query.where("status", Operators.eq("active"))
|> Flowfull.Query.where("email", Operators.not_null())
|> Flowfull.Query.execute()
IO.inspect(response, label: "Response")
IO.puts("\n=== Example 3: Sorting ===")
{:ok, response} =
Flowfull.query(client, "/users")
|> Flowfull.Query.sort("created_at", :desc)
|> Flowfull.Query.sort("name", :asc)
|> Flowfull.Query.execute()
IO.inspect(response, label: "Response")
IO.puts("\n=== Example 4: Pagination ===")
{:ok, response} =
Flowfull.query(client, "/users")
|> Flowfull.Query.page(1)
|> Flowfull.Query.limit(10)
|> Flowfull.Query.execute()
IO.inspect(response, label: "Response")
IO.puts("\n=== Example 5: Field selection ===")
{:ok, response} =
Flowfull.query(client, "/users")
|> Flowfull.Query.select(["id", "name", "email"])
|> Flowfull.Query.execute()
IO.inspect(response, label: "Response")
IO.puts("\n=== Example 6: Complex query ===")
{:ok, response} =
Flowfull.query(client, "/products")
|> Flowfull.Query.where("price", Operators.between(10, 100))
|> Flowfull.Query.where("category", Operators.in_list(["electronics", "books"]))
|> Flowfull.Query.where("name", Operators.ilike("%laptop%"))
|> Flowfull.Query.where("stock", Operators.gt(0))
|> Flowfull.Query.sort("price", :asc)
|> Flowfull.Query.page(1)
|> Flowfull.Query.limit(20)
|> Flowfull.Query.execute()
IO.inspect(response, label: "Response")
IO.puts("\n=== Example 7: All operators ===")
# Comparison operators
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.eq(25))
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.ne(25))
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.gt(25))
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.gte(25))
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.lt(25))
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.lte(25))
# String operators
Flowfull.Query.where(Flowfull.query(client, "/users"), "name", Operators.like("%John%"))
Flowfull.Query.where(Flowfull.query(client, "/users"), "email", Operators.ilike("%@gmail.com"))
Flowfull.Query.where(Flowfull.query(client, "/users"), "name", Operators.starts_with("A"))
Flowfull.Query.where(Flowfull.query(client, "/users"), "email", Operators.ends_with("@example.com"))
# Array operators
Flowfull.Query.where(Flowfull.query(client, "/users"), "status", Operators.in_list(["active", "pending"]))
Flowfull.Query.where(Flowfull.query(client, "/users"), "role", Operators.not_in(["admin", "super"]))
# Null operators
Flowfull.Query.where(Flowfull.query(client, "/users"), "deleted_at", Operators.is_null())
Flowfull.Query.where(Flowfull.query(client, "/users"), "email", Operators.not_null())
# Range operators
Flowfull.Query.where(Flowfull.query(client, "/users"), "age", Operators.between(18, 65))
Flowfull.Query.where(Flowfull.query(client, "/users"), "price", Operators.not_between(0, 10))
IO.puts("All operators demonstrated!")
end
end
QueryExample.run()