Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
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
124 changes: 77 additions & 47 deletions src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,53 +134,6 @@ function Database(instance, name, poolOptions) {
* });
*/
exists: true,

/**
* @typedef {array} GetDatabaseResponse
* @property {Database} 0 The {@link Database}.
* @property {object} 1 The full API response.
*/
/**
* @callback GetDatabaseCallback
* @param {?Error} err Request error, if any.
* @param {Database} database The {@link Database}.
* @param {object} apiResponse The full API response.
*/
/**
* Get a database if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @method Database#get
* @param {options} [options] Configuration object.
* @param {boolean} [options.autoCreate=false] Automatically create the
* object if it does not exist.
* @param {GetDatabaseCallback} [callback] Callback function.
* @returns {Promise<GetDatabaseResponse>}
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
* const database = instance.database('my-database');
*
* database.get(function(err, database, apiResponse) {
* // `database.metadata` has been populated.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* database.get().then(function(data) {
* var database = data[0];
* var apiResponse = data[0];
* });
*/
get: true,
};

commonGrpc.ServiceObject.call(this, {
Expand Down Expand Up @@ -492,6 +445,83 @@ Database.prototype.delete = function(callback) {
});
};

/**
* @typedef {array} GetDatabaseResponse
* @property {Database} 0 The {@link Database}.
* @property {object} 1 The full API response.
*/
/**
* @callback GetDatabaseCallback
* @param {?Error} err Request error, if any.
* @param {Database} database The {@link Database}.
* @param {object} apiResponse The full API response.
*/
/**
* Get a database if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @param {options} [options] Configuration object.
* @param {boolean} [options.autoCreate=false] Automatically create the
* object if it does not exist.
* @param {GetDatabaseCallback} [callback] Callback function.
* @returns {Promise<GetDatabaseResponse>}
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
* const database = instance.database('my-database');
*
* database.get(function(err, database, apiResponse) {
* // `database.metadata` has been populated.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* database.get().then(function(data) {
* var database = data[0];
* var apiResponse = data[0];
* });
*/
Database.prototype.get = function(options, callback) {
var self = this;

if (is.fn(options)) {
callback = options;
options = {};
}

this.getMetadata(function(err, metadata) {
if (err) {
if (options.autoCreate && err.code === 5) {
self.create(options, function(err, database, operation) {
if (err) {
callback(err);
return;
}

operation.on('error', callback).on('complete', function(metadata) {
self.metadata = metadata;
callback(null, self, metadata);
});
});
return;
}

callback(err);
return;
}

callback(null, self, metadata);
});
};

/**
* @typedef {array} GetDatabaseMetadataResponse
* @property {object} 0 The {@link Database} metadata.
Expand Down
122 changes: 76 additions & 46 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,52 +128,6 @@ function Instance(spanner, name) {
* });
*/
exists: true,

/**
* @typedef {array} GetInstanceResponse
* @property {Instance} 0 The {@link Instance}.
* @property {object} 1 The full API response.
*/
/**
* @callback GetInstanceCallback
* @param {?Error} err Request error, if any.
* @param {Instance} instance The {@link Instance}.
* @param {object} apiResponse The full API response.
*/
/**
* Get an instance if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @method Instance#get
* @param {options} [options] Configuration object.
* @param {boolean} [options.autoCreate=false] Automatically create the
* object if it does not exist.
* @param {GetInstanceCallback} [callback] Callback function.
* @returns {Promise<GetInstanceResponse>}
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
*
* instance.get(function(err, instance, apiResponse) {
* // `instance.metadata` has been populated.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* instance.get().then(function(data) {
* var instance = data[0];
* var apiResponse = data[0];
* });
*/
get: true,
};

commonGrpc.ServiceObject.call(this, {
Expand Down Expand Up @@ -453,6 +407,82 @@ Instance.prototype.delete = function(callback) {
});
};

/**
* @typedef {array} GetInstanceResponse
* @property {Instance} 0 The {@link Instance}.
* @property {object} 1 The full API response.
*/
/**
* @callback GetInstanceCallback
* @param {?Error} err Request error, if any.
* @param {Instance} instance The {@link Instance}.
* @param {object} apiResponse The full API response.
*/
/**
* Get an instance if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @param {options} [options] Configuration object.
* @param {boolean} [options.autoCreate=false] Automatically create the
* object if it does not exist.
* @param {GetInstanceCallback} [callback] Callback function.
* @returns {Promise<GetInstanceResponse>}
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
*
* instance.get(function(err, instance, apiResponse) {
* // `instance.metadata` has been populated.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* instance.get().then(function(data) {
* var instance = data[0];
* var apiResponse = data[0];
* });
*/
Instance.prototype.get = function(options, callback) {
var self = this;

if (is.fn(options)) {
callback = options;
options = {};
}

this.getMetadata(function(err, metadata) {
if (err) {
if (err.code === 5 && options.autoCreate) {
self.create(options, function(err, database, operation) {
if (err) {
callback(err);
return;
}

operation.on('error', callback).on('complete', function(metadata) {
self.metadata = metadata;
callback(null, self, metadata);
});
});
return;
}

callback(err);
return;
}

callback(null, self, metadata);
});
};

/**
* Query object for listing databases.
*
Expand Down
44 changes: 34 additions & 10 deletions system-test/spanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,21 @@ var spanner = new Spanner({projectId: process.env.GCLOUD_PROJECT});
describe('Spanner', function() {
var instance = spanner.instance(generateName('instance'));

var INSTANCE_CONFIG = {
config: 'regional-us-central1',
nodes: 1,
labels: {
'gcloud-tests': 'true',
},
};

before(function(done) {
async.series(
[
deleteTestResources,

function(next) {
instance.create(
{
config: 'regional-us-central1',
nodes: 1,
labels: {
'gcloud-tests': 'true',
},
},
execAfterOperationComplete(next)
);
instance.create(INSTANCE_CONFIG, execAfterOperationComplete(next));
},
],
done
Expand Down Expand Up @@ -618,6 +617,22 @@ describe('Spanner', function() {
});
});

it('should auto create an instance', function(done) {
var instance = spanner.instance(generateName('instance'));

var config = extend(
{
autoCreate: true,
},
INSTANCE_CONFIG
);

instance.get(config, function(err) {
assert.ifError(err);
instance.getMetadata(done);
});
});

it('should list the instances', function(done) {
spanner.getInstances(function(err, instances) {
assert.ifError(err);
Expand Down Expand Up @@ -719,6 +734,15 @@ describe('Spanner', function() {
});
});

it('should auto create a database', function(done) {
var database = instance.database(generateName('database'));

database.get({autoCreate: true}, function(err) {
assert.ifError(err);
database.getMetadata(done);
});
});

it('should have created the database', function(done) {
database.getMetadata(function(err, metadata) {
assert.ifError(err);
Expand Down
Loading