Skip to content

Commit

Permalink
[api] Now able to work with other user's dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Aug 13, 2012
1 parent cb991fe commit 04d4ee2
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions node.js/lib/client/databases.js
Expand Up @@ -23,14 +23,22 @@ var Databases = exports.Databases = function (options) {
util.inherits(Databases, Client);

//
// ### function create (databaseType, databaseName, callback)
// ### function create (username, databaseType, databaseName, callback)
// #### @username {string} Username
// #### @databaseType {string} Type of database to create, valid values: redis, couch, mongo
// #### @databaseName {string} Name of the database to create
// #### @callback {function} Continuation to pass control to when complete
// Provisions a database for the user
//
Databases.prototype.create = function (databaseType, databaseName, callback) {
this.addons.client.users.databases.create(this.options.get('username'), {
Databases.prototype.create = function (username, databaseType, databaseName, callback) {
if (arguments.length == 3) {
callback = databaseName;
databaseName = databaseType;
databaseType = username;
username = this.options.get('username');
}

this.addons.client.users.databases.create(username, {
type: databaseType,
name: databaseName
}, function(err, res, body){
Expand All @@ -39,36 +47,56 @@ Databases.prototype.create = function (databaseType, databaseName, callback) {
};

//
// ### function get (databaseName, callback)
// ### function get (username, databaseName, callback)
// #### @username {String} Username
// #### @databaseName {string} Name of the database to get
// #### @callback {function} Continuation to pass control to when complete
// Gets the metadata for the specified database
//
Databases.prototype.get = function (databaseName, callback) {
this.addons.client.users.databases.get(this.options.get('username'), databaseName, function(err, res, body){
Databases.prototype.get = function (username, databaseName, callback) {
if (arguments.length == 2) {
callback = databaseName;
databaseName = username;
username = this.options.get('username');
}

this.addons.client.users.databases.get(username, databaseName, function(err, res, body){
callback(err, body);
});
};

//
// ### function list (callback)
// ### function list (username, callback)
// #### @username {String} Username
// #### @callback {function} Continuation to pass control to when complete
// Gets the list of databases assigned to the user
//
Databases.prototype.list = function (callback) {
this.addons.client.users.databases(this.options.get('username'), function(err, res, body){
Databases.prototype.list = function (username, callback) {
if (arguments.length == 1) {
callback = username;
username = this.options.get('username');
}

this.addons.client.users.databases(username, function(err, res, body){
callback(err, body);
});
};

//
// ### function destroy (databaseName, callback)
// ### function destroy (username, databaseName, callback)
// #### @username {String} Username
// #### @databaseName {string} Name of the database to delete
// #### @callback {function} Continuation to pass control to when complete
// Deprovisions specified database
//
Databases.prototype.destroy = function (databaseName, callback) {
this.addons.client.users.databases.destroy(this.options.get('username'), databaseName, function(err, res, body){
Databases.prototype.destroy = function (username, databaseName, callback) {
if (arguments.length == 2) {
callback = databaseName;
databaseName = username;
username = this.options.get('username');
}

this.addons.client.users.databases.destroy(username, databaseName, function(err, res, body){
callback(err, body);
});
};

1 comment on commit 04d4ee2

@Marak
Copy link
Contributor

@Marak Marak commented on 04d4ee2 Aug 13, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be using utile.args instead of custom argument parsing. https://github.com/flatiron/utile/blob/master/lib/args.js

Checking length of arguments with non-strict equals is not the best idea.

Please sign in to comment.