Sign In

raven-shell

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

raven-shell - npm Package Compare versions

Comparing version
0.0.9
to
0.0.10
Cakefile

Sorry, the diff of this file is not supported yet

+460
// 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('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) {
try {
return r.context.db.getDocumentCount(args, 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);
#!/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('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) {
try {
return r.context.db.getDocumentCount(args, 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);
#!/usr/bin/env coffee
repl = require('repl')
ravendb = require('ravendb')
ArgumentParser = require('argparse').ArgumentParser
version = '0.0.10' # Keep in sync with package.json
createDatastore = (r, url, databaseName) ->
r.context.db = ravendb(url, databaseName)
r.context.store = r.context.db.datastore
useDatabase = (r, databaseName) ->
throw new Error('The datastore must first be set') unless r?.context?.store?
r.context.db = ravendb(r.context.store.url, databaseName)
currentDatabaseString = (r) ->
'Using database "' + r.context.db.name + '" in datastore at: "' + r.context.store.url + '"'
defineCommands = (r) ->
r.defineCommand 'store',
help: 'Use the RavenDB datastore at a url ".store <url>"'
action: (url) ->
if url?
createDatastore(r, url, r.context.db.name) # TODO: IS THIS A GOOD IDEA, TO USE THE SAME DB NAME?
else
url = r.context.store.url
console.log currentDatabaseString(r)
r.displayPrompt()
r.defineCommand 'use',
help: 'Use the database with the given name ".use <database-name>"'
action: (name) ->
if name?
useDatabase(r, name)
else
name = r.context.db.name
console.log currentDatabaseString(r)
r.displayPrompt()
r.defineCommand 'stats',
help: 'Show statistics for the current database'
action: ->
try
r.context.db.getStats (error, stats) ->
if error?
console.error error
r.displayPrompt()
return
console.log stats
r.context._ = stats
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'collections',
help: 'Show all collections in the current database'
action: ->
try
r.context.db.getCollections (error, collections) ->
if error?
console.error error
r.displayPrompt()
return
if collections?.length?
console.log collection for collection in collections
else
console.log "No collections found."
r.context._ = collections
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'create',
help: 'Save a document to a collection (e.g., .create CollectionName { id: "users/tony", firstName: "Tony" })'
action: (args) ->
try
match = /(\w+)\s+(.*)/.exec(args)
throw Error('Wrong number of arguments; see .help for .savedoc usage') if match.length != 3
collection = match[1]
eval('doc = ' + match[2])
r.context.db.saveDocument collection, doc, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'read',
help: 'Get a document given its id (e.g., .read users/tony)',
action: (args) ->
try
throw Error('Wrong number of arguments; see .help for more information') unless args?
id = args
r.context.db.getDocument id, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'delete',
help: 'Delete a document given its id (e.g., .delete users/tony)'
action: (args) ->
try
throw Error('Wrong number of arguments; see .help for more information') unless args?
id = args
r.context.db.deleteDocument id, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'find',
help: 'Find documents ".find <JSON object> [start [count]]" (e.g., .find { firstName: "Tony" } 20 100)'
action: (args) ->
try
throw Error('Wrong number of arguments; see .help for .find usage') unless args?
# Parse args: { attr: value } [start [count]]
# .find { some: 'thing'} 20 100
# ^-------1------^^--2--^
# 3 5
# ^4-^
# match[0] = "{ some: 'thing'} 20 100"
# match[1] = "{ some: 'thing'}"
# match[2] = " 20 100"
# match[3] = "20"
# match[4] = " 100"
# match[5] = "100"
match = /(\{.*\})(\s+(\d+)(\s+(\d+))?)?/.exec(args)
eval('doc = ' + match[1])
start = if match[3]? then parseInt(match[3]) else null
count = if match[5]? then parseInt(match[5]) else null
r.context.db.find doc, start, count, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'docs',
help: 'Retrieve documents in a collection (e.g., .doc Users)'
action: (args) ->
try
# Parse args: [collection [start [count]]]
# .count Users 50 100
# ^-1-^^--2--^
# 3 5
# ^4-^
# match[0] = "Users 50 100"
# match[1] = "Users"
# match[2] = " 50 100"
# match[3] = "50"
# match[4] = " 100"
# match[5] = "100"
match = /(\w+)(\s+(\d+)(\s+(\d+))?)?/.exec(args)
collection = start = count = null
if match?
collection = match[1]
start = if match[3]? then parseInt(match[3]) else null
count = if match[5]? then parseInt(match[5]) else null
r.context.db.getDocsInCollection collection, start, count, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
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: (args) ->
try
r.context.db.getDocumentCount args, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
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: (args) ->
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 )
r.context.db.createIndex name, createIndexIndex['map'], createIndexIndex['reduce'], (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'deleteIndex',
help: 'Delete an given its name (e.g., .delete AlbumsByGenre)'
action: (args) ->
try
throw Error('Wrong number of arguments; see .help for more information') unless args?
name = args
r.context.db.deleteIndex name, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'createDatabase',
help: 'Create a database tenant (e.g., .createDatabase Foobar)'
action: (args) ->
try
throw Error('Wrong number of arguments; see .help for more information') unless args?
name = args
r.context.store.createDatabase name, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
r.defineCommand 'deleteDatabase',
help: 'Delete a database given its name (e.g., .deleteDatabase Foobar)'
action: (args) ->
try
throw Error('Wrong number of arguments; see .help for more information') unless args?
name = args
r.context.store.deleteDatabase name, (error, result) ->
console.error error if error?
console.log result if result?
r.context._ = result
r.displayPrompt()
catch e
console.error e
r.displayPrompt()
startREPL = (store, databaseName) ->
console.log "RavenDB shell version #{version}"
r = repl.start "> "
createDatastore(r, store, databaseName)
defineCommands(r)
console.log currentDatabaseString(r)
r.displayPrompt()
r
startInteractiveREPL = (store, databaseName) ->
startREPL(store, databaseName)
startEvalREPL = (string, store, databaseName) ->
r = startREPL(store, databaseName)
lines = string.split('\n')
lines.forEach (line) ->
if line?
r.rli.write(line + '\n')
r
startFileREPL = (filename, store, databaseName) ->
r = startREPL(store, databaseName)
r.rli.write ".load #{filename}\n"
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?
shell = startEvalREPL(evalString, store, database)
else if file?
shell = startFileREPL(file, store, database)
unless shell?
shell = startInteractiveREPL(store, database)
else
unless keepOpen?
shell.rli.close()
+7
-5
{
"name": "raven-shell",
"preferGlobal": "true",
"version": "0.0.9",
"version": "0.0.10",
"author": "Tony Heupel <tonyheupel@gmail.com>",

@@ -12,8 +12,9 @@ "description": "A command-line interface to RavenDB",

"bin": {
"raven": "shell.js"
"raven": "./raven-shell.js"
},
"scripts": {
"start": "raven"
"start": "raven",
"build": "cake build"
},
"main": "./shell",
"main": "./lib/shell",
"repository": {

@@ -25,3 +26,3 @@ "type": "git",

"cli",
"ravendb"
"ravendb"
],

@@ -34,2 +35,3 @@ "dependencies" : {

"devDependencies": {
"coffee-script": ">=1.3.3"
},

@@ -36,0 +38,0 @@ "bundleDependencies": [

#!/usr/bin/env node
var repl = require('repl')
, ravendb = require('ravendb')
, ArgumentParser = require('argparse').ArgumentParser
var version = '0.0.8' // Keep in sync with package.json
var createDatastore = function(r, url, databaseName) {
r.context.db = ravendb(url, databaseName)
r.context.store = r.context.db.datastore
}
var useDatabase = function(r, databaseName) {
if (!r.context.store) throw new Error('The datastore must first be set')
r.context.db = ravendb(r.context.store.url, databaseName)
}
var currentDatabaseString = function(r) {
return 'Using database "' + r.context.db.name + '" in datastore at: "' + r.context.store.url + '"'
}
var defineCommands = function(r) {
r.defineCommand('store', {
help: 'Use the RavenDB datastore at a url ".store <url>"',
action: function(url) {
if (!url) url = r.context.store.url
else createDatastore(r, url, r.context.db.name) // TODO: IS THIS A GOOD IDEA, TO USE THE SAME DB NAME?
console.log(currentDatabaseString(r))
r.displayPrompt()
}
})
r.defineCommand('use', {
help: 'Use the database with the given name ".use <database-name>"',
action: function(name) {
if (!name) name = r.context.db.name
else useDatabase(r, name)
console.log(currentDatabaseString(r))
r.displayPrompt()
}
})
r.defineCommand('stats', {
help: 'Show statistics for the current database',
action: function() {
try {
r.context.db.getStats(function(error, stats) {
if (error) {
console.error(error)
r.displayPrompt()
return
}
console.log(stats)
r.context._ = stats
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('collections', {
help: 'Show all collections in the current database',
action: function() {
try {
r.context.db.getCollections(function(error, collections) {
if (error) {
console.error(error)
r.displayPrompt()
return
}
if (!collections) console.log("No collections found.")
else {
for(var i=0; i < collections.length; i++) {
console.log(collections[i])
}
}
r.context._ = collections
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('create', {
help: 'Save a document to a collection (e.g., .create CollectionName { id: "users/tony", firstName: "Tony" })',
action: function(args) {
try {
var match = /(\w+)\s+(.*)/.exec(args)
if (match.length != 3) throw Error('Wrong number of arguments; see .help for .savedoc usage')
var collection = match[1]
eval('var doc = ' + match[2])
r.context.db.saveDocument(collection, doc, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('read', {
help: 'Get a document given its id (e.g., .read users/tony)',
action: function(args) {
try {
if (!args) throw Error('Wrong number of arguments; see .help for more information')
var id = args
r.context.db.getDocument(id, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('delete', {
help: 'Delete a document given its id (e.g., .delete users/tony)',
action: function(args) {
try {
if (!args) throw Error('Wrong number of arguments; see .help for more information')
var id = args
r.context.db.deleteDocument(id, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('find', {
help: 'Find documents ".find <JSON object> [start [count]]" (e.g., .find { firstName: "Tony" } 20 100)',
action: function(args) {
try {
if (!args) throw Error('Wrong number of arguments; see .help for .find usage')
// Parse args: { attr: value } [start [count]]
// .find { some: 'thing'} 20 100
// ^-------1------^^--2--^
// 3 5
// ^4-^
// match[0] = "{ some: 'thing'} 20 100"
// match[1] = "{ some: 'thing'}"
// match[2] = " 20 100"
// match[3] = "20"
// match[4] = " 100"
// match[5] = "100"
var match = /(\{.*\})(\s+(\d+)(\s+(\d+))?)?/.exec(args)
eval('var doc = ' + match[1])
var start = match[3] ? parseInt(match[3]) : null
var count = match[5] ? parseInt(match[5]) : null
r.context.db.find(doc, start, count, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('docs', {
help: 'Retrieve documents in a collection (e.g., .doc Users)',
action: function(args) {
try {
// Parse args: [collection [start [count]]]
// .count Users 50 100
// ^-1-^^--2--^
// 3 5
// ^4-^
// match[0] = "Users 50 100"
// match[1] = "Users"
// match[2] = " 50 100"
// match[3] = "50"
// match[4] = " 100"
// match[5] = "100"
var match = /(\w+)(\s+(\d+)(\s+(\d+))?)?/.exec(args)
var collection = start = count = null
if (match) {
collection = match[1]
start = match[3] ? parseInt(match[3]) : null
count = match[5] ? parseInt(match[5]) : null
}
r.context.db.getDocsInCollection(collection, start, count, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
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) {
try {
r.context.db.getDocumentCount(args, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
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) {
try {
var name, index = null
var 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 ('var createIndexIndex = ' + index )
r.context.db.createIndex(name,
createIndexIndex['map'],
createIndexIndex['reduce'],
function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch(e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('deleteIndex', {
help: 'Delete an given its name (e.g., .delete AlbumsByGenre)',
action: function(args) {
try {
if (!args) throw Error('Wrong number of arguments; see .help for more information')
var name = args
r.context.db.deleteIndex(name, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('createDatabase', {
help: 'Create a database tenant (e.g., .createDatabase Foobar)',
action: function(args) {
try {
if (!args) throw Error('Wrong number of arguments; see .help for more information')
var name = args
r.context.store.createDatabase(name, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
r.defineCommand('deleteDatabase', {
help: 'Delete a database given its name (e.g., .deleteDatabase Foobar)',
action: function(args) {
try {
if (!args) throw Error('Wrong number of arguments; see .help for more information')
var name = args
r.context.store.deleteDatabase(name, function(error, result) {
if (error) console.error(error)
if (result) console.log(result)
r.context._ = result
r.displayPrompt()
})
} catch (e) {
console.error(e)
r.displayPrompt()
}
}
})
}
var startREPL = function(store, databaseName) {
console.log('RavenDB shell version ' + version)
var r = repl.start("> ")
createDatastore(r, store, databaseName)
defineCommands(r)
console.log(currentDatabaseString(r))
r.displayPrompt()
return r
}
var startInteractiveREPL = function(store, databaseName) {
return startREPL(store, databaseName)
}
var startEvalREPL = function(string, store, databaseName) {
var r = startREPL(store, databaseName)
var lines = string.split('\n')
lines.forEach(function(line) {
if (line) {
r.rli.write(line + '\n')
}
})
return r
}
var startFileREPL = function(filename, store, databaseName) {
var r = startREPL(store, databaseName)
r.rli.write('.load ' + filename + '\n')
return r
}
var 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'
}
)
var args = parser.parseArgs();
var shell
var keepOpen = args.keepOpen
var evalString = args.eval
var file = args.file
var store = args.store
var database = args.database
if (evalString) {
shell = startEvalREPL(evalString, store, database)
} else if (file) {
shell = startFileREPL(file, store, database)
}
if (!shell) {
shell = startInteractiveREPL(store, database)
} else {
if (!keepOpen) {
shell.rli.close()
}
}