Skip to content

Instantly share code, notes, and snippets.

@inDream
Created May 28, 2021 09:45
Show Gist options
  • Save inDream/9adaa6c5a80db98689d6e644c6c96e09 to your computer and use it in GitHub Desktop.
Save inDream/9adaa6c5a80db98689d6e644c6c96e09 to your computer and use it in GitHub Desktop.
parse-server Upsert Patch
diff --git a/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js b/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js
index 79bb747..a85e5ad 100644
--- a/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js
+++ b/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js
@@ -488,6 +488,9 @@ class MongoStorageAdapter {
upsertOneObject(className, schema, query, update, transactionalSession) {
schema = convertParseSchemaToMongoSchema(schema);
const mongoUpdate = (0, _MongoTransform.transformUpdate)(className, update, schema);
+ mongoUpdate.$setOnInsert = {};
+ mongoUpdate.$setOnInsert._created_at = mongoUpdate.$set._created_at;
+ delete mongoUpdate.$set._created_at;
const mongoWhere = (0, _MongoTransform.transformWhere)(className, query, schema);
return this._adaptiveCollection(className).then(collection => collection.upsertOne(mongoWhere, mongoUpdate, transactionalSession)).catch(err => this.handleError(err));
} // Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
diff --git a/node_modules/parse-server/lib/RestWrite.js b/node_modules/parse-server/lib/RestWrite.js
index 74e60a5..e23901f 100644
--- a/node_modules/parse-server/lib/RestWrite.js
+++ b/node_modules/parse-server/lib/RestWrite.js
@@ -1266,7 +1266,9 @@ RestWrite.prototype.runDatabaseOperation = function () {
} // Run a create
- return this.config.database.create(this.className, this.data, this.runOptions, false, this.validSchemaController).catch(error => {
+ // return this.config.database.create(this.className, this.data, this.runOptions, false, this.validSchemaController)
+ this.runOptions.upsert = true;
+ return this.config.database.update(this.className, { objectId: this.data.objectId }, this.data, this.runOptions, false, false, this.validSchemaController).catch(error => {
if (this.className !== '_User' || error.code !== Parse.Error.DUPLICATE_VALUE) {
throw error;
} // Quick check, if we were able to infer the duplicated field name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment