Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"concat-stream": "^1.5.0",
"create-error-class": "^3.0.2",
"extend": "^3.0.1",
"google-auto-auth": "^0.8.2",
"google-auto-auth": "^0.9.0",
"google-gax": "^0.14.2",
"google-proto-files": "^0.13.1",
"is": "^3.0.1",
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,22 @@ function Datastore(options) {

this.clients_ = new Map();
this.datastore = this;

/**
* @name Datastore#namespace
* @type {string}
*/
this.namespace = options.namespace;

const userProvidedProjectId =
options.projectId || process.env.DATASTORE_PROJECT_ID;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

const defaultProjectId = '{{projectId}}';

/**
* @name Datastore#projectId
* @type {string}
*/
this.projectId =
process.env.DATASTORE_PROJECT_ID || options.projectId || '{{projectId}}';
this.projectId = userProvidedProjectId || defaultProjectId;

this.defaultBaseUrl_ = 'datastore.googleapis.com';
this.determineBaseUrl_(options.apiEndpoint);
Expand All @@ -392,10 +397,10 @@ function Datastore(options) {
scopes: gapic.v1.DatastoreClient.scopes,
servicePath: this.baseUrl_,
port: is.number(this.port_) ? this.port_ : 443,
projectId: userProvidedProjectId,
},
options
);

if (this.customEndpoint_) {
this.options.sslCreds = grpc.credentials.createInsecure();
}
Expand Down
24 changes: 21 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ describe('Datastore', function() {
var PROJECT_ID = 'project-id';
var NAMESPACE = 'namespace';

var DATASTORE_PROJECT_ID_CACHED = process.env.DATASTORE_PROJECT_ID;

var OPTIONS = {
projectId: PROJECT_ID,
apiEndpoint: 'http://endpoint',
Expand Down Expand Up @@ -123,6 +125,14 @@ describe('Datastore', function() {
});
});

afterEach(function() {
if (typeof DATASTORE_PROJECT_ID_CACHED === 'string') {
process.env.DATASTORE_PROJECT_ID = DATASTORE_PROJECT_ID_CACHED;
} else {
delete process.env.DATASTORE_PROJECT_ID;
}
});

after(function() {
createInsecureOverride = null;
googleAutoAuthOverride = null;
Expand Down Expand Up @@ -167,23 +177,28 @@ describe('Datastore', function() {

it('should localize the projectId', function() {
assert.strictEqual(datastore.projectId, PROJECT_ID);
assert.strictEqual(datastore.options.projectId, PROJECT_ID);
});

it('should default project ID to placeholder', function() {
var datastore = new Datastore({});
assert.strictEqual(datastore.projectId, '{{projectId}}');
});

it('should not default options.projectId to placeholder', function() {
var datastore = new Datastore({});
assert.strictEqual(datastore.options.projectId, undefined);
});

it('should use DATASTORE_PROJECT_ID', function() {
var datastoreProjectIdCached = process.env.DATASTORE_PROJECT_ID;
var projectId = 'overridden-project-id';

process.env.DATASTORE_PROJECT_ID = projectId;

var datastore = new Datastore(OPTIONS);
process.env.DATASTORE_PROJECT_ID = datastoreProjectIdCached;
var datastore = new Datastore({});

assert.strictEqual(datastore.projectId, projectId);
assert.strictEqual(datastore.options.projectId, projectId);
});

it('should set the default base URL', function() {
Expand All @@ -204,6 +219,8 @@ describe('Datastore', function() {
});

it('should localize the options', function() {
delete process.env.DATASTORE_PROJECT_ID;

var options = {
a: 'b',
c: 'd',
Expand All @@ -222,6 +239,7 @@ describe('Datastore', function() {
scopes: v1.DatastoreClient.scopes,
servicePath: datastore.baseUrl_,
port: 443,
projectId: undefined,

This comment was marked as spam.

This comment was marked as spam.

},
options
)
Expand Down