sequel-jsonapi_eager 0.0.1
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/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/lib/sequel/plugins/jsonapi_eager.rb +35 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7f90fa7b6e3f683a6a25a63901985bac74893597
|
4
|
+
data.tar.gz: cf500a78f631337659560ee93f3c9511569d8fea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 333cefb4fa6d1b2fbe327e77be20a2230f1d66c829ba2875591c7877be44d5d25d9cae7860e05294806619e3f0ac7f54f342f3f260d831a14ed4b4eb1c4576f3
|
7
|
+
data.tar.gz: 3e8f33edb42496dccb4afc36ba51c3b1e39dedc2f4090bf16a533f1b86397518d92c5a148fe03b23b4fb496f8170152e662a38fc0a11d90dee548553f7c29cd8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Janko Marohnić
|
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,77 @@
|
|
1
|
+
# Sequel::Plugins::JsonapiEager
|
2
|
+
|
3
|
+
This [Sequel](http://sequel.jeremyevans.net/) plugin enables you to dynamically
|
4
|
+
eager load associations of objects you're exposing for your JSON API.
|
5
|
+
|
6
|
+
## Motivation
|
7
|
+
|
8
|
+
The [JSON-API](http://jsonapi.org/) specification introduces the ability for
|
9
|
+
clients to choose which associations they want to include in the response:
|
10
|
+
|
11
|
+
```http
|
12
|
+
GET /users?include=quizzes.questions,posts
|
13
|
+
```
|
14
|
+
|
15
|
+
You as an author of the API want to eager load those associations, to avoid
|
16
|
+
N+1 queries. However, Sequel has a slighly different API for eager loading
|
17
|
+
associations:
|
18
|
+
|
19
|
+
```rb
|
20
|
+
User.eager(:quizzes => :questions).eager(:posts)
|
21
|
+
```
|
22
|
+
|
23
|
+
This plugin translates the JSON-API `include` format into Sequel.
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'sequel-jsonapi_eager'
|
29
|
+
```
|
30
|
+
|
31
|
+
This plugin exposes the `jsonapi_eager` method to your Sequel objects, to which
|
32
|
+
you can pass in the `include` query parameter.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
User.plugin :jsonapi_eager
|
36
|
+
|
37
|
+
users = User.jsonapi_eager("quizzes.questions,posts")
|
38
|
+
# translates into `User.eager(:quizzes => :questions).eager(:posts)`
|
39
|
+
|
40
|
+
users.all
|
41
|
+
# SELECT * FROM `users`
|
42
|
+
# SELECT * FROM `quizzes` WHERE (`quizzes`.`user_id` IN (3, 54, 5))
|
43
|
+
# SELECT * FROM `questions` WHERE (`questions`.`quiz_id` IN (2, 17, 19, 43))
|
44
|
+
# SELECT * FROM `posts` WHERE (`posts`.`user_id` IN (3, 54, 5))
|
45
|
+
```
|
46
|
+
|
47
|
+
You can also use this method in the dataset:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
User.active.jsonapi_eager("quizzes.questions,posts")
|
51
|
+
```
|
52
|
+
|
53
|
+
What this plugin also gives you, which was the non-trivial part of the plugin,
|
54
|
+
is the ability to eager load associations on model *instances*.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
user = User[user_id]
|
58
|
+
user.jsonapi_eager("quizzes.questions,posts")
|
59
|
+
```
|
60
|
+
|
61
|
+
You need this when exposing single resources in your API, because you need to
|
62
|
+
determine what to eager load at serialization time, at which point you only
|
63
|
+
have the instance. Moreover, Sequel removes the eager loading when you fetch
|
64
|
+
single records, so the following doesn't work:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
user = User.eager(:quizzes => :questions)[user_id]
|
68
|
+
user # Doesn't contain the eager loading anymore
|
69
|
+
user.quizzes.map(&:questions) # N+1 query
|
70
|
+
```
|
71
|
+
|
72
|
+
This plugins helps you make very flexible APIs, while still keeping the
|
73
|
+
performance tip-top.
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
[MIT](/LICENSE.txt)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module JsonapiEager
|
4
|
+
module DatasetMethods
|
5
|
+
def jsonapi_eager(includes)
|
6
|
+
includes = includes.split(",") if includes.is_a?(String)
|
7
|
+
|
8
|
+
eager_args = includes.map do |relationship|
|
9
|
+
path = relationship.split(".").map(&:to_sym)
|
10
|
+
path.reverse.inject { |hash, rel| {rel => hash} }
|
11
|
+
end
|
12
|
+
|
13
|
+
eager(*eager_args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
ClassMethods = DatasetMethods
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def jsonapi_eager(includes)
|
21
|
+
includes = includes.split(",") if includes.is_a?(String)
|
22
|
+
|
23
|
+
includes.each do |relationship|
|
24
|
+
association_name, association_include = relationship.split(".", 2)
|
25
|
+
association_dataset = send("#{association_name}_dataset")
|
26
|
+
.jsonapi_eager(association_include.to_s)
|
27
|
+
associations[association_name.to_sym] = association_dataset.all
|
28
|
+
end
|
29
|
+
|
30
|
+
self
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-jsonapi_eager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janko Marohnić
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-hooks
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- janko.marohnic@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- lib/sequel/plugins/jsonapi_eager.rb
|
79
|
+
homepage: https://github.com/janko-m/sequel-jsonapi_eager
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.4.5
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: A Sequel plugin for eager loading associations in JSON-API specification.
|
103
|
+
test_files: []
|
104
|
+
has_rdoc:
|