2016-02-27 02:23:57 -08:00
|
|
|
|
2016-03-01 20:04:15 -08:00
|
|
|
import MongoCollection from './MongoCollection';
|
2016-03-09 15:20:59 -08:00
|
|
|
import MongoSchemaCollection from './MongoSchemaCollection';
|
2016-03-14 13:29:02 -07:00
|
|
|
import {parse as parseUrl, format as formatUrl} from './mongodbUrl';
|
2016-03-01 20:04:15 -08:00
|
|
|
|
2016-02-27 02:23:57 -08:00
|
|
|
let mongodb = require('mongodb');
|
|
|
|
|
let MongoClient = mongodb.MongoClient;
|
|
|
|
|
|
2016-03-09 15:20:59 -08:00
|
|
|
const MongoSchemaCollectionName = '_SCHEMA';
|
|
|
|
|
|
2016-02-27 02:23:57 -08:00
|
|
|
export class MongoStorageAdapter {
|
|
|
|
|
// Private
|
|
|
|
|
_uri: string;
|
2016-03-10 16:49:45 +00:00
|
|
|
_options: Object;
|
2016-02-27 02:23:57 -08:00
|
|
|
// Public
|
|
|
|
|
connectionPromise;
|
|
|
|
|
database;
|
|
|
|
|
|
2016-03-10 16:49:45 +00:00
|
|
|
constructor(uri: string, options: Object) {
|
2016-02-27 02:23:57 -08:00
|
|
|
this._uri = uri;
|
2016-03-10 16:49:45 +00:00
|
|
|
this._options = options;
|
2016-02-27 02:23:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
|
if (this.connectionPromise) {
|
|
|
|
|
return this.connectionPromise;
|
|
|
|
|
}
|
|
|
|
|
|
Add URI encoding to mongo auth parameters
The mongodb driver requires auth values be URI encoded:
https://github.com/mongodb/node-mongodb-native/commit/044063097dc4dd5e6cf3a3574c555fec7559d38b
This uses node's built-in url module to encode the auth portion, by
parsing and re-formatting it, which causes special characters to get URI
encoded properly:
https://nodejs.org/api/url.html#url_escaped_characters
This is all a bit silly since mongodb just takes our passed uri, and
runs it through the same url parser again, but not before explicitly
erroring on '@' characters in the uri.
This is similiar to #148 (reverted by #297), but with much less code,
and hopefully less breakage. Also, note that `uri_decode_auth` is no
longer needed. That was removed in the above referenced
node-mongodb-native commit.
I've tested this on usernames and passwords with @, !, +, and a space.
Presumably this would also work with usernames and passwords that are
already URI encoded (since parseUrl will simply unescape it, and
formatUrl will escape it again).
2016-03-11 16:10:44 -08:00
|
|
|
// parsing and re-formatting causes the auth value (if there) to get URI
|
|
|
|
|
// encoded
|
|
|
|
|
const encodedUri = formatUrl(parseUrl(this._uri));
|
|
|
|
|
|
|
|
|
|
this.connectionPromise = MongoClient.connect(encodedUri, this._options).then(database => {
|
2016-02-27 02:23:57 -08:00
|
|
|
this.database = database;
|
|
|
|
|
});
|
|
|
|
|
return this.connectionPromise;
|
|
|
|
|
}
|
2016-02-27 03:02:38 -08:00
|
|
|
|
|
|
|
|
collection(name: string) {
|
|
|
|
|
return this.connect().then(() => {
|
|
|
|
|
return this.database.collection(name);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 20:04:15 -08:00
|
|
|
adaptiveCollection(name: string) {
|
|
|
|
|
return this.connect()
|
|
|
|
|
.then(() => this.database.collection(name))
|
|
|
|
|
.then(rawCollection => new MongoCollection(rawCollection));
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 15:20:59 -08:00
|
|
|
schemaCollection(collectionPrefix: string) {
|
|
|
|
|
return this.connect()
|
|
|
|
|
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
|
|
|
|
|
.then(collection => new MongoSchemaCollection(collection));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 19:41:05 -08:00
|
|
|
collectionExists(name: string) {
|
|
|
|
|
return this.connect().then(() => {
|
|
|
|
|
return this.database.listCollections({ name: name }).toArray();
|
|
|
|
|
}).then(collections => {
|
|
|
|
|
return collections.length > 0;
|
|
|
|
|
});
|
2016-02-29 17:04:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dropCollection(name: string) {
|
|
|
|
|
return this.collection(name).then(collection => collection.drop());
|
|
|
|
|
}
|
2016-02-27 03:02:38 -08:00
|
|
|
// Used for testing only right now.
|
|
|
|
|
collectionsContaining(match: string) {
|
|
|
|
|
return this.connect().then(() => {
|
|
|
|
|
return this.database.collections();
|
|
|
|
|
}).then(collections => {
|
|
|
|
|
return collections.filter(collection => {
|
|
|
|
|
if (collection.namespace.match(/\.system\./)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return (collection.collectionName.indexOf(match) == 0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-27 02:23:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MongoStorageAdapter;
|
|
|
|
|
module.exports = MongoStorageAdapter; // Required for tests
|