Skip to content

fix: Remove unused option sessionToken from Parse.Query.aggregate #2547

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

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions src/ParseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,29 +780,17 @@ class ParseQuery<T extends ParseObject = ParseObject> {
* Executes a distinct query and returns unique values
*
* @param {string} key A field to find distinct values
* @param {object} options
* @param {string} [options.sessionToken] A valid session token, used for making a request on behalf of a specific user.
* @returns {Promise} A promise that is resolved with the query completes.
*/
distinct<K extends keyof T['attributes'], V = T['attributes'][K]>(
key: K,
options?: { sessionToken?: string }
): Promise<V[]> {
options = options || {};
const distinctOptions: { sessionToken?: string; useMasterKey: boolean } = {
useMasterKey: true,
};
if (Object.hasOwn(options, 'sessionToken')) {
distinctOptions.sessionToken = options.sessionToken;
}
distinct<K extends keyof T['attributes'], V = T['attributes'][K]>(key: K): Promise<V[]> {
const distinctOptions = { useMasterKey: true };
this._setRequestTask(distinctOptions);

const controller = CoreManager.getQueryController();
const params = {
distinct: key,
where: this._where,
hint: this._hint,
};
const controller = CoreManager.getQueryController();
return controller.aggregate(this.className, params, distinctOptions).then(results => {
return results.results!;
});
Expand All @@ -812,39 +800,28 @@ class ParseQuery<T extends ParseObject = ParseObject> {
* Executes an aggregate query and returns aggregate results
*
* @param {(Array|object)} pipeline Array or Object of stages to process query
* @param {object} options
* @param {string} [options.sessionToken] A valid session token, used for making a request on behalf of a specific user.
* @returns {Promise} A promise that is resolved with the query completes.
*/
aggregate(pipeline: any, options?: { sessionToken?: string }): Promise<any[]> {
options = options || {};
const aggregateOptions: { sessionToken?: string; useMasterKey: boolean } = {
useMasterKey: true,
};
if (Object.hasOwn(options, 'sessionToken')) {
aggregateOptions.sessionToken = options.sessionToken;
}
this._setRequestTask(aggregateOptions);

const controller = CoreManager.getQueryController();

aggregate(pipeline: any): Promise<any[]> {
if (!Array.isArray(pipeline) && typeof pipeline !== 'object') {
throw new Error('Invalid pipeline must be Array or Object');
}

if (Object.keys(this._where || {}).length) {
if (!Array.isArray(pipeline)) {
pipeline = [pipeline];
}
pipeline.unshift({ $match: this._where });
}

const params = {
pipeline,
hint: this._hint,
explain: this._explain,
readPreference: this._readPreference,
};
const aggregateOptions = { useMasterKey: true };
this._setRequestTask(aggregateOptions);

const controller = CoreManager.getQueryController();
return controller.aggregate(this.className, params, aggregateOptions).then(results => {
return results.results!;
});
Expand Down
22 changes: 5 additions & 17 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2619,7 +2619,6 @@ describe('ParseQuery', () => {
},
});
expect(options.useMasterKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.requestTask).toBeDefined();
return Promise.resolve({
results: ['L'],
Expand All @@ -2629,9 +2628,7 @@ describe('ParseQuery', () => {

const q = new ParseQuery('Item');
q.equalTo('size', 'small')
.distinct('size', {
sessionToken: '1234',
})
.distinct('size')
.then(results => {
expect(results[0]).toBe('L');
done();
Expand All @@ -2651,7 +2648,6 @@ describe('ParseQuery', () => {
hint: '_id_',
});
expect(options.useMasterKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.requestTask).toBeDefined();
return Promise.resolve({
results: ['L'],
Expand All @@ -2662,9 +2658,7 @@ describe('ParseQuery', () => {
const q = new ParseQuery('Item');
q.equalTo('size', 'small')
.hint('_id_')
.distinct('size', {
sessionToken: '1234',
})
.distinct('size')
.then(results => {
expect(results[0]).toBe('L');
done();
Expand Down Expand Up @@ -2799,17 +2793,14 @@ describe('ParseQuery', () => {
expect(className).toBe('Item');
expect(params.pipeline).toEqual([{ group: { objectId: '$name' } }]);
expect(options.useMasterKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
return Promise.resolve({
results: [],
});
},
});

const q = new ParseQuery('Item');
q.aggregate(pipeline, {
sessionToken: '1234',
}).then(results => {
q.aggregate(pipeline).then(results => {
expect(results).toEqual([]);
done();
});
Expand All @@ -2831,7 +2822,7 @@ describe('ParseQuery', () => {
// Query
const q = new ParseQuery('Item');
q.readPreference('SECONDARY');
const results = await q.aggregate([], { sessionToken: '1234' });
const results = await q.aggregate([]);
// Validate
expect(results).toEqual([]);
});
Expand All @@ -2845,7 +2836,6 @@ describe('ParseQuery', () => {
expect(params.pipeline).toEqual([{ group: { objectId: '$name' } }]);
expect(params.hint).toEqual('_id_');
expect(options.useMasterKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
return Promise.resolve({
results: [],
});
Expand All @@ -2854,9 +2844,7 @@ describe('ParseQuery', () => {

const q = new ParseQuery('Item');
q.hint('_id_')
.aggregate(pipeline, {
sessionToken: '1234',
})
.aggregate(pipeline)
.then(results => {
expect(results).toEqual([]);
done();
Expand Down
12 changes: 2 additions & 10 deletions types/ParseQuery.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,16 @@ declare class ParseQuery<T extends ParseObject = ParseObject> {
* Executes a distinct query and returns unique values
*
* @param {string} key A field to find distinct values
* @param {object} options
* @param {string} [options.sessionToken] A valid session token, used for making a request on behalf of a specific user.
* @returns {Promise} A promise that is resolved with the query completes.
*/
distinct<K extends keyof T['attributes'], V = T['attributes'][K]>(key: K, options?: {
sessionToken?: string;
}): Promise<V[]>;
distinct<K extends keyof T['attributes'], V = T['attributes'][K]>(key: K): Promise<V[]>;
/**
* Executes an aggregate query and returns aggregate results
*
* @param {(Array|object)} pipeline Array or Object of stages to process query
* @param {object} options
* @param {string} [options.sessionToken] A valid session token, used for making a request on behalf of a specific user.
* @returns {Promise} A promise that is resolved with the query completes.
*/
aggregate(pipeline: any, options?: {
sessionToken?: string;
}): Promise<any[]>;
aggregate(pipeline: any): Promise<any[]>;
/**
* Retrieves at most one Parse.Object that satisfies this query.
*
Expand Down
Loading