Skip to content

Commit a970913

Browse files
authored
feat: Add comment to MongoDB query via Parse.Query.comment (#2088)
1 parent 920e1ba commit a970913

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

integration/test/ParseQueryTest.js

+15
Original file line numberDiff line numberDiff line change
@@ -2401,4 +2401,19 @@ describe('Parse Query', () => {
24012401
expect(results[1].get('arrayField')).toEqual([{ subfield: 1 }]);
24022402
expect(results[2].get('arrayField')).toEqual(null);
24032403
});
2404+
2405+
it('can query and send a comment', async () => {
2406+
const obj1 = new TestObject({ number: 1 });
2407+
const obj2 = new TestObject({ number: 2 });
2408+
const obj3 = new TestObject({ number: 3 });
2409+
await Parse.Object.saveAll([obj1, obj2, obj3]);
2410+
2411+
const query = new Parse.Query(TestObject);
2412+
const comment = 'Hello Parse';
2413+
query.comment(comment);
2414+
query.explain();
2415+
const explain = await query.find();
2416+
assert.equal(explain.command.comment, comment);
2417+
});
2418+
24042419
});

src/ParseQuery.js

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type QueryJSON = {
3636
readPreference?: string,
3737
includeReadPreference?: string,
3838
subqueryReadPreference?: string,
39+
comment?: string,
3940
};
4041

4142
/**
@@ -242,6 +243,7 @@ class ParseQuery {
242243
_hint: mixed;
243244
_explain: boolean;
244245
_xhrRequest: any;
246+
_comment: string;
245247

246248
/**
247249
* @param {(string | Parse.Object)} objectClass An instance of a subclass of Parse.Object, or a Parse className string.
@@ -283,6 +285,7 @@ class ParseQuery {
283285
task: null,
284286
onchange: () => {},
285287
};
288+
this._comment = null;
286289
}
287290

288291
/**
@@ -468,6 +471,9 @@ class ParseQuery {
468471
if (this._explain) {
469472
params.explain = true;
470473
}
474+
if(this._comment) {
475+
params.comment = this._comment;
476+
}
471477
for (const key in this._extraOptions) {
472478
params[key] = this._extraOptions[key];
473479
}
@@ -553,6 +559,10 @@ class ParseQuery {
553559
this._explain = !!json.explain;
554560
}
555561

562+
if(json.comment) {
563+
this._comment = json.comment;
564+
}
565+
556566
for (const key in json) {
557567
if (json.hasOwnProperty(key)) {
558568
if (
@@ -569,6 +579,7 @@ class ParseQuery {
569579
'subqueryReadPreference',
570580
'hint',
571581
'explain',
582+
'comment',
572583
].indexOf(key) === -1
573584
) {
574585
this._extraOptions[key] = json[key];
@@ -2111,6 +2122,25 @@ class ParseQuery {
21112122
this._xhrRequest.onchange();
21122123
};
21132124
}
2125+
2126+
/**
2127+
* Sets a comment to the query so that the query
2128+
*can be identified when using a the profiler for MongoDB.
2129+
*
2130+
* @param {string} value a comment can make your profile data easier to interpret and trace.
2131+
* @returns {Parse.Query} Returns the query, so you can chain this call.
2132+
*/
2133+
comment(value: string): ParseQuery {
2134+
if (value == null) {
2135+
delete this._comment;
2136+
return this;
2137+
}
2138+
if (typeof value !== 'string') {
2139+
throw new Error('The value of a comment to be sent with this query must be a string.');
2140+
}
2141+
this._comment = value;
2142+
return this;
2143+
}
21142144
}
21152145

21162146
const DefaultController = {

src/__tests__/ParseQuery-test.js

+48
Original file line numberDiff line numberDiff line change
@@ -3824,4 +3824,52 @@ describe('ParseQuery LocalDatastore', () => {
38243824
expect(subscription.sessionToken).toBe('r:test');
38253825
expect(subscription.query).toEqual(query);
38263826
});
3827+
3828+
it('can add comment to query', () => {
3829+
const query = new ParseQuery('TestObject');
3830+
const comment = 'Hello Parse';
3831+
query.comment(comment);
3832+
expect(query.toJSON()).toEqual({
3833+
where: {},
3834+
comment: comment,
3835+
});
3836+
});
3837+
3838+
it('can add comment to query from json', () => {
3839+
const query = new ParseQuery('Item');
3840+
const comment = 'Hello Parse';
3841+
query.comment(comment);
3842+
const json = query.toJSON();
3843+
expect(json).toEqual({
3844+
where: {},
3845+
comment: comment,
3846+
});
3847+
const query2 = new ParseQuery('Item');
3848+
query2.withJSON(json);
3849+
expect(query2._comment).toEqual(comment);
3850+
});
3851+
3852+
it('comment can only be string', () => {
3853+
const obj1 = {
3854+
className: 'Item',
3855+
objectId: 'objectId1',
3856+
password: 123,
3857+
number: 3,
3858+
string: 'a',
3859+
};
3860+
const query = new ParseQuery('TestObject');
3861+
expect(query.comment.bind(query, obj1)).toThrow(
3862+
'The value of a comment to be sent with this query must be a string.'
3863+
);
3864+
});
3865+
3866+
it('clear comment when no value passed', () => {
3867+
const query = new ParseQuery('Item');
3868+
const comment = 'Hello Parse';
3869+
query.comment(comment);
3870+
expect(query._comment).toBe(comment);
3871+
query.comment();
3872+
expect(query._comment).toBeUndefined();
3873+
});
3874+
38273875
});

types/ParseQuery.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type QueryJSON = {
1818
readPreference?: string;
1919
includeReadPreference?: string;
2020
subqueryReadPreference?: string;
21+
comment?: string,
2122
};
2223
export default ParseQuery;
2324
/**
@@ -140,6 +141,7 @@ declare class ParseQuery {
140141
_hint: mixed;
141142
_explain: boolean;
142143
_xhrRequest: any;
144+
_comment: string;
143145
/**
144146
* Adds constraint that at least one of the passed in queries matches.
145147
*
@@ -915,6 +917,14 @@ declare class ParseQuery {
915917
*/
916918
cancel(): ParseQuery;
917919
_setRequestTask(options: any): void;
920+
/**
921+
* Sets a comment to the query so that the query
922+
* can be identified when using a the profiler for MongoDB.
923+
*
924+
* @param {string} value a comment can make your profile data easier to interpret and trace.
925+
* @returns {Parse.Query} Returns the query, so you can chain this call.
926+
*/
927+
comment(value: string): ParseQuery;
918928
}
919929
import { FullOptions } from './RESTController';
920930
import ParseObject from './ParseObject';

0 commit comments

Comments
 (0)