Skip to content

Commit 9e8d234

Browse files
datastore: recognize DATASTORE_DATASET env var
1 parent 36a9f5c commit 9e8d234

File tree

6 files changed

+54
-26
lines changed

6 files changed

+54
-26
lines changed

lib/datastore/dataset.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ var SCOPES = [
6868
* Interact with a dataset from the
6969
* [Google Cloud Datastore](https://developers.google.com/datastore/).
7070
*
71+
* If a project ID is not specified, the `DATASTORE_DATASET` environment
72+
* variable from the gcloud SDK is used.
73+
*
7174
* @constructor
7275
* @alias module:datastore/dataset
7376
* @mixes module:datastore/request
@@ -106,13 +109,14 @@ function Dataset(options) {
106109

107110
options = options || {};
108111

109-
if (!options.projectId) {
110-
throw util.missingProjectIdError;
112+
this.datasetId = options.projectId || process.env.DATASTORE_DATASET;
113+
114+
if (!this.datasetId) {
115+
throw new Error('A project or dataset ID is required to use a Dataset.');
111116
}
112117

113118
this.determineApiEndpoint_(options.apiEndpoint);
114119
this.namespace = options.namespace;
115-
this.projectId = options.projectId;
116120

117121
this.makeAuthenticatedRequest_ = util.makeAuthenticatedRequestFactory({
118122
customEndpoint: this.customEndpoint,
@@ -306,7 +310,7 @@ Dataset.prototype.runInTransaction = function(fn, callback) {
306310
* @private
307311
*/
308312
Dataset.prototype.createTransaction_ = function() {
309-
return new Transaction(this, this.projectId);
313+
return new Transaction(this, this.datasetId);
310314
};
311315

312316
module.exports = Dataset;

lib/datastore/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,10 @@ DatastoreRequest.prototype.makeReq_ = function(method, body, callback) {
784784

785785
var reqOpts = {
786786
method: 'POST',
787-
uri: format('{apiEndpoint}/{path}/{projectId}/{method}', {
787+
uri: format('{apiEndpoint}/{path}/{datasetId}/{method}', {
788788
apiEndpoint: this.apiEndpoint,
789789
path: 'datastore/v1beta2/datasets',
790-
projectId: this.projectId,
790+
datasetId: this.datasetId,
791791
method: method
792792
}),
793793
body: is.empty(body) ? '' : pbRequest,

lib/datastore/transaction.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ var extend = require('extend');
4141
*
4242
* @param {module:common/connection#Connection} connection - An authenticated
4343
* connection to Google Cloud Datastore.
44-
* @param {string} projectId - Dataset ID. This is your project ID from the
45-
* Google Developers Console.
44+
* @param {string} datasetId - Dataset ID.
4645
*
4746
* @example
4847
* // This is how to create a transaction object directly using this Transaction
@@ -69,11 +68,11 @@ var extend = require('extend');
6968
* // `transaction` is a Transaction object.
7069
* }, function(err) {});
7170
*/
72-
function Transaction(dataset, projectId) {
71+
function Transaction(dataset, datasetId) {
7372
this.id = null;
7473
this.apiEndpoint = dataset.apiEndpoint;
7574
this.makeAuthenticatedRequest_ = dataset.makeAuthenticatedRequest_;
76-
this.projectId = projectId;
75+
this.datasetId = datasetId;
7776

7877
// A queue for entity modifications made during the transaction.
7978
this.modifiedEntities_ = [];

test/datastore/dataset.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ util.makeAuthenticatedRequestFactory = function() {
3131
return makeAuthenticatedRequestFactoryCache.apply(this, arguments);
3232
};
3333

34+
function FakeTransaction() {
35+
this.calledWith_ = arguments;
36+
}
37+
3438
describe('Dataset', function() {
3539
var Dataset;
3640
var dataset;
@@ -46,6 +50,7 @@ describe('Dataset', function() {
4650

4751
before(function() {
4852
mockery.registerMock('../common/util.js', util);
53+
mockery.registerMock('./transaction.js', FakeTransaction);
4954

5055
mockery.enable({
5156
useCleanCache: true,
@@ -61,15 +66,30 @@ describe('Dataset', function() {
6166
});
6267

6368
beforeEach(function() {
69+
delete process.env.DATASTORE_DATASET;
6470
makeAuthenticatedRequestFactoryOverride = null;
6571
dataset = new Dataset(OPTIONS);
6672
});
6773

6874
describe('instantiation', function() {
75+
it('should localize the dataset id', function() {
76+
assert.strictEqual(dataset.datasetId, OPTIONS.projectId);
77+
});
78+
79+
it('should detect the dataset ID', function() {
80+
var datasetId = 'dataset-id';
81+
process.env.DATASTORE_DATASET = datasetId;
82+
83+
var ds = new Dataset();
84+
assert.strictEqual(ds.datasetId, datasetId);
85+
86+
delete process.env.DATASTORE_DATASET;
87+
});
88+
6989
it('should throw if a projectId is not specified', function() {
7090
assert.throws(function() {
7191
new Dataset();
72-
}, /Sorry, we cannot connect/);
92+
}, 'A project or dataset ID is required to use a Dataset.');
7393
});
7494

7595
it('should set default API connection details', function(done) {
@@ -85,6 +105,10 @@ describe('Dataset', function() {
85105
new Dataset(OPTIONS);
86106
});
87107

108+
it('should localize the namespace', function() {
109+
assert.strictEqual(dataset.namespace, OPTIONS.namespace);
110+
});
111+
88112
it('should create an authenticated request factory', function() {
89113
var authenticatedRequest = {};
90114
var customEndpoint = 'custom-endpoint';
@@ -112,14 +136,6 @@ describe('Dataset', function() {
112136
var ds = new Dataset(OPTIONS);
113137
assert.strictEqual(ds.makeAuthenticatedRequest_, authenticatedRequest);
114138
});
115-
116-
it('should localize the project id', function() {
117-
assert.strictEqual(dataset.projectId, OPTIONS.projectId);
118-
});
119-
120-
it('should localize the namespace', function() {
121-
assert.strictEqual(dataset.namespace, OPTIONS.namespace);
122-
});
123139
});
124140

125141
describe('key', function() {
@@ -337,4 +353,13 @@ describe('Dataset', function() {
337353
});
338354
});
339355
});
356+
357+
describe('createTransaction_', function() {
358+
it('should create and return a Transaction', function() {
359+
var transaction = dataset.createTransaction_();
360+
assert(transaction instanceof FakeTransaction);
361+
assert.strictEqual(transaction.calledWith_[0], dataset);
362+
assert.strictEqual(transaction.calledWith_[1], dataset.datasetId);
363+
});
364+
});
340365
});

test/datastore/request.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,15 +864,15 @@ describe('Request', function() {
864864

865865
it('should assemble correct request', function(done) {
866866
var method = 'commit';
867-
var projectId = 'project-id';
867+
var datasetId = 'dataset-id';
868868
var expectedUri =
869-
format('{apiEndpoint}/datastore/v1beta2/datasets/{pId}/{method}', {
869+
format('{apiEndpoint}/datastore/v1beta2/datasets/{dId}/{method}', {
870870
apiEndpoint: CUSTOM_ENDPOINT,
871-
pId: projectId,
871+
dId: datasetId,
872872
method: method
873873
});
874874

875-
request.projectId = projectId;
875+
request.datasetId = datasetId;
876876
request.makeAuthenticatedRequest_ = function(opts) {
877877
assert.equal(opts.method, 'POST');
878878
assert.equal(opts.uri, expectedUri);

test/datastore/transaction.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ describe('Transaction', function() {
7979

8080
describe('instantiation', function() {
8181
it('should assign default properties', function() {
82-
var projectId = 'abc';
82+
var datasetId = 'abc';
8383
var fakeDataset = {
8484
apiEndpoint: 'http://localhost:8080',
8585
makeAuthenticatedRequest_: function fakeMakeAuthenticatedRequest_() {}
8686
};
8787

88-
var transaction = new Transaction(fakeDataset, projectId);
88+
var transaction = new Transaction(fakeDataset, datasetId);
8989

9090
assert.strictEqual(transaction.id, null);
9191
assert.deepEqual(transaction.apiEndpoint, fakeDataset.apiEndpoint);
9292
assert.equal(
9393
transaction.makeAuthenticatedRequest_,
9494
fakeDataset.makeAuthenticatedRequest_
9595
);
96-
assert.equal(transaction.projectId, projectId);
96+
assert.equal(transaction.datasetId, datasetId);
9797
});
9898
});
9999

0 commit comments

Comments
 (0)