Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: add declarative methods - fixes #392 #393

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ DatastoreRequest.prototype.get = function(keys, callback) {
});
};

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

This comment was marked as spam.

this.save(isArray ? entities : entities[0], callback);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

};

/**
* 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 @@ -527,6 +536,24 @@ 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) {
var isArray = util.is(entities, 'array');
entities = util.arrayize(entities).map(util.propAssign('method', 'update'));
this.save(isArray ? entities : entities[0], callback);
};

/**
* Maps to {module:datastore/dataset#save}, forcing the method to be `upsert`.
*/
DatastoreRequest.prototype.upsert = function(entities, callback) {
var isArray = util.is(entities, 'array');
entities = util.arrayize(entities).map(util.propAssign('method', 'upsert'));
this.save(isArray ? entities : entities[0], 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