methods_common.js 5.6 KB
Newer Older
S
Sercan 已提交
1 2 3
/**
 * Created by RSercan on 5.3.2016.
 */
S
Sercan 已提交
4

S
Sercan 已提交
5 6 7
Meteor.methods({
    'listCollectionNames': function (dbName) {
        LOGGER.info('[listCollectionNames]', dbName);
S
Sercan 已提交
8 9

        return Async.runSync(function (done) {
S
Sercan 已提交
10 11 12 13 14 15 16 17 18 19 20
            try {
                var wishedDB = database.db(dbName);
                wishedDB.listCollections().toArray(function (err, collections) {
                    done(err, collections);
                });

            }
            catch (ex) {
                LOGGER.error('[listCollectionNames]', ex);
                done(new Meteor.Error(ex.message), null);
            }
S
Sercan 已提交
21 22 23 24
        });

    },

S
Sercan 已提交
25 26
    'getDatabases': function () {
        LOGGER.info('[getDatabases]');
S
Sercan 已提交
27 28

        return Async.runSync(function (done) {
S
Sercan 已提交
29 30 31 32 33 34 35 36 37
            try {
                database.admin().listDatabases(function (err, dbs) {
                    done(err, dbs.databases);
                });
            }
            catch (ex) {
                LOGGER.error('[getDatabases]', ex);
                done(new Meteor.Error(ex.message), null);
            }
S
Sercan 已提交
38
        });
S
Sercan 已提交
39
    },
S
Sercan 已提交
40

S
Sercan 已提交
41 42
    'disconnect': function () {
        database.close();
S
Sercan 已提交
43 44
    },

45 46
    'connect': function (connectionId) {
        var connection = Connections.findOne({_id: connectionId});
S
Sercan 已提交
47
        var connectionUrl = getConnectionUrl(connection);
48
        var connectionOptions = getConnectionOptions(connection);
S
Sercan 已提交
49

50
        LOGGER.info('[connect]', connectionUrl, clearConnectionOptionsForLog(connectionOptions));
S
Sercan 已提交
51 52

        return Async.runSync(function (done) {
S
Sercan 已提交
53 54 55 56 57 58 59 60 61 62
            if (connection.sshAddress) {
                var config = {
                    dstPort: connection.port,
                    host: connection.sshAddress,
                    port: connection.sshPort,
                    username: connection.sshUser
                };

                if (connection.sshCertificate) {
                    config.privateKey = connection.sshCertificate;
S
Sercan 已提交
63
                }
S
Sercan 已提交
64 65 66

                if (connection.sshPassPhrase) {
                    config.passphrase = connection.sshPassPhrase;
S
Sercan 已提交
67
                }
S
Sercan 已提交
68 69 70

                if (connection.sshPassword) {
                    config.password = connection.sshPassword;
S
Sercan 已提交
71
                }
S
Sercan 已提交
72

S
Sercan 已提交
73
                var tunnel = new Meteor.npmRequire('tunnel-ssh');
S
Sercan 已提交
74 75 76 77 78 79 80 81 82 83
                tunnel(config, function (error) {
                    if (error) {
                        done(new Meteor.Error(error.message), null);
                        return;
                    }
                    proceedConnectingMongodb(connectionUrl, connectionOptions, done);
                });
            } else {
                proceedConnectingMongodb(connectionUrl, connectionOptions, done);
            }
S
Sercan 已提交
84 85 86
        });
    },

S
Sercan 已提交
87 88
    'dropDB': function () {
        LOGGER.info('[dropDatabase]');
S
Sercan 已提交
89 90

        return Async.runSync(function (done) {
S
Sercan 已提交
91 92 93 94 95 96 97 98 99
            try {
                database.dropDatabase(function (err, result) {
                    done(err, result);
                });
            }
            catch (ex) {
                LOGGER.error('[dropDatabase]', ex);
                done(new Meteor.Error(ex.message), null);
            }
S
Sercan 已提交
100 101 102
        });
    },

S
Sercan 已提交
103 104
    'dropCollection': function (collectionName) {
        LOGGER.info('[dropCollection]', collectionName);
S
Sercan 已提交
105 106

        return Async.runSync(function (done) {
S
Sercan 已提交
107 108 109 110 111 112 113 114 115 116
            try {
                var collection = database.collection(collectionName);
                collection.drop(function (dropError) {
                    done(dropError, null);
                });
            }
            catch (ex) {
                LOGGER.error('[dropCollection]', ex);
                done(new Meteor.Error(ex.message), null);
            }
S
Sercan 已提交
117 118 119
        });
    },

S
Sercan 已提交
120
    'dropAllCollections': function () {
S
Sercan 已提交
121 122

        return Async.runSync(function (done) {
S
Sercan 已提交
123 124 125 126 127 128 129
            try {
                database.collections(function (err, collections) {
                    collections.forEach(function (collection) {
                        if (!collection.collectionName.startsWith('system')) {
                            collection.drop(function (dropError) {
                            });
                        }
S
Sercan 已提交
130
                    });
S
Sercan 已提交
131 132 133 134 135 136 137 138

                    done(err, {});
                });
            }
            catch (ex) {
                LOGGER.error('[dropAllCollections]', ex);
                done(new Meteor.Error(ex.message), null);
            }
S
Sercan 已提交
139 140 141
        });
    },

S
Sercan 已提交
142 143
    'createCollection': function (collectionName, options) {
        LOGGER.info('[createCollection]', collectionName, options);
S
Sercan 已提交
144 145

        return Async.runSync(function (done) {
S
Sercan 已提交
146 147
            try {
                database.createCollection(collectionName, options, function (err, result) {
S
Sercan 已提交
148
                    done(err, null);
S
Sercan 已提交
149 150 151 152 153 154
                });
            }
            catch (ex) {
                LOGGER.error('[createCollection]', ex);
                done(new Meteor.Error(ex.message), null);
            }
S
Sercan 已提交
155 156
        });
    }
S
Sercan 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
});

var proceedConnectingMongodb = function (connectionUrl, connectionOptions, done) {
    var mongodbApi = Meteor.npmRequire('mongodb').MongoClient;
    mongodbApi.connect(connectionUrl, connectionOptions, function (mainError, db) {
        if (mainError || db == null || db == undefined) {
            done(mainError, db);
            if (db) {
                db.close();
            }
            return;
        }
        try {
            database = db;
            database.listCollections().toArray(function (err, collections) {
                done(err, collections);
            });
        }
        catch (ex) {
            LOGGER.error('[connect]', ex);
            done(new Meteor.Error(ex.message), null);
            if (database) {
                database.close();
            }
        }
    });
S
#53  
Sercan 已提交
183
};