all files / src/connectors/ MongoDBConnector.js

10.45% Statements 7/67
0% Branches 0/18
0% Functions 0/23
10.45% Lines 7/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                                                                                                                                                                                                                                             
'use strict';
const rxjs_1 = require('rxjs');
const mongodb = require('mongodb');
const DataConnector_1 = require('./DataConnector');
class MongoClient {
    static connect(url, options) {
        return rxjs_1.Observable.from(mongodb.MongoClient.connect(url, options))
            .map(db => new Db(db));
    }
}
exports.MongoClient = MongoClient;
class Db {
    constructor(db) {
        this._db = db;
    }
    collection(name) {
        return new Collection(this._db.collection(name));
    }
    close() {
        return rxjs_1.Observable.from(this._db.close());
    }
}
exports.Db = Db;
class Collection {
    constructor(collection) {
        this._collection = collection;
    }
    find(query) {
        query = Object.assign({}, { where: {} }, query || {});
        let cursor = this._collection.find(query.where);
        Object.keys(query).forEach(key => {
            if (key !== 'where') {
                cursor = cursor[key](query[key]);
            }
        });
        return rxjs_1.Observable.create(subscriber => {
            cursor.forEach(document => {
                subscriber.next(document);
            }, error => {
                if (error) {
                    subscriber.error(error);
                }
                subscriber.complete();
            });
        });
    }
    count(query) {
        query = query || {};
        return rxjs_1.Observable.from(this._collection.count(query));
    }
    delete(query) {
        query = query || {};
        return rxjs_1.Observable.from(this._collection.deleteMany(query));
    }
    insert(doc) {
        return rxjs_1.Observable.from(this._collection.insertOne(doc))
            .map(response => response['ops'][0]);
    }
    update(query, doc) {
        return rxjs_1.Observable.from(this._collection.updateOne(query, { $set: doc }))
            .map(response => {
            if (response['modifiedCount']) {
                doc._id = query._id.toString();
                return doc;
            }
            else {
                return null;
            }
        });
    }
}
exports.Collection = Collection;
class MongoDBConnector extends DataConnector_1.DataConnector {
    _buildQueryWhereForId(id) {
        return this._prepareQueryWhere({
            _id: id
        });
    }
    _prepareQueryWhere(queryWhere) {
        const where = {};
        Object.keys(queryWhere).forEach(key => {
            let value = queryWhere[key];
            if (key === '_id') {
                value = new mongodb.ObjectID(value);
            }
            where[key] = value;
        });
        return where;
    }
    _collection(collectionName) {
        return this._db.collection(collectionName);
    }
    get ObjectID() {
        return mongodb.ObjectID;
    }
    connect() {
        this._state.connecting();
        return rxjs_1.Observable.from(MongoClient.connect(this._settings['url']))
            .do(db => {
            this._db = db;
            this._state.connected();
        })
            .mapTo(this);
    }
    disconnect() {
        this._state.disconnecting();
        return rxjs_1.Observable.from(this._db.close())
            .do(() => this._state.disconnected())
            .mapTo(this);
    }
    find(collectionName, query) {
        return this._collection(collectionName)
            .find(this._prepareQuery(query));
    }
    count(collectionName, query) {
        return this._collection(collectionName)
            .count(this._prepareQuery(query).where);
    }
    delete(collectionName, query) {
        return this._collection(collectionName)
            .delete(this._prepareQuery(query).where)
            .map(response => response['result'].n);
    }
    deleteById(collectionName, id) {
        return this._collection(collectionName)
            .delete(this._buildQueryWhereForId(id))
            .map(response => response['result'].n === 1);
    }
    get(collectionName, id) {
        return this._collection(collectionName)
            .find({ where: this._buildQueryWhereForId(id) });
    }
    save(collectionName, doc) {
        const hasId = Object.keys(doc).indexOf('_id') > -1 && doc['_id'];
        if (!hasId) {
            return this._collection(collectionName).insert(doc);
        }
        else {
            return this._collection(collectionName).update(this._buildQueryWhereForId(doc['_id']), doc);
        }
    }
    updateAttributes(collectionName, id, attributes) {
        delete attributes['_id'];
        return this.get(collectionName, id)
            .map(result => Object.assign(result, attributes))
            .mergeMap(doc => this.save(collectionName, doc));
    }
}
exports.MongoDBConnector = MongoDBConnector;