Skip to content

Commit 9a72641

Browse files
committed
Merge pull request #794 from ParsePlatform/nlutsenko.schemasRouter
Completely migrate SchemasRouter to new MongoCollection API.
2 parents 06fb8d4 + 99cb05e commit 9a72641

File tree

4 files changed

+38
-55
lines changed

4 files changed

+38
-55
lines changed

spec/Schema.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ describe('Schema', () => {
188188
foo: {type: 'String'}
189189
}))
190190
.catch(error => {
191-
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
192-
expect(error.error).toEqual('class NewClass already exists');
191+
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
192+
expect(error.message).toEqual('Class NewClass already exists.');
193193
done();
194194
});
195195
});
@@ -216,7 +216,7 @@ describe('Schema', () => {
216216
Promise.all([p1,p2])
217217
.catch(error => {
218218
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
219-
expect(error.error).toEqual('class NewClass already exists');
219+
expect(error.message).toEqual('Class NewClass already exists.');
220220
done();
221221
});
222222
});
@@ -561,7 +561,8 @@ describe('Schema', () => {
561561
.then(() => config.database.collectionExists('_Join:aRelation:HasPointersAndRelations'))
562562
.then(exists => {
563563
if (!exists) {
564-
fail('Relation collection should exist after save.');
564+
fail('Relation collection ' +
565+
'should exist after save.');
565566
}
566567
})
567568
.then(() => config.database.loadSchema())

spec/schemas.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe('schemas', () => {
175175
expect(response.statusCode).toEqual(400);
176176
expect(body).toEqual({
177177
code: 103,
178-
error: 'class HASALLPOD does not exist',
178+
error: 'Class HASALLPOD does not exist.',
179179
});
180180
done();
181181
});
@@ -224,7 +224,7 @@ describe('schemas', () => {
224224
expect(response.statusCode).toEqual(400);
225225
expect(body).toEqual({
226226
code: Parse.Error.INVALID_CLASS_NAME,
227-
error: 'class name mismatch between B and A',
227+
error: 'Class name mismatch between B and A.',
228228
});
229229
done();
230230
});
@@ -240,7 +240,7 @@ describe('schemas', () => {
240240
expect(response.statusCode).toEqual(400);
241241
expect(body).toEqual({
242242
code: 135,
243-
error: 'POST /schemas needs class name',
243+
error: 'POST /schemas needs a class name.',
244244
});
245245
done();
246246
})
@@ -267,7 +267,7 @@ describe('schemas', () => {
267267
expect(response.statusCode).toEqual(400);
268268
expect(body).toEqual({
269269
code: Parse.Error.INVALID_CLASS_NAME,
270-
error: 'class A already exists',
270+
error: 'Class A already exists.'
271271
});
272272
done();
273273
});
@@ -353,7 +353,7 @@ describe('schemas', () => {
353353
}, (error, response, body) => {
354354
expect(response.statusCode).toEqual(400);
355355
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
356-
expect(body.error).toEqual('class name mismatch between WrongClassName and NewClass');
356+
expect(body.error).toEqual('Class name mismatch between WrongClassName and NewClass.');
357357
done();
358358
});
359359
});
@@ -733,7 +733,7 @@ describe('schemas', () => {
733733
//Expect _SCHEMA entry to be gone.
734734
expect(response.statusCode).toEqual(400);
735735
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
736-
expect(body.error).toEqual('class MyOtherClass does not exist');
736+
expect(body.error).toEqual('Class MyOtherClass does not exist.');
737737
done();
738738
});
739739
});

src/Routers/SchemasRouter.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import PromiseRouter from '../PromiseRouter';
88
import * as middleware from "../middlewares";
99

1010
function classNameMismatchResponse(bodyClass, pathClass) {
11-
return Promise.resolve({
12-
status: 400,
13-
response: {
14-
code: Parse.Error.INVALID_CLASS_NAME,
15-
error: 'class name mismatch between ' + bodyClass + ' and ' + pathClass,
16-
}
17-
});
11+
throw new Parse.Error(
12+
Parse.Error.INVALID_CLASS_NAME,
13+
`Class name mismatch between ${bodyClass} and ${pathClass}.`
14+
);
1815
}
1916

2017
function mongoSchemaAPIResponseFields(schema) {
@@ -45,16 +42,16 @@ function getAllSchemas(req) {
4542
}
4643

4744
function getOneSchema(req) {
48-
return req.config.database.collection('_SCHEMA')
49-
.then(coll => coll.findOne({'_id': req.params.className}))
50-
.then(schema => ({response: mongoSchemaToSchemaAPIResponse(schema)}))
51-
.catch(() => ({
52-
status: 400,
53-
response: {
54-
code: 103,
55-
error: 'class ' + req.params.className + ' does not exist',
45+
const className = req.params.className;
46+
return req.config.database.adaptiveCollection('_SCHEMA')
47+
.then(collection => collection.find({ '_id': className }, { limit: 1 }))
48+
.then(results => {
49+
if (results.length != 1) {
50+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
5651
}
57-
}));
52+
return results[0];
53+
})
54+
.then(schema => ({ response: mongoSchemaToSchemaAPIResponse(schema) }));
5855
}
5956

6057
function createSchema(req) {
@@ -63,23 +60,15 @@ function createSchema(req) {
6360
return classNameMismatchResponse(req.body.className, req.params.className);
6461
}
6562
}
66-
var className = req.params.className || req.body.className;
63+
64+
const className = req.params.className || req.body.className;
6765
if (!className) {
68-
return Promise.resolve({
69-
status: 400,
70-
response: {
71-
code: 135,
72-
error: 'POST ' + req.path + ' needs class name',
73-
},
74-
});
66+
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
7567
}
68+
7669
return req.config.database.loadSchema()
7770
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
78-
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }))
79-
.catch(error => ({
80-
status: 400,
81-
response: error,
82-
}));
71+
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
8372
}
8473

8574
function modifySchema(req) {

src/Schema.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,29 +340,22 @@ function buildMergedSchemaObject(mongoObject, putRequest) {
340340
// enabled) before calling this function.
341341
Schema.prototype.addClassIfNotExists = function(className, fields) {
342342
if (this.data[className]) {
343-
return Promise.reject({
344-
code: Parse.Error.INVALID_CLASS_NAME,
345-
error: 'class ' + className + ' already exists',
346-
});
343+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
347344
}
348345

349-
var mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
350-
346+
let mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
351347
if (!mongoObject.result) {
352348
return Promise.reject(mongoObject);
353349
}
354350

355351
return this.collection.insertOne(mongoObject.result)
356-
.then(result => result.ops[0])
357-
.catch(error => {
358-
if (error.code === 11000) { //Mongo's duplicate key error
359-
return Promise.reject({
360-
code: Parse.Error.INVALID_CLASS_NAME,
361-
error: 'class ' + className + ' already exists',
362-
});
363-
}
364-
return Promise.reject(error);
365-
});
352+
.then(result => result.ops[0])
353+
.catch(error => {
354+
if (error.code === 11000) { //Mongo's duplicate key error
355+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
356+
}
357+
return Promise.reject(error);
358+
});
366359
};
367360

368361
// Returns a promise that resolves successfully to the new schema

0 commit comments

Comments
 (0)