diff --git a/packages/noodl-editor/src/editor/src/utils/schemahandler.ts b/packages/noodl-editor/src/editor/src/utils/schemahandler.ts index c4cd696..0b78428 100644 --- a/packages/noodl-editor/src/editor/src/utils/schemahandler.ts +++ b/packages/noodl-editor/src/editor/src/utils/schemahandler.ts @@ -13,6 +13,7 @@ export default class SchemaHandler { public dbCollections: TSFixme[]; public systemCollections: TSFixme[]; public configSchema: TSFixme; + public parseServerVersion: string; constructor() { EventDispatcher.instance.on( @@ -119,6 +120,20 @@ export default class SchemaHandler { console.log(e); } }); + + // Get Parse Server Version & Supported features + fetch(environment.url + '/serverInfo', { + method: 'POST', + body: JSON.stringify({ + "_method": "GET", + "_ApplicationId": environment.appId, + "_MasterKey": environment.masterKey, + }) + }) + .then((response) => response.json()) + .then((json) => { + this.parseServerVersion = json.parseServerVersion; + }); }); }); } @@ -129,10 +144,20 @@ export default class SchemaHandler { ProjectModel.instance.setMetaData('dbCollections', this.dbCollections); ProjectModel.instance.setMetaData('systemCollections', this.systemCollections); ProjectModel.instance.setMetaData('dbConfigSchema', this.configSchema); + + const versionNumbers = this.parseServerVersion?.split(".") + if (versionNumbers && versionNumbers.length > 0) { + // Let's only save the major version number, + // since this will be used to determine which verison of the API to use. + ProjectModel.instance.setMetaData('dbVersionMajor', versionNumbers[0]); + } else { + ProjectModel.instance.setMetaData('dbVersionMajor', undefined); + } } else { ProjectModel.instance.setMetaData('dbCollections', undefined); ProjectModel.instance.setMetaData('systemCollections', undefined); ProjectModel.instance.setMetaData('dbConfigSchema', undefined); + ProjectModel.instance.setMetaData('dbVersionMajor', undefined); } } } diff --git a/packages/noodl-runtime/src/api/cloudstore.js b/packages/noodl-runtime/src/api/cloudstore.js index 644b46f..346b665 100644 --- a/packages/noodl-runtime/src/api/cloudstore.js +++ b/packages/noodl-runtime/src/api/cloudstore.js @@ -32,12 +32,15 @@ class CloudStore { _initCloudServices() { _collections = undefined; // clear collection cache, so it's refetched - const cloudServices = NoodlRuntime.instance.getMetaData('cloudservices'); + const cloudServices = NoodlRuntime.instance.getMetaData('cloudservices'); if (cloudServices) { this.appId = cloudServices.appId; this.endpoint = cloudServices.endpoint; } + + const dbVersionMajor = NoodlRuntime.instance.getMetaData('dbVersionMajor'); + this.dbVersionMajor = dbVersionMajor; } on() { @@ -168,13 +171,10 @@ class CloudStore { return; } - if (options.where) args.push('match=' + encodeURIComponent(JSON.stringify(options.where))); if (options.limit) args.push('limit=' + options.limit); if (options.skip) args.push('skip=' + options.skip); - const grouping = { - objectId: null - }; + const grouping = {}; Object.keys(options.group).forEach((k) => { const _g = {}; @@ -188,7 +188,20 @@ class CloudStore { grouping[k] = _g; }); - args.push('group=' + JSON.stringify(grouping)); + // I don't know which version the API was changed, lets just say above 4 for now. + if (this.dbVersionMajor && this.dbVersionMajor > 4) { + grouping._id = null; + + if (options.where) args.push('$match=' + encodeURIComponent(JSON.stringify(options.where))); + + args.push('$group=' + JSON.stringify(grouping)); + } else { + grouping.objectId = null; + + if (options.where) args.push('match=' + encodeURIComponent(JSON.stringify(options.where))); + + args.push('group=' + JSON.stringify(grouping)); + } this._makeRequest('/aggregate/' + options.collection + (args.length > 0 ? '?' + args.join('&') : ''), { success: function (response) {