Skip to content

Commit d3fbe4c

Browse files
Properly handle custom types from Table#insert().
1 parent f10f61f commit d3fbe4c

5 files changed

Lines changed: 79 additions & 4 deletions

File tree

packages/bigquery/src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ BigQuery.prototype.mergeSchemaWithRows_ = function(schema, rows) {
196196
* });
197197
*/
198198
BigQuery.date =
199-
BigQuery.prototype.date = function(value) {
199+
BigQuery.prototype.date = function BigQueryDate(value) {
200200
if (!(this instanceof BigQuery.date)) {
201201
return new BigQuery.date(value);
202202
}
@@ -241,7 +241,7 @@ BigQuery.prototype.date = function(value) {
241241
* });
242242
*/
243243
BigQuery.datetime =
244-
BigQuery.prototype.datetime = function(value) {
244+
BigQuery.prototype.datetime = function BigQueryDatetime(value) {
245245
if (!(this instanceof BigQuery.datetime)) {
246246
return new BigQuery.datetime(value);
247247
}
@@ -291,7 +291,7 @@ BigQuery.prototype.datetime = function(value) {
291291
* });
292292
*/
293293
BigQuery.time =
294-
BigQuery.prototype.time = function(value) {
294+
BigQuery.prototype.time = function BigQueryTime(value) {
295295
if (!(this instanceof BigQuery.time)) {
296296
return new BigQuery.time(value);
297297
}
@@ -318,7 +318,7 @@ BigQuery.prototype.time = function(value) {
318318
* var timestamp = bigquery.timestamp(new Date());
319319
*/
320320
BigQuery.timestamp =
321-
BigQuery.prototype.timestamp = function(value) {
321+
BigQuery.prototype.timestamp = function BigQueryTimestamp(value) {
322322
if (!(this instanceof BigQuery.timestamp)) {
323323
return new BigQuery.timestamp(value);
324324
}

packages/bigquery/src/table.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ Table.encodeValue_ = function(value) {
274274
return value.toString('base64');
275275
}
276276

277+
var customTypeConstructorNames = [
278+
'BigQueryDate',
279+
'BigQueryDatetime',
280+
'BigQueryTime',
281+
'BigQueryTimestamp',
282+
];
283+
var constructorName = value.constructor.name;
284+
var isCustomType = customTypeConstructorNames.indexOf(constructorName) > -1;
285+
286+
if (isCustomType) {
287+
return value.value;
288+
}
289+
277290
if (is.date(value)) {
278291
return value.toJSON();
279292
}

packages/bigquery/system-test/bigquery.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,31 @@ describe('BigQuery', function() {
11391139
});
11401140
});
11411141

1142+
describe('Custom Types', function(done) {
1143+
var table;
1144+
1145+
var DATE = bigquery.date('2017-01-01');
1146+
var DATETIME = bigquery.datetime('2017-01-01 13:00:00');
1147+
var TIME = bigquery.time('14:00:00');
1148+
var TIMESTAMP = bigquery.timestamp(new Date());
1149+
1150+
before(function() {
1151+
table = dataset.table(generateName('table'));
1152+
return table.create({
1153+
schema: 'date:DATE, datetime:DATETIME, time:TIME, timestamp:TIMESTAMP'
1154+
});
1155+
});
1156+
1157+
it('inserts with custom types', function() {
1158+
return table.insert({
1159+
date: DATE,
1160+
datetime: DATETIME,
1161+
time: TIME,
1162+
timestamp: TIMESTAMP
1163+
});
1164+
});
1165+
});
1166+
11421167
describe('Provided Tests', function() {
11431168
var table = dataset.table(generateName('table'));
11441169
var schema = require('./data/schema.json');

packages/bigquery/test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ describe('BigQuery', function() {
379379
assert(instanceD instanceof bq.date);
380380
});
381381

382+
it('should have the correct constructor name', function() {
383+
var date = bq.date(INPUT_STRING);
384+
assert.strictEqual(date.constructor.name, 'BigQueryDate');
385+
});
386+
382387
it('should accept a string', function() {
383388
var date = bq.date(INPUT_STRING);
384389
assert.strictEqual(date.value, INPUT_STRING);
@@ -415,6 +420,11 @@ describe('BigQuery', function() {
415420
assert(instanceDt instanceof bq.datetime);
416421
});
417422

423+
it('should have the correct constructor name', function() {
424+
var datetime = bq.datetime(INPUT_STRING);
425+
assert.strictEqual(datetime.constructor.name, 'BigQueryDatetime');
426+
});
427+
418428
it('should accept an object', function() {
419429
var datetime = bq.datetime(INPUT_OBJ);
420430
assert.strictEqual(datetime.value, EXPECTED_VALUE);
@@ -455,6 +465,11 @@ describe('BigQuery', function() {
455465
assert(instanceT instanceof bq.time);
456466
});
457467

468+
it('should have the correct constructor name', function() {
469+
var time = bq.time(INPUT_STRING);
470+
assert.strictEqual(time.constructor.name, 'BigQueryTime');
471+
});
472+
458473
it('should accept a string', function() {
459474
var time = bq.time(INPUT_STRING);
460475
assert.strictEqual(time.value, INPUT_STRING);
@@ -496,6 +511,11 @@ describe('BigQuery', function() {
496511
assert(instanceT instanceof bq.timestamp);
497512
});
498513

514+
it('should have the correct constructor name', function() {
515+
var timestamp = bq.timestamp(INPUT_STRING);
516+
assert.strictEqual(timestamp.constructor.name, 'BigQueryTimestamp');
517+
});
518+
499519
it('should accept a string', function() {
500520
var timestamp = bq.timestamp(INPUT_STRING);
501521
assert.strictEqual(timestamp.value, EXPECTED_VALUE);

packages/bigquery/test/table.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,23 @@ describe('BigQuery/Table', function() {
257257
assert.strictEqual(Table.encodeValue_(date), date.toJSON());
258258
});
259259

260+
it('should properly encode custom types', function() {
261+
function BigQueryDate(value) { this.value = value; }
262+
function BigQueryDatetime(value) { this.value = value; }
263+
function BigQueryTime(value) { this.value = value; }
264+
function BigQueryTimestamp(value) { this.value = value; }
265+
266+
var date = new BigQueryDate('date');
267+
var datetime = new BigQueryDatetime('datetime');
268+
var time = new BigQueryTime('time');
269+
var timestamp = new BigQueryTimestamp('timestamp');
270+
271+
assert.strictEqual(Table.encodeValue_(date), 'date');
272+
assert.strictEqual(Table.encodeValue_(datetime), 'datetime');
273+
assert.strictEqual(Table.encodeValue_(time), 'time');
274+
assert.strictEqual(Table.encodeValue_(timestamp), 'timestamp');
275+
});
276+
260277
it('should properly encode arrays', function() {
261278
var buffer = new Buffer('test');
262279
var date = new Date();

0 commit comments

Comments
 (0)