airrel 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.idea/.gitignore +8 -0
- data/.idea/airrel.iml +58 -0
- data/.idea/misc.xml +4 -0
- data/.idea/modules.xml +8 -0
- data/.idea/vcs.xml +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +210 -0
- data/Rakefile +12 -0
- data/examples/demo.rb +50 -0
- data/lib/airrel/formula_builder.rb +142 -0
- data/lib/airrel/relation.rb +287 -0
- data/lib/airrel/version.rb +5 -0
- data/lib/airrel/where_clause.rb +41 -0
- data/lib/airrel.rb +11 -0
- data/sig/airrel.rbs +4 -0
- metadata +72 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6b961c23489f4318b5feaceb9dd0b278a402ae3361a7516b4f7f5c7514459843
|
|
4
|
+
data.tar.gz: 5635e4654a366fec1e9aff98394d99675dba861d8514845fbf75cd64ac88ba53
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0cd6592f458ad4aa04a3d43dcbf6d9fe737a9a10a8998b6efa9bc268bd119fa35e0303a8a76a9343007967111a8033fe6762228e966939d7ac97b944772a43a7
|
|
7
|
+
data.tar.gz: e406cf4797e68a2ee05765bf67cc0cd7900cfddb70d7530e88bbe17ee1dd62c1cc0c29fd2a89a80e6aa7838763f5882a5f073b7811f3e27c5b923ed7d56c2f21
|
data/.idea/.gitignore
ADDED
data/.idea/airrel.iml
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
|
3
|
+
<component name="ModuleRunConfigurationManager">
|
|
4
|
+
<shared />
|
|
5
|
+
</component>
|
|
6
|
+
<component name="NewModuleRootManager">
|
|
7
|
+
<content url="file://$MODULE_DIR$">
|
|
8
|
+
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
|
9
|
+
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
|
10
|
+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
|
11
|
+
</content>
|
|
12
|
+
<orderEntry type="inheritedJdk" />
|
|
13
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
14
|
+
<orderEntry type="library" scope="PROVIDED" name="ast (v2.4.3, mise: 3.4.3) [gem]" level="application" />
|
|
15
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v2.7.2, mise: 3.4.3) [gem]" level="application" />
|
|
16
|
+
<orderEntry type="library" scope="PROVIDED" name="connection_pool (v2.5.4, mise: 3.4.3) [gem]" level="application" />
|
|
17
|
+
<orderEntry type="library" scope="PROVIDED" name="date (v3.5.0, mise: 3.4.3) [gem]" level="application" />
|
|
18
|
+
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.6.2, mise: 3.4.3) [gem]" level="application" />
|
|
19
|
+
<orderEntry type="library" scope="PROVIDED" name="erb (v6.0.0, mise: 3.4.3) [gem]" level="application" />
|
|
20
|
+
<orderEntry type="library" scope="PROVIDED" name="faraday (v2.14.0, mise: 3.4.3) [gem]" level="application" />
|
|
21
|
+
<orderEntry type="library" scope="PROVIDED" name="faraday-net_http (v3.4.2, mise: 3.4.3) [gem]" level="application" />
|
|
22
|
+
<orderEntry type="library" scope="PROVIDED" name="faraday-net_http_persistent (v2.3.1, mise: 3.4.3) [gem]" level="application" />
|
|
23
|
+
<orderEntry type="library" scope="PROVIDED" name="io-console (v0.8.1, mise: 3.4.3) [gem]" level="application" />
|
|
24
|
+
<orderEntry type="library" scope="PROVIDED" name="irb (v1.15.3, mise: 3.4.3) [gem]" level="application" />
|
|
25
|
+
<orderEntry type="library" scope="PROVIDED" name="json (v2.16.0, mise: 3.4.3) [gem]" level="application" />
|
|
26
|
+
<orderEntry type="library" scope="PROVIDED" name="language_server-protocol (v3.17.0.5, mise: 3.4.3) [gem]" level="application" />
|
|
27
|
+
<orderEntry type="library" scope="PROVIDED" name="lint_roller (v1.1.0, mise: 3.4.3) [gem]" level="application" />
|
|
28
|
+
<orderEntry type="library" scope="PROVIDED" name="logger (v1.7.0, mise: 3.4.3) [gem]" level="application" />
|
|
29
|
+
<orderEntry type="library" scope="PROVIDED" name="net-http (v0.8.0, mise: 3.4.3) [gem]" level="application" />
|
|
30
|
+
<orderEntry type="library" scope="PROVIDED" name="net-http-persistent (v4.0.6, mise: 3.4.3) [gem]" level="application" />
|
|
31
|
+
<orderEntry type="library" scope="PROVIDED" name="norairrecord (v0.2.0, mise: 3.4.3) [gem]" level="application" />
|
|
32
|
+
<orderEntry type="library" scope="PROVIDED" name="parallel (v1.27.0, mise: 3.4.3) [gem]" level="application" />
|
|
33
|
+
<orderEntry type="library" scope="PROVIDED" name="parser (v3.3.10.0, mise: 3.4.3) [gem]" level="application" />
|
|
34
|
+
<orderEntry type="library" scope="PROVIDED" name="pp (v0.6.3, mise: 3.4.3) [gem]" level="application" />
|
|
35
|
+
<orderEntry type="library" scope="PROVIDED" name="prettyprint (v0.2.0, mise: 3.4.3) [gem]" level="application" />
|
|
36
|
+
<orderEntry type="library" scope="PROVIDED" name="prism (v1.6.0, mise: 3.4.3) [gem]" level="application" />
|
|
37
|
+
<orderEntry type="library" scope="PROVIDED" name="psych (v5.2.6, mise: 3.4.3) [gem]" level="application" />
|
|
38
|
+
<orderEntry type="library" scope="PROVIDED" name="racc (v1.8.1, mise: 3.4.3) [gem]" level="application" />
|
|
39
|
+
<orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, mise: 3.4.3) [gem]" level="application" />
|
|
40
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v13.3.1, mise: 3.4.3) [gem]" level="application" />
|
|
41
|
+
<orderEntry type="library" scope="PROVIDED" name="rdoc (v6.15.1, mise: 3.4.3) [gem]" level="application" />
|
|
42
|
+
<orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.11.3, mise: 3.4.3) [gem]" level="application" />
|
|
43
|
+
<orderEntry type="library" scope="PROVIDED" name="reline (v0.6.3, mise: 3.4.3) [gem]" level="application" />
|
|
44
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec (v3.13.2, mise: 3.4.3) [gem]" level="application" />
|
|
45
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.13.6, mise: 3.4.3) [gem]" level="application" />
|
|
46
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.13.5, mise: 3.4.3) [gem]" level="application" />
|
|
47
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.13.7, mise: 3.4.3) [gem]" level="application" />
|
|
48
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.13.6, mise: 3.4.3) [gem]" level="application" />
|
|
49
|
+
<orderEntry type="library" scope="PROVIDED" name="rubocop (v1.81.7, mise: 3.4.3) [gem]" level="application" />
|
|
50
|
+
<orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.48.0, mise: 3.4.3) [gem]" level="application" />
|
|
51
|
+
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, mise: 3.4.3) [gem]" level="application" />
|
|
52
|
+
<orderEntry type="library" scope="PROVIDED" name="stringio (v3.1.8, mise: 3.4.3) [gem]" level="application" />
|
|
53
|
+
<orderEntry type="library" scope="PROVIDED" name="tsort (v0.2.0, mise: 3.4.3) [gem]" level="application" />
|
|
54
|
+
<orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v3.2.0, mise: 3.4.3) [gem]" level="application" />
|
|
55
|
+
<orderEntry type="library" scope="PROVIDED" name="unicode-emoji (v4.1.0, mise: 3.4.3) [gem]" level="application" />
|
|
56
|
+
<orderEntry type="library" scope="PROVIDED" name="uri (v1.1.1, mise: 3.4.3) [gem]" level="application" />
|
|
57
|
+
</component>
|
|
58
|
+
</module>
|
data/.idea/misc.xml
ADDED
data/.idea/modules.xml
ADDED
data/.idea/vcs.xml
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 24c02
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# Airrel
|
|
2
|
+
|
|
3
|
+
arel-like relational algebra for airtable. it's like arel but AIR. get it?
|
|
4
|
+
|
|
5
|
+
chainable queries that don't execute until you iterate. builds airtable formulas for you.
|
|
6
|
+
|
|
7
|
+
## installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'airrel'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## usage
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require 'airrel'
|
|
17
|
+
require 'norairrecord'
|
|
18
|
+
|
|
19
|
+
class User < Norairrecord::Table
|
|
20
|
+
self.base_key = "appXXX"
|
|
21
|
+
self.table_name = "Users"
|
|
22
|
+
|
|
23
|
+
# add the relation magic
|
|
24
|
+
def self.all
|
|
25
|
+
Airrel::Relation.new(self)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# now you can chain!
|
|
30
|
+
User.all.where(role: "admin").where(active: true).order(created_at: :desc).limit(10)
|
|
31
|
+
|
|
32
|
+
# queries are lazy - nothing executes until you iterate
|
|
33
|
+
users = User.all.where(role: "admin") # no API call yet
|
|
34
|
+
users.each { |u| puts u["Name"] } # now it executes
|
|
35
|
+
|
|
36
|
+
# all the usual suspects
|
|
37
|
+
User.all.where(age: 18..65) # range queries
|
|
38
|
+
User.all.where(role: ["admin", "mod"]) # IN queries
|
|
39
|
+
User.all.where(email: nil) # BLANK() checks
|
|
40
|
+
User.all.order(:name) # sorting
|
|
41
|
+
User.all.order(name: :asc, age: :desc)
|
|
42
|
+
User.all.limit(10)
|
|
43
|
+
User.all.offset(20)
|
|
44
|
+
|
|
45
|
+
# finder methods
|
|
46
|
+
User.all.find_by(email: "test@example.com")
|
|
47
|
+
User.all.find_by!(email: "test@example.com") # raises if not found
|
|
48
|
+
User.all.first
|
|
49
|
+
User.all.first(5)
|
|
50
|
+
User.all.order(:created_at).last # requires an order!
|
|
51
|
+
User.all.count
|
|
52
|
+
|
|
53
|
+
# raw formulas still work
|
|
54
|
+
User.all.where("{Age} > 18")
|
|
55
|
+
User.all.where("AND({Active} = TRUE(), {Role} = 'admin')")
|
|
56
|
+
|
|
57
|
+
# inspect the query without executing
|
|
58
|
+
User.all.where(role: "admin").to_airtable
|
|
59
|
+
# => {:filter=>"AND({role} = 'admin')"}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## formula builder
|
|
63
|
+
|
|
64
|
+
build complex airtable formulas programmatically:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
include Airrel::FormulaBuilder
|
|
68
|
+
|
|
69
|
+
# basic predicates
|
|
70
|
+
eq("Name", "Alice") # => "{Name} = 'Alice'"
|
|
71
|
+
neq("Age", 25) # => "NOT({Age} = 25)"
|
|
72
|
+
gt("Age", 18) # => "{Age} > 18"
|
|
73
|
+
gte("Age", 18) # => "{Age} >= 18"
|
|
74
|
+
lt("Age", 65) # => "{Age} < 65"
|
|
75
|
+
lte("Age", 65) # => "{Age} <= 65"
|
|
76
|
+
|
|
77
|
+
# boolean logic
|
|
78
|
+
all(eq("Role", "admin"), eq("Active", true))
|
|
79
|
+
any(eq("Role", "admin"), eq("Role", "moderator"))
|
|
80
|
+
none(blank("Email"))
|
|
81
|
+
|
|
82
|
+
# helpers
|
|
83
|
+
blank("Email") # => "{Email} = BLANK()"
|
|
84
|
+
present("Email") # => "NOT({Email} = BLANK())"
|
|
85
|
+
find("Name", "Alice") # => "FIND('Alice', {Name})"
|
|
86
|
+
search("Tags", "important") # => "SEARCH('important', {Tags})"
|
|
87
|
+
|
|
88
|
+
# use in queries
|
|
89
|
+
User.all.where(
|
|
90
|
+
all(
|
|
91
|
+
gte("Age", 18),
|
|
92
|
+
present("Email")
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## how it works
|
|
98
|
+
|
|
99
|
+
`Airrel::Relation` is a lazy query builder. it:
|
|
100
|
+
|
|
101
|
+
1. stores query state (where clauses, ordering, limits)
|
|
102
|
+
2. doesn't execute until you iterate or call a terminal method
|
|
103
|
+
3. returns new relation objects when you chain (immutable)
|
|
104
|
+
4. converts everything to airtable API params when executed
|
|
105
|
+
|
|
106
|
+
hash conditions get converted to airtable formulas:
|
|
107
|
+
- `{ role: "admin" }` → `{role} = 'admin'`
|
|
108
|
+
- `{ age: nil }` → `{age} = BLANK()`
|
|
109
|
+
- `{ active: true }` → `{active} = TRUE()`
|
|
110
|
+
- `{ age: 18..65 }` → `AND({age} >= 18, {age} <= 65)`
|
|
111
|
+
- `{ role: ["admin", "mod"] }` → `OR({role} = 'admin', {role} = 'mod')`
|
|
112
|
+
|
|
113
|
+
multiple where clauses get AND'd together:
|
|
114
|
+
```ruby
|
|
115
|
+
User.all.where(role: "admin").where(active: true)
|
|
116
|
+
# => AND({role} = 'admin', {active} = TRUE())
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## combining with field mappings
|
|
120
|
+
|
|
121
|
+
if you're using field mappings (like in airctiverecord), you can pass them to the formula builder:
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
field_mappings = { first_name: "First Name", last_name: "Last Name" }
|
|
125
|
+
|
|
126
|
+
Airrel::FormulaBuilder.hash_to_formula(
|
|
127
|
+
{ first_name: "Alice" },
|
|
128
|
+
field_mappings
|
|
129
|
+
)
|
|
130
|
+
# => "{First Name} = 'Alice'"
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## security & escaping
|
|
134
|
+
|
|
135
|
+
**string escaping**
|
|
136
|
+
|
|
137
|
+
all string values are properly escaped for airtable formulas:
|
|
138
|
+
- single and double quotes are backslash-escaped: `O'Reilly` → `'O\'Reilly'`
|
|
139
|
+
- works for hash conditions, find/search helpers, and manual formula building
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
User.where(name: "O'Reilly")
|
|
143
|
+
# => {filter: "{name} = 'O\'Reilly'"}
|
|
144
|
+
|
|
145
|
+
find("Name", "it's working")
|
|
146
|
+
# => "FIND('it\'s working', {Name})"
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**field names**
|
|
150
|
+
|
|
151
|
+
field names are used as-is in `{fieldName}` syntax. if you're accepting user input for field names, validate them first - airtable field names can contain most characters but shouldn't contain `{` or `}`.
|
|
152
|
+
|
|
153
|
+
**injection safety**
|
|
154
|
+
|
|
155
|
+
because we properly escape all string values, formula injection attacks are prevented:
|
|
156
|
+
|
|
157
|
+
```ruby
|
|
158
|
+
User.where(name: "'; DROP TABLE users; --")
|
|
159
|
+
# => {filter: "{name} = '\'; DROP TABLE users; --'"}
|
|
160
|
+
# safe! airtable will look for a name with that exact string
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## performance considerations
|
|
164
|
+
|
|
165
|
+
**automatic optimizations**
|
|
166
|
+
|
|
167
|
+
Airrel automatically optimizes common queries:
|
|
168
|
+
|
|
169
|
+
```ruby
|
|
170
|
+
User.first # limit(1) - only loads 1 record
|
|
171
|
+
User.last # limit(1) with reversed order
|
|
172
|
+
User.any? # limit(1) - checks if at least 1 exists
|
|
173
|
+
User.empty? # !any? - same optimization
|
|
174
|
+
User.first(10) # limit(10) - only loads 10 records
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**count is expensive**
|
|
178
|
+
|
|
179
|
+
airtable doesn't have a count API, so `count` loads all matching records:
|
|
180
|
+
|
|
181
|
+
```ruby
|
|
182
|
+
User.where(role: "admin").count # loads ALL admin users!
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
if you just need to check existence, use `any?` or `exists?`:
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
User.where(role: "admin").any? # only loads 1 record ✓
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**pagination**
|
|
192
|
+
|
|
193
|
+
by default, norairrecord paginates through all results. for large tables (25k+ records), consider:
|
|
194
|
+
|
|
195
|
+
```ruby
|
|
196
|
+
# process in batches
|
|
197
|
+
User.limit(100).offset(0).each { |u| process(u) }
|
|
198
|
+
User.limit(100).offset(100).each { |u| process(u) }
|
|
199
|
+
|
|
200
|
+
# or disable pagination for a single page
|
|
201
|
+
User.limit(100).without_pagination.to_a
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**field selection**
|
|
205
|
+
|
|
206
|
+
norairrecord supports field selection to reduce payload size (though not exposed in Airrel yet).
|
|
207
|
+
|
|
208
|
+
## license
|
|
209
|
+
|
|
210
|
+
MIT
|
data/Rakefile
ADDED
data/examples/demo.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "airrel"
|
|
6
|
+
|
|
7
|
+
# fake norairrecord table for testing
|
|
8
|
+
class User
|
|
9
|
+
def self.records(**params)
|
|
10
|
+
puts "Would call Airtable API with:"
|
|
11
|
+
puts params.inspect
|
|
12
|
+
[]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# basic chaining
|
|
17
|
+
puts "=== basic chaining ==="
|
|
18
|
+
relation = Airrel::Relation.new(User)
|
|
19
|
+
.where(role: "admin")
|
|
20
|
+
.where(active: true)
|
|
21
|
+
.order(created_at: :desc)
|
|
22
|
+
.limit(10)
|
|
23
|
+
|
|
24
|
+
puts relation.to_airtable
|
|
25
|
+
puts
|
|
26
|
+
|
|
27
|
+
# hash to formula conversion
|
|
28
|
+
puts "=== hash to formula ==="
|
|
29
|
+
puts Airrel::FormulaBuilder.hash_to_formula(email: "test@example.com", age: 25)
|
|
30
|
+
puts Airrel::FormulaBuilder.hash_to_formula(age: 18..65)
|
|
31
|
+
puts Airrel::FormulaBuilder.hash_to_formula(role: ["admin", "moderator"])
|
|
32
|
+
puts Airrel::FormulaBuilder.hash_to_formula(deleted_at: nil)
|
|
33
|
+
puts
|
|
34
|
+
|
|
35
|
+
# formula builder helpers
|
|
36
|
+
puts "=== formula builder helpers ==="
|
|
37
|
+
include Airrel::FormulaBuilder
|
|
38
|
+
|
|
39
|
+
puts eq("Name", "Alice")
|
|
40
|
+
puts gt("Age", 18)
|
|
41
|
+
puts all(eq("Role", "admin"), present("Email"))
|
|
42
|
+
puts any(eq("Role", "admin"), eq("Role", "moderator"))
|
|
43
|
+
puts
|
|
44
|
+
|
|
45
|
+
# lazy execution
|
|
46
|
+
puts "=== lazy execution ==="
|
|
47
|
+
query = Airrel::Relation.new(User).where(role: "admin")
|
|
48
|
+
puts "Query built (no API call yet)"
|
|
49
|
+
puts "Iterating now..."
|
|
50
|
+
query.each { |u| puts u }
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Airrel
|
|
4
|
+
module FormulaBuilder
|
|
5
|
+
extend self
|
|
6
|
+
|
|
7
|
+
def hash_to_formula(conditions, field_mappings = {})
|
|
8
|
+
formulas = conditions.map do |key, value|
|
|
9
|
+
field_name = field_mappings[key.to_s] || key.to_s
|
|
10
|
+
build_predicate(field_name, value)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
formulas.size == 1 ? formulas.first : "AND(#{formulas.join(', ')})"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def build_predicate(field, value)
|
|
17
|
+
case value
|
|
18
|
+
when nil
|
|
19
|
+
"{#{field}} = BLANK()"
|
|
20
|
+
when true
|
|
21
|
+
"{#{field}} = TRUE()"
|
|
22
|
+
when false
|
|
23
|
+
"{#{field}} = FALSE()"
|
|
24
|
+
when String
|
|
25
|
+
"{#{field}} = #{escape_string(value)}"
|
|
26
|
+
when Numeric
|
|
27
|
+
"{#{field}} = #{value}"
|
|
28
|
+
when Range
|
|
29
|
+
if value.exclude_end?
|
|
30
|
+
"AND({#{field}} >= #{escape_value(value.begin)}, {#{field}} < #{escape_value(value.end)})"
|
|
31
|
+
else
|
|
32
|
+
"AND({#{field}} >= #{escape_value(value.begin)}, {#{field}} <= #{escape_value(value.end)})"
|
|
33
|
+
end
|
|
34
|
+
when Array
|
|
35
|
+
# IN query - use OR
|
|
36
|
+
or_conditions = value.map { |v| build_predicate(field, v) }
|
|
37
|
+
"OR(#{or_conditions.join(', ')})"
|
|
38
|
+
else
|
|
39
|
+
# fallback - convert to string and escape
|
|
40
|
+
"{#{field}} = #{escape_string(value.to_s)}"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# escape a string value for use in airtable formulas
|
|
45
|
+
# airtable uses backslash escaping: \ escapes the next character
|
|
46
|
+
# so we need to escape backslashes first, then quotes
|
|
47
|
+
def escape_string(str)
|
|
48
|
+
# escape backslashes first (\ -> \\), then quotes (' -> \', " -> \")
|
|
49
|
+
escaped = str.to_s.gsub('\\', '\\\\\\\\').gsub(/['"]/, '\\\\\0')
|
|
50
|
+
"'#{escaped}'"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# escape any value (delegates to appropriate method)
|
|
54
|
+
def escape_value(value)
|
|
55
|
+
case value
|
|
56
|
+
when String
|
|
57
|
+
escape_string(value)
|
|
58
|
+
when Numeric
|
|
59
|
+
value.to_s
|
|
60
|
+
when nil
|
|
61
|
+
"BLANK()"
|
|
62
|
+
when true
|
|
63
|
+
"TRUE()"
|
|
64
|
+
when false
|
|
65
|
+
"FALSE()"
|
|
66
|
+
else
|
|
67
|
+
escape_string(value.to_s)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# helper methods for building formulas programmatically
|
|
72
|
+
|
|
73
|
+
def all(*formulas)
|
|
74
|
+
"AND(#{formulas.join(', ')})"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def any(*formulas)
|
|
78
|
+
"OR(#{formulas.join(', ')})"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def none(formula)
|
|
82
|
+
"NOT(#{formula})"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def eq(field, value)
|
|
86
|
+
build_predicate(field, value)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def neq(field, value)
|
|
90
|
+
none(eq(field, value))
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def gt(field, value)
|
|
94
|
+
"{#{field}} > #{value}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def gte(field, value)
|
|
98
|
+
"{#{field}} >= #{value}"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def lt(field, value)
|
|
102
|
+
"{#{field}} < #{value}"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def lte(field, value)
|
|
106
|
+
"{#{field}} <= #{value}"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def blank(field)
|
|
110
|
+
"{#{field}} = BLANK()"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def present(field)
|
|
114
|
+
none(blank(field))
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def find(field, search_string)
|
|
118
|
+
"FIND(#{escape_string(search_string)}, {#{field}})"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def search(field, search_string)
|
|
122
|
+
"SEARCH(#{escape_string(search_string)}, {#{field}})"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# for multi-select fields (checks if array contains value)
|
|
126
|
+
def contains(field, value)
|
|
127
|
+
"FIND(#{escape_string(value.to_s)}, {#{field}})"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# check if multi-select contains ANY of the values
|
|
131
|
+
def contains_any(field, *values)
|
|
132
|
+
formulas = values.map { |v| contains(field, v) }
|
|
133
|
+
formulas.size == 1 ? formulas.first : any(*formulas)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# check if multi-select contains ALL of the values
|
|
137
|
+
def contains_all(field, *values)
|
|
138
|
+
formulas = values.map { |v| contains(field, v) }
|
|
139
|
+
formulas.size == 1 ? formulas.first : all(*formulas)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Airrel
|
|
4
|
+
class Relation
|
|
5
|
+
include Enumerable
|
|
6
|
+
|
|
7
|
+
attr_reader :klass, :where_clause, :order_values, :limit_value, :offset_value
|
|
8
|
+
|
|
9
|
+
def initialize(klass)
|
|
10
|
+
@klass = klass
|
|
11
|
+
@where_clause = WhereClause.new
|
|
12
|
+
@order_values = []
|
|
13
|
+
@limit_value = nil
|
|
14
|
+
@offset_value = nil
|
|
15
|
+
@loaded = false
|
|
16
|
+
@records = nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# chaining methods (return new relations)
|
|
20
|
+
|
|
21
|
+
def where(conditions)
|
|
22
|
+
spawn.where!(conditions)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def where!(conditions)
|
|
26
|
+
@where_clause = @where_clause.merge(conditions)
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def order(*args)
|
|
31
|
+
spawn.order!(*args)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def order!(*args)
|
|
35
|
+
@order_values += parse_order_args(args)
|
|
36
|
+
self
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def limit(value)
|
|
40
|
+
spawn.limit!(value)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def limit!(value)
|
|
44
|
+
@limit_value = value
|
|
45
|
+
self
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def offset(value)
|
|
49
|
+
spawn.offset!(value)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def offset!(value)
|
|
53
|
+
@offset_value = value
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def reorder(*args)
|
|
58
|
+
spawn.reorder!(*args)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def reorder!(*args)
|
|
62
|
+
@order_values = parse_order_args(args)
|
|
63
|
+
self
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# finder methods (execute queries)
|
|
67
|
+
|
|
68
|
+
def first(limit = nil)
|
|
69
|
+
if limit
|
|
70
|
+
# explicitly set limit to avoid loading more than needed
|
|
71
|
+
self.limit(limit).to_a
|
|
72
|
+
else
|
|
73
|
+
# only load 1 record
|
|
74
|
+
self.limit(1).to_a.first
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def last(limit = nil)
|
|
79
|
+
# if no order specified, airtable returns oldest first by default
|
|
80
|
+
# so we need to explicitly reverse or it's meaningless
|
|
81
|
+
if @order_values.empty?
|
|
82
|
+
raise ArgumentError, "last requires an order to be specified (use .order(:created_at) or similar)"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
reversed = reverse_order
|
|
86
|
+
if limit
|
|
87
|
+
# only load what we need
|
|
88
|
+
reversed.limit(limit).to_a.reverse
|
|
89
|
+
else
|
|
90
|
+
# only load 1 record
|
|
91
|
+
reversed.limit(1).to_a.first
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def find(id)
|
|
96
|
+
klass.find(id)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def find_by(conditions)
|
|
100
|
+
where(conditions).first
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def find_by!(conditions)
|
|
104
|
+
# Raise error class that will be defined by the consumer (norairrecord)
|
|
105
|
+
error_class = defined?(Norairrecord::RecordNotFoundError) ? Norairrecord::RecordNotFoundError : StandardError
|
|
106
|
+
find_by(conditions) || raise(error_class, "Record not found")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def all
|
|
110
|
+
spawn
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def to_a
|
|
114
|
+
load_records
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
alias to_ary to_a
|
|
118
|
+
|
|
119
|
+
def each(&block)
|
|
120
|
+
load_records.each(&block)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# batch iteration for large result sets
|
|
124
|
+
def find_each(batch_size: 100, &block)
|
|
125
|
+
find_in_batches(batch_size: batch_size) do |batch|
|
|
126
|
+
batch.each(&block)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def find_in_batches(batch_size: 100, &block)
|
|
131
|
+
current_offset = 0
|
|
132
|
+
|
|
133
|
+
loop do
|
|
134
|
+
# create a new relation with limit and offset
|
|
135
|
+
batch_relation = spawn
|
|
136
|
+
batch_relation.instance_variable_set(:@limit_value, batch_size)
|
|
137
|
+
batch_relation.instance_variable_set(:@offset_value, current_offset)
|
|
138
|
+
|
|
139
|
+
batch = batch_relation.to_a
|
|
140
|
+
break if batch.empty?
|
|
141
|
+
|
|
142
|
+
yield batch
|
|
143
|
+
|
|
144
|
+
break if batch.size < batch_size # last batch
|
|
145
|
+
current_offset += batch_size
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def count
|
|
150
|
+
# airtable doesn't have a count API, but we can optimize by limiting fields
|
|
151
|
+
# or using exists? check for any?
|
|
152
|
+
load_records.size
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def empty?
|
|
156
|
+
!any?
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def any?
|
|
160
|
+
# optimize: only load 1 record to check existence
|
|
161
|
+
limit(1).load_records.any?
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def exists?
|
|
165
|
+
any?
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# inspection
|
|
169
|
+
|
|
170
|
+
def inspect
|
|
171
|
+
entries = load_records.take(11).map!(&:inspect)
|
|
172
|
+
entries[10] = "..." if entries.size == 11
|
|
173
|
+
|
|
174
|
+
"#<#{self.class.name} [#{entries.join(', ')}]>"
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def to_airtable
|
|
178
|
+
to_airtable_params
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def to_sql
|
|
182
|
+
to_airtable_params.inspect
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# execution
|
|
186
|
+
|
|
187
|
+
def load
|
|
188
|
+
@records = exec_queries unless loaded?
|
|
189
|
+
self
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def reload
|
|
193
|
+
reset
|
|
194
|
+
load
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def loaded?
|
|
198
|
+
@loaded
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def reset
|
|
202
|
+
@loaded = false
|
|
203
|
+
@records = nil
|
|
204
|
+
self
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# disable automatic pagination for better control
|
|
208
|
+
def without_pagination
|
|
209
|
+
@paginate = false
|
|
210
|
+
self
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
protected
|
|
214
|
+
|
|
215
|
+
def spawn
|
|
216
|
+
clone.tap { |r| r.reset }
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def load_records
|
|
220
|
+
load unless loaded?
|
|
221
|
+
@records
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def exec_queries
|
|
225
|
+
@loaded = true
|
|
226
|
+
params = to_airtable_params
|
|
227
|
+
klass.records(**params)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def to_airtable_params
|
|
231
|
+
params = {}
|
|
232
|
+
|
|
233
|
+
# filter
|
|
234
|
+
if @where_clause.any?
|
|
235
|
+
params[:filter] = @where_clause.to_airtable_formula
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# sort
|
|
239
|
+
if @order_values.any?
|
|
240
|
+
params[:sort] = @order_values.map { |field, direction|
|
|
241
|
+
{ field: field.to_s, direction: direction.to_s }
|
|
242
|
+
}
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# limit
|
|
246
|
+
params[:max_records] = @limit_value if @limit_value
|
|
247
|
+
|
|
248
|
+
# offset (airtable calls it offset in pagination)
|
|
249
|
+
params[:offset] = @offset_value if @offset_value
|
|
250
|
+
|
|
251
|
+
# pagination control
|
|
252
|
+
params[:paginate] = @paginate if defined?(@paginate)
|
|
253
|
+
|
|
254
|
+
params
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def reverse_order
|
|
258
|
+
spawn.tap do |r|
|
|
259
|
+
r.instance_variable_set(:@order_values, @order_values.map { |f, d| [f, d == :asc ? :desc : :asc] })
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def parse_order_args(args)
|
|
264
|
+
args.flat_map do |arg|
|
|
265
|
+
case arg
|
|
266
|
+
when Hash
|
|
267
|
+
arg.map { |k, v| [k, normalize_direction(v)] }
|
|
268
|
+
when Symbol, String
|
|
269
|
+
[[arg, :asc]]
|
|
270
|
+
else
|
|
271
|
+
raise ArgumentError, "Invalid order argument: #{arg.inspect}"
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def normalize_direction(direction)
|
|
277
|
+
case direction.to_s.downcase
|
|
278
|
+
when "asc", "ascending"
|
|
279
|
+
:asc
|
|
280
|
+
when "desc", "descending"
|
|
281
|
+
:desc
|
|
282
|
+
else
|
|
283
|
+
raise ArgumentError, "Invalid sort direction: #{direction}"
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Airrel
|
|
4
|
+
class WhereClause
|
|
5
|
+
attr_reader :predicates
|
|
6
|
+
|
|
7
|
+
def initialize(predicates = [])
|
|
8
|
+
@predicates = predicates
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def merge(conditions)
|
|
12
|
+
new_predicates = case conditions
|
|
13
|
+
when Hash
|
|
14
|
+
[FormulaBuilder.hash_to_formula(conditions)]
|
|
15
|
+
when String
|
|
16
|
+
[conditions]
|
|
17
|
+
when Array
|
|
18
|
+
conditions
|
|
19
|
+
else
|
|
20
|
+
[conditions.to_s]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
WhereClause.new(@predicates + new_predicates)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def any?
|
|
27
|
+
@predicates.any?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def to_airtable_formula
|
|
31
|
+
return nil if @predicates.empty?
|
|
32
|
+
return @predicates.first if @predicates.size == 1
|
|
33
|
+
|
|
34
|
+
"AND(#{@predicates.join(', ')})"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def inspect
|
|
38
|
+
@predicates.inspect
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/airrel.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "airrel/version"
|
|
4
|
+
|
|
5
|
+
module Airrel
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
autoload :Relation, "airrel/relation"
|
|
9
|
+
autoload :WhereClause, "airrel/where_clause"
|
|
10
|
+
autoload :FormulaBuilder, "airrel/formula_builder"
|
|
11
|
+
end
|
data/sig/airrel.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: airrel
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- 24c02
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: norairrecord
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.1.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.1.0
|
|
26
|
+
description: chainable query interface for airtable formulas. it's like arel but AIR.
|
|
27
|
+
get it?
|
|
28
|
+
email:
|
|
29
|
+
- 163450896+24c02@users.noreply.github.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- ".idea/.gitignore"
|
|
35
|
+
- ".idea/airrel.iml"
|
|
36
|
+
- ".idea/misc.xml"
|
|
37
|
+
- ".idea/modules.xml"
|
|
38
|
+
- ".idea/vcs.xml"
|
|
39
|
+
- LICENSE.txt
|
|
40
|
+
- README.md
|
|
41
|
+
- Rakefile
|
|
42
|
+
- examples/demo.rb
|
|
43
|
+
- lib/airrel.rb
|
|
44
|
+
- lib/airrel/formula_builder.rb
|
|
45
|
+
- lib/airrel/relation.rb
|
|
46
|
+
- lib/airrel/version.rb
|
|
47
|
+
- lib/airrel/where_clause.rb
|
|
48
|
+
- sig/airrel.rbs
|
|
49
|
+
homepage: https://github.com/24c02/airrel
|
|
50
|
+
licenses:
|
|
51
|
+
- MIT
|
|
52
|
+
metadata:
|
|
53
|
+
homepage_uri: https://github.com/24c02/airrel
|
|
54
|
+
source_code_uri: https://github.com/24c02/airrel
|
|
55
|
+
rdoc_options: []
|
|
56
|
+
require_paths:
|
|
57
|
+
- lib
|
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: 3.0.0
|
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
requirements: []
|
|
69
|
+
rubygems_version: 3.6.7
|
|
70
|
+
specification_version: 4
|
|
71
|
+
summary: arel-like relational algebra for airtable
|
|
72
|
+
test_files: []
|