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

Refactor Transaction and Dataset into DatastoreRequest #231

Closed
wants to merge 9 commits into from
127 changes: 8 additions & 119 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var Transaction = require('./transaction.js');
* @private
*/
var util = require('../common/util.js');
var nodeutil = require('util');

/**
* Scopes for Google Datastore access.
Expand All @@ -66,6 +67,8 @@ var SCOPES = [
'https://www.googleapis.com/auth/userinfo.email'
];

var DatastoreRequest = require('./datastore_request.js');

/**
* Interact with a dataset from the
* [Google Cloud Datastore]{@link https://developers.google.com/datastore/}.
Expand Down Expand Up @@ -101,11 +104,13 @@ function Dataset(options) {
keyFilename: options.keyFilename,
scopes: SCOPES
});

this.projectId = options.projectId;
this.namespace = options.namespace;
this.transaction = this.createTransaction_();
}

nodeutil.inherits(Dataset, DatastoreRequest);

/**
* Helper to create a Key object, scoped to the dataset's namespace by default.
*
Expand Down Expand Up @@ -166,117 +171,6 @@ Dataset.prototype.createQuery = function(namespace, kinds) {
return new Query(namespace, util.arrayize(kinds));
};

/**
* Retrieve the entities identified with the specified key(s) in the current
* transaction. Get operations require a valid key to retrieve the
* key-identified entity from Datastore.
*
* @borrows {module:datastore/transaction#get} as get
*
* @param {Key|Key[]} key - Datastore key object(s).
* @param {function} callback - The callback function.
*
* @example
* dataset.get([
* dataset.key(['Company', 123]),
* dataset.key(['Product', 'Computer'])
* ], function(err, entities) {});
*/
Dataset.prototype.get = function(key, callback) {
this.transaction.get(key, callback);
};

/**
* Insert or update the specified object(s) in the current transaction. If a
* key is incomplete, its associated object is inserted and its generated
* identifier is returned to the callback.
*
* @borrows {module:datastore/transaction#save} as save
*
* @param {object|object[]} entities - Datastore key object(s).
* @param {Key} entities.key - Datastore key object.
* @param {object} entities.data - Data to save with the provided key.
* @param {function} callback - The callback function.
*
* @example
* // Save a single entity.
* dataset.save({
* key: dataset.key('Company'),
* data: {
* rating: '10'
* }
* }, function(err, key) {
* // Because we gave an incomplete key as an argument, `key` will be
* // populated with the complete, generated key.
* });
*
* // Save multiple entities at once.
* dataset.save([
* {
* key: dataset.key(['Company', 123]),
* data: {
* HQ: 'Dallas, TX'
* }
* },
* {
* key: dataset.key(['Product', 'Computer']),
* data: {
* vendor: 'Dell'
* }
* }
* ], function(err, keys) {});
*/
Dataset.prototype.save = function(key, obj, callback) {
this.transaction.save(key, obj, callback);
};

/**
* Delete all entities identified with the specified key(s) in the current
* transaction.
*
* @param {Key|Key[]} key - Datastore key object(s).
* @param {function} callback - The callback function.
*
* @borrows {module:datastore/transaction#delete} as delete
*
* @example
* // Delete a single entity.
* dataset.delete(dataset.key(['Company', 123]), function(err) {});
*
* // Delete multiple entities at once.
* dataset.delete([
* dataset.key(['Company', 123]),
* dataset.key(['Product', 'Computer'])
* ], function(err) {});
*/
Dataset.prototype.delete = function(key, callback) {
this.transaction.delete(key, callback);
};

/**
* Datastore allows you to query entities by kind, filter them by property
* filters, and sort them by a property name. Projection and pagination are also
* supported. If more results are available, a query to retrieve the next page
* is provided to the callback function.
*
* @borrows {module:datastore/transaction#runQuery} as runQuery
*
* @param {module:datastore/query} query - Query object.
* @param {function} callback - The callback function.
*
* @example
* // Retrieve 5 companies.
* dataset.runQuery(queryObject, function(err, entities, nextQuery) {
* // `nextQuery` is not null if there are more results.
* if (nextQuery) {
* dataset.runQuery(nextQuery, function(err, entities, nextQuery) {});
* }
* });
*/
Dataset.prototype.runQuery = function(q, callback) {
this.transaction.runQuery(q, callback);
};

/**
* Run a function in the context of a new transaction. Transactions allow you to
* perform multiple operations, committing your changes atomically.
Expand Down Expand Up @@ -342,7 +236,8 @@ Dataset.prototype.allocateIds = function(incompleteKey, n, callback) {
for (var i = 0; i < n; i++) {
incompleteKeys.push(entity.keyToKeyProto(incompleteKey));
}
this.transaction.makeReq(

this.createRequest(
'allocateIds',
new pb.AllocateIdsRequest({ key: incompleteKeys }),
pb.AllocateIdsResponse, function(err, resp) {
Expand All @@ -358,12 +253,6 @@ Dataset.prototype.allocateIds = function(incompleteKey, n, callback) {
});
};

/**
* Create a new Transaction object using the existing connection and dataset.
*
* @return {module:datastore/transaction}
* @private
*/
Dataset.prototype.createTransaction_ = function() {
return new Transaction(this.connection, this.projectId);
};
Expand Down
Loading