Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow saving with custom objectId #1097

Closed
JeromeDeLeon opened this issue Feb 25, 2020 · 4 comments · Fixed by #1309
Closed

Allow saving with custom objectId #1097

JeromeDeLeon opened this issue Feb 25, 2020 · 4 comments · Fixed by #1309
Labels
type:feature New feature or improvement of existing feature

Comments

@JeromeDeLeon
Copy link
Contributor

Is your feature request related to a problem? Please describe.
This feature was already merged on Parse Server but I just tried it with this SDK and got the result of Object not found and upon looking up, ParseObject#L313 was the cause of it because if it detects an objectId being present, it will simply update the object hence the PUT method. Correct me if I'm wrong.

Describe the solution you'd like
Ability to set objectId without making an update to the non-existent record.

Describe alternatives you've considered
For now, we could just use REST API to manually create an object.

Additional context
I thought of a solution that we could check if it is new or not but looking up to isNew method, it checks whether this.id is present or not and unfortunately, hitting save will automatically replace this.id the objectId we assigned to it.

@JeromeDeLeon
Copy link
Contributor Author

JeromeDeLeon commented Feb 25, 2020

At first, I thought of adding _existed property to ParseObject but seems like too cumbersome and not a good solution IMO, so I thought of another one. We could just replace _localId of the objectId that we set instead of uuid? but the problem is that how will I now when to replace id with _localId? I'm open for discussion of how will this be implemented on the SDK.

I can add enableCustomObjectId method and inside the default object controller:

save(target, options) {
  // ...
  const isCustomObjectId = CoreManager.get('CUSTOM_ID');

  if (target.isNew() && isCustomObjectId) {
    target.id = target._localId;
  }
  // ...
}

or just change the _getSaveParams():

_getSaveParams() {
  const method = this.id ? 'PUT' : 'POST';
  let path = 'classes/' + this.className;
  if (this.id) {
    path += '/' + this.id;
  } else if (this.className === '_User') {
    path = 'users';
  }
  
  if (this.isNew() && CoreManager.get('CUSTOM_ID')) {
    this.id = this._localId;
  }

  const body = this._getSaveJSON();
  return {
    method,
    body,
      path
  };
}

I'm not sure if _getSaveJSON will include the id property and if this passes on PServer, will it convert id to objectId property because RestWrite#L56 only supports objectId and not id?

@JeromeDeLeon
Copy link
Contributor Author

JeromeDeLeon commented Feb 25, 2020

Do we also need to support reverting id back to undefined in case it failed to save?

@dplewis
Copy link
Member

dplewis commented Feb 12, 2021

@JeromeDeLeon Thanks for your suggestions.

Have global config CoreManager.get('AllowCustomId') this will allow us to make sure that an objectId is required (or we could reuse _localId) before saving.

The next problem is with PUT vs POST. I think we can use the fact that a saved / existing object doesn't have a _localId.

@parse-community/core-maintainers Thoughts?

@davimacedo
Copy link
Member

It looks a good approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants