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: throw if an unrecognized commit method is given #885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
109 changes: 52 additions & 57 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,12 @@ DatastoreRequest.prototype.insert = function(entities, callback) {
*
* @borrows {module:datastore/transaction#save} as save
*
* @throws {Error} If an unrecognized method is provided.
*
* @param {object|object[]} entities - Datastore key object(s).
* @param {Key} entities.key - Datastore key object.
* @param {string=} entities.method - Optional method to explicity use for save.
* The choices include 'insert', 'update', 'upsert' and 'auto_insert_id'.
* The choices include 'insert', 'update', 'upsert' and 'insert_auto_id'.
* @param {object|object[]} entities.data - Data to save with the provided key.
* If you provide an array of objects, you must use the explicit syntax:
* `name` for the name of the property and `value` for its value. You may
Expand Down Expand Up @@ -382,75 +384,68 @@ DatastoreRequest.prototype.save = function(entities, callback) {

var insertIndexes = [];

var req = {
mutation: entities.reduce(function(acc, entityObject, index) {
entityObject = extend(true, {}, entityObject);
var mutation = {
insert: [],
update: [],
upsert: [],
insert_auto_id: []

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

};

var ent = {};
var method = entityObject.method;
delete entityObject.method;
// Iterate over the entity objects, build a proto from all keys and values,
// then place in the correct mutation array (insert, update, etc).
entities.forEach(function(entityObject, index) {
entityObject = extend(true, {}, entityObject);

if (Array.isArray(entityObject.data)) {
ent.property = entityObject.data.map(function(data) {
data.value = entity.valueToProperty(data.value);
var entityProto = {};
var method = entityObject.method;

if (is.boolean(data.excludeFromIndexes)) {
var indexed = !data.excludeFromIndexes;
if (is.array(entityObject.data)) {
entityProto.property = entityObject.data.map(function(data) {
data.value = entity.valueToProperty(data.value);

if (is.array(data.value.list_value)) {
data.value.list_value =
data.value.list_value.map(propAssign('indexed', indexed));
} else {
data.value.indexed = indexed;
}
if (is.boolean(data.excludeFromIndexes)) {
var indexed = !data.excludeFromIndexes;

delete data.excludeFromIndexes;
if (is.array(data.value.list_value)) {
data.value.list_value =
data.value.list_value.map(propAssign('indexed', indexed));
} else {
data.value.indexed = indexed;
}

return data;
});
} else {
ent = entity.entityToEntityProto(entityObject.data);
}
delete data.excludeFromIndexes;
}

ent.key = entity.keyToKeyProto(entityObject.key);
return data;
});
} else {
entityProto = entity.entityToEntityProto(entityObject.data);
}

if (method) {
switch (method) {
case 'insert': {
acc.insert.push(ent);
break;
}
case 'update': {
acc.update.push(ent);
break;
}
case 'upsert': {
acc.upsert.push(ent);
break;
}
case 'insert_auto_id': {
insertIndexes.push(index);
acc.insert_auto_id.push(ent);
break;
}
}
} else {
if (entity.isKeyComplete(entityObject.key)) {
acc.upsert.push(ent);
} else {
entityProto.key = entity.keyToKeyProto(entityObject.key);

if (method) {
if (mutation[method]) {
mutation[method].push(entityProto);

if (method === 'insert_auto_id') {
insertIndexes.push(index);
acc.insert_auto_id.push(ent);
}
} else {
throw new Error('Method ' + method + ' not recognized.');
}
} else {
if (entity.isKeyComplete(entityObject.key)) {
mutation.upsert.push(entityProto);
} else {
insertIndexes.push(index);
mutation.insert_auto_id.push(entityProto);
}
}
});

return acc;
}, {
insert: [],
update: [],
upsert: [],
insert_auto_id: []
})
var req = {
mutation: mutation
};

if (this.id) {
Expand Down
12 changes: 12 additions & 0 deletions test/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ describe('Request', function() {
], done);
});

it('should throw if a given method is not recognized', function() {
assert.throws(function() {
request.save({
key: key,
method: 'auto_insert_id',
data: {
k: 'v'
}
}, assert.ifError);
}, /Method auto_insert_id not recognized/);
});

it('should not alter the provided data object', function(done) {
var entities = [
{
Expand Down