UNPKG

raven-shell

Version:

A command-line interface to RavenDB

494 lines (465 loc) 15.3 kB
#!/usr/bin/env node // Generated by CoffeeScript 1.3.3 (function() { var ArgumentParser, args, createDatastore, currentDatabaseString, database, defineCommands, evalString, file, keepOpen, parser, ravendb, repl, shell, startEvalREPL, startFileREPL, startInteractiveREPL, startREPL, store, useDatabase, version; repl = require('repl'); ravendb = require('ravendb'); ArgumentParser = require('argparse').ArgumentParser; version = '0.0.10'; createDatastore = function(r, url, databaseName) { r.context.db = ravendb(url, databaseName); return r.context.store = r.context.db.datastore; }; useDatabase = function(r, databaseName) { var _ref; if ((r != null ? (_ref = r.context) != null ? _ref.store : void 0 : void 0) == null) { throw new Error('The datastore must first be set'); } return r.context.db = ravendb(r.context.store.url, databaseName); }; currentDatabaseString = function(r) { return 'Using database "' + r.context.db.name + '" in datastore at: "' + r.context.store.url + '"'; }; defineCommands = function(r) { r.defineCommand('store', { help: 'Use the RavenDB datastore at a url ".store <url>"', action: function(url) { if (url != null) { createDatastore(r, url, r.context.db.name); } else { url = r.context.store.url; } console.log(currentDatabaseString(r)); return r.displayPrompt(); } }); r.defineCommand('use', { help: 'Use the database with the given name ".use <database-name>"', action: function(name) { if (name != null) { useDatabase(r, name); } else { name = r.context.db.name; } console.log(currentDatabaseString(r)); return r.displayPrompt(); } }); r.defineCommand('stats', { help: 'Show statistics for the current database', action: function() { try { return r.context.db.getStats(function(error, stats) { if (error != null) { console.error(error); r.displayPrompt(); return; } console.log(stats); r.context._ = stats; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('collections', { help: 'Show all collections in the current database', action: function() { try { return r.context.db.getCollections(function(error, collections) { var collection, _i, _len; if (error != null) { console.error(error); r.displayPrompt(); return; } if ((collections != null ? collections.length : void 0) != null) { for (_i = 0, _len = collections.length; _i < _len; _i++) { collection = collections[_i]; console.log(collection); } } else { console.log("No collections found."); } r.context._ = collections; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('create', { help: 'Save a document to a collection (e.g., .create CollectionName { id: "users/tony", firstName: "Tony" })', action: function(args) { var collection, match; try { match = /(\w+)\s+(.*)/.exec(args); if (match.length !== 3) { throw Error('Wrong number of arguments; see .help for .savedoc usage'); } collection = match[1]; eval('doc = ' + match[2]); return r.context.db.saveDocument(collection, doc, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('read', { help: 'Get a document given its id (e.g., .read users/tony)', action: function(args) { var id; try { if (args == null) { throw Error('Wrong number of arguments; see .help for more information'); } id = args; return r.context.db.getDocument(id, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('delete', { help: 'Delete a document given its id (e.g., .delete users/tony)', action: function(args) { var id; try { if (args == null) { throw Error('Wrong number of arguments; see .help for more information'); } id = args; return r.context.db.deleteDocument(id, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('deleteDocsByIndex', { help: 'Delete a set of documents using an index/query (e.g., .deleteDocumentsByIndex DocsByCollectionName MyCollection)', action: function(args) { var id; try { if (args == null) { throw Error('Wrong number of arguments; see .help for more information'); } args = args.split(' '); if (args.length < 1) { throw Error('Wrong number of arguments; see .help for more information'); } index = args[0]; query = args.length > 1 ? args[1] : null; return r.context.db.deleteDocuments(index, query, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('find', { help: 'Find documents ".find <JSON object> [start [count]]" (e.g., .find { firstName: "Tony" } 20 100)', action: function(args) { var count, match, start; try { if (args == null) { throw Error('Wrong number of arguments; see .help for .find usage'); } match = /(\{.*\})(\s+(\d+)(\s+(\d+))?)?/.exec(args); eval('doc = ' + match[1]); start = match[3] != null ? parseInt(match[3]) : null; count = match[5] != null ? parseInt(match[5]) : null; return r.context.db.find(doc, start, count, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('docs', { help: 'Retrieve documents in a collection (e.g., .doc Users)', action: function(args) { var collection, count, match, start; try { match = /(\w+)(\s+(\d+)(\s+(\d+))?)?/.exec(args); collection = start = count = null; if (match != null) { collection = match[1]; start = match[3] != null ? parseInt(match[3]) : null; count = match[5] != null ? parseInt(match[5]) : null; } return r.context.db.getDocsInCollection(collection, start, count, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('count', { help: 'Show the count of documents in a collection or in the database if left blank (e.g., .count Users)', action: function(args) { var collection; try { collection = args || null; return r.context.db.getDocumentCount(collection, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('createIndex', { help: "Create an index with a name, map, and optional reduce: .create-index foobar { map: 'from doc in docs.Albums\rwhere doc.Genre != null\rselect new { Genre = doc.Genre.Id }'}", action: function(args) { var index, match, name; try { name = index = null; match = /^(\w+)\s+(.*)$/.exec(args); if (match.length !== 3) { console.error('Unable to craete index: wrong number of arguments. Type ".help" to see usage'); return; } name = match[1]; index = match[2]; eval('createIndexIndex = ' + index); return r.context.db.createIndex(name, createIndexIndex['map'], createIndexIndex['reduce'], function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('deleteIndex', { help: 'Delete an given its name (e.g., .delete AlbumsByGenre)', action: function(args) { var name; try { if (args == null) { throw Error('Wrong number of arguments; see .help for more information'); } name = args; return r.context.db.deleteIndex(name, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); r.defineCommand('createDatabase', { help: 'Create a database tenant (e.g., .createDatabase Foobar)', action: function(args) { var name; try { if (args == null) { throw Error('Wrong number of arguments; see .help for more information'); } name = args; return r.context.store.createDatabase(name, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); return r.defineCommand('deleteDatabase', { help: 'Delete a database given its name (e.g., .deleteDatabase Foobar)', action: function(args) { var name; try { if (args == null) { throw Error('Wrong number of arguments; see .help for more information'); } name = args; return r.context.store.deleteDatabase(name, function(error, result) { if (error != null) { console.error(error); } if (result != null) { console.log(result); } r.context._ = result; return r.displayPrompt(); }); } catch (e) { console.error(e); return r.displayPrompt(); } } }); }; startREPL = function(store, databaseName) { var r; console.log("RavenDB shell version " + version); r = repl.start("> "); createDatastore(r, store, databaseName); defineCommands(r); console.log(currentDatabaseString(r)); r.displayPrompt(); return r; }; startInteractiveREPL = function(store, databaseName) { return startREPL(store, databaseName); }; startEvalREPL = function(string, store, databaseName) { var lines, r; r = startREPL(store, databaseName); lines = string.split('\n'); lines.forEach(function(line) { if (line != null) { return r.rli.write(line + '\n'); } }); return r; }; startFileREPL = function(filename, store, databaseName) { var r; r = startREPL(store, databaseName); r.rli.write(".load " + filename + "\n"); return r; }; parser = new ArgumentParser({ 'version': version, addHelp: true, description: 'RavenDB command line shell' }); parser.addArgument(['-f', '--file'], { help: 'load and execute a file of shell commands', dest: 'file' }); parser.addArgument(['-e', '--eval'], { help: 'evaluate a single string of shell commands', dest: 'eval' }); parser.addArgument(['-s', '--store'], { help: 'specify which data store to use (defaults to http://localhost:8080 if not specfied)', defaultValue: 'http://localhost:8080', dest: 'store' }); parser.addArgument(['-db', '--database'], { help: 'specify which database to use (defaults to "Default" if not specified)', defaultValue: 'Default', dest: 'database' }); parser.addArgument(['-ko', '--keep-open'], { help: 'keep the shell open when the passed in file or eval is done executing', action: 'storeTrue', defaultValue: false, dest: 'keepOpen' }); args = parser.parseArgs(); shell = null; keepOpen = args.keepOpen; evalString = args["eval"]; file = args.file; store = args.store; database = args.database; if (evalString != null) { shell = startEvalREPL(evalString, store, database); } else if (file != null) { shell = startFileREPL(file, store, database); } if (shell == null) { shell = startInteractiveREPL(store, database); } else { if (keepOpen == null) { shell.rli.close(); } } }).call(this);