-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add MongoCollection wrapper and move few basic uses of collection to it. #752
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
let mongodb = require('mongodb'); | ||
let Collection = mongodb.Collection; | ||
|
||
export default class MongoCollection { | ||
_mongoCollection:Collection; | ||
|
||
constructor(mongoCollection:Collection) { | ||
this._mongoCollection = mongoCollection; | ||
} | ||
|
||
// Does a find with "smart indexing". | ||
// Currently this just means, if it needs a geoindex and there is | ||
// none, then build the geoindex. | ||
// This could be improved a lot but it's not clear if that's a good | ||
// idea. Or even if this behavior is a good idea. | ||
find(query, { skip, limit, sort } = {}) { | ||
return this._rawFind(query, { skip, limit, sort }) | ||
.catch(error => { | ||
// Check for "no geoindex" error | ||
if (error.code != 17007 || | ||
!error.message.match(/unable to find index for .geoNear/)) { | ||
throw error; | ||
} | ||
// Figure out what key needs an index | ||
let key = error.message.match(/field=([A-Za-z_0-9]+) /)[1]; | ||
if (!key) { | ||
throw error; | ||
} | ||
|
||
var index = {}; | ||
index[key] = '2d'; | ||
//TODO: condiser moving index creation logic into Schema.js | ||
return this._mongoCollection.createIndex(index) | ||
// Retry, but just once. | ||
.then(() => this._rawFind(query, { skip, limit, sort })); | ||
}); | ||
} | ||
|
||
_rawFind(query, { skip, limit, sort } = {}) { | ||
return this._mongoCollection | ||
.find(query, { skip, limit, sort }) | ||
.toArray(); | ||
} | ||
|
||
count(query, { skip, limit, sort } = {}) { | ||
return this._mongoCollection.count(query, { skip, limit, sort }); | ||
} | ||
|
||
drop() { | ||
return this._mongoCollection.drop(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,10 @@ function mongoSchemaToSchemaAPIResponse(schema) { | |
} | ||
|
||
function getAllSchemas(req) { | ||
return req.config.database.collection('_SCHEMA') | ||
.then(coll => coll.find({}).toArray()) | ||
.then(schemas => ({response: { | ||
results: schemas.map(mongoSchemaToSchemaAPIResponse) | ||
}})); | ||
return req.config.database.adaptiveCollection('_SCHEMA') | ||
.then(collection => collection.find({})) | ||
.then(schemas => schemas.map(mongoSchemaToSchemaAPIResponse)) | ||
.then(schemas => ({ response: { results: schemas }})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting this in a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (y) That makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here it is a very small perf impact, so I don't think it really matters. In the dashboard I would ask you to change it :p |
||
} | ||
|
||
function getOneSchema(req) { | ||
|
@@ -152,7 +151,7 @@ function deleteSchema(req) { | |
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, Schema.invalidClassNameMessage(req.params.className)); | ||
} | ||
|
||
return req.config.database.collection(req.params.className) | ||
return req.config.database.adaptiveCollection(req.params.className) | ||
.then(collection => { | ||
return collection.count() | ||
.then(count => { | ||
|
@@ -161,19 +160,19 @@ function deleteSchema(req) { | |
} | ||
return collection.drop(); | ||
}) | ||
.then(() => { | ||
// We've dropped the collection now, so delete the item from _SCHEMA | ||
// and clear the _Join collections | ||
return req.config.database.collection('_SCHEMA') | ||
.then(coll => coll.findAndRemove({_id: req.params.className}, [])) | ||
.then(doc => { | ||
if (doc.value === null) { | ||
//tried to delete non-existent class | ||
return Promise.resolve(); | ||
} | ||
return removeJoinTables(req.config.database, doc.value); | ||
}); | ||
}) | ||
}) | ||
.then(() => { | ||
// We've dropped the collection now, so delete the item from _SCHEMA | ||
// and clear the _Join collections | ||
return req.config.database.collection('_SCHEMA') | ||
.then(coll => coll.findAndRemove({_id: req.params.className}, [])) | ||
.then(doc => { | ||
if (doc.value === null) { | ||
//tried to delete non-existent class | ||
return Promise.resolve(); | ||
} | ||
return removeJoinTables(req.config.database, doc.value); | ||
}); | ||
}) | ||
.then(() => { | ||
// Success | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Everything going to go through a private method now, which will retry find just once.