diff --git a/packages/bigquery/src/index.js b/packages/bigquery/src/index.js index 50fa39091dc..875984472d4 100644 --- a/packages/bigquery/src/index.js +++ b/packages/bigquery/src/index.js @@ -196,7 +196,7 @@ BigQuery.prototype.mergeSchemaWithRows_ = function(schema, rows) { * }); */ BigQuery.date = -BigQuery.prototype.date = function(value) { +BigQuery.prototype.date = function BigQueryDate(value) { if (!(this instanceof BigQuery.date)) { return new BigQuery.date(value); } @@ -241,7 +241,7 @@ BigQuery.prototype.date = function(value) { * }); */ BigQuery.datetime = -BigQuery.prototype.datetime = function(value) { +BigQuery.prototype.datetime = function BigQueryDatetime(value) { if (!(this instanceof BigQuery.datetime)) { return new BigQuery.datetime(value); } @@ -291,7 +291,7 @@ BigQuery.prototype.datetime = function(value) { * }); */ BigQuery.time = -BigQuery.prototype.time = function(value) { +BigQuery.prototype.time = function BigQueryTime(value) { if (!(this instanceof BigQuery.time)) { return new BigQuery.time(value); } @@ -318,7 +318,7 @@ BigQuery.prototype.time = function(value) { * var timestamp = bigquery.timestamp(new Date()); */ BigQuery.timestamp = -BigQuery.prototype.timestamp = function(value) { +BigQuery.prototype.timestamp = function BigQueryTimestamp(value) { if (!(this instanceof BigQuery.timestamp)) { return new BigQuery.timestamp(value); } diff --git a/packages/bigquery/src/table.js b/packages/bigquery/src/table.js index d3c158b81a5..cdfc817df6c 100644 --- a/packages/bigquery/src/table.js +++ b/packages/bigquery/src/table.js @@ -274,6 +274,19 @@ Table.encodeValue_ = function(value) { return value.toString('base64'); } + var customTypeConstructorNames = [ + 'BigQueryDate', + 'BigQueryDatetime', + 'BigQueryTime', + 'BigQueryTimestamp', + ]; + var constructorName = value.constructor.name; + var isCustomType = customTypeConstructorNames.indexOf(constructorName) > -1; + + if (isCustomType) { + return value.value; + } + if (is.date(value)) { return value.toJSON(); } diff --git a/packages/bigquery/system-test/bigquery.js b/packages/bigquery/system-test/bigquery.js index 0f154f70873..24157bc4d77 100644 --- a/packages/bigquery/system-test/bigquery.js +++ b/packages/bigquery/system-test/bigquery.js @@ -1139,6 +1139,31 @@ describe('BigQuery', function() { }); }); + describe('Custom Types', function() { + var table; + + var DATE = bigquery.date('2017-01-01'); + var DATETIME = bigquery.datetime('2017-01-01 13:00:00'); + var TIME = bigquery.time('14:00:00'); + var TIMESTAMP = bigquery.timestamp(new Date()); + + before(function() { + table = dataset.table(generateName('table')); + return table.create({ + schema: 'date:DATE, datetime:DATETIME, time:TIME, timestamp:TIMESTAMP' + }); + }); + + it('inserts with custom types', function() { + return table.insert({ + date: DATE, + datetime: DATETIME, + time: TIME, + timestamp: TIMESTAMP + }); + }); + }); + describe('Provided Tests', function() { var table = dataset.table(generateName('table')); var schema = require('./data/schema.json'); diff --git a/packages/bigquery/test/index.js b/packages/bigquery/test/index.js index 5209372e8e2..58c46e520fa 100644 --- a/packages/bigquery/test/index.js +++ b/packages/bigquery/test/index.js @@ -379,6 +379,11 @@ describe('BigQuery', function() { assert(instanceD instanceof bq.date); }); + it('should have the correct constructor name', function() { + var date = bq.date(INPUT_STRING); + assert.strictEqual(date.constructor.name, 'BigQueryDate'); + }); + it('should accept a string', function() { var date = bq.date(INPUT_STRING); assert.strictEqual(date.value, INPUT_STRING); @@ -415,6 +420,11 @@ describe('BigQuery', function() { assert(instanceDt instanceof bq.datetime); }); + it('should have the correct constructor name', function() { + var datetime = bq.datetime(INPUT_STRING); + assert.strictEqual(datetime.constructor.name, 'BigQueryDatetime'); + }); + it('should accept an object', function() { var datetime = bq.datetime(INPUT_OBJ); assert.strictEqual(datetime.value, EXPECTED_VALUE); @@ -455,6 +465,11 @@ describe('BigQuery', function() { assert(instanceT instanceof bq.time); }); + it('should have the correct constructor name', function() { + var time = bq.time(INPUT_STRING); + assert.strictEqual(time.constructor.name, 'BigQueryTime'); + }); + it('should accept a string', function() { var time = bq.time(INPUT_STRING); assert.strictEqual(time.value, INPUT_STRING); @@ -496,6 +511,11 @@ describe('BigQuery', function() { assert(instanceT instanceof bq.timestamp); }); + it('should have the correct constructor name', function() { + var timestamp = bq.timestamp(INPUT_STRING); + assert.strictEqual(timestamp.constructor.name, 'BigQueryTimestamp'); + }); + it('should accept a string', function() { var timestamp = bq.timestamp(INPUT_STRING); assert.strictEqual(timestamp.value, EXPECTED_VALUE); diff --git a/packages/bigquery/test/table.js b/packages/bigquery/test/table.js index a649b6a0ece..41da8edc05b 100644 --- a/packages/bigquery/test/table.js +++ b/packages/bigquery/test/table.js @@ -257,6 +257,23 @@ describe('BigQuery/Table', function() { assert.strictEqual(Table.encodeValue_(date), date.toJSON()); }); + it('should properly encode custom types', function() { + function BigQueryDate(value) { this.value = value; } + function BigQueryDatetime(value) { this.value = value; } + function BigQueryTime(value) { this.value = value; } + function BigQueryTimestamp(value) { this.value = value; } + + var date = new BigQueryDate('date'); + var datetime = new BigQueryDatetime('datetime'); + var time = new BigQueryTime('time'); + var timestamp = new BigQueryTimestamp('timestamp'); + + assert.strictEqual(Table.encodeValue_(date), 'date'); + assert.strictEqual(Table.encodeValue_(datetime), 'datetime'); + assert.strictEqual(Table.encodeValue_(time), 'time'); + assert.strictEqual(Table.encodeValue_(timestamp), 'timestamp'); + }); + it('should properly encode arrays', function() { var buffer = new Buffer('test'); var date = new Date();