parse-stack-next 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.bundle/config +2 -0
- data/.env.sample +112 -0
- data/.env.test +10 -0
- data/.github/workflows/ruby.yml +36 -0
- data/.gitignore +49 -0
- data/.ruby-version +1 -0
- data/.solargraph.yml +22 -0
- data/CHANGELOG.md +5816 -0
- data/Gemfile +30 -0
- data/Gemfile.lock +175 -0
- data/LICENSE.txt +23 -0
- data/Makefile +63 -0
- data/README.md +5655 -0
- data/Rakefile +573 -0
- data/bin/console +38 -0
- data/bin/parse-console +136 -0
- data/bin/server +17 -0
- data/bin/setup +7 -0
- data/config/parse-config.json +12 -0
- data/docs/TEST_SERVER.md +271 -0
- data/docs/_config.yml +1 -0
- data/docs/mcp_guide.md +3484 -0
- data/docs/mongodb_direct_guide.md +1348 -0
- data/docs/mongodb_index_optimization_guide.md +631 -0
- data/examples/transaction_example.rb +219 -0
- data/lib/parse/acl_scope.rb +728 -0
- data/lib/parse/agent/cancellation_token.rb +80 -0
- data/lib/parse/agent/constraint_translator.rb +480 -0
- data/lib/parse/agent/describe.rb +420 -0
- data/lib/parse/agent/errors.rb +133 -0
- data/lib/parse/agent/mcp_client.rb +557 -0
- data/lib/parse/agent/mcp_dispatcher.rb +1023 -0
- data/lib/parse/agent/mcp_rack_app.rb +1143 -0
- data/lib/parse/agent/mcp_server.rb +376 -0
- data/lib/parse/agent/metadata_audit.rb +259 -0
- data/lib/parse/agent/metadata_dsl.rb +733 -0
- data/lib/parse/agent/metadata_registry.rb +794 -0
- data/lib/parse/agent/pipeline_validator.rb +82 -0
- data/lib/parse/agent/prompts.rb +351 -0
- data/lib/parse/agent/rate_limiter.rb +158 -0
- data/lib/parse/agent/relation_graph.rb +162 -0
- data/lib/parse/agent/result_formatter.rb +453 -0
- data/lib/parse/agent/tools.rb +5489 -0
- data/lib/parse/agent.rb +3249 -0
- data/lib/parse/api/aggregate.rb +79 -0
- data/lib/parse/api/all.rb +26 -0
- data/lib/parse/api/analytics.rb +18 -0
- data/lib/parse/api/batch.rb +33 -0
- data/lib/parse/api/cloud_functions.rb +58 -0
- data/lib/parse/api/config.rb +125 -0
- data/lib/parse/api/files.rb +29 -0
- data/lib/parse/api/hooks.rb +117 -0
- data/lib/parse/api/objects.rb +146 -0
- data/lib/parse/api/path_segment.rb +75 -0
- data/lib/parse/api/push.rb +20 -0
- data/lib/parse/api/schema.rb +49 -0
- data/lib/parse/api/server.rb +50 -0
- data/lib/parse/api/sessions.rb +24 -0
- data/lib/parse/api/users.rb +250 -0
- data/lib/parse/atlas_search/index_manager.rb +353 -0
- data/lib/parse/atlas_search/result.rb +204 -0
- data/lib/parse/atlas_search/search_builder.rb +604 -0
- data/lib/parse/atlas_search/session.rb +253 -0
- data/lib/parse/atlas_search.rb +995 -0
- data/lib/parse/client/authentication.rb +97 -0
- data/lib/parse/client/batch.rb +234 -0
- data/lib/parse/client/body_builder.rb +240 -0
- data/lib/parse/client/caching.rb +203 -0
- data/lib/parse/client/logging.rb +293 -0
- data/lib/parse/client/profiling.rb +181 -0
- data/lib/parse/client/protocol.rb +91 -0
- data/lib/parse/client/request.rb +233 -0
- data/lib/parse/client/response.rb +208 -0
- data/lib/parse/client.rb +1104 -0
- data/lib/parse/clp_scope.rb +361 -0
- data/lib/parse/live_query/circuit_breaker.rb +256 -0
- data/lib/parse/live_query/client.rb +1001 -0
- data/lib/parse/live_query/configuration.rb +224 -0
- data/lib/parse/live_query/event.rb +115 -0
- data/lib/parse/live_query/event_queue.rb +272 -0
- data/lib/parse/live_query/health_monitor.rb +214 -0
- data/lib/parse/live_query/logging.rb +149 -0
- data/lib/parse/live_query/subscription.rb +294 -0
- data/lib/parse/live_query.rb +163 -0
- data/lib/parse/lookup_rewriter.rb +445 -0
- data/lib/parse/model/acl.rb +968 -0
- data/lib/parse/model/associations/belongs_to.rb +275 -0
- data/lib/parse/model/associations/collection_proxy.rb +435 -0
- data/lib/parse/model/associations/has_many.rb +597 -0
- data/lib/parse/model/associations/has_one.rb +158 -0
- data/lib/parse/model/associations/pointer_collection_proxy.rb +134 -0
- data/lib/parse/model/associations/relation_collection_proxy.rb +177 -0
- data/lib/parse/model/bytes.rb +62 -0
- data/lib/parse/model/classes/audience.rb +262 -0
- data/lib/parse/model/classes/installation.rb +363 -0
- data/lib/parse/model/classes/job_schedule.rb +153 -0
- data/lib/parse/model/classes/job_status.rb +264 -0
- data/lib/parse/model/classes/product.rb +75 -0
- data/lib/parse/model/classes/push_status.rb +263 -0
- data/lib/parse/model/classes/role.rb +751 -0
- data/lib/parse/model/classes/session.rb +201 -0
- data/lib/parse/model/classes/user.rb +943 -0
- data/lib/parse/model/clp.rb +544 -0
- data/lib/parse/model/core/actions.rb +1268 -0
- data/lib/parse/model/core/builder.rb +139 -0
- data/lib/parse/model/core/create_lock.rb +386 -0
- data/lib/parse/model/core/describe.rb +382 -0
- data/lib/parse/model/core/enhanced_change_tracking.rb +159 -0
- data/lib/parse/model/core/errors.rb +38 -0
- data/lib/parse/model/core/fetching.rb +566 -0
- data/lib/parse/model/core/field_guards.rb +220 -0
- data/lib/parse/model/core/indexing.rb +382 -0
- data/lib/parse/model/core/parse_reference.rb +407 -0
- data/lib/parse/model/core/properties.rb +809 -0
- data/lib/parse/model/core/querying.rb +491 -0
- data/lib/parse/model/core/schema.rb +202 -0
- data/lib/parse/model/core/search_indexing.rb +174 -0
- data/lib/parse/model/date.rb +88 -0
- data/lib/parse/model/email.rb +213 -0
- data/lib/parse/model/file.rb +527 -0
- data/lib/parse/model/geojson.rb +271 -0
- data/lib/parse/model/geopoint.rb +261 -0
- data/lib/parse/model/model.rb +260 -0
- data/lib/parse/model/object.rb +2068 -0
- data/lib/parse/model/phone.rb +520 -0
- data/lib/parse/model/pointer.rb +443 -0
- data/lib/parse/model/polygon.rb +406 -0
- data/lib/parse/model/push.rb +975 -0
- data/lib/parse/model/shortnames.rb +8 -0
- data/lib/parse/model/time_zone.rb +141 -0
- data/lib/parse/model/validations/uniqueness_validator.rb +97 -0
- data/lib/parse/model/validations.rb +96 -0
- data/lib/parse/mongodb.rb +2300 -0
- data/lib/parse/pipeline_security.rb +554 -0
- data/lib/parse/query/constraint.rb +198 -0
- data/lib/parse/query/constraints.rb +3279 -0
- data/lib/parse/query/cursor.rb +434 -0
- data/lib/parse/query/n_plus_one_detector.rb +445 -0
- data/lib/parse/query/operation.rb +104 -0
- data/lib/parse/query/ordering.rb +66 -0
- data/lib/parse/query.rb +7028 -0
- data/lib/parse/schema/index_migrator.rb +291 -0
- data/lib/parse/schema/search_index_migrator.rb +289 -0
- data/lib/parse/schema.rb +494 -0
- data/lib/parse/stack/generators/rails.rb +40 -0
- data/lib/parse/stack/generators/templates/model.erb +51 -0
- data/lib/parse/stack/generators/templates/model_installation.rb +4 -0
- data/lib/parse/stack/generators/templates/model_role.rb +4 -0
- data/lib/parse/stack/generators/templates/model_session.rb +4 -0
- data/lib/parse/stack/generators/templates/model_user.rb +11 -0
- data/lib/parse/stack/generators/templates/parse.rb +12 -0
- data/lib/parse/stack/generators/templates/webhooks.rb +10 -0
- data/lib/parse/stack/railtie.rb +18 -0
- data/lib/parse/stack/tasks.rb +563 -0
- data/lib/parse/stack/version.rb +11 -0
- data/lib/parse/stack.rb +455 -0
- data/lib/parse/two_factor_auth/user_extension.rb +449 -0
- data/lib/parse/two_factor_auth.rb +310 -0
- data/lib/parse/webhooks/payload.rb +360 -0
- data/lib/parse/webhooks/registration.rb +199 -0
- data/lib/parse/webhooks/replay_protection.rb +189 -0
- data/lib/parse/webhooks.rb +510 -0
- data/lib/parse-stack-next.rb +5 -0
- data/lib/parse-stack.rb +5 -0
- data/parse-stack-next.gemspec +82 -0
- data/parse-stack.png +0 -0
- data/scripts/debug-ips.js +35 -0
- data/scripts/docker/Dockerfile.parse +13 -0
- data/scripts/docker/atlas-init.js +284 -0
- data/scripts/docker/docker-compose.atlas.yml +76 -0
- data/scripts/docker/docker-compose.test.yml +106 -0
- data/scripts/docker/mongo-init.js +21 -0
- data/scripts/eval_mcp_with_lm_studio.rb +274 -0
- data/scripts/start-parse.sh +90 -0
- data/scripts/start_mcp_server.rb +78 -0
- data/scripts/test_server_connection.rb +82 -0
- metadata +377 -0
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative "../pointer"
|
|
5
|
+
require_relative "collection_proxy"
|
|
6
|
+
require_relative "pointer_collection_proxy"
|
|
7
|
+
require_relative "relation_collection_proxy"
|
|
8
|
+
|
|
9
|
+
module Parse
|
|
10
|
+
module Associations
|
|
11
|
+
|
|
12
|
+
# Parse has many ways to implement one-to-many and many-to-many
|
|
13
|
+
# associations: `Array`, `Parse Relation` or through a `Query`. How you decide
|
|
14
|
+
# to implement your associations, will affect how `has_many` works in
|
|
15
|
+
# Parse-Stack. Parse natively supports one-to-many and many-to-many
|
|
16
|
+
# relationships using `Array` and `Relations`, as described in
|
|
17
|
+
# {http://docs.parseplatform.org/js/guide/#relations Parse Relational Data}.
|
|
18
|
+
# Both of these methods require you define a specific column type in your
|
|
19
|
+
# Parse table that will be used to store information about the association.
|
|
20
|
+
#
|
|
21
|
+
# In addition to `Array` and `Relation`, Parse-Stack also implements the
|
|
22
|
+
# standard `has_many` behavior prevalent in other frameworks through a query
|
|
23
|
+
# where the associated class contains a foreign pointer to the local class,
|
|
24
|
+
# usually the inverse of a `belongs_to`. This requires that the associated
|
|
25
|
+
# class has a defined column that contains a pointer the refers to the
|
|
26
|
+
# defining class.
|
|
27
|
+
#
|
|
28
|
+
# *Query-Approach*
|
|
29
|
+
#
|
|
30
|
+
# In this `Query` implementation, a `has_many` association for a Parse class
|
|
31
|
+
# requires that another Parse class will have a foreign pointer that refers
|
|
32
|
+
# to instances of this class. This is the standard way that `has_many`
|
|
33
|
+
# relationships work in most databases systems. This is usually the case when
|
|
34
|
+
# you have a class that has a `belongs_to` relationship to instances of the
|
|
35
|
+
# local class.
|
|
36
|
+
#
|
|
37
|
+
# In the example below, many songs belong to a specific artist. We set this
|
|
38
|
+
# association by setting {Associations::BelongsTo.belongs_to :belongs_to} relationship from `Song` to `Artist`.
|
|
39
|
+
# Knowing there is a column in `Song` that points to instances of an `Artist`,
|
|
40
|
+
# we can setup a `has_many` association to `Song` instances in the `Artist`
|
|
41
|
+
# class. Doing so will generate a helper query method on the `Artist` instance
|
|
42
|
+
# objects.
|
|
43
|
+
#
|
|
44
|
+
# class Song < Parse::Object
|
|
45
|
+
# property :released, :date
|
|
46
|
+
# # this class will have a pointer column to an Artist
|
|
47
|
+
# belongs_to :artist
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# class Artist < Parse::Object
|
|
51
|
+
# has_many :songs
|
|
52
|
+
# end
|
|
53
|
+
#
|
|
54
|
+
# artist = Artist.first
|
|
55
|
+
#
|
|
56
|
+
# artist.songs # => [all songs belonging to artist]
|
|
57
|
+
# # equivalent: Song.all(artist: artist)
|
|
58
|
+
#
|
|
59
|
+
# # filter also by release date
|
|
60
|
+
# artist.songs(:released.after => 1.year.ago)
|
|
61
|
+
# # equivalent: Song.all(artist: artist, :released.after => 1.year.ago)
|
|
62
|
+
#
|
|
63
|
+
# In order to modify the associated objects (ex. `songs`), you must modify
|
|
64
|
+
# their corresponding `belongs_to` field (in this case `song.artist`), to
|
|
65
|
+
# another record and save it.
|
|
66
|
+
#
|
|
67
|
+
# Options for `has_many` using the `Query` approach are `:as` and `:field`. The
|
|
68
|
+
# `:as` option behaves similarly to the {Associations::BelongsTo.belongs_to :belongs_to} counterpart. The
|
|
69
|
+
# `:field` option can be used to override the derived column name located
|
|
70
|
+
# in the foreign class. The default value for `:field` is the columnized
|
|
71
|
+
# version of the Parse subclass `parse_class` method.
|
|
72
|
+
#
|
|
73
|
+
# class Parse::User
|
|
74
|
+
# # since the foreign column name is :agent
|
|
75
|
+
# has_many :artists, field: :agent
|
|
76
|
+
# end
|
|
77
|
+
#
|
|
78
|
+
# class Artist < Parse::Object
|
|
79
|
+
# belongs_to :manager, as: :user, field: :agent
|
|
80
|
+
# end
|
|
81
|
+
#
|
|
82
|
+
# artist.manager # => Parse::User object
|
|
83
|
+
#
|
|
84
|
+
# user.artists # => [artists where :agent column is user]
|
|
85
|
+
#
|
|
86
|
+
#
|
|
87
|
+
# When using this approach, you may also employ the use of scopes to filter the particular data from the `has_many` association.
|
|
88
|
+
#
|
|
89
|
+
# class Artist
|
|
90
|
+
# has_many :songs, ->(timeframe) { where(:created_at.after => timeframe) }
|
|
91
|
+
# end
|
|
92
|
+
#
|
|
93
|
+
# artist.songs(6.months.ago)
|
|
94
|
+
# # => [artist's songs created in the last 6 months]
|
|
95
|
+
#
|
|
96
|
+
# You may also call property methods in your scopes related to the instance.
|
|
97
|
+
# *Note:* You also have access to the instance object for the local class
|
|
98
|
+
# through a special "*i*" method in the scope.
|
|
99
|
+
#
|
|
100
|
+
# class Concert
|
|
101
|
+
# property :city
|
|
102
|
+
# belongs_to :artist
|
|
103
|
+
# end
|
|
104
|
+
#
|
|
105
|
+
# class Artist
|
|
106
|
+
# property :hometown
|
|
107
|
+
# has_many :local_concerts, -> { where(:city => hometown) }, as: :concerts
|
|
108
|
+
# end
|
|
109
|
+
#
|
|
110
|
+
# # assume
|
|
111
|
+
# artist.hometown = "San Diego"
|
|
112
|
+
#
|
|
113
|
+
# # artist's concerts in their hometown of 'San Diego'
|
|
114
|
+
# artist.local_concerts
|
|
115
|
+
# # equivalent: Concert.all(artist: artist, city: artist.hometown)
|
|
116
|
+
#
|
|
117
|
+
# You may also omit the association completely, as rely on the scope to fetch the
|
|
118
|
+
# associated records. This makes the `has_many` work as a macro query setting the :scope_only
|
|
119
|
+
# option to true:
|
|
120
|
+
#
|
|
121
|
+
# class Author < Parse::Object
|
|
122
|
+
# property :name
|
|
123
|
+
# has_many :posts, ->{ where :tags.in => name.downcase }, scope_only: true
|
|
124
|
+
# end
|
|
125
|
+
#
|
|
126
|
+
# class Post < Parse::Object
|
|
127
|
+
# property :tags, :array
|
|
128
|
+
# end
|
|
129
|
+
#
|
|
130
|
+
# author.posts # => Posts where author's name is a tag
|
|
131
|
+
# # equivalent: Post.all( :tags.in => artist.name.downcase )
|
|
132
|
+
#
|
|
133
|
+
# *Array-Approach*
|
|
134
|
+
#
|
|
135
|
+
# In the `Array` implemenatation, you can designate a column to be of `Array`
|
|
136
|
+
# type that contains a list of Parse pointers. Parse-Stack supports this by
|
|
137
|
+
# passing the option `through: :array` to the `has_many` method. If you use
|
|
138
|
+
# this approach, it is recommended that this is used for associations where
|
|
139
|
+
# the quantity is less than 100 in order to maintain query and fetch
|
|
140
|
+
# performance. You would be in charge of maintaining the array with the proper
|
|
141
|
+
# list of Parse pointers that are associated to the object. Parse-Stack does
|
|
142
|
+
# help by wrapping the array in a {Parse::PointerCollectionProxy} which provides dirty tracking.
|
|
143
|
+
#
|
|
144
|
+
# class Artist < Parse::Object
|
|
145
|
+
# end
|
|
146
|
+
#
|
|
147
|
+
# class Band < Parse::Object
|
|
148
|
+
# has_many :artists, through: :array
|
|
149
|
+
# end
|
|
150
|
+
#
|
|
151
|
+
# artist = Artist.first
|
|
152
|
+
#
|
|
153
|
+
# # find all bands that contain this artist
|
|
154
|
+
# bands = Band.all( :artists.in => [artist.pointer] )
|
|
155
|
+
#
|
|
156
|
+
# band = bands.first
|
|
157
|
+
# band.artists # => [array of Artist pointers]
|
|
158
|
+
#
|
|
159
|
+
# # remove artists
|
|
160
|
+
# band.artists.remove artist
|
|
161
|
+
#
|
|
162
|
+
# # add artist
|
|
163
|
+
# band.artists.add artist
|
|
164
|
+
#
|
|
165
|
+
# # save changes
|
|
166
|
+
# band.save
|
|
167
|
+
#
|
|
168
|
+
# *ParseRelation-Approach*
|
|
169
|
+
#
|
|
170
|
+
# Other than the use of arrays, Parse supports native one-to-many and many-to-many
|
|
171
|
+
# associations through what is referred to as a {http://docs.parseplatform.org/js/guide/#many-to-many Parse Relation}.
|
|
172
|
+
# This is implemented by defining a column to be of type `Relation` which
|
|
173
|
+
# refers to a foreign class. Parse-Stack supports this by passing the
|
|
174
|
+
# `through: :relation` option to the `has_many` method. Designating a column
|
|
175
|
+
# as a Parse relation to another class type, will create a one-way intermediate
|
|
176
|
+
# "join-list" between the local class and the foreign class. One important
|
|
177
|
+
# distinction of this compared to other types of data stores (ex. PostgresSQL) is that:
|
|
178
|
+
#
|
|
179
|
+
# *1*. The inverse relationship association is not available automatically. Therefore, having a column of `artists` in a `Band` class that relates to members of the band (as `Artist` class), does not automatically make a set of `Band` records available to `Artist` records for which they have been related. If you need to maintain both the inverse relationship between a foreign class to its associations, you will need to manually manage that by adding two Parse relation columns in each class, or by creating a separate class (ex. `ArtistBands`) that is used as a join table.
|
|
180
|
+
#
|
|
181
|
+
# *2*. Querying the relation is actually performed against the implicit join table, not the local one.
|
|
182
|
+
#
|
|
183
|
+
# *3*. Applying query constraints for a set of records within a relation is performed against the foreign table class, not the class having the relational column.
|
|
184
|
+
#
|
|
185
|
+
# The Parse documentation provides more details on associations, see {http://docs.parseplatform.org/ios/guide/#relations Parse Relations Guide}.
|
|
186
|
+
# Parse-Stack will handle the work for (2) and (3) automatically.
|
|
187
|
+
#
|
|
188
|
+
# In the example below, a `Band` can have thousands of `Fans`. We setup a
|
|
189
|
+
# `Relation<Fan>` column in the `Band` class that references the `Fan` class.
|
|
190
|
+
# Parse-Stack provides methods to manage the relationship under the {Parse::RelationCollectionProxy}
|
|
191
|
+
# class.
|
|
192
|
+
#
|
|
193
|
+
# class Fan < Parse::Object
|
|
194
|
+
# # .. lots of properties ...
|
|
195
|
+
# property :location, :geopoint
|
|
196
|
+
# end
|
|
197
|
+
#
|
|
198
|
+
# class Band < Parse::Object
|
|
199
|
+
# has_many :fans, through: :relation
|
|
200
|
+
# end
|
|
201
|
+
#
|
|
202
|
+
# band = Band.first
|
|
203
|
+
#
|
|
204
|
+
# # the number of fans in the relation
|
|
205
|
+
# band.fans.count
|
|
206
|
+
#
|
|
207
|
+
# # get the first object in relation
|
|
208
|
+
# fan = bands.fans.first # => Parse::User object
|
|
209
|
+
#
|
|
210
|
+
# # use `add` or `remove` to modify relations
|
|
211
|
+
# band.fans.add user
|
|
212
|
+
# bands.fans.remove user
|
|
213
|
+
#
|
|
214
|
+
# # updates the relation as well as changes to `band`
|
|
215
|
+
# band.fans.save
|
|
216
|
+
#
|
|
217
|
+
# # Find 50 fans who are near San Diego, CA
|
|
218
|
+
# downtown = Parse::GeoPoint.new(32.82, -117.23)
|
|
219
|
+
# fans = band.fans.all :location.near => downtown
|
|
220
|
+
#
|
|
221
|
+
# You can perform atomic additions and removals of objects from `has_many`
|
|
222
|
+
# relations using the methods below. Parse allows this by providing a specific atomic operation
|
|
223
|
+
# request. The operation is performed directly on Parse server
|
|
224
|
+
# and *NOT* on your instance object.
|
|
225
|
+
#
|
|
226
|
+
# # atomically add/remove
|
|
227
|
+
# band.artists.add! objects # { __op: :AddUnique }
|
|
228
|
+
# band.artists.remove! objects # { __op: :AddUnique }
|
|
229
|
+
#
|
|
230
|
+
# # atomically add unique Artist
|
|
231
|
+
# band.artists.add_unique! objects # { __op: :AddUnique }
|
|
232
|
+
#
|
|
233
|
+
# # atomically add/remove relations
|
|
234
|
+
# band.fans.add! users # { __op: :Add }
|
|
235
|
+
# band.fans.remove! users # { __op: :Remove }
|
|
236
|
+
#
|
|
237
|
+
# # atomically perform a delete operation on this field name
|
|
238
|
+
# # this should set it as `undefined`.
|
|
239
|
+
# band.op_destroy!("category") # { __op: :Delete }
|
|
240
|
+
#
|
|
241
|
+
# You can also perform queries against class entities to find related objects. Assume
|
|
242
|
+
# that users can like a band. The `Band` class can have a `likes` column that is
|
|
243
|
+
# a Parse relation to the {Parse::User} class containing the users who have liked a
|
|
244
|
+
# specific band.
|
|
245
|
+
#
|
|
246
|
+
#
|
|
247
|
+
# class Band < Parse::Object
|
|
248
|
+
# # likes is a Parse relation column of user objects.
|
|
249
|
+
# has_many :likes, through: :relation, as: :user
|
|
250
|
+
# end
|
|
251
|
+
#
|
|
252
|
+
# You can now find all {Parse::User} records who have liked a specific band. In the
|
|
253
|
+
# example below, the `:likes` key refers to the `likes` column defined in the `Band`
|
|
254
|
+
# collection which contains the set of user records.
|
|
255
|
+
#
|
|
256
|
+
# band = Band.first # get a band
|
|
257
|
+
#
|
|
258
|
+
# # find all users who have liked this band, where :likes is a column
|
|
259
|
+
# # in the Band collection - NOT in the User collection.
|
|
260
|
+
# users = Parse::User.all :likes.related_to => band
|
|
261
|
+
#
|
|
262
|
+
# You can also find all bands that a specific user has liked.
|
|
263
|
+
#
|
|
264
|
+
# user = Parse::User.first
|
|
265
|
+
#
|
|
266
|
+
# # find all bands where this user
|
|
267
|
+
# # is in the `likes` column of the Band collection
|
|
268
|
+
# bands_liked_by_user = Band.all :likes => user
|
|
269
|
+
#
|
|
270
|
+
module HasMany
|
|
271
|
+
|
|
272
|
+
# @!attribute [rw] self.relations
|
|
273
|
+
# A hash mapping of all has_many associations that use the ParseRelation implementation.
|
|
274
|
+
# @return [Hash]
|
|
275
|
+
|
|
276
|
+
# Define a one-to-many or many-to-many association between the local model and a foreign class.
|
|
277
|
+
# Options for `has_many` are the same as the {Associations::BelongsTo.belongs_to} counterpart with
|
|
278
|
+
# support for `:required`, `:as` and `:field`. It has additional options.
|
|
279
|
+
#
|
|
280
|
+
# @!method self.has_many(key, scope = nil, opts = {})
|
|
281
|
+
# @param [Symbol] key The pluralized version of the foreign class. Using the :query method,
|
|
282
|
+
# this implies the name of the foreign column that a pointer to this record.
|
|
283
|
+
# Using the :array or :relation method, this implies the name of the local
|
|
284
|
+
# column that contains either an array of Parse::Pointers in the case of :array,
|
|
285
|
+
# or the Parse Relation, in the case of :relation.
|
|
286
|
+
# @param [Proc] scope Only applicable using :query. A proc that can customize the query by applying
|
|
287
|
+
# additional constraints when fetching the associated records. Works similarly as
|
|
288
|
+
# ActiveModel associations described in section {http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Customizing the Query}
|
|
289
|
+
# @option opts [Symbol] :through The type of implementation to use: :query (default), :array or :relation.
|
|
290
|
+
# If set to `:array`, it defines the column in Parse as being an array of
|
|
291
|
+
# Parse pointer objects and will be managed locally using a {Parse::PointerCollectionProxy}.
|
|
292
|
+
# If set to `:relation`, it defines a column of type Parse Relation with
|
|
293
|
+
# the foreign class and will be managed locally using a {Parse::RelationCollectionProxy}.
|
|
294
|
+
# If set to `:query`, no storage is required on the local class as the
|
|
295
|
+
# associated records will be fetched using a Parse query.
|
|
296
|
+
# @option opts [Symbol] :field override the name of the remote column to use when fetching the association.
|
|
297
|
+
# When using through :query, this is the column name of the remote column
|
|
298
|
+
# of the foreign class that will be used for matching. When using :array,
|
|
299
|
+
# this is the name of the remote column of the local class that contains
|
|
300
|
+
# an array of pointers to the foreign class. When using :relation, this
|
|
301
|
+
# is the name of the remote column of the local class that contains the Parse Relation.
|
|
302
|
+
# @option opts [Symbol] :as override the inferred Parse::Object subclass of the association.
|
|
303
|
+
# By default this is inferred as the singularized camel case version of
|
|
304
|
+
# the key parameter. This option allows you to override the typecast of
|
|
305
|
+
# foreign Parse model of the association, while allowing you to have a
|
|
306
|
+
# different accessor name.
|
|
307
|
+
# @example
|
|
308
|
+
# has_many :fans, as: :users, through: :relation, field: "awesomeFans"
|
|
309
|
+
# has_many :songs
|
|
310
|
+
# has_many :likes, as: :users, through: :relation
|
|
311
|
+
# has_many :artists, field: "managedArtists"
|
|
312
|
+
#
|
|
313
|
+
# @return [Array<Parse::Object>] if through :query
|
|
314
|
+
# @return [PointerCollectionProxy] if through :array
|
|
315
|
+
# @return [RelationCollectionProxy] if through :relation
|
|
316
|
+
# @see PointerCollectionProxy
|
|
317
|
+
# @see RelationCollectionProxy
|
|
318
|
+
|
|
319
|
+
# @!visibility private
|
|
320
|
+
def self.included(base)
|
|
321
|
+
base.extend(ClassMethods)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# @!visibility private
|
|
325
|
+
module ClassMethods
|
|
326
|
+
attr_writer :relations
|
|
327
|
+
|
|
328
|
+
def relations
|
|
329
|
+
@relations ||= {}
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# Examples:
|
|
333
|
+
# has_many :fans, as: :users, through: :relation, field: "awesomeFans"
|
|
334
|
+
# has_many :songs
|
|
335
|
+
# has_many :likes, as: :users, through: :relation
|
|
336
|
+
# has_many :artists, field: "managedArtists"
|
|
337
|
+
# The first item in the has_many is the name of the local attribute. This will create
|
|
338
|
+
# several methods for accessing the relation type. By default, the remote column name
|
|
339
|
+
# relating to this attribute will be the lower-first-camelcase version of this key.
|
|
340
|
+
# Ex. a relation to :my_songs, would imply that the remote column name is "mySongs". This behavior
|
|
341
|
+
# can be overriden by using the field: option and passing the literal field name in the Parse table.
|
|
342
|
+
# This allows you to use a local attribute name while still having a different remote column name.
|
|
343
|
+
# Since these types of collections are of a particular "type", we will assume that the name of the
|
|
344
|
+
# key is the plural version of the name of the local camelized-named class. Ex. If the property is named :songs, then
|
|
345
|
+
# we will assume there is a local class name defined as 'Song'. This can be overriden by using the as: parameter.
|
|
346
|
+
# This allows you to name your local attribute differently to what the responsible class for this association.
|
|
347
|
+
# Ex. You could define a has_many :favorite_songs property that points to the User class by using the 'as: :songs'. This would
|
|
348
|
+
# imply that the instance object has a set of Song objects through the attribute :favorite_songs.
|
|
349
|
+
# By default, all associations are stored in 'through: :array' form. If you are working with a Parse Relation, you
|
|
350
|
+
# should specify the 'through: :relation' property instead. This will switch the internal storage mechanisms
|
|
351
|
+
# from using a PointerCollectionProxy to a RelationCollectionProxy.
|
|
352
|
+
def has_many_queried(key, scope = nil, **opts)
|
|
353
|
+
# key will be the name of the property
|
|
354
|
+
# the remote class is either key or as.
|
|
355
|
+
opts[:scope_only] ||= false
|
|
356
|
+
klassName = (opts[:as] || key).to_parse_class singularize: true
|
|
357
|
+
foreign_field = (opts[:field] || parse_class.columnize).to_sym
|
|
358
|
+
|
|
359
|
+
define_method(key) do |*args, &block|
|
|
360
|
+
return [] if @id.nil?
|
|
361
|
+
query = Parse::Query.new(klassName, limit: :max)
|
|
362
|
+
|
|
363
|
+
query.where(foreign_field => self) unless opts[:scope_only] == true
|
|
364
|
+
|
|
365
|
+
if scope.is_a?(Proc)
|
|
366
|
+
# magic, override the singleton method_missing with accessing object level methods
|
|
367
|
+
# that don't collide with Parse::Query instance. Still accessible under :i
|
|
368
|
+
instance = self
|
|
369
|
+
query.define_singleton_method(:method_missing) { |m, *args, &block| instance.send(m, *args, &block) }
|
|
370
|
+
query.define_singleton_method(:i) { instance }
|
|
371
|
+
# if the scope takes no arguments, assume arguments are additional conditions
|
|
372
|
+
if scope.arity.zero?
|
|
373
|
+
query.instance_exec(&scope)
|
|
374
|
+
query.conditions(*args) if args.present?
|
|
375
|
+
else
|
|
376
|
+
query.instance_exec(*args, &scope)
|
|
377
|
+
end
|
|
378
|
+
instance = nil # help clean up ruby gc
|
|
379
|
+
elsif args.present?
|
|
380
|
+
query.conditions(*args)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
query.define_singleton_method(:method_missing) do |m, *args, &chained_block|
|
|
384
|
+
klass = Parse::Model.find_class klassName
|
|
385
|
+
|
|
386
|
+
if klass.present? && klass.respond_to?(m)
|
|
387
|
+
klass_scope = klass.send(m, *args) # blocks only passed to final result set
|
|
388
|
+
return klass_scope unless klass_scope.is_a?(Parse::Query)
|
|
389
|
+
# merge constraints
|
|
390
|
+
add_constraints(klass_scope.constraints)
|
|
391
|
+
# if a block was passed, execute the query, otherwise return the query
|
|
392
|
+
return chained_block.present? ? results(&chained_block) : self
|
|
393
|
+
end
|
|
394
|
+
results.send(m, *args, &chained_block)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
Parse::Query.apply_auto_introspection!(query)
|
|
398
|
+
|
|
399
|
+
return query if block.nil?
|
|
400
|
+
query.results(&block)
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# Define a one-to-many or many-to-many association between the local model and a foreign class.
|
|
405
|
+
def has_many(key, scope = nil, **opts)
|
|
406
|
+
opts[:through] ||= :query
|
|
407
|
+
|
|
408
|
+
if opts[:through] == :query
|
|
409
|
+
return has_many_queried(key, scope, **opts)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# below this is the same
|
|
413
|
+
opts.reverse_merge!({
|
|
414
|
+
field: key.to_s.camelize(:lower),
|
|
415
|
+
required: false,
|
|
416
|
+
as: key,
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
klassName = opts[:as].to_parse_class singularize: true
|
|
420
|
+
parse_field = opts[:field].to_sym # name of the column (local or remote)
|
|
421
|
+
access_type = opts[:through].to_sym
|
|
422
|
+
|
|
423
|
+
ivar = :"@#{key}"
|
|
424
|
+
will_change_method = :"#{key}_will_change!"
|
|
425
|
+
set_attribute_method = :"#{key}_set_attribute!"
|
|
426
|
+
|
|
427
|
+
# verify that the user did not duplicate properties or defined different properties with the same name
|
|
428
|
+
if self.fields[key].present? && Parse::Properties::BASE_FIELD_MAP[key].nil?
|
|
429
|
+
warn "Has_many property #{self}##{key} already defined with type #{klassName}"
|
|
430
|
+
return false
|
|
431
|
+
end
|
|
432
|
+
if self.fields[parse_field].present?
|
|
433
|
+
warn "Alias has_many #{self}##{parse_field} conflicts with previously defined property."
|
|
434
|
+
return false
|
|
435
|
+
end
|
|
436
|
+
# validations
|
|
437
|
+
validates_presence_of(key) if opts[:required]
|
|
438
|
+
|
|
439
|
+
# default proxy class.
|
|
440
|
+
proxyKlass = Parse::PointerCollectionProxy
|
|
441
|
+
|
|
442
|
+
#if this is a relation type, use this proxy instead. Relations are stored
|
|
443
|
+
# in the relations hash. If a PointerCollectionProxy is used, we store those
|
|
444
|
+
# as we would normal properties.
|
|
445
|
+
if access_type == :relation
|
|
446
|
+
proxyKlass = Parse::RelationCollectionProxy
|
|
447
|
+
self.relations[key] = klassName
|
|
448
|
+
else
|
|
449
|
+
self.attributes.merge!(parse_field => :array)
|
|
450
|
+
# Add them to the list of fields in our class model
|
|
451
|
+
self.fields.merge!(key => :array, parse_field => :array)
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
self.field_map.merge!(key => parse_field)
|
|
455
|
+
# dirty tracking
|
|
456
|
+
define_attribute_methods key
|
|
457
|
+
|
|
458
|
+
# The first method to be defined is a getter.
|
|
459
|
+
define_method(key) do
|
|
460
|
+
val = instance_variable_get(ivar)
|
|
461
|
+
# if the value for this is nil and we are a pointer, or if this is a
|
|
462
|
+
# selectively fetched object and this field wasn't included, then autofetch
|
|
463
|
+
should_autofetch = val.nil? && (pointer? || (has_selective_keys? && !field_was_fetched?(key)))
|
|
464
|
+
if should_autofetch
|
|
465
|
+
# If autofetch is disabled and we're accessing an unfetched field on a
|
|
466
|
+
# selectively fetched object, raise an error to make the issue explicit
|
|
467
|
+
if autofetch_disabled? && has_selective_keys? && !field_was_fetched?(key)
|
|
468
|
+
raise Parse::UnfetchedFieldAccessError.new(key, self.class.name)
|
|
469
|
+
end
|
|
470
|
+
autofetch!(key)
|
|
471
|
+
val = instance_variable_get ivar
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# if the result is not a collection proxy, then create a new one.
|
|
475
|
+
unless val.is_a?(Parse::PointerCollectionProxy)
|
|
476
|
+
results = []
|
|
477
|
+
#results = val.parse_objects if val.respond_to?(:parse_objects)
|
|
478
|
+
val = proxyKlass.new results, delegate: self, key: key
|
|
479
|
+
instance_variable_set(ivar, val)
|
|
480
|
+
end
|
|
481
|
+
val
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# proxy setter that forwards with dirty tracking
|
|
485
|
+
define_method("#{key}=") do |val|
|
|
486
|
+
send set_attribute_method, val, true
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
# This will set the content of the proxy.
|
|
490
|
+
define_method(set_attribute_method) do |val, track = true|
|
|
491
|
+
# If it is a hash, with a __type of Relation, createa a new RelationCollectionProxy, regardless
|
|
492
|
+
# of what is defined because we must have gotten this from Parse.
|
|
493
|
+
|
|
494
|
+
# if val is nil or it is the delete operation, then set to empty array.
|
|
495
|
+
# this will create a new proxyKlass later on
|
|
496
|
+
if val.nil? || val == Parse::Properties::DELETE_OP
|
|
497
|
+
val = []
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
# Always trust the declared klassName — never the className the
|
|
501
|
+
# server (or attacker-controlled mass assignment) supplied.
|
|
502
|
+
# Prevents type-confusion attacks that would mark this relation
|
|
503
|
+
# proxy as a different parse_class than the model declared.
|
|
504
|
+
if val.is_a?(Hash) && val[Parse::Model::KEY_CLASS_NAME] &&
|
|
505
|
+
val[Parse::Model::KEY_CLASS_NAME] != klassName &&
|
|
506
|
+
%w[Relation].include?(val["__type"])
|
|
507
|
+
warn "[#{self.class}] has_many :#{key} expected className=#{klassName.inspect}, ignoring incoming className=#{val[Parse::Model::KEY_CLASS_NAME].inspect}"
|
|
508
|
+
end
|
|
509
|
+
if val.is_a?(Hash) && val["__type"] == "Relation"
|
|
510
|
+
relation_objects = val["objects"] || []
|
|
511
|
+
val = Parse::RelationCollectionProxy.new relation_objects, delegate: self, key: key, parse_class: klassName
|
|
512
|
+
elsif val.is_a?(Hash) && val["__op"] == "AddRelation" && val["objects"].present?
|
|
513
|
+
_collection = proxyKlass.new [], delegate: self, key: key, parse_class: klassName
|
|
514
|
+
_collection.loaded = true
|
|
515
|
+
_collection.add val["objects"].parse_objects(klassName)
|
|
516
|
+
val = _collection
|
|
517
|
+
elsif val.is_a?(Hash) && val["__op"] == "RemoveRelation" && val["objects"].present?
|
|
518
|
+
_collection = proxyKlass.new [], delegate: self, key: key, parse_class: klassName
|
|
519
|
+
_collection.loaded = true
|
|
520
|
+
_collection.remove val["objects"].parse_objects(klassName)
|
|
521
|
+
val = _collection
|
|
522
|
+
elsif val.is_a?(Array)
|
|
523
|
+
# Otherwise create a new collection based on what the user
|
|
524
|
+
# defined; always coerce array elements to the declared class.
|
|
525
|
+
val = proxyKlass.new val.parse_objects(klassName), delegate: self, key: key, parse_class: klassName
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
# send dirty tracking if set
|
|
529
|
+
if track == true
|
|
530
|
+
prepare_for_dirty_tracking!(key)
|
|
531
|
+
send will_change_method unless val == instance_variable_get(ivar)
|
|
532
|
+
end
|
|
533
|
+
# TODO: Only allow empty proxy collection class as a value or nil.
|
|
534
|
+
if val.is_a?(Parse::CollectionProxy)
|
|
535
|
+
instance_variable_set(ivar, val)
|
|
536
|
+
else
|
|
537
|
+
warn "[#{self.class}] Invalid value #{val} for :has_many field #{key}. Should be an Array or a CollectionProxy"
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
data_type = opts[:through]
|
|
542
|
+
# if the type is a relation association, add these methods to the delegate
|
|
543
|
+
# that will be used when creating the collection proxies. See Collection proxies
|
|
544
|
+
# for more information.
|
|
545
|
+
if data_type == :relation
|
|
546
|
+
# return a query given the foreign table class name.
|
|
547
|
+
define_method("#{key}_relation_query") do
|
|
548
|
+
Parse::Query.new(klassName, key.to_sym.related_to => self.pointer, limit: :max)
|
|
549
|
+
end
|
|
550
|
+
# fetch the contents of the relation
|
|
551
|
+
define_method("#{key}_fetch!") do
|
|
552
|
+
q = self.send :"#{key}_relation_query"
|
|
553
|
+
q.results || []
|
|
554
|
+
end
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
# if the remote field name and the local field name are the same
|
|
558
|
+
# don't create alias methods
|
|
559
|
+
return if parse_field.to_sym == key.to_sym
|
|
560
|
+
|
|
561
|
+
if self.method_defined?(parse_field) == false
|
|
562
|
+
alias_method parse_field, key
|
|
563
|
+
alias_method "#{parse_field}=", "#{key}="
|
|
564
|
+
alias_method "#{parse_field}_set_attribute!", set_attribute_method
|
|
565
|
+
elsif parse_field.to_sym != :objectId
|
|
566
|
+
warn "Alias has_many method #{self}##{parse_field} already defined."
|
|
567
|
+
end
|
|
568
|
+
end # has_many_array
|
|
569
|
+
end #ClassMethods
|
|
570
|
+
|
|
571
|
+
# A hash list of all has_many associations that use a Parse Relation.
|
|
572
|
+
# @return [Hash]
|
|
573
|
+
# @see Associations::HasMany.relations
|
|
574
|
+
def relations
|
|
575
|
+
self.class.relations
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
# A hash of all the relation changes that have been performed on this
|
|
579
|
+
# instance. This is only used when the association uses Parse Relations.
|
|
580
|
+
# @return [Hash]
|
|
581
|
+
def relation_updates
|
|
582
|
+
h = {}
|
|
583
|
+
changed.each do |key|
|
|
584
|
+
next unless relations[key.to_sym].present? && send(key).changed?
|
|
585
|
+
remote_field = self.field_map[key.to_sym] || key
|
|
586
|
+
h[remote_field] = send key # we still need to send a proxy collection
|
|
587
|
+
end
|
|
588
|
+
h
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
# @return [Boolean] true if there are pending relational changes for
|
|
592
|
+
def relation_changes?
|
|
593
|
+
changed.any? { |key| relations[key.to_sym] }
|
|
594
|
+
end
|
|
595
|
+
end # HasMany
|
|
596
|
+
end #Associations
|
|
597
|
+
end # Parse
|