2
votes

I am new to parse sdk but here is the situation which I've been struggling with. I'm using android client and some CloudCode function.

I have 2 classes Joins and Groups.

Joins( stores every join requests )

userId - Pointer to _User class
numberOfPeople
groupId - Pointer to Groups

Groups( identifies a group )

placeId - Pointer to Place class
dateOfVisit

The group is identified by the placeId and dateOfVisit(these would construct the primary key).

For example:

placeId dateOfVisit
111111111 2015-06-13 16:00
111111111 2015-06-14 16:00

When a user wants to join with placeId:1111111111 and dateOfVisit:2015-06-14 16:00 I want to check if there is an existing group or I need to create a new group and assign this groupId to this join request.

I have read the parse documentation and it is not possible to make unique rows in Groups because the lack of transaction/system locks, however there are some workarounds for it.

Solution1: beforeSave + afterSave

would be to create a beforeSave and an aftersave hook JS method on Groups.
BeforeSave would check
- if there is already an existing group (with the placeId and dateOfVisit in the request) then do nothing
- else save/create a new group.
AfterSave would
do a query.first equalTo( placeId and dateOfVisit in the request) with ascending order on the createdAt column and return this groupId and save this for the join request.

Solution2: afterSave + background job

I don't care at all the duplicate entries
AfterSave would do a query.first equalTo( placeId and dateOfVisit in the request) with ascending order on the createdAt column and return this groupId and save this for the join request. Background job would periodically delete all the duplicate entries except the min.createdAt entry.

Does these sound an overkill? Any suggestions appreciated! Thank you.

1

1 Answers

0
votes

You don't need to make beforeSave and afterSave at the same time, just beforeSave is enough.

In your Cloud Code, define a beforeSave function which:

  • use equalTo("placeId", placeId) to Groups class to check if there is existing group.
  • if the result returns null, you can create a new Group and assign the userId sent along with original request. Otherwise, associate the userId with the returned object.

Check more information about the Cloud Code here and you might need to know about Promises too