graphiti 2.0.0.beta.6 → 2.0.0.beta.8

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.
Files changed (246) hide show
  1. checksums.yaml +4 -4
  2. data/.git-blame-ignore-revs +2 -0
  3. data/.github/workflows/ci.yml +55 -3
  4. data/.github/workflows/docs.yml +1 -1
  5. data/.github/workflows/notify-example-app.yml +18 -0
  6. data/.gitignore +2 -0
  7. data/.standard.yml +6 -0
  8. data/CHANGELOG.md +54 -0
  9. data/CONTRIBUTING.md +3 -1
  10. data/README.md +13 -206
  11. data/Rakefile +6 -3
  12. data/UPGRADING.md +1 -1
  13. data/graphiti.gemspec +8 -4
  14. data/lib/generators/graphiti/generator_mixin.rb +1 -1
  15. data/lib/generators/graphiti/install_generator.rb +1 -2
  16. data/lib/generators/graphiti/resource_generator.rb +6 -3
  17. data/lib/generators/graphiti/templates/application_resource.rb.erb +19 -0
  18. data/lib/generators/graphiti/templates/controller.rb.erb +26 -8
  19. data/lib/generators/graphiti/templates/index_request_spec.rb.erb +2 -2
  20. data/lib/generators/graphiti/templates/resource_reads_spec.rb.erb +4 -4
  21. data/lib/generators/graphiti/templates/show_request_spec.rb.erb +2 -2
  22. data/lib/graphiti/audit/report.rb +6 -6
  23. data/lib/graphiti/audit.rb +3 -4
  24. data/lib/graphiti/delegates/pagination.rb +2 -2
  25. data/lib/graphiti/deserializer.rb +3 -3
  26. data/lib/graphiti/errors.rb +6 -6
  27. data/lib/graphiti/extensions/temp_id.rb +1 -1
  28. data/lib/graphiti/query.rb +27 -8
  29. data/lib/graphiti/rails/context.rb +2 -1
  30. data/lib/graphiti/rails/controller.rb +3 -0
  31. data/lib/graphiti/rails/debugging.rb +1 -1
  32. data/lib/graphiti/rails/responders.rb +2 -1
  33. data/lib/graphiti/resource/configuration.rb +1 -0
  34. data/lib/graphiti/resource/dsl.rb +2 -2
  35. data/lib/graphiti/resource/interface.rb +3 -3
  36. data/lib/graphiti/resource/links.rb +1 -1
  37. data/lib/graphiti/resource/persistence.rb +1 -1
  38. data/lib/graphiti/resource/polymorphism.rb +1 -1
  39. data/lib/graphiti/resource.rb +2 -2
  40. data/lib/graphiti/resource_proxy.rb +0 -1
  41. data/lib/graphiti/runner.rb +8 -3
  42. data/lib/graphiti/scope.rb +13 -0
  43. data/lib/graphiti/scoping/filter.rb +5 -3
  44. data/lib/graphiti/scoping/paginate.rb +1 -1
  45. data/lib/graphiti/scoping/sort.rb +1 -1
  46. data/lib/graphiti/sideload/has_many.rb +1 -1
  47. data/lib/graphiti/spec_helpers/errors_proxy.rb +1 -1
  48. data/lib/graphiti/spec_helpers/matchers.rb +1 -1
  49. data/lib/graphiti/spec_helpers.rb +5 -0
  50. data/lib/graphiti/types.rb +5 -1
  51. data/lib/graphiti/util/cache_debug.rb +3 -3
  52. data/lib/graphiti/util/hash.rb +1 -1
  53. data/lib/graphiti/util/persistence.rb +6 -6
  54. data/lib/graphiti/util/serializer_relationships.rb +1 -1
  55. data/lib/graphiti/version.rb +1 -1
  56. data/lib/tasks/graphiti.rake +1 -1
  57. data/package.json +1 -0
  58. metadata +31 -197
  59. data/docs/concepts/backends-and-models.md +0 -122
  60. data/docs/concepts/endpoints.md +0 -183
  61. data/docs/concepts/links.md +0 -212
  62. data/docs/concepts/overview.md +0 -80
  63. data/docs/concepts/persisting.md +0 -376
  64. data/docs/concepts/relationships.md +0 -627
  65. data/docs/concepts/resources.md +0 -677
  66. data/docs/getting-started/first-api.md +0 -289
  67. data/docs/getting-started/installation.md +0 -185
  68. data/docs/intro.md +0 -307
  69. data/docs/js/authentication.md +0 -63
  70. data/docs/js/ddau.md +0 -20
  71. data/docs/js/extra-params.md +0 -41
  72. data/docs/js/index.md +0 -112
  73. data/docs/js/installation.md +0 -120
  74. data/docs/js/middleware.md +0 -72
  75. data/docs/js/models.md +0 -202
  76. data/docs/js/reads.md +0 -494
  77. data/docs/js/state-syncing.md +0 -100
  78. data/docs/js/writes.md +0 -373
  79. data/docs/reference/vandal.md +0 -63
  80. data/docs/reference/why.md +0 -13
  81. data/docs/topics/authorization.md +0 -155
  82. data/docs/topics/caching.md +0 -55
  83. data/docs/topics/customizing-sideloads.md +0 -156
  84. data/docs/topics/debugging.md +0 -242
  85. data/docs/topics/error-handling.md +0 -210
  86. data/docs/topics/etags.md +0 -46
  87. data/docs/topics/hopping-relationships.md +0 -149
  88. data/docs/topics/json-attributes.md +0 -77
  89. data/docs/topics/openstruct-models.md +0 -50
  90. data/docs/topics/remote-resources.md +0 -291
  91. data/docs/topics/testing.md +0 -894
  92. data/docs/topics/without-activerecord.md +0 -324
  93. data/docs/tutorial/index.md +0 -58
  94. data/docs/tutorial/step_0.md +0 -107
  95. data/docs/tutorial/step_1.md +0 -199
  96. data/docs/tutorial/step_2.md +0 -312
  97. data/docs/tutorial/step_3.md +0 -142
  98. data/docs/tutorial/step_4.md +0 -135
  99. data/docs/tutorial/step_5.md +0 -69
  100. data/docs/tutorial/step_6.md +0 -82
  101. data/docs/tutorial/step_7.md +0 -205
  102. data/docs/tutorial/step_8.md +0 -128
  103. data/docs/tutorial/step_9.md +0 -171
  104. data/docs/upgrading.md +0 -266
  105. data/website/.gitignore +0 -20
  106. data/website/README.md +0 -43
  107. data/website/docusaurus.config.js +0 -197
  108. data/website/package-lock.json +0 -19474
  109. data/website/package.json +0 -46
  110. data/website/sidebars.js +0 -82
  111. data/website/src/css/custom.css +0 -85
  112. data/website/src/pages/markdown-page.mdx +0 -7
  113. data/website/static/.nojekyll +0 -0
  114. data/website/static/1.13/2019/03/31/graphiti-1-0.html +0 -205
  115. data/website/static/1.13/2019/05/08/graphiti-1-1.html +0 -212
  116. data/website/static/1.13/2019/05/20/graphiti-1-2.html +0 -214
  117. data/website/static/1.13/2019/10/14/tutorial.html +0 -198
  118. data/website/static/1.13/CNAME +0 -1
  119. data/website/static/1.13/README.md +0 -16
  120. data/website/static/1.13/assets/css/syntax.css +0 -60
  121. data/website/static/1.13/assets/favicons/android-chrome-192x192.png +0 -0
  122. data/website/static/1.13/assets/favicons/android-chrome-256x256.png +0 -0
  123. data/website/static/1.13/assets/favicons/apple-touch-icon.png +0 -0
  124. data/website/static/1.13/assets/favicons/browserconfig.xml +0 -9
  125. data/website/static/1.13/assets/favicons/favicon-16x16.png +0 -0
  126. data/website/static/1.13/assets/favicons/favicon-32x32.png +0 -0
  127. data/website/static/1.13/assets/favicons/favicon.ico +0 -0
  128. data/website/static/1.13/assets/favicons/mstile-150x150.png +0 -0
  129. data/website/static/1.13/assets/favicons/safari-pinned-tab.svg +0 -1
  130. data/website/static/1.13/assets/favicons/site.webmanifest +0 -19
  131. data/website/static/1.13/assets/img/backend.gif +0 -0
  132. data/website/static/1.13/assets/img/conformity.png +0 -0
  133. data/website/static/1.13/assets/img/error_payload.png +0 -0
  134. data/website/static/1.13/assets/img/fancy-cushion.png +0 -0
  135. data/website/static/1.13/assets/img/gh.png +0 -0
  136. data/website/static/1.13/assets/img/home-bg.jpg +0 -0
  137. data/website/static/1.13/assets/img/lifecycle.gif +0 -0
  138. data/website/static/1.13/assets/img/logo-500.png +0 -0
  139. data/website/static/1.13/assets/img/logo.png +0 -0
  140. data/website/static/1.13/assets/img/love-graffiti.jpg +0 -0
  141. data/website/static/1.13/assets/img/meta_total_count.png +0 -0
  142. data/website/static/1.13/assets/img/persist.jpg +0 -0
  143. data/website/static/1.13/assets/img/resource.gif +0 -0
  144. data/website/static/1.13/assets/img/rest-graffiti.jpg +0 -0
  145. data/website/static/1.13/assets/img/rest1.gif +0 -0
  146. data/website/static/1.13/assets/img/rest2.gif +0 -0
  147. data/website/static/1.13/assets/img/rest3.gif +0 -0
  148. data/website/static/1.13/assets/img/rethink-rest-graffiti.jpg +0 -0
  149. data/website/static/1.13/assets/img/sunrise.png +0 -0
  150. data/website/static/1.13/assets/img/why.png +0 -0
  151. data/website/static/1.13/assets/js/highlight.pack.js +0 -2
  152. data/website/static/1.13/assets/main.css +0 -15518
  153. data/website/static/1.13/assets/main.css.map +0 -1
  154. data/website/static/1.13/bin/bundle +0 -109
  155. data/website/static/1.13/bin/jekyll +0 -27
  156. data/website/static/1.13/bin/kramdown +0 -27
  157. data/website/static/1.13/bin/listen +0 -27
  158. data/website/static/1.13/bin/rake +0 -27
  159. data/website/static/1.13/bin/rougify +0 -27
  160. data/website/static/1.13/bin/safe_yaml +0 -27
  161. data/website/static/1.13/bin/sass +0 -27
  162. data/website/static/1.13/bin/sass-convert +0 -27
  163. data/website/static/1.13/bin/scss +0 -27
  164. data/website/static/1.13/blog.html +0 -259
  165. data/website/static/1.13/cheatsheet.html +0 -316
  166. data/website/static/1.13/cookbooks/authorization.md +0 -0
  167. data/website/static/1.13/cookbooks/caching.md +0 -0
  168. data/website/static/1.13/cookbooks/customizing-sideloads.html +0 -325
  169. data/website/static/1.13/cookbooks/etags.md +0 -0
  170. data/website/static/1.13/cookbooks/hopping-relationships.html +0 -324
  171. data/website/static/1.13/cookbooks/json_attributes.md +0 -0
  172. data/website/static/1.13/cookbooks/openstruct-models.md +0 -0
  173. data/website/static/1.13/cookbooks/remote-resources.md +0 -0
  174. data/website/static/1.13/cookbooks/without-activerecord.html +0 -510
  175. data/website/static/1.13/features.html +0 -249
  176. data/website/static/1.13/feed.xml +0 -106
  177. data/website/static/1.13/guides/concepts/backends-and-models.html +0 -467
  178. data/website/static/1.13/guides/concepts/debugging.html +0 -440
  179. data/website/static/1.13/guides/concepts/endpoints.html +0 -432
  180. data/website/static/1.13/guides/concepts/error-handling.html +0 -396
  181. data/website/static/1.13/guides/concepts/links.html +0 -501
  182. data/website/static/1.13/guides/concepts/remote-resources.html +0 -536
  183. data/website/static/1.13/guides/concepts/resources.html +0 -2176
  184. data/website/static/1.13/guides/concepts/testing.html +0 -1469
  185. data/website/static/1.13/guides/getting-started/installation.html +0 -420
  186. data/website/static/1.13/guides/graphiti-rails-migration.html +0 -242
  187. data/website/static/1.13/guides/index.html +0 -269
  188. data/website/static/1.13/guides/overview.html +0 -325
  189. data/website/static/1.13/guides/upgrading-2-0.html +0 -193
  190. data/website/static/1.13/guides/upgrading.html +0 -314
  191. data/website/static/1.13/guides/vandal.html +0 -282
  192. data/website/static/1.13/guides/why.html +0 -1121
  193. data/website/static/1.13/index.html +0 -72
  194. data/website/static/1.13/js/authentication.html +0 -295
  195. data/website/static/1.13/js/ddau.html +0 -238
  196. data/website/static/1.13/js/extra-params.html +0 -270
  197. data/website/static/1.13/js/index.html +0 -321
  198. data/website/static/1.13/js/installation.html +0 -637
  199. data/website/static/1.13/js/introduction.html +0 -257
  200. data/website/static/1.13/js/middleware.html +0 -318
  201. data/website/static/1.13/js/reads/fieldsets.html +0 -271
  202. data/website/static/1.13/js/reads/filtering.html +0 -289
  203. data/website/static/1.13/js/reads/includes.html +0 -260
  204. data/website/static/1.13/js/reads/index.html +0 -497
  205. data/website/static/1.13/js/reads/nested-queries.html +0 -353
  206. data/website/static/1.13/js/reads/pagination.html +0 -260
  207. data/website/static/1.13/js/reads/sorting.html +0 -265
  208. data/website/static/1.13/js/reads/statistics.html +0 -289
  209. data/website/static/1.13/js/state-syncing.html +0 -340
  210. data/website/static/1.13/js/writes/deferred.html +0 -296
  211. data/website/static/1.13/js/writes/dirty-tracking.html +0 -399
  212. data/website/static/1.13/js/writes/index.html +0 -391
  213. data/website/static/1.13/js/writes/nested.html +0 -330
  214. data/website/static/1.13/js/writes/validations.html +0 -272
  215. data/website/static/1.13/quickstart.html +0 -660
  216. data/website/static/1.13/template +0 -161
  217. data/website/static/1.13/tutorial/index.html +0 -250
  218. data/website/static/1.13/tutorial/step_0.html +0 -292
  219. data/website/static/1.13/tutorial/step_1.html +0 -517
  220. data/website/static/1.13/tutorial/step_2.html +0 -481
  221. data/website/static/1.13/tutorial/step_3.html +0 -323
  222. data/website/static/1.13/tutorial/step_4.html +0 -318
  223. data/website/static/1.13/tutorial/step_5.html +0 -265
  224. data/website/static/1.13/tutorial/step_6.html +0 -276
  225. data/website/static/1.13/tutorial/step_7.html +0 -390
  226. data/website/static/1.13/tutorial/step_8.html +0 -316
  227. data/website/static/1.13/tutorial/step_9.html +0 -365
  228. data/website/static/CNAME +0 -1
  229. data/website/static/assets/img/error_payload.png +0 -0
  230. data/website/static/assets/img/legacy/legacy-0378a3bb39.png +0 -0
  231. data/website/static/assets/img/legacy/legacy-05bbd3e5fd.png +0 -0
  232. data/website/static/assets/img/legacy/legacy-07aa104495.png +0 -0
  233. data/website/static/assets/img/legacy/legacy-0c75a16b3a.gif +0 -0
  234. data/website/static/assets/img/legacy/legacy-3076df6209.png +0 -0
  235. data/website/static/assets/img/legacy/legacy-7f6889bc89.png +0 -0
  236. data/website/static/assets/img/legacy/legacy-a2cc4363c3.png +0 -0
  237. data/website/static/assets/img/legacy/legacy-f67cfa89ab.png +0 -0
  238. data/website/static/assets/img/meta_total_count.png +0 -0
  239. data/website/static/img/docusaurus-social-card.jpg +0 -0
  240. data/website/static/img/docusaurus.png +0 -0
  241. data/website/static/img/favicon.ico +0 -0
  242. data/website/static/img/logo.png +0 -0
  243. data/website/static/img/logo.svg +0 -1
  244. data/website/static/img/undraw_docusaurus_mountain.svg +0 -171
  245. data/website/static/img/undraw_docusaurus_react.svg +0 -170
  246. data/website/static/img/undraw_docusaurus_tree.svg +0 -40
data/docs/js/reads.md DELETED
@@ -1,494 +0,0 @@
1
- ---
2
- title: 'Reads'
3
- sidebar_position: 4
4
- ---
5
-
6
- The interface for read operations is a simpler version of the [ActiveRecord Query Interface](http://guides.rubyonrails.org/active_record_querying.html). Instead of generating SQL, we'll be generating JSONAPI requests.
7
-
8
- ## Basic Finders
9
-
10
- Execute queries with `.all()`, `find()`, or `.first()`:
11
-
12
- ```typescript
13
- let response = await Post.all()
14
- response.data // array of Post instances
15
- ```
16
- ```javascript
17
- Post.all().then(function(response) {
18
- response.data // array of Post instances
19
- });
20
- ```
21
- <blockquote class="url">
22
- <p>GET /posts</p>
23
- </blockquote>
24
-
25
- ```typescript
26
- let response = await Post.find(123)
27
- response.data // Post instance
28
- ```
29
- ```javascript
30
- Post.find(123).then(function(response) {
31
- response.data // Post instance
32
- });
33
- ```
34
- <blockquote class="url">
35
- <p>GET /posts/123</p>
36
- </blockquote>
37
-
38
- ```typescript
39
- let response = await Post.first()
40
- response.data // Post instance
41
- ```
42
- ```javascript
43
- Post.first().then(function(response) {
44
- response.data // Post instance
45
- });
46
- ```
47
- <blockquote class="url">
48
- <p>GET /posts?page[size]=1</p>
49
- </blockquote>
50
-
51
- ## Composable Queries with Scopes
52
-
53
- The beauty of ORMs is their ability to compose queries. We'll be doing this by chaining together `Scope`s (query fragments). All of the methods you see on this page can be chained together - the request will not fire until the chain ends with `all()`, `first()`, or `find`. Example:
54
-
55
- ```typescript
56
- let scope = Post.order({ name: "desc" })
57
-
58
- if (someCheckboxIsChecked) {
59
- scope = scope.where({ important: true })
60
- } else {
61
- scope = scope.where({ important: false })
62
- }
63
-
64
- scope.all() // request fires
65
- ```
66
-
67
- ```javascript
68
- var scope = Post.order({ name: "desc" });
69
-
70
- if (someCheckboxIsChecked) {
71
- scope = scope.where({ important: true });
72
- } else {
73
- scope = scope.where({ important: false });
74
- }
75
-
76
- scope.all() // request fires
77
- ```
78
- <blockquote class="url">
79
- <p>/posts?sort=-name&filter[important]=true</p>
80
- <p>/posts?sort=-name&filter[important]=false</p>
81
- </blockquote>
82
-
83
- In practice, you'll probably have some scopes you want to re-use across different contexts. A best practice is to store these scopes as class methods (static methods) in the model:
84
-
85
- ```typescript
86
- class Post extends ApplicationRecord {
87
- // ... code ...
88
- static superImportant() {
89
- return this
90
- .where({ ranking_gt: 8 })
91
- .order({ ranking: 'desc' })
92
- .stats({ total 'count' })
93
- }
94
- }
95
-
96
- // get 10 super important posts
97
- let scope = Post.superImportant().per(10)
98
- scope.all() // fire query
99
- ```
100
-
101
- ```javascript
102
- const Post = ApplicationRecord.extend({
103
- // ... code ...
104
- static: {
105
- superImportant() {
106
- return this
107
- .where({ ranking_gt: 8 })
108
- .order({ ranking: 'desc' })
109
- .stats({ total 'count' })
110
- }
111
- }
112
- })
113
-
114
- // get 10 super important posts
115
- var scope = Post.superImportant().per(10);
116
- scope.all() // fire query
117
- ```
118
- <blockquote class="url">
119
- <p>/posts?sort=-ranking&stats[total]=count&page[size]=10&filter[ranking_gt]=8</p>
120
- </blockquote>
121
-
122
- ## Metadata
123
-
124
- The [meta information](http://jsonapi.org/format/#document-meta) of the JSONAPI response is available as a POJO on the response:
125
-
126
- ```typescript
127
- let response = await Post.all()
128
- response.meta // { stats: { total: { count: 100 } } }
129
- ```
130
- ```javascript
131
- await Post.all().then(function(response) {
132
- response.meta // { stats: { total: { count: 100 } } }
133
- })
134
- ```
135
-
136
- ## Promises and Async/Await
137
-
138
- The result of `all()`, `first()` or `find` is a [Promise](https://developers.google.com/web/fundamentals/primers/promises). The promise will resolve to a `Response` object.
139
-
140
- A `Response` object has three keys - `data`, `meta`, and `raw`. `data` - the one you'll be using the most - will be a `Model` instance (or array of `Model`) instances. `meta` will be the [Meta Information](http://jsonapi.org/format/#document-meta) returned by the API (mostly used for statistics in our case). `raw` is only used to introspect the raw response document.
141
-
142
- ```typescript
143
- Post.all().then((response) => {
144
- response.data // array of Post instances
145
- response.meta // js object from the server
146
- response.raw // js response document
147
- })
148
- ```
149
-
150
- ```javascript
151
- Post.all().then(function(response) {
152
- response.data // array of Post instances
153
- response.meta // js object from the server
154
- response.raw // js response document
155
- });
156
- ```
157
- <blockquote class="url">
158
- <p>/posts</p>
159
- </blockquote>
160
-
161
- Hopefully you're running in an environment that supports ES7's [Async/Await](https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9). This makes things even easier:
162
-
163
- ```typescript
164
- let { data } = await Post.all()
165
- data // array of Post instances
166
-
167
- // alternatively
168
-
169
- let posts = (await Post.all()).data
170
- posts // array of Post instances
171
- ```
172
- <blockquote class="url">
173
- <p>/posts</p>
174
- </blockquote>
175
-
176
- ## Filtering
177
-
178
- Use `#where()` to apply filters:
179
-
180
- ```typescript
181
- Post.where({ important: true }).all()
182
- ```
183
- <blockquote class="url">
184
- <p>/posts?filter[important]=true</p>
185
- </blockquote>
186
-
187
- `#where()` clauses can be chained together. If the same key is seen twice, it will be overridden:
188
-
189
- ```typescript
190
- Post
191
- .where({ important: true })
192
- .where({ ranking: 10 })
193
- .where({ important: false })
194
- .all()
195
- ```
196
- <blockquote class="url">
197
- <p>/posts?filter[important]=false&filter[ranking]=10</p>
198
- </blockquote>
199
-
200
- `#where()` clauses are based on **server implementation**. The key should be exactly as the server understands it. Here are some common conventions we promote:
201
-
202
- ```typescript
203
- // id greater than 5
204
- Post.where({ id_gt: 5 }).all()
205
-
206
- // id greater than or equal to 5
207
- Post.where({ id_gte: 5 }).all()
208
-
209
- // id less than 5
210
- Post.where({ id_lt: 5 }).all()
211
-
212
- // id less or equal to 5
213
- Post.where({ id_lte: 5 }).all()
214
-
215
- // title starts with "foo"
216
- Post.where({ title: { prefix: "foo" } }).all()
217
-
218
- // OR these two values
219
- Post.where({ status_or: ['draft', 'review'] })
220
-
221
- // AND these two values (default)
222
- Post.where({ status: ['draft', 'review'] })
223
- ```
224
-
225
- ### Escaping Values
226
-
227
- [Graphiti treats a comma as a delimiter of multiple values](/concepts/resources#escaping-values). To escape the comma and tell Graphiti this is a single value, wrap it in `{{curlies}}`:
228
-
229
- ```typescript
230
- Post.where({ title: "{{Hello World, here I am}}" })
231
- ```
232
-
233
- ## Sorting
234
-
235
- Use `#order()` to sort.
236
-
237
- If passed a string, it will default to **ascending**:
238
-
239
- ```typescript
240
- Post.order("title").all()
241
- ```
242
- <blockquote class="url">
243
- <p>/posts?sort=title</p>
244
- </blockquote>
245
-
246
-
247
- Otherwise, pass an object:
248
-
249
- ```typescript
250
- Post.order({ title: "desc" }).all()
251
- ```
252
- <blockquote class="url">
253
- <p>/posts?sort=-title</p>
254
- </blockquote>
255
-
256
- For multisort, chain multiple `#order()` clauses:
257
-
258
- ```typescript
259
- Post
260
- .order({ title: "desc" })
261
- .order("ranking")
262
- .all()
263
- ```
264
- <blockquote class="url">
265
- <p>/posts?sort=-title,ranking</p>
266
- </blockquote>
267
-
268
- ## Pagination
269
-
270
- Use `#per()` to set the limit per page:
271
-
272
- ```typescript
273
- Post.per(10).all()
274
- ```
275
- <blockquote class="url">
276
- <p>/posts?page[size]=10</p>
277
- </blockquote>
278
-
279
- Use `#page()` to set the current page:
280
-
281
- ```typescript
282
- Post.page(5).all()
283
- ```
284
- <blockquote class="url">
285
- <p>/posts?page[number]=5</p>
286
- </blockquote>
287
-
288
- When chained together (10 per page, the 5th page):
289
-
290
- ```typescript
291
- Post.page(5).per(10).all()
292
- ```
293
- <blockquote class="url">
294
- <p>/posts?page[size]=10&page[number]=5</p>
295
- </blockquote>
296
-
297
- ## Fieldsets
298
-
299
- ### Sparse Fieldsets
300
-
301
- Use `#select()` to limit the fields returned by the server:
302
-
303
- ```typescript
304
- Post.select(['title', 'status']).all()
305
- ```
306
- <blockquote class="url">
307
- <p>/posts?fields[posts]=title,status</p>
308
- </blockquote>
309
-
310
- When dealing with relationships, it may be easier to pass an object, where the key is the corresponding JSONAPI type. This will be exactly what's sent to the server in `?fields`:
311
-
312
- ```typescript
313
- Post.select({
314
- posts: ['title', 'status'],
315
- comments: ['created_at']
316
- }).all()
317
- ```
318
- <blockquote class="url">
319
- <p>/posts?fields[posts]=title,status&fields[comments]=created_at</p>
320
- </blockquote>
321
-
322
- ### Extra Fieldsets
323
-
324
- Use `#selectExtra()` to explicitly request a field that doesn't usually come back (often computationally expensive):
325
-
326
- ```typescript
327
- Post.selectExtra(['highlights', 'cumulative_ranking']).all()
328
- ```
329
- <blockquote class="url">
330
- <p>/posts?extra_fields[posts]=highlights,cumulative_ranking</p>
331
- </blockquote>
332
-
333
- Just like the `select` example above, feel free to pass an object specifying the fields for each relationship.
334
-
335
- ## Includes
336
-
337
- Use `#includes()` to ["sideload"](http://jsonapi.org/format/#fetching-includes) associations:
338
-
339
- ```typescript
340
- Post.includes("comments").all()
341
- ```
342
- <blockquote class="url">
343
- <p>/posts?include=comments</p>
344
- </blockquote>
345
-
346
- You can also pass an array of associations:
347
-
348
- ```typescript
349
- Post.includes(["blog", "comments"]).all()
350
- ```
351
- <blockquote class="url">
352
- <p>/posts?include=blog,comments</p>
353
- </blockquote>
354
-
355
- Or an object for nested associations:
356
-
357
- ```typescript
358
- Post.includes(["blog", { comments: "author" }]).all()
359
- ```
360
- <blockquote class="url">
361
- <p>/posts?include=blog,comments.author</p>
362
- </blockquote>
363
-
364
- ## Nested Queries
365
-
366
- We can nest all read operations at any level of the graph. Let's say we wanted to fetch all `Post`s and their `Comment`s...but only return comments that are `active`, sorted by `created_at` descending. We can create a `Comment` scope as normal, then `#merge()` it into our `Post` scope:
367
-
368
- ```typescript
369
- let commentScope = Comment
370
- .where({ active: true })
371
- .order({ created_at: "desc" })
372
- Post
373
- .includes("comments")
374
- .merge({ comments: commentScope })
375
- .all()
376
- ```
377
-
378
- ```javascript
379
- var commentScope = Comment
380
- .where({ active: true })
381
- .order({ created_at: "desc" })
382
- Post
383
- .includes("comments")
384
- .merge({ comments: commentScope })
385
- .all()
386
- ```
387
- <blockquote class="url">
388
- <p>/posts?include=comments&filter[comments][active]=true&sort=-comments.active</p>
389
- </blockquote>
390
-
391
- Because this can get verbose, it's often desirable to store it on the class:
392
-
393
- ```typescript
394
- class Comment extends ApplicationRecord {
395
- // ... code ...
396
- static recent() {
397
- return this
398
- .where({ active: true })
399
- .order({ created_at: "desc" })
400
- }
401
- }
402
-
403
- Post.merge({ comments: Comment.recent() }).all()
404
- ```
405
-
406
- ```javascript
407
- const Comment = ApplicationRecord.extend({
408
- // ... code ...
409
- static: {
410
- recent: function() {
411
- return this
412
- .where({ active: true })
413
- .order({ created_at: "desc" })
414
- }
415
- }
416
- })
417
-
418
- Post
419
- .includes("comments")
420
- .merge({ comments: Comment.recent() })
421
- .all()
422
- ```
423
-
424
- Any number of scopes can be merged in. Just remember to `#include()` and `#merge()` relationship names **as the server understands them**:
425
-
426
- ```typescript
427
- class Dog extends ApplicationRecord {
428
- @BelongsTo() person: Person
429
- }
430
-
431
- // We've modeled this as Dog > person in javascript
432
- // And Person is jsonapiType "people"
433
- // But the server defined the relationship as "owner"
434
- Dog.includes("owner").merge({ owner: Person.limitedFields() })
435
- ```
436
-
437
- ```javascript
438
- const Dog = ApplicationRecord.extend({
439
- // ... code ...
440
- methods: {
441
- person: belongsTo()
442
- }
443
- })
444
-
445
- // We've modeled this as Dog > person in javascript
446
- // And Person is jsonapiType "people"
447
- // But the server defined the relationship as "owner"
448
- Dog.includes("owner").merge({ owner: Person.limitedFields() })
449
- ```
450
-
451
- ## Statistics
452
-
453
- Use `#stats()` to request statistics. Access stats within `meta`:
454
-
455
- ```typescript
456
- let { data } = await Post.stats({ total: "count" }).all()
457
- data.meta.stats.total.count // the total count
458
- ```
459
-
460
- ```javascript
461
- Post.stats({ total: "count" }).all().then(function(response) {
462
- response.meta.stats.total.count // the total count
463
- })
464
- ```
465
- <blockquote class="url">
466
- <p>/posts?stats[total]=count</p>
467
- </blockquote>
468
-
469
- Stats are always independent of pagination. If you request the total count, you'll get the total count even if you're limiting to 10 per page. This means to get **only** statistics - avoid returning `Post` instances altogether - request `0` results per page:
470
-
471
- ```typescript
472
- let { data } = await Post.per(0)stats({ total: "count" }).all()
473
- data.meta.stats.total.count // the total count
474
- ```
475
-
476
- ```javascript
477
- Post
478
- .per(0)
479
- .stats({ total: "count" })
480
- .all().then(function(response) {
481
- response.meta.stats.total.count // the total count
482
- })
483
- ```
484
- <blockquote class="url">
485
- <p>/posts?stats[total]=count&page[size]=0</p>
486
- </blockquote>
487
-
488
- <h2 id="next">
489
- <a href="/js/writes">
490
- NEXT:
491
- <small>Writes</small>
492
- &raquo;
493
- </a>
494
- </h2>
@@ -1,100 +0,0 @@
1
- ---
2
- title: 'State Syncing'
3
- sidebar_position: 8
4
- ---
5
-
6
- ### State Syncing
7
-
8
- You may have encountered state management libraries like
9
- [Redux](https://redux.js.org) or [Vuex](https://vuex.vuejs.org/en/intro.html). These are fantastic libraries, but their usefulness is lessened with Spraypaint. As a full-fledged model layer, Spraypaint manages state for you, automatically.
10
-
11
- If you opt-in to this feature:
12
-
13
- ```typescript
14
- ApplicationRecord.sync = true
15
- ```
16
-
17
- Instances will sync up whenever the server tells us about updated state.
18
- Consider the scenario where an instance is initially loaded, then separately polled in the background:
19
-
20
- ```typescript
21
- let person = (await Person.find(1)).data
22
-
23
- let poll = () => {
24
- await Person.find(1)
25
- setTimeout(poll, 1000)
26
- }
27
- poll()
28
- ```
29
-
30
- ```javascript
31
- Person.find(1).then(function(response) {
32
- var person = response.data;
33
- });
34
-
35
- var poll = function() {
36
- Person.find(1);
37
- setTimeout(poll, 1000);
38
- }
39
- poll()
40
- ```
41
-
42
- Our `poll()` function **never assigns or updates `person`**. But if the server returns an updated `name` attribute, **`person.name` will be automatically updated**. This is true even if `person.name`
43
- is bound in 17 different nested components.
44
-
45
- Instances can still update their attributes independently - we only sync
46
- when the server returns updated data:
47
-
48
- ```typescript
49
- let instanceA = (await Person.find(1)).data
50
- let instanceB = (await Person.find(1)).data
51
-
52
- instanceA.name // "Jane"
53
- instanceB.name // "Jane"
54
-
55
- instanceB.name = "Silvia"
56
- instanceA.name // "Jane"
57
- instanceB.name // "Silvia"
58
-
59
- await instanceB.save()
60
- instanceA.name // "Silvia"
61
- instanceB.name // "Silvia"
62
- ```
63
-
64
- ```javascript
65
- var instanceA, instanceB;
66
- Person.find(1).then(function(response) {
67
- instanceA = response.data;
68
- });
69
- Person.find(1).then(function(response) {
70
- instanceB = response.data;
71
- });
72
-
73
- instanceA.name // "Jane"
74
- instanceB.name // "Jane"
75
-
76
- instanceB.name = "Silvia"
77
- instanceA.name // "Jane"
78
- instanceB.name // "Silvia"
79
-
80
- instanceB.save().then(function() {
81
- instanceA.name // "Silvia"
82
- instanceB.name // "Silvia"
83
- });
84
- ```
85
-
86
- #### Gotchas
87
-
88
- Under the hood, instances are listening for updates from a central data
89
- store. This means that you'll want to remove listeners whenever you no
90
- longer need the instance - otherwise it will never be garbage collected
91
- properly. To remove a listener:
92
-
93
- ```typescript
94
- instance.unlisten()
95
- ```
96
-
97
- In practice, when developing in a SPA, you'll want to `#unlisten()`
98
- whenever a view is destroyed and model instances no longer need to be referenced. If
99
- you are using VueJS, this is done automatically by adding [spraypaint-vue](https://github.com/graphiti-api/spraypaint-vue)
100
- to your application.