Skip to content

Commit

Permalink
Supporting patterns in classNames for Live Queries (#7131)
Browse files Browse the repository at this point in the history
* Parse LiveQuery Server. Supporting patterns in classNames.

* Parse LiveQuery Server. Supporting patterns in classNames. Small optimisation.

* Parse LiveQuery Server. Supporting patterns in classNames. Adding info to changelog.

* Parse LiveQuery Server. Supporting patterns in classNames. Test case.
  • Loading branch information
Nes-si authored and dplewis committed Feb 21, 2021
1 parent 16de5f4 commit 7638ff8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ___
- NEW: Added convenience method Parse.Cloud.sendEmail(...) to send email via email adapter in Cloud Code. [#7089](https://github.com/parse-community/parse-server/pull/7089). Thanks to [dblythy](https://github.com/dblythy)
- FIX: Winston Logger interpolating stdout to console [#7114](https://github.com/parse-community/parse-server/pull/7114). Thanks to [dplewis](https://github.com/dplewis)
- NEW: LiveQuery support for $and, $nor, $containedBy, $geoWithin, $geoIntersects queries [#7113](https://github.com/parse-community/parse-server/pull/7113). Thanks to [dplewis](https://github.com/dplewis)
- NEW: Supporting patterns in LiveQuery server's config parameter `classNames` [#7131](https://github.com/parse-community/parse-server/pull/7131). Thanks to [Nes-si](https://github.com/Nes-si)

### 4.5.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)
Expand Down
23 changes: 23 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ describe('ParseLiveQuery', function () {
await object.save();
});

it('can use patterns in className', async done => {
await reconfigureServer({
liveQuery: {
classNames: ['Test.*'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
const object = new TestObject();
await object.save();

const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe();
subscription.on('update', object => {
expect(object.get('foo')).toBe('bar');
done();
});
object.set({ foo: 'bar' });
await object.save();
});

it('expect afterEvent create', async done => {
await reconfigureServer({
liveQuery: {
Expand Down
11 changes: 9 additions & 2 deletions src/Controllers/LiveQueryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export class LiveQueryController {
if (!config || !config.classNames) {
this.classNames = new Set();
} else if (config.classNames instanceof Array) {
this.classNames = new Set(config.classNames);
const classNames = config.classNames
.map(name => new RegExp("^" + name + "$"));
this.classNames = new Set(classNames);
} else {
throw 'liveQuery.classes should be an array of string';
}
Expand Down Expand Up @@ -43,7 +45,12 @@ export class LiveQueryController {
}

hasLiveQuery(className: string): boolean {
return this.classNames.has(className);
for (const name of this.classNames) {
if (name.test(className)) {
return true;
}
}
return false;
}

_makePublisherRequest(currentObject: any, originalObject: any, classLevelPermissions: ?any): any {
Expand Down

0 comments on commit 7638ff8

Please sign in to comment.