Futzing with read preference (#3963)

* allow setting readpreference when using rest api.

* take out partially complete unit test.

* oops. nit

* Include read preference option for find directly from api and adding few more tests

* Adding catch for all tests

* Keep same check for get and find

* Turn read preference case insensitive

* Includes and subqueries read preferences through API

* Fixing bugs regarding changes that were done in master branch during the last year

* Changing behavior to make includeReadPreference and subqueryReadPreference to follow readPreference by default
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-05-14 12:58:02 -07:00
committed by GitHub
parent 893f1d376e
commit afa74d655d
6 changed files with 915 additions and 333 deletions

View File

@@ -41,16 +41,12 @@ describe('GridFSBucket and GridStore interop', () => {
expect(gfsResult.toString('utf8')).toBe(twoMegabytesFile);
});
it(
'properly deletes a file from GridFS',
async () => {
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
await gfsAdapter.createFile('myFileName', 'a simple file');
await gfsAdapter.deleteFile('myFileName');
await expectMissingFile(gfsAdapter, 'myFileName');
},
1000000
);
it('properly deletes a file from GridFS', async () => {
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
await gfsAdapter.createFile('myFileName', 'a simple file');
await gfsAdapter.deleteFile('myFileName');
await expectMissingFile(gfsAdapter, 'myFileName');
}, 1000000);
it('properly overrides files', async () => {
const gfsAdapter = new GridFSBucketAdapter(databaseURI);

File diff suppressed because it is too large Load Diff

View File

@@ -926,6 +926,9 @@ export class MongoStorageAdapter implements StorageAdapter {
}
_parseReadPreference(readPreference: ?string): ?string {
if (readPreference) {
readPreference = readPreference.toUpperCase();
}
switch (readPreference) {
case 'PRIMARY':
readPreference = ReadPreference.PRIMARY;

View File

@@ -14,6 +14,9 @@ const AlwaysSelectedKeys = ['objectId', 'createdAt', 'updatedAt', 'ACL'];
// include
// keys
// redirectClassNameForKey
// readPreference
// includeReadPreference
// subqueryReadPreference
function RestQuery(
config,
auth,
@@ -362,6 +365,8 @@ RestQuery.prototype.replaceInQuery = function() {
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
} else if (this.restOptions.readPreference) {
additionalOptions.readPreference = this.restOptions.readPreference;
}
var subquery = new RestQuery(
@@ -421,6 +426,8 @@ RestQuery.prototype.replaceNotInQuery = function() {
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
} else if (this.restOptions.readPreference) {
additionalOptions.readPreference = this.restOptions.readPreference;
}
var subquery = new RestQuery(
@@ -484,6 +491,8 @@ RestQuery.prototype.replaceSelect = function() {
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
} else if (this.restOptions.readPreference) {
additionalOptions.readPreference = this.restOptions.readPreference;
}
var subquery = new RestQuery(
@@ -545,6 +554,8 @@ RestQuery.prototype.replaceDontSelect = function() {
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
} else if (this.restOptions.readPreference) {
additionalOptions.readPreference = this.restOptions.readPreference;
}
var subquery = new RestQuery(
@@ -809,6 +820,8 @@ function includePath(config, auth, response, path, restOptions = {}) {
includeRestOptions.readPreference = restOptions.includeReadPreference;
includeRestOptions.includeReadPreference =
restOptions.includeReadPreference;
} else if (restOptions.readPreference) {
includeRestOptions.readPreference = restOptions.readPreference;
}
const queryPromises = Object.keys(pointersHash).map(className => {

View File

@@ -3,7 +3,13 @@ import rest from '../rest';
import _ from 'lodash';
import Parse from 'parse/node';
const ALLOWED_GET_QUERY_KEYS = ['keys', 'include'];
const ALLOWED_GET_QUERY_KEYS = [
'keys',
'include',
'readPreference',
'includeReadPreference',
'subqueryReadPreference',
];
export class ClassesRouter extends PromiseRouter {
className(req) {
@@ -57,12 +63,21 @@ export class ClassesRouter extends PromiseRouter {
}
}
if (typeof body.keys == 'string') {
if (typeof body.keys === 'string') {
options.keys = body.keys;
}
if (body.include) {
options.include = String(body.include);
}
if (typeof body.readPreference === 'string') {
options.readPreference = body.readPreference;
}
if (typeof body.includeReadPreference === 'string') {
options.includeReadPreference = body.includeReadPreference;
}
if (typeof body.subqueryReadPreference === 'string') {
options.subqueryReadPreference = body.subqueryReadPreference;
}
return rest
.get(
@@ -154,6 +169,9 @@ export class ClassesRouter extends PromiseRouter {
'includeAll',
'redirectClassNameForKey',
'where',
'readPreference',
'includeReadPreference',
'subqueryReadPreference',
];
for (const key of Object.keys(body)) {
@@ -188,6 +206,15 @@ export class ClassesRouter extends PromiseRouter {
if (body.includeAll) {
options.includeAll = true;
}
if (typeof body.readPreference === 'string') {
options.readPreference = body.readPreference;
}
if (typeof body.includeReadPreference === 'string') {
options.includeReadPreference = body.includeReadPreference;
}
if (typeof body.subqueryReadPreference === 'string') {
options.subqueryReadPreference = body.subqueryReadPreference;
}
return options;
}

View File

@@ -101,7 +101,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
code === 10 /*\n*/ ||
code === 12 /*\f*/ ||
code === 160 /*\u00A0*/ ||
code === 65279 /*\uFEFF*/;
code === 65279; /*\uFEFF*/
if (start === -1) {
if (isWs) continue;
lastPos = start = i;
@@ -193,7 +193,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
// how the browser resolves relative URLs.
if (slashesDenoteHost || proto || /^\/\/[^@\/]+@[^@\/]+/.test(rest)) {
var slashes =
rest.charCodeAt(0) === 47 /*/*/ && rest.charCodeAt(1) === 47 /*/*/;
rest.charCodeAt(0) === 47 /*/*/ && rest.charCodeAt(1) === 47; /*/*/
if (slashes && !(proto && hostlessProtocol[proto])) {
rest = rest.slice(2);
this.slashes = true;
@@ -285,7 +285,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
// assume that it's an IPv6 address.
var ipv6Hostname =
hostname.charCodeAt(0) === 91 /*[*/ &&
hostname.charCodeAt(hostname.length - 1) === 93 /*]*/;
hostname.charCodeAt(hostname.length - 1) === 93; /*]*/
// validate a little.
if (!ipv6Hostname) {
@@ -868,11 +868,11 @@ Url.prototype.resolveObject = function(relative) {
// put the host back
if (psychotic) {
result.hostname = result.host = isAbsolute
? ''
: srcPath.length
? srcPath.shift()
: '';
if (isAbsolute) {
result.hostname = result.host = '';
} else {
result.hostname = result.host = srcPath.length ? srcPath.shift() : '';
}
//occasionally the auth can get stuck only in host
//this especially happens in cases like
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')