import MongoCollection from './MongoCollection'; import MongoSchemaCollection from './MongoSchemaCollection'; import {parse as parseUrl, format as formatUrl} from './mongodbUrl'; let mongodb = require('mongodb'); let MongoClient = mongodb.MongoClient; const MongoSchemaCollectionName = '_SCHEMA'; export class MongoStorageAdapter { // Private _uri: string; _options: Object; // Public connectionPromise; database; constructor(uri: string, options: Object) { this._uri = uri; this._options = options; } connect() { if (this.connectionPromise) { return this.connectionPromise; } // 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 => { this.database = database; }); return this.connectionPromise; } collection(name: string) { return this.connect().then(() => { return this.database.collection(name); }); } adaptiveCollection(name: string) { return this.connect() .then(() => this.database.collection(name)) .then(rawCollection => new MongoCollection(rawCollection)); } schemaCollection(collectionPrefix: string) { return this.connect() .then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName)) .then(collection => new MongoSchemaCollection(collection)); } collectionExists(name: string) { return this.connect().then(() => { return this.database.listCollections({ name: name }).toArray(); }).then(collections => { return collections.length > 0; }); } dropCollection(name: string) { return this.collection(name).then(collection => collection.drop()); } // 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); }); }); } } export default MongoStorageAdapter; module.exports = MongoStorageAdapter; // Required for tests