Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completely migrate SchemasRouter to new MongoCollection API. #794

Merged
merged 2 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ describe('Schema', () => {
foo: {type: 'String'}
}))
.catch(error => {
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
expect(error.error).toEqual('class NewClass already exists');
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(error.message).toEqual('Class NewClass already exists.');
done();
});
});
Expand All @@ -216,7 +216,7 @@ describe('Schema', () => {
Promise.all([p1,p2])
.catch(error => {
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(error.error).toEqual('class NewClass already exists');
expect(error.message).toEqual('Class NewClass already exists.');
done();
});
});
Expand Down Expand Up @@ -524,7 +524,8 @@ describe('Schema', () => {
.then(() => config.database.collectionExists('_Join:aRelation:HasPointersAndRelations'))
.then(exists => {
if (!exists) {
fail('Relation collection should exist after save.');
fail('Relation collection ' +
'should exist after save.');
}
})
.then(() => config.database.loadSchema())
Expand Down
12 changes: 6 additions & 6 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: 103,
error: 'class HASALLPOD does not exist',
error: 'Class HASALLPOD does not exist.',
});
done();
});
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class name mismatch between B and A',
error: 'Class name mismatch between B and A.',
});
done();
});
Expand All @@ -240,7 +240,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: 135,
error: 'POST /schemas needs class name',
error: 'POST /schemas needs a class name.',
});
done();
})
Expand All @@ -267,7 +267,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class A already exists',
error: 'Class A already exists.'
});
done();
});
Expand Down Expand Up @@ -353,7 +353,7 @@ describe('schemas', () => {
}, (error, response, body) => {
expect(response.statusCode).toEqual(400);
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(body.error).toEqual('class name mismatch between WrongClassName and NewClass');
expect(body.error).toEqual('Class name mismatch between WrongClassName and NewClass.');
done();
});
});
Expand Down Expand Up @@ -733,7 +733,7 @@ describe('schemas', () => {
//Expect _SCHEMA entry to be gone.
expect(response.statusCode).toEqual(400);
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(body.error).toEqual('class MyOtherClass does not exist');
expect(body.error).toEqual('Class MyOtherClass does not exist.');
done();
});
});
Expand Down
47 changes: 18 additions & 29 deletions src/Routers/SchemasRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares";

function classNameMismatchResponse(bodyClass, pathClass) {
return Promise.resolve({
status: 400,
response: {
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class name mismatch between ' + bodyClass + ' and ' + pathClass,
}
});
throw new Parse.Error(
Parse.Error.INVALID_CLASS_NAME,
`Class name mismatch between ${bodyClass} and ${pathClass}.`
);
}

function mongoSchemaAPIResponseFields(schema) {
Expand Down Expand Up @@ -45,16 +42,16 @@ function getAllSchemas(req) {
}

function getOneSchema(req) {
return req.config.database.collection('_SCHEMA')
.then(coll => coll.findOne({'_id': req.params.className}))
.then(schema => ({response: mongoSchemaToSchemaAPIResponse(schema)}))
.catch(() => ({
status: 400,
response: {
code: 103,
error: 'class ' + req.params.className + ' does not exist',
const className = req.params.className;
return req.config.database.adaptiveCollection('_SCHEMA')
.then(collection => collection.find({ '_id': className }, { limit: 1 }))
.then(results => {
if (results.length != 1) {
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
}
}));
return results[0];
})
.then(schema => ({ response: mongoSchemaToSchemaAPIResponse(schema) }));
}

function createSchema(req) {
Expand All @@ -63,23 +60,15 @@ function createSchema(req) {
return classNameMismatchResponse(req.body.className, req.params.className);
}
}
var className = req.params.className || req.body.className;

const className = req.params.className || req.body.className;
if (!className) {
return Promise.resolve({
status: 400,
response: {
code: 135,
error: 'POST ' + req.path + ' needs class name',
},
});
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
}

return req.config.database.loadSchema()
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }))
.catch(error => ({
status: 400,
response: error,
}));
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
}

function modifySchema(req) {
Expand Down
25 changes: 9 additions & 16 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,22 @@ function buildMergedSchemaObject(mongoObject, putRequest) {
// enabled) before calling this function.
Schema.prototype.addClassIfNotExists = function(className, fields) {
if (this.data[className]) {
return Promise.reject({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class ' + className + ' already exists',
});
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
}

var mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);

let mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
if (!mongoObject.result) {
return Promise.reject(mongoObject);
}

return this.collection.insertOne(mongoObject.result)
.then(result => result.ops[0])
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
return Promise.reject({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class ' + className + ' already exists',
});
}
return Promise.reject(error);
});
.then(result => result.ops[0])
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
}
return Promise.reject(error);
});
};

// Returns a promise that resolves successfully to the new schema
Expand Down