Skip to content

Commit

Permalink
Made RowKey optional, minor bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pofallon committed Apr 29, 2012
1 parent f198e98 commit 469df1a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/bluesky.js
Expand Up @@ -4,6 +4,6 @@
* MIT Licensed * MIT Licensed
*/ */


exports.version = '0.5.0alpha'; exports.version = '0.5.1';


exports.storage = require('./storage'); exports.storage = require('./storage');
54 changes: 45 additions & 9 deletions lib/table.js
Expand Up @@ -9,6 +9,7 @@ var TableQuery = require("azure/lib/services/table/tablequery");
var ISO8061Date = require("azure/lib/util/iso8061date"); var ISO8061Date = require("azure/lib/util/iso8061date");
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var skyutil = require('./util'); var skyutil = require('./util');
var uuid = require('node-uuid');


function Table (t,options) { function Table (t,options) {
this.options = options; this.options = options;
Expand Down Expand Up @@ -144,6 +145,12 @@ Table.prototype.insert = function(partition,row,elem,callback) {


var entity = {}; var entity = {};


if ((typeof row === "object") || (typeof row === "function")) {
callback = elem;
elem = row;
row = null;
}

switch (typeof elem) { switch (typeof elem) {
case "function": case "function":
callback = elem; callback = elem;
Expand All @@ -157,30 +164,30 @@ Table.prototype.insert = function(partition,row,elem,callback) {
callback = skyutil.safeCallback(callback); callback = skyutil.safeCallback(callback);


entity.PartitionKey = partition; entity.PartitionKey = partition;
entity.RowKey = row; entity.RowKey = row || getRowKey(this.options.rowKey,elem);


this.options.tableService.insertEntity(this.name, formatEntity(entity), function(err) { this.options.tableService.insertEntity(this.name, formatEntity(entity), function(err) {
if (err) { if (err) {
callback(err); callback(err);
} else { } else {
callback(null,true); callback(null,entity.RowKey);
} }
}); });


}; };


Table.prototype.select = function() { Table.prototype.select = function() {
this._query = TableQuery.select.apply(this._query, arguments).from(this.name); this._query = TableQuery.select.apply(this._query, Array.prototype.slice.call(arguments)).from(this.name);
return this; return this;
} }


Table.prototype.whereKeys = function() { Table.prototype.whereKeys = function() {
this._query.whereKeys.apply(this._query, arguments); this._query.whereKeys.apply(this._query, Array.prototype.slice.call(arguments));
return this; return this;
} }


Table.prototype.where = function() { Table.prototype.where = function() {
this._query.where.apply(this._query, arguments); this._query.where.apply(this._query, Array.prototype.slice.call(arguments));
return this; return this;
} }


Expand All @@ -207,17 +214,17 @@ Table.prototype.filter = function(criteria) {
} }


Table.prototype.and = function() { Table.prototype.and = function() {
this._query.and.apply(this._query, arguments); this._query.and.apply(this._query, Array.prototype.slice.call(arguments));
return this; return this;
} }


Table.prototype.or = function() { Table.prototype.or = function() {
this._query.or.apply(this._query, arguments); this._query.or.apply(this._query, Array.prototype.slice.call(arguments));
return this; return this;
} }


Table.prototype.top = function() { Table.prototype.top = function(count) {
this._query.top.apply(this._query, arguments); this._query.top.apply(this._query, Array.prototype.slice.call(arguments));
return this; return this;
} }


Expand Down Expand Up @@ -301,4 +308,33 @@ function formatEntity(entity) {


} }


function getRowKey(keyPref,elem) {

var rowKey = null;

switch(typeof keyPref) {
case 'function':
rowKey = keyPref(elem);
break;
case 'string':
switch(rowType) {
case 'timestamp':
rowKey = (new Date()).toISOString();
break;
case 'epoch':
rowKey = Date.now();
break;
case 'uuid':
rowKey = uuid.v4();
default:
rowKey = uuid.v4();
}
default:
rowKey = uuid.v4();
}

return(rowKey);

}

module.exports = Table; module.exports = Table;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"loremipstream" : "0.0.x" "loremipstream" : "0.0.x"
}, },
"engine": "node >= 0.6.0", "engine": "node >= 0.6.0",
"version": "0.5.0", "version": "0.5.1",
"files": [ "files": [
"index.js", "index.js",
"lib", "lib",
Expand Down
17 changes: 15 additions & 2 deletions test/storage-table.js
Expand Up @@ -226,15 +226,28 @@ module.exports = testCase({
}); });
}, },


/* tableTopRows: function(test) { tableTopRows: function(test) {
var t = storage.table(this.tableName); var t = storage.table(this.tableName);
t.top(2).rows(function(err,rows) { t.top(2).rows(function(err,rows) {
test.equals(err,null); test.equals(err,null);
test.notEqual(rows,null); test.notEqual(rows,null);
test.strictEqual(rows.length,2); test.strictEqual(rows.length,2);
test.done(); test.done();
}); });
}, */ },

tableInsertNoRowKey: function (test) {
var that = this;
var t = storage.table(this.tableName);
t.insert(this.partitionKey,{'test':'uuidtest'}, function(err, rowKey) {
test.equals(err,null);
test.notEqual(rowKey,null);
t.whereKeys(that.partitionKey,rowKey).rows(function(err, rows) {
test.equals(rows.length,1);
test.done();
});
});
},


tableDeleteRow: function (test) { tableDeleteRow: function (test) {
var t = storage.table(this.tableName); var t = storage.table(this.tableName);
Expand Down

0 comments on commit 469df1a

Please sign in to comment.