fmrest 0.11.1 → 0.15.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 +4 -4
- data/.yardopts +2 -0
- data/CHANGELOG.md +33 -0
- data/README.md +247 -847
- metadata +82 -101
- data/.github/workflows/ci.yml +0 -33
- data/.gitignore +0 -26
- data/.rspec +0 -3
- data/.travis.yml +0 -5
- data/Gemfile +0 -3
- data/Rakefile +0 -6
- data/fmrest.gemspec +0 -38
- data/lib/fmrest.rb +0 -36
- data/lib/fmrest/connection_settings.rb +0 -124
- data/lib/fmrest/errors.rb +0 -30
- data/lib/fmrest/spyke.rb +0 -21
- data/lib/fmrest/spyke/base.rb +0 -23
- data/lib/fmrest/spyke/container_field.rb +0 -59
- data/lib/fmrest/spyke/model.rb +0 -36
- data/lib/fmrest/spyke/model/associations.rb +0 -82
- data/lib/fmrest/spyke/model/attributes.rb +0 -171
- data/lib/fmrest/spyke/model/auth.rb +0 -43
- data/lib/fmrest/spyke/model/connection.rb +0 -135
- data/lib/fmrest/spyke/model/container_fields.rb +0 -25
- data/lib/fmrest/spyke/model/global_fields.rb +0 -40
- data/lib/fmrest/spyke/model/http.rb +0 -37
- data/lib/fmrest/spyke/model/orm.rb +0 -212
- data/lib/fmrest/spyke/model/serialization.rb +0 -99
- data/lib/fmrest/spyke/model/uri.rb +0 -30
- data/lib/fmrest/spyke/portal.rb +0 -55
- data/lib/fmrest/spyke/relation.rb +0 -359
- data/lib/fmrest/spyke/spyke_formatter.rb +0 -273
- data/lib/fmrest/spyke/validation_error.rb +0 -25
- data/lib/fmrest/string_date.rb +0 -220
- data/lib/fmrest/token_store.rb +0 -12
- data/lib/fmrest/token_store/active_record.rb +0 -74
- data/lib/fmrest/token_store/base.rb +0 -25
- data/lib/fmrest/token_store/memory.rb +0 -26
- data/lib/fmrest/token_store/moneta.rb +0 -41
- data/lib/fmrest/token_store/redis.rb +0 -45
- data/lib/fmrest/v1.rb +0 -23
- data/lib/fmrest/v1/auth.rb +0 -30
- data/lib/fmrest/v1/connection.rb +0 -115
- data/lib/fmrest/v1/container_fields.rb +0 -114
- data/lib/fmrest/v1/dates.rb +0 -81
- data/lib/fmrest/v1/paths.rb +0 -47
- data/lib/fmrest/v1/raise_errors.rb +0 -57
- data/lib/fmrest/v1/token_session.rb +0 -133
- data/lib/fmrest/v1/token_store/active_record.rb +0 -13
- data/lib/fmrest/v1/token_store/memory.rb +0 -13
- data/lib/fmrest/v1/type_coercer.rb +0 -192
- data/lib/fmrest/v1/utils.rb +0 -94
- data/lib/fmrest/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89540bf2ad2452a234fe6d42f1711d46dd3533e667980a050258c95c84c02597
|
4
|
+
data.tar.gz: ecbe0af46dcc374411c6d007a463f220a69904a15b0cfbca87160d7204cad9a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd7765bc8695fe8b63393965b807d1828ecad81d995f82987e8e36579092f7442b3f31b7a6948c423915eb51d27c7cd83f4bac6edb7f32dc7107a573cb5e3f24
|
7
|
+
data.tar.gz: b515ff6e9ae14c9b5cc69e6ede9e53e32e9580c607feb0bc613ec067b1176c8b4ff9829f564ed622f765c9b02a116eabe9f93aa567b0a6af8f2c320a1e643cd0
|
data/.yardopts
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,38 @@
|
|
1
1
|
## Changelog
|
2
2
|
|
3
|
+
### 0.15.0
|
4
|
+
|
5
|
+
* Much improved querying API (see documentation on querying), adding new
|
6
|
+
`.query` capabilities, as well as two new methods: `.match` and `.or`
|
7
|
+
|
8
|
+
### 0.14.0
|
9
|
+
|
10
|
+
* Aliased `FmRest::Spyke::Base` as `FmRest::Layout` (now preferred), and
|
11
|
+
provided a shortcut version for setting the layout name (e.g. `class Foo <
|
12
|
+
FmRest::Layout("LayoutName")`)
|
13
|
+
* Made `layout` class setting subclass-inheritable
|
14
|
+
|
15
|
+
### 0.13.1
|
16
|
+
|
17
|
+
* Fix downloading of container field data from FMS19+
|
18
|
+
|
19
|
+
### 0.13.0
|
20
|
+
|
21
|
+
* Split `fmrest` gem into `fmrest-core` and `fmrest-spyke`. `fmrest` becomes a
|
22
|
+
wrapper for the two new gems.
|
23
|
+
* Fix bug preventing connection databases with spaces in their names.
|
24
|
+
* Improved portal support with ability to delete portal records, and better
|
25
|
+
refreshing of portal records after saving the parent.
|
26
|
+
* `FmRest::Spyke::Base#__record_id` and `FmRest::Spyke::Base#__mod_id` now
|
27
|
+
always return integers if set.
|
28
|
+
|
29
|
+
### 0.12.0
|
30
|
+
|
31
|
+
* Rename `FmRest::Spyke::Base#id=` to `FmRest::Spyke::Base#__record_id=` to
|
32
|
+
prevent clobbering of FileMaker layout-defined fields
|
33
|
+
* Removed previously deprecated `FmRest::Spyke::Base(config)` syntax
|
34
|
+
* Better yard documentation
|
35
|
+
|
3
36
|
### 0.11.1
|
4
37
|
|
5
38
|
* Fix a couple crashes due to missing constants
|
data/README.md
CHANGED
@@ -7,117 +7,151 @@ A Ruby client for
|
|
7
7
|
[FileMaker 18 and 19's Data API](https://help.claris.com/en/data-api-guide)
|
8
8
|
using
|
9
9
|
[Faraday](https://github.com/lostisland/faraday) and with optional
|
10
|
-
[Spyke](https://github.com/balvig/spyke)
|
11
|
-
|
12
|
-
If you're looking for a Ruby client for the legacy XML/Custom Web Publishing
|
13
|
-
API try the fabulous [ginjo-rfm gem](https://github.com/ginjo/rfm) instead.
|
10
|
+
ActiveRecord-ish ORM features through [Spyke](https://github.com/balvig/spyke).
|
14
11
|
|
15
12
|
fmrest-ruby only partially implements FileMaker 18's Data API.
|
16
13
|
See the [implementation completeness table](#api-implementation-completeness-table)
|
17
14
|
to see if a feature you need is natively supported by the gem.
|
18
15
|
|
16
|
+
## Gems
|
17
|
+
|
18
|
+
The `fmrest` gem is a wrapper for two other gems:
|
19
|
+
|
20
|
+
* `fmrest-spyke`, providing an ActiveRecord-like ORM library built on top
|
21
|
+
of `fmrest-core` and [Spyke](https://github.com/balvig/spyke).
|
22
|
+
* `fmrest-core`, providing the core Faraday connection builder, session
|
23
|
+
management, and other core utilities.
|
24
|
+
|
19
25
|
## Installation
|
20
26
|
|
21
|
-
Add this
|
27
|
+
Add this to your Gemfile:
|
22
28
|
|
23
29
|
```ruby
|
24
30
|
gem 'fmrest'
|
25
|
-
|
26
|
-
# Optional but recommended (for ORM features)
|
27
|
-
gem 'spyke'
|
28
31
|
```
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
To get a Faraday connection that can handle FM's Data API auth workflow:
|
33
|
+
Or if you just want to use the Faraday connection without the ORM features:
|
33
34
|
|
34
35
|
```ruby
|
35
|
-
|
36
|
-
host: "example.com",
|
37
|
-
database: "database name",
|
38
|
-
username: "username",
|
39
|
-
password: "password"
|
40
|
-
)
|
36
|
+
gem 'fmrest-core'
|
41
37
|
```
|
42
38
|
|
43
|
-
|
44
|
-
|
45
|
-
|
39
|
+
## Simple examples
|
40
|
+
|
41
|
+
### ORM example
|
46
42
|
|
47
|
-
|
43
|
+
Most people would want to use the ORM features:
|
48
44
|
|
49
45
|
```ruby
|
50
|
-
#
|
51
|
-
|
46
|
+
# A Layout model connecting to the "Honeybees Web" FileMaker layout
|
47
|
+
class Honeybee < FmRest::Layout("Honeybees Web")
|
48
|
+
# Connection settings
|
49
|
+
self.fmrest_config = {
|
50
|
+
host: "…",
|
51
|
+
database: "…",
|
52
|
+
username: "…",
|
53
|
+
password: "…"
|
54
|
+
}
|
52
55
|
|
53
|
-
#
|
54
|
-
|
55
|
-
req.url "layouts/MyFancyLayout/records"
|
56
|
+
# Mapped attributes
|
57
|
+
attributes name: "Bee Name", age: "Bee Age", created_on: "Created On"
|
56
58
|
|
57
|
-
#
|
58
|
-
|
59
|
+
# Portal associations
|
60
|
+
has_portal :tasks
|
61
|
+
|
62
|
+
# File containers
|
63
|
+
container :photo, field_name: "Bee Photo"
|
64
|
+
|
65
|
+
# Scopes
|
66
|
+
scope :can_legally_fly, -> { query(age: ">18") }
|
67
|
+
|
68
|
+
# Client-side validations
|
69
|
+
validates :name, presence: true
|
70
|
+
|
71
|
+
# Callbacks
|
72
|
+
before_save :set_created_on
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def set_created_on
|
77
|
+
self.created_on = Date.today
|
78
|
+
end
|
59
79
|
end
|
60
|
-
```
|
61
80
|
|
62
|
-
|
63
|
-
|
81
|
+
# Find a record by id
|
82
|
+
bee = Honeybee.find(9)
|
64
83
|
|
65
|
-
|
84
|
+
bee.name = "Hutch"
|
85
|
+
|
86
|
+
# Add a new record to portal
|
87
|
+
bee.tasks.build(urgency: "Today")
|
66
88
|
|
67
|
-
|
68
|
-
|
69
|
-
in order to log out from the session
|
70
|
-
([see docs](https://fmhelp.filemaker.com/docs/18/en/dataapi/#connect-database_log-out)).
|
89
|
+
bee.save
|
90
|
+
```
|
71
91
|
|
72
|
-
|
73
|
-
token is required to build the logout URL, this becomes a non-trivial action.
|
92
|
+
### Barebones connection example (without ORM)
|
74
93
|
|
75
|
-
|
76
|
-
|
77
|
-
|
94
|
+
In case you don't need the advanced ORM features (e.g. if you only need minimal
|
95
|
+
Data API interaction and just want a lightweight solution) you can simply use
|
96
|
+
the Faraday connection provided by `fmrest-core`:
|
78
97
|
|
79
98
|
```ruby
|
80
|
-
|
81
|
-
|
99
|
+
connection = FmRest::V1.build_connection(
|
100
|
+
host: "…",
|
101
|
+
database: "…",
|
102
|
+
username: "…",
|
103
|
+
password: "…"
|
104
|
+
)
|
105
|
+
|
106
|
+
# Get all records (as parsed JSON)
|
107
|
+
connection.get("layouts/FancyLayout/records")
|
108
|
+
|
109
|
+
# Create new record
|
110
|
+
connection.post do |req|
|
111
|
+
req.url "layouts/FancyLayout/records"
|
112
|
+
|
113
|
+
# You can just pass a hash for the JSON body
|
114
|
+
req.body = { … }
|
115
|
+
end
|
82
116
|
```
|
83
117
|
|
84
|
-
|
85
|
-
|
118
|
+
See the [main document on using the base
|
119
|
+
connection](docs/BaseConnectionUsage.md) for more.
|
86
120
|
|
87
121
|
## Connection settings
|
88
122
|
|
89
|
-
|
90
|
-
|
91
|
-
|
123
|
+
The minimum required connection settings are `:host`, `:database`, `:username`
|
124
|
+
and `:password`, but fmrest-ruby has many other options you can pass when
|
125
|
+
setting up a connection (see [full list](#full-list-of-available-options) below).
|
92
126
|
|
93
|
-
|
127
|
+
`:ssl` and `:proxy` are forwarded to the underlying
|
128
|
+
[Faraday](https://github.com/lostisland/faraday) connection. You can use this
|
129
|
+
to, for instance, disable SSL verification:
|
94
130
|
|
95
131
|
```ruby
|
96
|
-
|
97
|
-
host:
|
98
|
-
|
99
|
-
ssl:
|
100
|
-
|
132
|
+
{
|
133
|
+
host: "…",
|
134
|
+
…
|
135
|
+
ssl: { verify: false }
|
136
|
+
}
|
101
137
|
```
|
102
138
|
|
103
139
|
You can also pass a `:log` option for basic request logging, see the section on
|
104
140
|
[Logging](#Logging) below.
|
105
141
|
|
106
|
-
`:username` is also aliased as `:account_name` to provide cross-compatibility
|
107
|
-
with the ginjo-rfm gem.
|
108
|
-
|
109
142
|
### Full list of available options
|
110
143
|
|
111
144
|
Option | Description | Format | Default
|
112
145
|
--------------------|--------------------------------------------|-----------------------------|--------
|
113
146
|
`:host` | Hostname with optional port, e.g. `"example.com:9000"` | String | None
|
114
|
-
`:database` |
|
115
|
-
`:username` |
|
116
|
-
`:password` |
|
147
|
+
`:database` | The name of the database to connect to | String | None
|
148
|
+
`:username` | A Data API-ready account | String | None
|
149
|
+
`:password` | Your password | String | None
|
150
|
+
`:account_name` | Alias of `:username` | String | None
|
117
151
|
`:ssl` | SSL options to be forwarded to Faraday | Faraday SSL options | None
|
118
152
|
`:proxy` | Proxy options to be forwarded to Faraday | Faraday proxy options | None
|
119
153
|
`:log` | Log JSON responses to STDOUT | Boolean | `false`
|
120
|
-
`:coerce_dates` | See section on [date fields](#date-fields) | Boolean \| `:hybrid` \| `:full` | `false`
|
154
|
+
`:coerce_dates` | See section on [date fields](#date-fields-and-timezones) | Boolean \| `:hybrid` \| `:full` | `false`
|
121
155
|
`:date_format` | Date parsing format | String (FM date format) | `"MM/dd/yyyy"`
|
122
156
|
`:timestamp_format` | Timestmap parsing format | String (FM date format) | `"MM/dd/yyyy HH:mm:ss"`
|
123
157
|
`:time_format` | Time parsing format | String (FM date format) | `"HH:mm:ss"`
|
@@ -132,232 +166,62 @@ through `FmRest.default_connection_settings=`. E.g.:
|
|
132
166
|
|
133
167
|
```ruby
|
134
168
|
FmRest.default_connection_settings = {
|
135
|
-
host: "
|
136
|
-
database: "
|
137
|
-
username: "
|
138
|
-
password: "
|
169
|
+
host: "…",
|
170
|
+
database: "…",
|
171
|
+
username: "…",
|
172
|
+
password: "…"
|
139
173
|
}
|
140
174
|
```
|
141
175
|
|
142
|
-
|
143
|
-
|
144
|
-
|
176
|
+
These settings will be used by default by `FmRest::Layout` models whenever you
|
177
|
+
don't set `fmrest_config=` explicitly, as well as by
|
178
|
+
`FmRest::V1.build_connection` in case you're setting up your Faraday connection
|
179
|
+
manually.
|
145
180
|
|
146
181
|
## Session token store
|
147
182
|
|
148
|
-
|
149
|
-
This is generally good enough for development, but not good enough for
|
150
|
-
production, as in-memory tokens aren't shared across threads/processes.
|
151
|
-
|
152
|
-
Besides the default token store the following token stores are bundled with fmrest-ruby:
|
153
|
-
|
154
|
-
### ActiveRecord
|
155
|
-
|
156
|
-
On Rails apps already using ActiveRecord setting up this token store should be
|
157
|
-
dead simple:
|
158
|
-
|
159
|
-
```ruby
|
160
|
-
# config/initializers/fmrest.rb
|
161
|
-
require "fmrest/token_store/active_record"
|
162
|
-
|
163
|
-
FmRest.token_store = FmRest::TokenStore::ActiveRecord
|
164
|
-
```
|
165
|
-
|
166
|
-
No migrations are needed, the token store table will be created automatically
|
167
|
-
when needed, defaulting to the table name "fmrest_session_tokens". If you want
|
168
|
-
to change the table name you can do so by initializing the token store and
|
169
|
-
passing it the `:table_name` option:
|
170
|
-
|
171
|
-
```ruby
|
172
|
-
FmRest.token_store = FmRest::TokenStore::ActiveRecord.new(table_name: "my_token_store")
|
173
|
-
```
|
174
|
-
|
175
|
-
### Redis
|
176
|
-
|
177
|
-
To use the Redis token store do:
|
178
|
-
|
179
|
-
```ruby
|
180
|
-
require "fmrest/token_store/redis"
|
181
|
-
|
182
|
-
FmRest.token_store = FmRest::TokenStore::Redis
|
183
|
-
```
|
184
|
-
|
185
|
-
You can also initialize it with the following options:
|
186
|
-
|
187
|
-
* `:redis` - A `Redis` object to use as connection, if ommited a new `Redis`
|
188
|
-
object will be created with remaining options
|
189
|
-
* `:prefix` - The prefix to use for token keys, by default `"fmrest-token:"`
|
190
|
-
* Any other options will be passed to `Redis.new` if `:redis` isn't provided
|
191
|
-
|
192
|
-
Examples:
|
193
|
-
|
194
|
-
```ruby
|
195
|
-
# Passing a Redis connection explicitly
|
196
|
-
FmRest.token_store = FmRest::TokenStore::Redis.new(redis: Redis.new, prefix: "my-fancy-prefix:")
|
197
|
-
|
198
|
-
# Passing options for Redis.new
|
199
|
-
FmRest.token_store = FmRest::TokenStore::Redis.new(prefix: "my-fancy-prefix:", host: "10.0.1.1", port: 6380, db: 15)
|
200
|
-
```
|
201
|
-
|
202
|
-
NOTE: redis-rb is not included as a gem dependency of fmrest-ruby, so you'll
|
203
|
-
have to add it to your Gemfile.
|
204
|
-
|
205
|
-
### Moneta
|
206
|
-
|
207
|
-
[Moneta](https://github.com/moneta-rb/moneta) is a key/value store wrapper
|
208
|
-
around many different storage backends. If ActiveRecord or Redis don't suit
|
209
|
-
your needs, chances are Moneta will.
|
210
|
-
|
211
|
-
To use it:
|
212
|
-
|
213
|
-
```ruby
|
214
|
-
# config/initializers/fmrest.rb
|
215
|
-
require "fmrest/token_store/moneta"
|
216
|
-
|
217
|
-
FmRest.token_store = FmRest::TokenStore::Moneta
|
218
|
-
```
|
219
|
-
|
220
|
-
By default the `:Memory` moneta backend will be used.
|
221
|
-
|
222
|
-
You can also initialize it with the following options:
|
223
|
-
|
224
|
-
* `:backend` - The moneta backend to initialize the store with
|
225
|
-
* `:prefix` - The prefix to use for token keys, by default `"fmrest-token:"`
|
226
|
-
* Any other options will be passed to `Moneta.new`
|
227
|
-
|
228
|
-
Examples:
|
229
|
-
|
230
|
-
```ruby
|
231
|
-
# Using YAML as a backend with a custom prefix
|
232
|
-
FmRest.token_store = FmRest::TokenStore::Moneta.new(
|
233
|
-
backend: :YAML,
|
234
|
-
file: "tmp/tokens.yml",
|
235
|
-
prefix: "my-tokens"
|
236
|
-
)
|
237
|
-
```
|
238
|
-
|
239
|
-
NOTE: the moneta gem is not included as a dependency of fmrest-ruby, so
|
240
|
-
you'll have to add it to your Gemfile.
|
241
|
-
|
242
|
-
|
243
|
-
## Date fields
|
244
|
-
|
245
|
-
Since the Data API uses JSON (wich doesn't provide a native date/time object),
|
246
|
-
dates and timestamps are received in string format. By default fmrest-ruby
|
247
|
-
leaves those string fields untouched, but it provides an opt-in feature to try
|
248
|
-
to automatically "coerce" them into Ruby date objects.
|
249
|
-
|
250
|
-
The connection option `:coerce_dates` controls this feature. Possible values
|
251
|
-
are:
|
252
|
-
|
253
|
-
* `:full` - whenever a string matches the given date/timestamp/time format,
|
254
|
-
convert them to `Date` or `DateTime` objects as appropriate
|
255
|
-
* `:hybrid` or `true` - similar as above, but instead of converting to regular
|
256
|
-
`Date`/`DateTime` it converts strings to `FmRest::StringDate` and
|
257
|
-
`FmRest::StringDateTime`, "hybrid" classes provided by fmrest-ruby that
|
258
|
-
retain the functionality of `String` while also providing most the
|
259
|
-
functionality of `Date`/`DateTime` (more on this below)
|
260
|
-
* `false` - disable date coercion entirely (default), leave original string
|
261
|
-
values untouched
|
262
|
-
|
263
|
-
Enabling date coercion works with both basic fmrest-ruby connections and Spyke
|
264
|
-
models (ORM).
|
183
|
+
fmrest-ruby includes a number of options for storing session tokens:
|
265
184
|
|
266
|
-
|
267
|
-
|
268
|
-
|
185
|
+
* Memory
|
186
|
+
* ActiveRecord
|
187
|
+
* Redis
|
188
|
+
* Moneta
|
269
189
|
|
270
|
-
|
271
|
-
|
272
|
-
a DSL in model classes).
|
190
|
+
See the [main document on token stores](docs/TokenStore.md) for detailed info
|
191
|
+
on how to set up each store.
|
273
192
|
|
274
|
-
|
193
|
+
## Date fields and timezones
|
275
194
|
|
276
|
-
|
277
|
-
|
278
|
-
and delegate any methods not provided by `String` to those objects. In other
|
279
|
-
words, they quack like a duck *and* bark like a dog.
|
195
|
+
fmrest-ruby has automatic detection and coercion of date fields to and from
|
196
|
+
Ruby date/time objects. Basic timezone support is also provided.
|
280
197
|
|
281
|
-
|
282
|
-
but you don't want to worry about date coercion of false positives (i.e. a
|
283
|
-
string field that gets converted to `Date` because it just so matched the given
|
284
|
-
date format).
|
198
|
+
See the [main document on date fields](docs/DateFields.md) for more info.
|
285
199
|
|
286
|
-
|
287
|
-
(see GitHub wiki for more info). Some of those gothas can be removed by calling
|
288
|
-
|
289
|
-
```ruby
|
290
|
-
FmRest::StringDateAwareness.enable
|
291
|
-
```
|
292
|
-
|
293
|
-
Which will extend the core `Date` and `DateTime` classes to be aware of
|
294
|
-
`FmRest::StringDate`, especially when calling `Date.===`, `Date.parse` or
|
295
|
-
`Date._parse`.
|
296
|
-
|
297
|
-
If you're working with ActiveRecord models this will also make them accept
|
298
|
-
`FmRest::StringDate` values for date fields.
|
299
|
-
|
300
|
-
### Timezones
|
301
|
-
|
302
|
-
fmrest-ruby has basic timezone support. You can set the `:timezone` option in
|
303
|
-
your connection settings to one of the following values:
|
304
|
-
|
305
|
-
* `:local` - dates will be converted to your system local time offset (as
|
306
|
-
defined by `ENV["TZ"]`), or the timezone set by `Time.zone` if you're using
|
307
|
-
ActiveSupport
|
308
|
-
* `:utc` - dates will be converted to UTC offset
|
309
|
-
* `nil` - (default) ignore timezones altogether
|
310
|
-
|
311
|
-
|
312
|
-
## Spyke support (ActiveRecord-like ORM)
|
200
|
+
## ActiveRecord-like ORM (fmrest-spyke)
|
313
201
|
|
314
202
|
[Spyke](https://github.com/balvig/spyke) is an ActiveRecord-like gem for
|
315
|
-
building REST models. fmrest-ruby
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
```ruby
|
320
|
-
gem 'spyke'
|
321
|
-
```
|
322
|
-
|
323
|
-
Then require fmrest-ruby's Spyke support:
|
324
|
-
|
325
|
-
```ruby
|
326
|
-
# Put this in config/initializers/fmrest.rb if it's a Rails project
|
327
|
-
require "fmrest/spyke"
|
328
|
-
```
|
203
|
+
building REST ORM models. fmrest-ruby builds its ORM features atop Spyke,
|
204
|
+
bundled in the `fmrest-spyke` gem (already included if you're using the
|
205
|
+
`fmrest` gem).
|
329
206
|
|
330
|
-
|
207
|
+
To create a model you can inherit directly from `FmRest::Layout` (itself a
|
208
|
+
subclass of `Spyke::Base`).
|
331
209
|
|
332
210
|
```ruby
|
333
|
-
class Honeybee <
|
334
|
-
include FmRest::Spyke
|
211
|
+
class Honeybee < FmRest::Layout
|
335
212
|
end
|
336
213
|
```
|
337
214
|
|
338
|
-
|
339
|
-
token session auth. Find, create, update and destroy actions should all work
|
340
|
-
as expected.
|
341
|
-
|
342
|
-
Alternatively you can inherit directly from the shorthand
|
343
|
-
`FmRest::Spyke::Base`, which is in itself a subclass of `Spyke::Base` with
|
344
|
-
`FmRest::Spyke` already included:
|
345
|
-
|
346
|
-
```ruby
|
347
|
-
class Honeybee < FmRest::Spyke::Base
|
348
|
-
end
|
349
|
-
```
|
350
|
-
|
351
|
-
All of Spyke's basic ORM operations work:
|
215
|
+
All of Spyke's basic ORM operations work as expected:
|
352
216
|
|
353
217
|
```ruby
|
354
218
|
bee = Honeybee.new
|
355
219
|
|
356
220
|
bee.name = "Hutch"
|
357
|
-
bee.save # POST request
|
221
|
+
bee.save # POST request (creates new record)
|
358
222
|
|
359
223
|
bee.name = "ハッチ"
|
360
|
-
bee.save # PATCH request
|
224
|
+
bee.save # PATCH request (updates existing record)
|
361
225
|
|
362
226
|
bee.reload # GET request
|
363
227
|
|
@@ -366,54 +230,44 @@ bee.destroy # DELETE request
|
|
366
230
|
bee = Honeybee.find(9) # GET request
|
367
231
|
```
|
368
232
|
|
369
|
-
|
233
|
+
It's recommended that you read Spyke's documentation for more information on
|
234
|
+
these basic features. If you've used ActiveRecord or similar ORM libraries
|
235
|
+
you'll find it quite familiar.
|
236
|
+
|
237
|
+
Notice that `FmRest::Layout` is aliased as `FmRest::Spyke::Base`. Previous
|
238
|
+
versions of fmrest-ruby only provided the latter version, so if you're already
|
239
|
+
using `FmRest::Spyke::Base` there's no need to rename your classes to
|
240
|
+
`FmRest::Layout`, both will continue to work interchangeably.
|
370
241
|
|
371
|
-
In addition `FmRest::
|
242
|
+
In addition, `FmRest::Layout` extends `Spyke::Base` with the following
|
372
243
|
features:
|
373
244
|
|
374
|
-
###
|
245
|
+
### FmRest::Layout.fmrest_config=
|
375
246
|
|
376
|
-
|
247
|
+
This allows you to set Data API connection settings specific to your model
|
248
|
+
class:
|
377
249
|
|
378
250
|
```ruby
|
379
|
-
class Honeybee <
|
380
|
-
self.connection = Faraday.new(...)
|
381
|
-
end
|
382
|
-
```
|
383
|
-
|
384
|
-
fmrest-ruby simplfies the process of setting up your Spyke model with a Faraday
|
385
|
-
connection by allowing you to just set your Data API connection settings:
|
386
|
-
|
387
|
-
```ruby
|
388
|
-
class Honeybee < Spyke::Base
|
389
|
-
include FmRest::Spyke
|
390
|
-
|
251
|
+
class Honeybee < FmRest::Layout
|
391
252
|
self.fmrest_config = {
|
392
|
-
host: "
|
393
|
-
database: "
|
394
|
-
username: "
|
395
|
-
password: "
|
253
|
+
host: "…",
|
254
|
+
database: "…",
|
255
|
+
username: "…",
|
256
|
+
password: "…"
|
396
257
|
}
|
397
258
|
end
|
398
259
|
```
|
399
260
|
|
400
|
-
This will automatically create a proper Faraday connection
|
401
|
-
settings.
|
261
|
+
This will automatically create a proper Faraday connection using those
|
262
|
+
connection settings, so you don't have to worry about setting that up.
|
402
263
|
|
403
264
|
Note that these settings are inheritable, so you could create a base class that
|
404
265
|
does the initial connection setup and then inherit from it in models using that
|
405
266
|
same connection. E.g.:
|
406
267
|
|
407
268
|
```ruby
|
408
|
-
class BeeBase <
|
409
|
-
|
410
|
-
|
411
|
-
self.fmrest_config = {
|
412
|
-
host: "example.com",
|
413
|
-
database: "My Database",
|
414
|
-
username: "...",
|
415
|
-
password: "..."
|
416
|
-
}
|
269
|
+
class BeeBase < FmRest::Layout
|
270
|
+
self.fmrest_config = { host: "…", database: "…", … }
|
417
271
|
end
|
418
272
|
|
419
273
|
class Honeybee < BeeBase
|
@@ -421,76 +275,46 @@ class Honeybee < BeeBase
|
|
421
275
|
end
|
422
276
|
```
|
423
277
|
|
424
|
-
|
425
|
-
|
426
|
-
There may be cases where you want to use different connection settings
|
427
|
-
depending on context, for example if you want to use username and password
|
428
|
-
provided by the user in a web application. Since `Model.fmrest_config` is
|
429
|
-
global, changing the username/password for one context would also change it for
|
430
|
-
all other contexts, leading to security issues.
|
431
|
-
|
432
|
-
`Model.fmrest_config_overlay=` solves that issue by allowing you to override
|
433
|
-
some settings in a thread-local and reversible manner. That way, using the same
|
434
|
-
example as above, you could connect to the Data API with user-provided
|
435
|
-
credentials without having them leak into other users of your web application.
|
436
|
-
|
437
|
-
E.g.:
|
438
|
-
|
439
|
-
```ruby
|
440
|
-
class BeeBase < Spyke::Base
|
441
|
-
include FmRest::Spyke
|
442
|
-
|
443
|
-
# Host and database provided as base settings
|
444
|
-
self.fmrest_config = {
|
445
|
-
host: "example.com",
|
446
|
-
database: "My Database"
|
447
|
-
}
|
448
|
-
end
|
449
|
-
|
450
|
-
# E.g. in a controller-action of a Rails application:
|
278
|
+
Also, if not set, your model will try to use
|
279
|
+
`FmRest.default_connection_settings` instead.
|
451
280
|
|
452
|
-
|
453
|
-
BeeBase.fmrest_config_overlay = {
|
454
|
-
username: params[:username],
|
455
|
-
password: params[:password]
|
456
|
-
}
|
281
|
+
#### Connection settings overlays
|
457
282
|
|
458
|
-
|
459
|
-
|
283
|
+
There may be cases where you want to use a different set of connection settings
|
284
|
+
depending on context. For example, if you want to use username and password
|
285
|
+
provided by the user in a web application. Since `.fmrest_config`
|
286
|
+
is set at the class level, changing the username/password for the model in one
|
287
|
+
context would also change it in all other contexts, leading to security issues.
|
460
288
|
|
461
|
-
|
289
|
+
To solve this scenario, fmrest-ruby provides a way of defining thread-local and
|
290
|
+
reversible connection settings overlays through
|
291
|
+
`.fmrest_config_overlay=`.
|
462
292
|
|
463
|
-
|
293
|
+
See the [main document on connection setting overlays](docs/ConfigOverlays.md)
|
294
|
+
for details on how it works.
|
464
295
|
|
465
|
-
###
|
296
|
+
### FmRest::Layout.layout
|
466
297
|
|
467
|
-
|
468
|
-
finishes running. It wraps execution in its own fiber, so it doesn't affect the
|
469
|
-
overlay of the currently-running thread.
|
298
|
+
Use `layout` to set the layout name for your model.
|
470
299
|
|
471
300
|
```ruby
|
472
|
-
Honeybee
|
473
|
-
|
301
|
+
class Honeybee < FmRest::Layout
|
302
|
+
layout "Honeybees Web"
|
474
303
|
end
|
475
304
|
```
|
476
305
|
|
477
|
-
|
478
|
-
|
479
|
-
Use `layout` to set the `:layout` part of API URLs, e.g.:
|
306
|
+
Alternatively, if you're inheriting from `FmRest::Layout` directly you can set
|
307
|
+
the layout name in the class definition line:
|
480
308
|
|
481
309
|
```ruby
|
482
|
-
class Honeybee < FmRest::
|
483
|
-
layout "Honeybees Web" # uri path will be "layouts/Honeybees%20Web/records(/:id)"
|
484
|
-
end
|
310
|
+
class Honeybee < FmRest::Layout("Honeybees Web")
|
485
311
|
```
|
486
312
|
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
Note that you only need to set this if the name of the model and the name of
|
491
|
-
the layout differ, otherwise the default will just work.
|
313
|
+
Note that you only need to manually set the layout name if the name of the
|
314
|
+
class and the name of the layout differ, otherwise fmrest-ruby will just use
|
315
|
+
the name of the class.
|
492
316
|
|
493
|
-
###
|
317
|
+
### FmRest::Layout.request_auth_token
|
494
318
|
|
495
319
|
Requests a Data API session token using the connection settings in
|
496
320
|
`fmrest_config` and returns it if successful, otherwise returns `false`.
|
@@ -499,16 +323,16 @@ You normally don't need to use this method as fmrest-ruby will automatically
|
|
499
323
|
request and store session tokens for you (provided that `:autologin` is
|
500
324
|
`true`).
|
501
325
|
|
502
|
-
###
|
326
|
+
### FmRest::Layout.logout
|
503
327
|
|
504
|
-
Use
|
505
|
-
that uses the database session you want to log out from).
|
328
|
+
Use `.logout` to log out from the database session (you may call it on any
|
329
|
+
model that uses the database session you want to log out from).
|
506
330
|
|
507
331
|
```ruby
|
508
332
|
Honeybee.logout
|
509
333
|
```
|
510
334
|
|
511
|
-
### Mapped
|
335
|
+
### Mapped FmRest::Layout.attributes
|
512
336
|
|
513
337
|
Spyke allows you to define your model's attributes using `attributes`, however
|
514
338
|
sometimes FileMaker's field names aren't very Ruby-ORM-friendly, especially
|
@@ -517,7 +341,7 @@ fmrest-ruby extends `attributes`' functionality to allow you to map
|
|
517
341
|
Ruby-friendly attribute names to FileMaker field names. E.g.:
|
518
342
|
|
519
343
|
```ruby
|
520
|
-
class Honeybee < FmRest::
|
344
|
+
class Honeybee < FmRest::Layout
|
521
345
|
attributes first_name: "First Name", last_name: "Last Name"
|
522
346
|
end
|
523
347
|
```
|
@@ -536,62 +360,21 @@ bee.first_name = "Queen"
|
|
536
360
|
bee.attributes # => { "First Name": "Queen", "Last Name": "Buzz" }
|
537
361
|
```
|
538
362
|
|
539
|
-
###
|
363
|
+
### FmRest::Layout.has_portal
|
540
364
|
|
541
|
-
You can define portal associations on your model as such:
|
365
|
+
You can define portal associations on your model wth `has_portal`, as such:
|
542
366
|
|
543
367
|
```ruby
|
544
|
-
class Honeybee < FmRest::
|
368
|
+
class Honeybee < FmRest::Layout
|
545
369
|
has_portal :flowers
|
546
370
|
end
|
547
371
|
|
548
|
-
class Flower < FmRest::
|
372
|
+
class Flower < FmRest::Layout
|
549
373
|
attributes :color, :species
|
550
374
|
end
|
551
375
|
```
|
552
376
|
|
553
|
-
|
554
|
-
name to be both "flowers", i.e. the expected portal JSON portion should look
|
555
|
-
like this:
|
556
|
-
|
557
|
-
```json
|
558
|
-
...
|
559
|
-
"portalData": {
|
560
|
-
"flowers": [
|
561
|
-
{
|
562
|
-
"flowers::color": "red",
|
563
|
-
"flowers::species": "rose"
|
564
|
-
}
|
565
|
-
]
|
566
|
-
}
|
567
|
-
```
|
568
|
-
|
569
|
-
If you need to specify different values for them you can do so with
|
570
|
-
`portal_key` for the portal table name, and `attribute_prefix` for the portal
|
571
|
-
object name, and `class_name`, e.g.:
|
572
|
-
|
573
|
-
```ruby
|
574
|
-
class Honeybee < FmRest::Spyke::Base
|
575
|
-
has_portal :pollinated_flowers, portal_key: "Bee Flowers",
|
576
|
-
attribute_prefix: "Flower",
|
577
|
-
class_name: "Flower"
|
578
|
-
end
|
579
|
-
```
|
580
|
-
|
581
|
-
The above will use the `Flower` model class and expects the following portal JSON
|
582
|
-
portion:
|
583
|
-
|
584
|
-
```json
|
585
|
-
...
|
586
|
-
"portalData": {
|
587
|
-
"Bee Flowers": [
|
588
|
-
{
|
589
|
-
"Flower::color": "white",
|
590
|
-
"Flower::species": "rose"
|
591
|
-
}
|
592
|
-
]
|
593
|
-
}
|
594
|
-
```
|
377
|
+
See the [main document on portal associations](docs/Portals.md) for details.
|
595
378
|
|
596
379
|
### Dirty attributes
|
597
380
|
|
@@ -621,208 +404,11 @@ Guides](https://guides.rubyonrails.org/active_model_basics.html#dirty).
|
|
621
404
|
Since Spyke is API-agnostic it only provides a wide-purpose `.where` method for
|
622
405
|
passing arbitrary parameters to the REST backend. fmrest-ruby however is well
|
623
406
|
aware of its backend API, so it extends Spkye models with a bunch of useful
|
624
|
-
querying methods
|
625
|
-
|
626
|
-
#### .limit
|
627
|
-
|
628
|
-
`.limit` sets the limit for get and find request:
|
629
|
-
|
630
|
-
```ruby
|
631
|
-
Honeybee.limit(10)
|
632
|
-
```
|
633
|
-
|
634
|
-
NOTE: You can also set a default limit value for a model class, see
|
635
|
-
[other notes on querying](#other-notes-on-querying).
|
636
|
-
|
637
|
-
You can also use `.limit` to set limits on portals:
|
638
|
-
|
639
|
-
```ruby
|
640
|
-
Honeybee.limit(hives: 3, flowers: 2)
|
641
|
-
```
|
642
|
-
|
643
|
-
To remove the limit on a portal set it to `nil`:
|
644
|
-
|
645
|
-
```ruby
|
646
|
-
Honeybee.limit(flowers: nil)
|
647
|
-
```
|
648
|
-
|
649
|
-
#### .offset
|
650
|
-
|
651
|
-
`.offset` sets the offset for get and find requests:
|
652
|
-
|
653
|
-
```ruby
|
654
|
-
Honeybee.offset(10)
|
655
|
-
```
|
656
|
-
|
657
|
-
You can also use `.offset` to set offsets on portals:
|
658
|
-
|
659
|
-
```ruby
|
660
|
-
Honeybee.offset(hives: 3, flowers: 2)
|
661
|
-
```
|
662
|
-
|
663
|
-
To remove the offset on a portal set it to `nil`:
|
664
|
-
|
665
|
-
```ruby
|
666
|
-
Honeybee.offset(flowers: nil)
|
667
|
-
```
|
668
|
-
|
669
|
-
#### .sort
|
670
|
-
|
671
|
-
`.sort` (or `.order`) sets sorting options for get and find requests:
|
672
|
-
|
673
|
-
```ruby
|
674
|
-
Honeybee.sort(:name, :age)
|
675
|
-
Honeybee.order(:name, :age) # alias method
|
676
|
-
```
|
677
|
-
|
678
|
-
You can set descending sort order by appending either `!` or `__desc` to a sort
|
679
|
-
attribute (defaults to ascending order):
|
680
|
-
|
681
|
-
```ruby
|
682
|
-
Honeybee.sort(:name, :age!)
|
683
|
-
Honeybee.sort(:name, :age__desc)
|
684
|
-
```
|
685
|
-
|
686
|
-
NOTE: You can also set default sort values for a model class, see
|
687
|
-
[Other notes on querying](#other-notes-on-querying).
|
688
|
-
|
689
|
-
#### .portal
|
690
|
-
|
691
|
-
`.portal` (aliased as `.includes` and `.portals`) sets which portals to fetch
|
692
|
-
(if any) for get and find requests (this recognizes portals defined with
|
693
|
-
`has_portal`):
|
694
|
-
|
695
|
-
```ruby
|
696
|
-
Honeybee.portal(:hives) # include just the :hives portal
|
697
|
-
Honeybee.includes(:hives) # alias method
|
698
|
-
Honeybee.portals(:hives, :flowers) # alias for pluralization fundamentalists
|
699
|
-
```
|
700
|
-
|
701
|
-
Chaining calls to `.portal` will add portals to the existing included list:
|
702
|
-
|
703
|
-
```ruby
|
704
|
-
Honeybee.portal(:flowers).portal(:hives) # include both portals
|
705
|
-
```
|
706
|
-
|
707
|
-
If you want to disable portals for the scope call `.portal(false)`:
|
708
|
-
|
709
|
-
```ruby
|
710
|
-
Honeybee.portal(false) # disable portals for this scope
|
711
|
-
```
|
712
|
-
|
713
|
-
If you want to include all portals call `.portal(true)`:
|
714
|
-
|
715
|
-
```ruby
|
716
|
-
Honeybee.portal(true) # include all portals
|
717
|
-
```
|
718
|
-
|
719
|
-
For convenience you can also use `.with_all_portals` and `.without_portals`,
|
720
|
-
which behave just as calling `.portal(true)` and `portal(false)` respectively.
|
721
|
-
|
722
|
-
NOTE: By default all portals are included.
|
723
|
-
|
724
|
-
#### .query
|
725
|
-
|
726
|
-
`.query` sets query conditions for a find request (and supports attributes as
|
727
|
-
defined with `attributes`):
|
728
|
-
|
729
|
-
```ruby
|
730
|
-
Honeybee.query(name: "Hutch")
|
731
|
-
# JSON -> {"query": [{"Bee Name": "Hutch"}]}
|
732
|
-
```
|
733
|
-
|
734
|
-
Passing multiple attributes to `.query` will group them in the same JSON object:
|
735
|
-
|
736
|
-
```ruby
|
737
|
-
Honeybee.query(name: "Hutch", age: 4)
|
738
|
-
# JSON -> {"query": [{"Bee Name": "Hutch", "Bee Age": 4}]}
|
739
|
-
```
|
740
|
-
|
741
|
-
Calling `.query` multiple times or passing it multiple hashes creates separate
|
742
|
-
JSON objects (so you can define OR queries):
|
743
|
-
|
744
|
-
```ruby
|
745
|
-
Honeybee.query(name: "Hutch").query(name: "Maya")
|
746
|
-
Honeybee.query({ name: "Hutch" }, { name: "Maya" })
|
747
|
-
# JSON -> {"query": [{"Bee Name": "Hutch"}, {"Bee Name": "Maya"}]}
|
748
|
-
```
|
749
|
-
|
750
|
-
#### .omit
|
751
|
-
|
752
|
-
`.omit` works like `.query` but excludes matches:
|
753
|
-
|
754
|
-
```ruby
|
755
|
-
Honeybee.omit(name: "Hutch")
|
756
|
-
# JSON -> {"query": [{"Bee Name": "Hutch", "omit": "true"}]}
|
757
|
-
```
|
758
|
-
|
759
|
-
You can get the same effect by passing `omit: true` to `.query`:
|
760
|
-
|
761
|
-
```ruby
|
762
|
-
Honeybee.query(name: "Hutch", omit: true)
|
763
|
-
# JSON -> {"query": [{"Bee Name": "Hutch", "omit": "true"}]}
|
764
|
-
```
|
765
|
-
|
766
|
-
#### .script
|
767
|
-
|
768
|
-
`.script` enables the execution of scripts during query requests.
|
769
|
-
|
770
|
-
```ruby
|
771
|
-
Honeybee.script("My script").find_some # Fetch records and execute a script
|
772
|
-
```
|
773
|
-
|
774
|
-
See section on [script execution](#script-execution) below for more info.
|
775
|
-
|
776
|
-
#### Other notes on querying
|
777
|
-
|
778
|
-
You can chain all query methods together:
|
779
|
-
|
780
|
-
```ruby
|
781
|
-
Honeybee.limit(10).offset(20).sort(:name, :age!).portal(:hives).query(name: "Hutch")
|
782
|
-
```
|
783
|
-
|
784
|
-
You can also set default values for limit and sort on the class:
|
785
|
-
|
786
|
-
```ruby
|
787
|
-
class Honeybee < FmRest::Spyke::Base
|
788
|
-
self.default_limit = 1000
|
789
|
-
self.default_sort = [:name, :age!]
|
790
|
-
end
|
791
|
-
```
|
792
|
-
|
793
|
-
Calling any `Enumerable` method on the resulting scope object will trigger a
|
794
|
-
server request, so you can treat the scope as a collection:
|
795
|
-
|
796
|
-
```ruby
|
797
|
-
Honeybee.limit(10).sort(:name).each { |bee| ... }
|
798
|
-
```
|
799
|
-
|
800
|
-
If you want to explicitly run the request instead you can use `.find_some` on
|
801
|
-
the scope object:
|
802
|
-
|
803
|
-
```ruby
|
804
|
-
Honeybee.limit(10).sort(:name).find_some # => [<Honeybee...>, ...]
|
805
|
-
```
|
806
|
-
|
807
|
-
If you want just a single result you can use `.first` instead (this will
|
808
|
-
force `.limit(1)`):
|
809
|
-
|
810
|
-
```ruby
|
811
|
-
Honeybee.query(name: "Hutch").first # => <Honeybee...>
|
812
|
-
```
|
813
|
-
|
814
|
-
If you know the id of the record you should use `.find(id)` instead of
|
815
|
-
`.query(id: id).first` (so that the sent request is
|
816
|
-
`GET ../:layout/records/:id` instead of `POST ../:layout/_find`).
|
817
|
-
|
818
|
-
```ruby
|
819
|
-
Honeybee.find(89) # => <Honeybee...>
|
820
|
-
```
|
821
|
-
|
822
|
-
Note also that if you use `.find(id)` your `.query()` parameters (as well as
|
823
|
-
limit, offset and sort parameters) will be discarded as they're not supported
|
824
|
-
by the single record endpoint.
|
407
|
+
querying methods: `.query`, `.match`, `.omit`, `.limit`, `.offset`, `.sort`,
|
408
|
+
`.portal`, `.script`, etc.
|
825
409
|
|
410
|
+
See the [main document on querying](docs/Querying.md) for detailed information
|
411
|
+
on the query API methods.
|
826
412
|
|
827
413
|
### Finding records in batches
|
828
414
|
|
@@ -830,249 +416,63 @@ Sometimes you want to iterate over a very large number of records to do some
|
|
830
416
|
processing, but requesting them all at once would result in one huge request to
|
831
417
|
the Data API, and loading too many records in memory all at once.
|
832
418
|
|
833
|
-
To mitigate this problem you can use `.find_in_batches` and `.find_each`.
|
834
|
-
you've used ActiveRecord you're probably familiar with how they operate:
|
835
|
-
|
836
|
-
```ruby
|
837
|
-
# Find records in batches of 100 each
|
838
|
-
Honeybee.query(hive: "Queensville").find_in_batches(batch_size: 100) do |batch|
|
839
|
-
dispatch_bees(batch)
|
840
|
-
end
|
841
|
-
|
842
|
-
# Iterate over all records using batches
|
843
|
-
Honeybee.query(hive: "Queensville").find_each(batch_size: 100) do |bee|
|
844
|
-
bee.dispatch
|
845
|
-
end
|
846
|
-
```
|
847
|
-
|
848
|
-
`.find_in_batches` yields collections of records (batches), while `.find_each`
|
849
|
-
yields individual records, but using batches behind the scenes.
|
850
|
-
|
851
|
-
Both methods accept a block-less form in which case they return an
|
852
|
-
`Enumerator`:
|
853
|
-
|
854
|
-
```ruby
|
855
|
-
batch_enum = Honeybee.find_in_batches
|
856
|
-
|
857
|
-
batch = batch_enum.next # => Spyke::Collection
|
858
|
-
|
859
|
-
batch_enum.each do |batch|
|
860
|
-
process_batch(batch)
|
861
|
-
end
|
862
|
-
|
863
|
-
record_enum = Honeybee.find_each
|
864
|
-
|
865
|
-
record_enum.next # => Honeybee
|
866
|
-
```
|
867
|
-
|
868
|
-
NOTE: By its nature, batch processing is subject to race conditions if other
|
869
|
-
processes are modifying the database.
|
419
|
+
To mitigate this problem you can use `.find_in_batches` and `.find_each`.
|
870
420
|
|
421
|
+
See the [main document on finding in batches](docs/FindInBatches.md) for
|
422
|
+
detailed information on how those work.
|
871
423
|
|
872
424
|
### Container fields
|
873
425
|
|
874
426
|
You can define container fields on your model class with `container`:
|
875
427
|
|
876
428
|
```ruby
|
877
|
-
class Honeybee < FmRest::
|
429
|
+
class Honeybee < FmRest::Layout
|
878
430
|
container :photo, field_name: "Beehive Photo ID"
|
879
431
|
end
|
880
432
|
```
|
881
433
|
|
882
|
-
|
883
|
-
|
884
|
-
example).
|
885
|
-
|
886
|
-
(Note that you don't need to define container fields with `attributes` in
|
887
|
-
addition to the `container` definition.)
|
888
|
-
|
889
|
-
This will provide you with the following instance methods:
|
890
|
-
|
891
|
-
```ruby
|
892
|
-
bee = Honeybee.new
|
893
|
-
|
894
|
-
bee.photo.url # The URL of the container file on the FileMaker server
|
895
|
-
|
896
|
-
bee.photo.download # Download the contents of the container as an IO object
|
897
|
-
|
898
|
-
bee.photo.upload(filename_or_io) # Upload a file to the container
|
899
|
-
```
|
900
|
-
|
901
|
-
`upload` also accepts an options hash with the following options:
|
902
|
-
|
903
|
-
* `:repetition` - Sets the field repetition
|
904
|
-
* `:filename` - The filename to use when uploading (defaults to
|
905
|
-
`filename_or_io.original_filename` if available)
|
906
|
-
* `:content_type` - The MIME content type to use (defaults to
|
907
|
-
`application/octet-stream`)
|
908
|
-
|
434
|
+
See the [main document on container fields](docs/ContainerFields.md) for
|
435
|
+
details on how to use it.
|
909
436
|
|
910
437
|
### Script execution
|
911
438
|
|
912
|
-
The Data API allows running scripts as part of many types of requests
|
913
|
-
|
914
|
-
#### Model.execute_script
|
915
|
-
As of FM18 you can execute scripts directly. To do that for a specific model
|
916
|
-
use `Model.execute_script`:
|
917
|
-
|
918
|
-
```ruby
|
919
|
-
result = Honeybee.execute_script("My Script", param: "optional parameter")
|
920
|
-
```
|
921
|
-
|
922
|
-
This will return a `Spyke::Result` object containing among other things the
|
923
|
-
result of the script execution:
|
924
|
-
|
925
|
-
```ruby
|
926
|
-
result.metadata[:script][:after]
|
927
|
-
# => { result: "oh hi", error: "0" }
|
928
|
-
```
|
929
|
-
|
930
|
-
#### Script options object format
|
931
|
-
|
932
|
-
All other script-capable requests take one or more of three possible script
|
933
|
-
execution options: `script.prerequest`, `script.presort` and plain `script`
|
934
|
-
(which fmrest-ruby dubs `after` for convenience).
|
935
|
-
|
936
|
-
Because of that fmrest-ruby uses a common object format for specifying script options
|
937
|
-
across multiple methods. That object format is as follows:
|
938
|
-
|
939
|
-
```ruby
|
940
|
-
# Just a string means to execute that `after' script without a parameter
|
941
|
-
"My Script"
|
942
|
-
|
943
|
-
# A 2-elemnent array means [script name, script parameter]
|
944
|
-
["My Script", "parameter"]
|
945
|
-
|
946
|
-
# A hash with keys :prerequest, :presort and/or :after sets those scripts for
|
947
|
-
{
|
948
|
-
prerequest: "My Prerequest Script",
|
949
|
-
presort: "My Presort Script",
|
950
|
-
after: "My Script"
|
951
|
-
}
|
952
|
-
|
953
|
-
# Using 2-element arrays as objects in the hash allows specifying parameters
|
954
|
-
{
|
955
|
-
prerequest: ["My Prerequest Script", "parameter"],
|
956
|
-
presort: ["My Presort Script", "parameter"],
|
957
|
-
after: ["My Script", "parameter"]
|
958
|
-
}
|
959
|
-
```
|
960
|
-
|
961
|
-
#### Script execution on record save, destroy and reload
|
962
|
-
|
963
|
-
A record instance's `.save` and `.destroy` methods both accept a `script:`
|
964
|
-
option to which you can pass a script options object with
|
965
|
-
[the above format](#script-options-object-format):
|
966
|
-
|
967
|
-
```ruby
|
968
|
-
# Save the record and execute an `after' script called "My Script"
|
969
|
-
bee.save(script: "My Script")
|
970
|
-
|
971
|
-
# Same as above but with an added parameter
|
972
|
-
bee.save(script: ["My Script", "parameter"])
|
973
|
-
|
974
|
-
# Save the record and execute a presort script and an `after' script
|
975
|
-
bee.save(script: { presort: "My Presort Script", after: "My Script" })
|
976
|
-
|
977
|
-
# Destroy the record and execute a prerequest script with a parameter
|
978
|
-
bee.destroy(script: { prerequest: ["My Prerequest Script", "parameter"] })
|
979
|
-
|
980
|
-
# Reload the record and execute a prerequest script with a parameter
|
981
|
-
bee.reload(script: { prerequest: ["My Prerequest Script", "parameter"] })
|
982
|
-
```
|
983
|
-
|
984
|
-
#### Retrieving script execution results
|
985
|
-
|
986
|
-
Every time a request is ran on a model or record instance of a model, a
|
987
|
-
thread-local `Model.last_request_metadata` attribute is set on that model,
|
988
|
-
which is a hash containing the results of script executions, if any were
|
989
|
-
performed, among other metadata.
|
990
|
-
|
991
|
-
The results for `:after`, `:prerequest` and `:presort` scripts are stored
|
992
|
-
separately, under their matching key.
|
993
|
-
|
994
|
-
```ruby
|
995
|
-
bee.save(script: { presort: "My Presort Script", after: "My Script" })
|
996
|
-
|
997
|
-
Honeybee.last_request_metadata.script
|
998
|
-
# => { after: { result: "oh hi", error: "0" }, presort: { result: "lo", error: "0" } }
|
999
|
-
```
|
1000
|
-
|
1001
|
-
#### Executing scripts through query requests
|
1002
|
-
|
1003
|
-
As mentioned under the [Query API](#query-api) section, you can use the
|
1004
|
-
`.script` query method to specify that you want scripts executed when a query
|
1005
|
-
is performed on that scope.
|
1006
|
-
|
1007
|
-
`.script` takes the same options object specified [above](#script-options-object-format):
|
1008
|
-
|
1009
|
-
```ruby
|
1010
|
-
# Find one Honeybee record executing a presort and after script
|
1011
|
-
Honeybee.script(presort: ["My Presort Script", "parameter"], after: "My Script").first
|
1012
|
-
```
|
1013
|
-
|
1014
|
-
The model class' `.last_request_metadata` will be set in case you need to get the result.
|
1015
|
-
|
1016
|
-
In the case of retrieving multiple results (i.e. via `.find_some`) the
|
1017
|
-
resulting collection will have a `.metadata` attribute method containing the
|
1018
|
-
same metadata hash with script execution results. Note that this does not apply
|
1019
|
-
to retrieving single records, in that case you'll have to use
|
1020
|
-
`.last_request_metadata`.
|
439
|
+
The FM Data API allows running scripts as part of many types of requests, and
|
440
|
+
`fmrest-spyke` provides mechanisms for all of them.
|
1021
441
|
|
442
|
+
See the [main document on script execution](docs/ScriptExecution.md) for
|
443
|
+
details.
|
1022
444
|
|
1023
445
|
### Setting global field values
|
1024
446
|
|
1025
|
-
You can call `.set_globals` on any `FmRest::
|
447
|
+
You can call `.set_globals` on any `FmRest::Layout` model to set global
|
1026
448
|
field values on the database that model is configured for.
|
1027
449
|
|
1028
|
-
|
1029
|
-
|
1030
|
-
table name and the inner keys being the field names:
|
1031
|
-
|
1032
|
-
```ruby
|
1033
|
-
Honeybee.set_globals(
|
1034
|
-
"beeTable::myVar" => "value",
|
1035
|
-
"beeTable::myOtherVar" => "also a value"
|
1036
|
-
)
|
1037
|
-
|
1038
|
-
# Equivalent to the above example
|
1039
|
-
Honeybee.set_globals(beeTable: { myVar: "value", myOtherVar: "also a value" })
|
1040
|
-
|
1041
|
-
# Combined
|
1042
|
-
Honeybee.set_globals(
|
1043
|
-
"beeTable::myVar" => "value",
|
1044
|
-
beeTable: { myOtherVar: "also a value" }
|
1045
|
-
)
|
1046
|
-
```
|
1047
|
-
|
450
|
+
See the [main document on setting global field values](docs/GlobalFields.md)
|
451
|
+
for details.
|
1048
452
|
|
1049
453
|
## Logging
|
1050
454
|
|
1051
|
-
If using fmrest-
|
1052
|
-
|
455
|
+
If using `fmrest-spyke` with Rails then pretty log output will be set up for
|
456
|
+
you automatically by Spyke (see [their
|
1053
457
|
README](https://github.com/balvig/spyke#log-output)).
|
1054
458
|
|
1055
|
-
You can also enable simple STDOUT logging (useful for
|
1056
|
-
`log: true` in the options hash for either
|
459
|
+
You can also enable simple Faraday STDOUT logging of raw requests (useful for
|
460
|
+
debugging) by passing `log: true` in the options hash for either
|
1057
461
|
`FmRest.default_connection_settings=` or your models' `fmrest_config=`, e.g.:
|
1058
462
|
|
1059
463
|
```ruby
|
1060
464
|
FmRest.default_connection_settings = {
|
1061
|
-
host:
|
1062
|
-
|
1063
|
-
|
1064
|
-
password: "abc123",
|
1065
|
-
log: true
|
465
|
+
host: "…",
|
466
|
+
…
|
467
|
+
log: true
|
1066
468
|
}
|
1067
469
|
|
1068
470
|
# Or in your model
|
1069
|
-
class LoggyBee < FmRest::
|
471
|
+
class LoggyBee < FmRest::Layout
|
1070
472
|
self.fmrest_config = {
|
1071
|
-
host:
|
1072
|
-
|
1073
|
-
|
1074
|
-
password: "...",
|
1075
|
-
log: true
|
473
|
+
host: "…",
|
474
|
+
…
|
475
|
+
log: true
|
1076
476
|
}
|
1077
477
|
end
|
1078
478
|
```
|
@@ -1082,7 +482,7 @@ If you need to set up more complex logging for your models can use the
|
|
1082
482
|
Faraday connection, e.g.:
|
1083
483
|
|
1084
484
|
```ruby
|
1085
|
-
class LoggyBee < FmRest::
|
485
|
+
class LoggyBee < FmRest::Layout
|
1086
486
|
faraday do |conn|
|
1087
487
|
conn.response :logger, MyApp.logger, bodies: true
|
1088
488
|
end
|
@@ -1093,33 +493,43 @@ end
|
|
1093
493
|
|
1094
494
|
FM Data API reference: https://fmhelp.filemaker.com/docs/18/en/dataapi/
|
1095
495
|
|
1096
|
-
| FM 18 Data API feature | Supported by basic connection | Supported by FmRest::
|
1097
|
-
|
1098
|
-
| Log in using HTTP Basic Auth | Yes | Yes
|
1099
|
-
| Log in using OAuth | No | No
|
1100
|
-
| Log in to an external data source | No | No
|
1101
|
-
| Log in using a FileMaker ID account | No | No
|
1102
|
-
| Log out | Yes | Yes
|
1103
|
-
| Get product information | Manual* | No
|
1104
|
-
| Get database names | Manual* | No
|
1105
|
-
| Get script names | Manual* | No
|
1106
|
-
| Get layout names | Manual* | No
|
1107
|
-
| Get layout metadata | Manual* | No
|
1108
|
-
| Create a record | Manual* | Yes
|
1109
|
-
| Edit a record | Manual* | Yes
|
1110
|
-
| Duplicate a record | Manual* | No
|
1111
|
-
| Delete a record | Manual* | Yes
|
1112
|
-
|
|
1113
|
-
| Get a
|
1114
|
-
| Get
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
| Run a script
|
496
|
+
| FM 18 Data API feature | Supported by basic connection | Supported by FmRest::Layout |
|
497
|
+
|-------------------------------------|-------------------------------|-----------------------------|
|
498
|
+
| Log in using HTTP Basic Auth | Yes | Yes |
|
499
|
+
| Log in using OAuth | No | No |
|
500
|
+
| Log in to an external data source | No | No |
|
501
|
+
| Log in using a FileMaker ID account | No | No |
|
502
|
+
| Log out | Yes | Yes |
|
503
|
+
| Get product information | Manual* | No |
|
504
|
+
| Get database names | Manual* | No |
|
505
|
+
| Get script names | Manual* | No |
|
506
|
+
| Get layout names | Manual* | No |
|
507
|
+
| Get layout metadata | Manual* | No |
|
508
|
+
| Create a record | Manual* | Yes |
|
509
|
+
| Edit a record | Manual* | Yes |
|
510
|
+
| Duplicate a record | Manual* | No |
|
511
|
+
| Delete a record | Manual* | Yes |
|
512
|
+
| Edit portal records | Manual* | Yes |
|
513
|
+
| Get a single record | Manual* | Yes |
|
514
|
+
| Get a range of records | Manual* | Yes |
|
515
|
+
| Get container data | Manual* | Yes |
|
516
|
+
| Upload container data | Manual* | Yes |
|
517
|
+
| Perform a find request | Manual* | Yes |
|
518
|
+
| Set global field values | Manual* | Yes |
|
519
|
+
| Run a script | Manual* | Yes |
|
520
|
+
| Run a script with another request | Manual* | Yes |
|
1120
521
|
|
1121
522
|
\* You can manually supply the URL and JSON to a `FmRest` connection.
|
1122
523
|
|
524
|
+
## Supported Ruby versions
|
525
|
+
|
526
|
+
fmrest-ruby aims to support and is [tested against](https://github.com/beezwax/fmrest-ruby/actions?query=workflow%3ACI)
|
527
|
+
the following Ruby implementations:
|
528
|
+
|
529
|
+
* Ruby 2.5
|
530
|
+
* Ruby 2.6
|
531
|
+
* Ruby 2.7
|
532
|
+
* Ruby 3.0
|
1123
533
|
|
1124
534
|
## Gem development
|
1125
535
|
|
@@ -1128,20 +538,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
1128
538
|
prompt that will allow you to experiment (it will auto-load all fixtures in
|
1129
539
|
spec/fixtures).
|
1130
540
|
|
1131
|
-
To install
|
1132
|
-
release a new version, update the version
|
1133
|
-
|
1134
|
-
git
|
1135
|
-
[rubygems.org](https://rubygems.org).
|
1136
|
-
|
1137
|
-
|
1138
|
-
## Contributing
|
1139
|
-
|
1140
|
-
Bug reports and pull requests are welcome. This project is intended to be a
|
1141
|
-
safe, welcoming space for collaboration, and contributors are expected to
|
1142
|
-
adhere to the [Contributor Covenant](http://contributor-covenant.org) code of
|
1143
|
-
conduct.
|
1144
|
-
|
541
|
+
To install all gems onto your local machine, run
|
542
|
+
`bundle exec rake all:install`. To release a new version, update the version
|
543
|
+
number in `lib/fmrest/version.rb`, and then run `bundle exec rake all:release`,
|
544
|
+
which will create a git tag for the version, push git commits and tags, and
|
545
|
+
push the `.gem` files to [rubygems.org](https://rubygems.org).
|
1145
546
|
|
1146
547
|
## License
|
1147
548
|
|
@@ -1149,7 +550,6 @@ The gem is available as open source under the terms of the
|
|
1149
550
|
[MIT License](https://opensource.org/licenses/MIT).
|
1150
551
|
See [LICENSE.txt](LICENSE.txt).
|
1151
552
|
|
1152
|
-
|
1153
553
|
## Disclaimer
|
1154
554
|
|
1155
555
|
This project is not sponsored by or otherwise affiliated with FileMaker, Inc,
|