Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
136 changes: 21 additions & 115 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.js');

/**
* Scopes for Google Datastore access.
* @const {array} SCOPES
Expand All @@ -73,7 +81,7 @@ var SCOPES = [
* @constructor
* @alias module:datastore/dataset
*
* @param {object=} options
* @param {object=} options options object

This comment was marked as spam.

* @param {string=} options.projectId - Dataset ID. This is your project ID from
* the Google Developers Console.
* @param {string=} options.keyFilename - Full path to the JSON key downloaded
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 @@ -135,6 +145,8 @@ function Dataset(options) {
* namespace: 'My-NS',
* path: ['Company', 123]
* });
*
* @return {Key} A newly created Key from the options given.
*/
Dataset.prototype.key = function(options) {
options = util.is(options, 'object') ? options : {
Expand Down Expand Up @@ -166,117 +178,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 +243,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 @@ -354,7 +256,7 @@ Dataset.prototype.allocateIds = function(incompleteKey, n, callback) {
(resp.key || []).forEach(function(k) {
keys.push(entity.keyFromKeyProto(k));
});
callback(null ,keys);
callback(null, keys);
});
};

Expand All @@ -368,4 +270,8 @@ Dataset.prototype.createTransaction_ = function() {
return new Transaction(this.connection, this.projectId);
};

/**
* Exports Dataset
* @type {Dataset}
*/
module.exports = Dataset;

This comment was marked as spam.

Loading