From 2170962a50fa353ed85eda3f11dce7ee3647b087 Mon Sep 17 00:00:00 2001 From: Oussama Meglali <56823121+Meglali20@users.noreply.github.com> Date: Sun, 3 Mar 2024 02:27:57 +0100 Subject: [PATCH] feat: Add support for MongoDB query comment (#8928) --- spec/ParseQuery.Comment.spec.js | 99 +++++++++++++++++++ src/Adapters/Storage/Mongo/MongoCollection.js | 36 ++++++- .../Storage/Mongo/MongoStorageAdapter.js | 22 ++++- src/Adapters/Storage/StorageAdapter.js | 7 +- src/Controllers/DatabaseController.js | 8 +- src/RestQuery.js | 1 + src/Routers/AggregateRouter.js | 4 + src/Routers/ClassesRouter.js | 4 + src/triggers.js | 4 + 9 files changed, 173 insertions(+), 12 deletions(-) create mode 100644 spec/ParseQuery.Comment.spec.js diff --git a/spec/ParseQuery.Comment.spec.js b/spec/ParseQuery.Comment.spec.js new file mode 100644 index 0000000000..c61c8c09c4 --- /dev/null +++ b/spec/ParseQuery.Comment.spec.js @@ -0,0 +1,99 @@ +'use strict'; + +const Config = require('../lib/Config'); +const TestUtils = require('../lib/TestUtils'); +const { MongoClient } = require('mongodb'); +const databaseURI = 'mongodb://localhost:27017/'; +const request = require('../lib/request'); + +let config, client, database; + +const masterKeyHeaders = { + 'X-Parse-Application-Id': 'test', + 'X-Parse-Rest-API-Key': 'rest', + 'X-Parse-Master-Key': 'test', + 'Content-Type': 'application/json', +}; + +const masterKeyOptions = { + headers: masterKeyHeaders, + json: true, +}; + +describe_only_db('mongo')('Parse.Query with comment testing', () => { + beforeEach(async () => { + config = Config.get('test'); + client = await MongoClient.connect(databaseURI, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); + database = client.db('parseServerMongoAdapterTestDatabase'); + const level = 2; + const profiling = await database.command({ profile: level }); + console.log(`profiling ${JSON.stringify(profiling)}`); + }); + + afterEach(async () => { + await client.close(); + await TestUtils.destroyAllDataPermanently(false); + }); + + it('send comment with query through REST', async () => { + const comment = 'Hello Parse'; + const object = new TestObject(); + object.set('name', 'object'); + await object.save(); + const options = Object.assign({}, masterKeyOptions, { + url: Parse.serverURL + '/classes/TestObject', + qs: { + explain: true, + comment: comment, + }, + }); + await request(options); + const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } }); + expect(result.command.explain.comment).toBe(comment); + }); + + it('send comment with query', async () => { + const comment = 'Hello Parse'; + const object = new TestObject(); + object.set('name', 'object'); + await object.save(); + const collection = await config.database.adapter._adaptiveCollection('TestObject'); + await collection._rawFind({ name: 'object' }, { comment: comment }); + const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } }); + expect(result.command.comment).toBe(comment); + }); + + it('send a comment with a count query', async () => { + const comment = 'Hello Parse'; + const object = new TestObject(); + object.set('name', 'object'); + await object.save(); + + const object2 = new TestObject(); + object2.set('name', 'object'); + await object2.save(); + + const collection = await config.database.adapter._adaptiveCollection('TestObject'); + const countResult = await collection.count({ name: 'object' }, { comment: comment }); + expect(countResult).toEqual(2); + const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } }); + expect(result.command.comment).toBe(comment); + }); + + it('attach a comment to an aggregation', async () => { + const comment = 'Hello Parse'; + const object = new TestObject(); + object.set('name', 'object'); + await object.save(); + const collection = await config.database.adapter._adaptiveCollection('TestObject'); + await collection.aggregate([{ $group: { _id: '$name' } }], { + explain: true, + comment: comment, + }); + const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } }); + expect(result.command.explain.comment).toBe(comment); + }); +}); diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 48de677c2e..4f02c5c8fa 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -15,7 +15,18 @@ export default class MongoCollection { // idea. Or even if this behavior is a good idea. find( query, - { skip, limit, sort, keys, maxTimeMS, readPreference, hint, caseInsensitive, explain } = {} + { + skip, + limit, + sort, + keys, + maxTimeMS, + readPreference, + hint, + caseInsensitive, + explain, + comment, + } = {} ) { // Support for Full Text Search - $text if (keys && keys.$score) { @@ -32,6 +43,7 @@ export default class MongoCollection { hint, caseInsensitive, explain, + comment, }).catch(error => { // Check for "no geoindex" error if (error.code != 17007 && !error.message.match(/unable to find index for .geoNear/)) { @@ -60,6 +72,7 @@ export default class MongoCollection { hint, caseInsensitive, explain, + comment, }) ) ); @@ -75,7 +88,18 @@ export default class MongoCollection { _rawFind( query, - { skip, limit, sort, keys, maxTimeMS, readPreference, hint, caseInsensitive, explain } = {} + { + skip, + limit, + sort, + keys, + maxTimeMS, + readPreference, + hint, + caseInsensitive, + explain, + comment, + } = {} ) { let findOperation = this._mongoCollection.find(query, { skip, @@ -83,6 +107,7 @@ export default class MongoCollection { sort, readPreference, hint, + comment, }); if (keys) { @@ -100,7 +125,7 @@ export default class MongoCollection { return explain ? findOperation.explain(explain) : findOperation.toArray(); } - count(query, { skip, limit, sort, maxTimeMS, readPreference, hint } = {}) { + count(query, { skip, limit, sort, maxTimeMS, readPreference, hint, comment } = {}) { // If query is empty, then use estimatedDocumentCount instead. // This is due to countDocuments performing a scan, // which greatly increases execution time when being run on large collections. @@ -118,6 +143,7 @@ export default class MongoCollection { maxTimeMS, readPreference, hint, + comment, }); return countOperation; @@ -127,9 +153,9 @@ export default class MongoCollection { return this._mongoCollection.distinct(field, query); } - aggregate(pipeline, { maxTimeMS, readPreference, hint, explain } = {}) { + aggregate(pipeline, { maxTimeMS, readPreference, hint, explain, comment } = {}) { return this._mongoCollection - .aggregate(pipeline, { maxTimeMS, readPreference, hint, explain }) + .aggregate(pipeline, { maxTimeMS, readPreference, hint, explain, comment }) .toArray(); } diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index 53d1bc4959..e9d49b2ecf 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -603,7 +603,17 @@ export class MongoStorageAdapter implements StorageAdapter { className: string, schema: SchemaType, query: QueryType, - { skip, limit, sort, keys, readPreference, hint, caseInsensitive, explain }: QueryOptions + { + skip, + limit, + sort, + keys, + readPreference, + hint, + caseInsensitive, + explain, + comment, + }: QueryOptions ): Promise { validateExplainValue(explain); schema = convertParseSchemaToMongoSchema(schema); @@ -646,6 +656,7 @@ export class MongoStorageAdapter implements StorageAdapter { hint, caseInsensitive, explain, + comment, }) ) .then(objects => { @@ -735,7 +746,9 @@ export class MongoStorageAdapter implements StorageAdapter { schema: SchemaType, query: QueryType, readPreference: ?string, - hint: ?mixed + _estimate: ?boolean, + hint: ?mixed, + comment: ?string ) { schema = convertParseSchemaToMongoSchema(schema); readPreference = this._parseReadPreference(readPreference); @@ -745,6 +758,7 @@ export class MongoStorageAdapter implements StorageAdapter { maxTimeMS: this._maxTimeMS, readPreference, hint, + comment, }) ) .catch(err => this.handleError(err)); @@ -777,7 +791,8 @@ export class MongoStorageAdapter implements StorageAdapter { pipeline: any, readPreference: ?string, hint: ?mixed, - explain?: boolean + explain?: boolean, + comment: ?string ) { validateExplainValue(explain); let isPointerField = false; @@ -811,6 +826,7 @@ export class MongoStorageAdapter implements StorageAdapter { maxTimeMS: this._maxTimeMS, hint, explain, + comment, }) ) .then(results => { diff --git a/src/Adapters/Storage/StorageAdapter.js b/src/Adapters/Storage/StorageAdapter.js index 7605784a43..d25c9753c0 100644 --- a/src/Adapters/Storage/StorageAdapter.js +++ b/src/Adapters/Storage/StorageAdapter.js @@ -19,6 +19,7 @@ export type QueryOptions = { caseInsensitive?: boolean, action?: string, addsField?: boolean, + comment?: string, }; export type UpdateQueryOptions = { @@ -97,7 +98,8 @@ export interface StorageAdapter { query: QueryType, readPreference?: string, estimate?: boolean, - hint?: mixed + hint?: mixed, + comment?: string ): Promise; distinct( className: string, @@ -111,7 +113,8 @@ export interface StorageAdapter { pipeline: any, readPreference: ?string, hint: ?mixed, - explain?: boolean + explain?: boolean, + comment?: string ): Promise; performInitialization(options: ?any): Promise; watch(callback: () => void): void; diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 5975e94053..c5c3d1ab05 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -1188,6 +1188,7 @@ class DatabaseController { hint, caseInsensitive = false, explain, + comment, }: any = {}, auth: any = {}, validSchemaController: SchemaController.SchemaController @@ -1237,6 +1238,7 @@ class DatabaseController { hint, caseInsensitive: this.options.enableCollationCaseComparison ? false : caseInsensitive, explain, + comment, }; Object.keys(sort).forEach(fieldName => { if (fieldName.match(/^authData\.([a-zA-Z0-9_]+)\.id$/)) { @@ -1306,7 +1308,8 @@ class DatabaseController { query, readPreference, undefined, - hint + hint, + comment ); } } else if (distinct) { @@ -1325,7 +1328,8 @@ class DatabaseController { pipeline, readPreference, hint, - explain + explain, + comment ); } } else if (explain) { diff --git a/src/RestQuery.js b/src/RestQuery.js index 5af678bb96..57fc435526 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -212,6 +212,7 @@ function _UnsafeRestQuery( case 'skip': case 'limit': case 'readPreference': + case 'comment': this.findOptions[option] = restOptions[option]; break; case 'order': diff --git a/src/Routers/AggregateRouter.js b/src/Routers/AggregateRouter.js index c69c9c40ca..f8bca7fd65 100644 --- a/src/Routers/AggregateRouter.js +++ b/src/Routers/AggregateRouter.js @@ -19,6 +19,10 @@ export class AggregateRouter extends ClassesRouter { options.explain = body.explain; delete body.explain; } + if (body.comment) { + options.comment = body.comment; + delete body.comment; + } if (body.readPreference) { options.readPreference = body.readPreference; delete body.readPreference; diff --git a/src/Routers/ClassesRouter.js b/src/Routers/ClassesRouter.js index 5dfba97ee0..0a68e25254 100644 --- a/src/Routers/ClassesRouter.js +++ b/src/Routers/ClassesRouter.js @@ -166,6 +166,7 @@ export class ClassesRouter extends PromiseRouter { 'subqueryReadPreference', 'hint', 'explain', + 'comment', ]; for (const key of Object.keys(body)) { @@ -215,6 +216,9 @@ export class ClassesRouter extends PromiseRouter { if (body.explain) { options.explain = body.explain; } + if (body.comment && typeof body.comment === 'string') { + options.comment = body.comment; + } return options; } diff --git a/src/triggers.js b/src/triggers.js index 5c4755af54..bfebde2e83 100644 --- a/src/triggers.js +++ b/src/triggers.js @@ -576,6 +576,10 @@ export function maybeRunQueryTrigger( restOptions = restOptions || {}; restOptions.hint = jsonQuery.hint; } + if (jsonQuery.comment) { + restOptions = restOptions || {}; + restOptions.comment = jsonQuery.comment; + } if (requestObject.readPreference) { restOptions = restOptions || {}; restOptions.readPreference = requestObject.readPreference;