Skip to content

Commit 4bccf96

Browse files
dplewisflovilmart
authored andcommitted
Add Indexes to Schema API (parse-community#4240)
* Add Indexes to Schema API * error handling * ci errors * postgres support * full text compound indexes * pg clean up * get indexes on startup * test compound index on startup * add default _id to index, full Text index on startup * lint * fix test
1 parent 6a15107 commit 4bccf96

10 files changed

+958
-76
lines changed

spec/MongoSchemaCollectionAdapter.spec.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ describe('MongoSchemaCollection', () => {
2121
"create":{"*":true},
2222
"delete":{"*":true},
2323
"addField":{"*":true},
24+
},
25+
"indexes": {
26+
"name1":{"deviceToken":1}
2427
}
2528
},
2629
"installationId":"string",
@@ -66,7 +69,10 @@ describe('MongoSchemaCollection', () => {
6669
update: { '*': true },
6770
delete: { '*': true },
6871
addField: { '*': true },
69-
}
72+
},
73+
indexes: {
74+
name1: {deviceToken: 1}
75+
},
7076
});
7177
done();
7278
});

spec/ParseQuery.FullTextSearch.spec.js

+90-34
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const fullTextHelper = () => {
3131
const request = {
3232
method: "POST",
3333
body: {
34-
subject: subjects[i]
34+
subject: subjects[i],
35+
comment: subjects[i],
3536
},
3637
path: "/1/classes/TestObject"
3738
};
@@ -280,42 +281,83 @@ describe('Parse.Query Full Text Search testing', () => {
280281
});
281282

282283
describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
283-
it('fullTextSearch: $search, only one text index', (done) => {
284-
return reconfigureServer({
285-
appId: 'test',
286-
restAPIKey: 'test',
287-
publicServerURL: 'http://localhost:8378/1',
288-
databaseAdapter: new MongoStorageAdapter({ uri: mongoURI })
284+
it('fullTextSearch: does not create text index if compound index exist', (done) => {
285+
fullTextHelper().then(() => {
286+
return databaseAdapter.dropAllIndexes('TestObject');
289287
}).then(() => {
290-
return rp.post({
291-
url: 'http://localhost:8378/1/batch',
292-
body: {
293-
requests: [
294-
{
295-
method: "POST",
296-
body: {
297-
subject: "coffee is java"
298-
},
299-
path: "/1/classes/TestObject"
300-
},
301-
{
302-
method: "POST",
303-
body: {
304-
subject: "java is coffee"
305-
},
306-
path: "/1/classes/TestObject"
288+
return databaseAdapter.getIndexes('TestObject');
289+
}).then((indexes) => {
290+
expect(indexes.length).toEqual(1);
291+
return databaseAdapter.createIndex('TestObject', {subject: 'text', comment: 'text'});
292+
}).then(() => {
293+
return databaseAdapter.getIndexes('TestObject');
294+
}).then((indexes) => {
295+
expect(indexes.length).toEqual(2);
296+
const where = {
297+
subject: {
298+
$text: {
299+
$search: {
300+
$term: 'coffee'
307301
}
308-
]
309-
},
310-
json: true,
302+
}
303+
}
304+
};
305+
return rp.post({
306+
url: 'http://localhost:8378/1/classes/TestObject',
307+
json: { where, '_method': 'GET' },
311308
headers: {
312309
'X-Parse-Application-Id': 'test',
313310
'X-Parse-REST-API-Key': 'test'
314311
}
315312
});
313+
}).then((resp) => {
314+
expect(resp.results.length).toEqual(3);
315+
return databaseAdapter.getIndexes('TestObject');
316+
}).then((indexes) => {
317+
expect(indexes.length).toEqual(2);
318+
rp.get({
319+
url: 'http://localhost:8378/1/schemas/TestObject',
320+
headers: {
321+
'X-Parse-Application-Id': 'test',
322+
'X-Parse-Master-Key': 'test',
323+
},
324+
json: true,
325+
}, (error, response, body) => {
326+
expect(body.indexes._id_).toBeDefined();
327+
expect(body.indexes._id_._id).toEqual(1);
328+
expect(body.indexes.subject_text_comment_text).toBeDefined();
329+
expect(body.indexes.subject_text_comment_text.subject).toEqual('text');
330+
expect(body.indexes.subject_text_comment_text.comment).toEqual('text');
331+
done();
332+
});
333+
}).catch(done.fail);
334+
});
335+
336+
it('fullTextSearch: does not create text index if schema compound index exist', (done) => {
337+
fullTextHelper().then(() => {
338+
return databaseAdapter.dropAllIndexes('TestObject');
316339
}).then(() => {
317-
return databaseAdapter.createIndex('TestObject', {random: 'text'});
340+
return databaseAdapter.getIndexes('TestObject');
341+
}).then((indexes) => {
342+
expect(indexes.length).toEqual(1);
343+
return rp.put({
344+
url: 'http://localhost:8378/1/schemas/TestObject',
345+
json: true,
346+
headers: {
347+
'X-Parse-Application-Id': 'test',
348+
'X-Parse-REST-API-Key': 'test',
349+
'X-Parse-Master-Key': 'test',
350+
},
351+
body: {
352+
indexes: {
353+
text_test: { subject: 'text', comment: 'text'},
354+
},
355+
},
356+
});
318357
}).then(() => {
358+
return databaseAdapter.getIndexes('TestObject');
359+
}).then((indexes) => {
360+
expect(indexes.length).toEqual(2);
319361
const where = {
320362
subject: {
321363
$text: {
@@ -334,12 +376,26 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
334376
}
335377
});
336378
}).then((resp) => {
337-
fail(`Should not be more than one text index: ${JSON.stringify(resp)}`);
338-
done();
339-
}).catch((err) => {
340-
expect(err.error.code).toEqual(Parse.Error.INTERNAL_SERVER_ERROR);
341-
done();
342-
});
379+
expect(resp.results.length).toEqual(3);
380+
return databaseAdapter.getIndexes('TestObject');
381+
}).then((indexes) => {
382+
expect(indexes.length).toEqual(2);
383+
rp.get({
384+
url: 'http://localhost:8378/1/schemas/TestObject',
385+
headers: {
386+
'X-Parse-Application-Id': 'test',
387+
'X-Parse-Master-Key': 'test',
388+
},
389+
json: true,
390+
}, (error, response, body) => {
391+
expect(body.indexes._id_).toBeDefined();
392+
expect(body.indexes._id_._id).toEqual(1);
393+
expect(body.indexes.text_test).toBeDefined();
394+
expect(body.indexes.text_test.subject).toEqual('text');
395+
expect(body.indexes.text_test.comment).toEqual('text');
396+
done();
397+
});
398+
}).catch(done.fail);
343399
});
344400

345401
it('fullTextSearch: $diacriticSensitive - false', (done) => {

spec/Schema.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('SchemaController', () => {
274274
fooSixteen: {type: 'String'},
275275
fooEighteen: {type: 'String'},
276276
fooNineteen: {type: 'String'},
277-
}, levelPermissions, config.database))
277+
}, levelPermissions, {}, config.database))
278278
.then(actualSchema => {
279279
const expectedSchema = {
280280
className: 'NewClass',
@@ -304,6 +304,9 @@ describe('SchemaController', () => {
304304
fooNineteen: {type: 'String'},
305305
},
306306
classLevelPermissions: { ...levelPermissions },
307+
indexes: {
308+
_id_: { _id: 1 }
309+
}
307310
};
308311

309312
expect(dd(actualSchema, expectedSchema)).toEqual(undefined);

0 commit comments

Comments
 (0)