Skip to content

Commit

Permalink
Merge pull request #393 from stephenplusplus/spp--datastore-declarati…
Browse files Browse the repository at this point in the history
…ve-methods

datastore: add declarative methods - fixes #392
  • Loading branch information
ryanseys committed Feb 16, 2015
2 parents f43a2b7 + ec84bb2 commit 217c753
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 6 deletions.
16 changes: 16 additions & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ function getType(value) {
return Object.prototype.toString.call(value).match(/\s(\w+)\]/)[1];
}

/**
* Assign a value to a property in an Array iterator.
*
* @param {string} prop - Name of the property to assign to the object.
* @param {*} value - Value of the property.
* @return {function}
*/
function propAssign(prop, value) {
return function(obj) {
obj[prop] = value;
return obj;
};
}

module.exports.propAssign = propAssign;

/**
* Check if an object is of the given type.
*
Expand Down
32 changes: 26 additions & 6 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ DatastoreRequest.prototype.get = function(keys, callback) {
});
};

/**
* Maps to {module:datastore/dataset#save}, forcing the method to be `insert`.
*/
DatastoreRequest.prototype.insert = function(entities, callback) {
entities = util.arrayize(entities).map(util.propAssign('method', 'insert'));
this.save(entities, callback);
};

/**
* Insert or update the specified object(s). If a key is incomplete, its
* associated object is inserted and the original Key object is updated to
Expand Down Expand Up @@ -243,8 +251,7 @@ DatastoreRequest.prototype.get = function(keys, callback) {
* ], function(err) {});
*/
DatastoreRequest.prototype.save = function(entities, callback) {
var isMultipleRequest = Array.isArray(entities);
entities = isMultipleRequest ? entities : [entities];
entities = util.arrayize(entities);

var insertIndexes = [];

Expand Down Expand Up @@ -346,14 +353,11 @@ DatastoreRequest.prototype.save = function(entities, callback) {
* ], function(err) {});
*/
DatastoreRequest.prototype.delete = function(keys, callback) {
var isMultipleRequest = Array.isArray(keys);
keys = isMultipleRequest ? keys : [keys];

callback = callback || util.noop;

var req = {
mutation: {
delete: keys.map(entity.keyToKeyProto)
delete: util.arrayize(keys).map(entity.keyToKeyProto)
}
};

Expand Down Expand Up @@ -527,6 +531,22 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
});
};

/**
* Maps to {module:datastore/dataset#save}, forcing the method to be `update`.
*/
DatastoreRequest.prototype.update = function(entities, callback) {
entities = util.arrayize(entities).map(util.propAssign('method', 'update'));
this.save(entities, callback);
};

/**
* Maps to {module:datastore/dataset#save}, forcing the method to be `upsert`.
*/
DatastoreRequest.prototype.upsert = function(entities, callback) {
entities = util.arrayize(entities).map(util.propAssign('method', 'upsert'));
this.save(entities, callback);
};

/**
* Make a request to the API endpoint. Properties to indicate a transactional or
* non-transactional operation are added automatically.
Expand Down
8 changes: 8 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,12 @@ describe('common/util', function() {
});
});
});

describe('propAssign', function() {
it('should assign a property and value to an object', function() {
var obj = {};
util.propAssign('prop', 'value')(obj);
assert.equal(obj.prop, 'value');
});
});
});
60 changes: 60 additions & 0 deletions test/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ describe('Request', function() {
});
});

describe('insert', function() {
it('should pass the correct arguments to save', function(done) {
request.save = function(entities, callback) {
assert.deepEqual(entities, [{
key: {
namespace: 'ns',
path: ['Company'],
},
data: {},
method: 'insert'
}]);

callback();
};

var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
request.insert({ key: key, data: {} }, done);
});
});

describe('save', function() {
it('should save with incomplete key', function(done) {
request.makeReq_ = function(method, req, callback) {
Expand Down Expand Up @@ -461,6 +481,46 @@ describe('Request', function() {
});
});

describe('update', function() {
it('should pass the correct arguments to save', function(done) {
request.save = function(entities, callback) {
assert.deepEqual(entities, [{
key: {
namespace: 'ns',
path: ['Company'],
},
data: {},
method: 'update'
}]);

callback();
};

var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
request.update({ key: key, data: {} }, done);
});
});

describe('upsert', function() {
it('should pass the correct arguments to save', function(done) {
request.save = function(entities, callback) {
assert.deepEqual(entities, [{
key: {
namespace: 'ns',
path: ['Company'],
},
data: {},
method: 'upsert'
}]);

callback();
};

var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
request.upsert({ key: key, data: {} }, done);
});
});

describe('allocateIds', function() {
it('should produce proper allocate IDs req protos', function(done) {
request.makeReq_ = function(method, req, callback) {
Expand Down

0 comments on commit 217c753

Please sign in to comment.