From 6bf2dbd1ec09689338ee21b1d8666a4e8b2a7367 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Thu, 5 Dec 2019 13:19:52 -0500 Subject: [PATCH] feat(table): allow opting out of default insert id (#582) --- src/table.ts | 24 ++++++++++++++++++------ test/table.ts | 10 ++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/table.ts b/src/table.ts index aeeb56de..cbe2d59d 100644 --- a/src/table.ts +++ b/src/table.ts @@ -69,6 +69,7 @@ export type JobMetadataResponse = [JobMetadata]; export type RowMetadata = any; export type InsertRowsOptions = bigquery.ITableDataInsertAllRequest & { + createInsertId?: boolean; raw?: boolean; schema?: string | {}; }; @@ -91,6 +92,11 @@ export type RowsCallback = PagedCallback< bigquery.ITableDataList | bigquery.ITable >; +export interface InsertRow { + insertId?: string; + json?: bigquery.IJsonObject; +} + export type TableRow = bigquery.ITableRow; export type TableRowField = bigquery.ITableCell; export type TableRowValue = string | TableRow; @@ -1726,6 +1732,8 @@ class Table extends common.ServiceObject { * * @param {object|object[]} rows The rows to insert into the table. * @param {object} [options] Configuration object. + * @param {boolean} [options.createInsertId=true] Automatically insert a + * default row id when one is not provided. * @param {boolean} [options.ignoreUnknownValues=false] Accept rows that contain * values that do not match the schema. The unknown values are ignored. * @param {boolean} [options.raw] If `true`, the `rows` argument is expected to @@ -1861,19 +1869,23 @@ class Table extends common.ServiceObject { throw new Error('You must provide at least 1 row to be inserted.'); } - const json = extend(true, {}, options, { - rows, - }); + const json = extend(true, {}, options, {rows}); if (!options.raw) { json.rows = rows.map((row: RowMetadata) => { - return { - insertId: uuid.v4(), - json: Table.encodeValue_(row), + const encoded: InsertRow = { + json: Table.encodeValue_(row)!, }; + + if (options.createInsertId !== false) { + encoded.insertId = uuid.v4(); + } + + return encoded; }); } + delete json.createInsertId; delete json.raw; let schema: string | {}; diff --git a/test/table.ts b/test/table.ts index 8fd976bb..533afef1 100644 --- a/test/table.ts +++ b/test/table.ts @@ -2045,6 +2045,16 @@ describe('BigQuery/Table', () => { table.insert([data[0]], done); }); + it('should omit the insertId if createInsertId is false', done => { + table.request = ({json}: DecorateRequestOptions) => { + assert.strictEqual(json.rows[0].insertId, undefined); + assert.strictEqual(json.createInsertId, undefined); + done(); + }; + + table.insert([data[0]], {createInsertId: false}, done); + }); + it('should execute callback with API response', done => { const apiResponse = {insertErrors: []};