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
126 changes: 13 additions & 113 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

'use strict';

var nodeutil = require('util');

/**
* @type module:common/connection
* @private
Expand Down Expand Up @@ -56,6 +58,12 @@ var Transaction = require('./transaction.js');
*/
var util = require('../common/util.js');

/**
* @type module:datastore/request
* @private
*/
var DatastoreRequest = require('./request');

/**
* Scopes for Google Datastore access.
* @const {array} SCOPES
Expand Down Expand Up @@ -101,11 +109,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 +176,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 +241,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 Down
Loading