| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #!/usr/bin/env node
- // Copyright 2015 The MemDB Authors.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- // implied. See the License for the specific language governing
- // permissions and limitations under the License. See the AUTHORS file
- // for names of contributors.
- 'use strict';
- var P = require('bluebird');
- var path = require('path');
- var minimist = require('minimist');
- var memdbLogger = require('memdb-logger');
- var util = require('util');
- var repl = require('repl');
- var helpStart = '\
- Usage: memdb [options]\n\
- Options:\n\
- -h, --host ip memdb shard ip\n\
- -p, --port port memdb shard port\n\
- -s, --shard shardId shardId\n\
- -c, --conf path Config file path\n\
- --help display help\n\n\
- Example: \n\
- memdb -h127.0.0.1 -p31017\n\
- memdb -s s1 -c memdb.conf.js\n';
- var helpCmd = '\n\
- Command Reference:\n\n\
- db.find(collection, idOrQuery)\n\
- db.insert(collection, docs)\n\
- db.update(collection, idOrQuery, modifier)\n\
- db.remove(collection, idOrQuery)\n\
- db.commit()\n\
- db.rollback()\n\
- _ last result\n';
- var startRepl = function(conn){
- var deferred = P.defer();
- var server = repl.start({
- prompt: 'memdb> ',
- terminal: true,
- input: process.stdin,
- output: process.stdout,
- useGlobal : false,
- ignoreUndefined : true,
- });
- var originEval = server.eval; //jshint ignore:line
- server.eval = function(cmd, context, filename, cb){ //jshint ignore:line
- if(cmd.trim() === 'help'){
- console.log(helpCmd);
- return cb();
- }
- originEval.call(server, cmd, context, filename, function(err, ret){
- if(err){
- console.log(helpCmd);
- return cb(err, ret);
- }
- if(P.is(ret)){
- ret.nodeify(function(err, ret){
- if(err){
- err = err.message + '\n (Changes are rolled back)';
- }
- cb(err, ret);
- });
- }
- else{
- cb(err, ret);
- }
- });
- };
- server.on('exit', function(){
- conn.removeAllListeners('close');
- return conn.close()
- .then(function(){
- console.log('Bye');
- deferred.resolve();
- }, function(e){
- deferred.reject(e);
- });
- });
- server.context.db = conn;
- conn.on('error', function(e){
- console.error(e.stack);
- });
- conn.on('close', function(){
- console.log('Connection lost');
- deferred.resolve();
- });
- return deferred.promise;
- };
- if (require.main === module) {
- // turn off logger by default
- memdbLogger.setGlobalLogLevel(memdbLogger.levels.OFF);
- console.log('MemDB shell');
- var argv = minimist(process.argv.slice(2));
- if(argv.help){
- console.log(helpStart);
- process.exit(0);
- }
- var connectOpts = {idleTimeout : 0};
- var shardId = argv.shard || argv.s;
- var confPath = argv.conf || argv.c;
- if(confPath){
- confPath = path.resolve(confPath);
- }
- if(shardId){
- var config = require('../app/config');
- config.init(confPath);
- var shardConfig = config.shardConfig(shardId);
- connectOpts.host = shardConfig.host;
- connectOpts.port = shardConfig.port;
- }
- else{
- if(confPath){
- throw new Error('shard not specified');
- }
- connectOpts.host = argv.host || argv.h || '127.0.0.1';
- connectOpts.port = argv.port || argv.p || 31017;
- }
- var memdb = require('../lib');
- var conn = null;
- P.try(function(){
- return memdb.connect(connectOpts);
- })
- .then(function(ret){
- console.log('connected to %s:%s', connectOpts.host, connectOpts.port);
- return startRepl(ret);
- }, function(e){
- console.error('failed to connect to %s:%s', connectOpts.host, connectOpts.port);
- process.exit(1);
- })
- .then(function(){
- process.exit(0);
- })
- .catch(function(e){
- console.error(e.stack);
- process.exit(1);
- });
- }
|