Skip to content

Commit d154b01

Browse files
authored
feat(table): allow opting out of default insert id (#582)
1 parent 14cf144 commit d154b01

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

handwritten/bigquery/src/table.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type JobMetadataResponse = [JobMetadata];
6969
export type RowMetadata = any;
7070

7171
export type InsertRowsOptions = bigquery.ITableDataInsertAllRequest & {
72+
createInsertId?: boolean;
7273
raw?: boolean;
7374
schema?: string | {};
7475
};
@@ -91,6 +92,11 @@ export type RowsCallback = PagedCallback<
9192
bigquery.ITableDataList | bigquery.ITable
9293
>;
9394

95+
export interface InsertRow {
96+
insertId?: string;
97+
json?: bigquery.IJsonObject;
98+
}
99+
94100
export type TableRow = bigquery.ITableRow;
95101
export type TableRowField = bigquery.ITableCell;
96102
export type TableRowValue = string | TableRow;
@@ -1726,6 +1732,8 @@ class Table extends common.ServiceObject {
17261732
*
17271733
* @param {object|object[]} rows The rows to insert into the table.
17281734
* @param {object} [options] Configuration object.
1735+
* @param {boolean} [options.createInsertId=true] Automatically insert a
1736+
* default row id when one is not provided.
17291737
* @param {boolean} [options.ignoreUnknownValues=false] Accept rows that contain
17301738
* values that do not match the schema. The unknown values are ignored.
17311739
* @param {boolean} [options.raw] If `true`, the `rows` argument is expected to
@@ -1861,19 +1869,23 @@ class Table extends common.ServiceObject {
18611869
throw new Error('You must provide at least 1 row to be inserted.');
18621870
}
18631871

1864-
const json = extend(true, {}, options, {
1865-
rows,
1866-
});
1872+
const json = extend(true, {}, options, {rows});
18671873

18681874
if (!options.raw) {
18691875
json.rows = rows.map((row: RowMetadata) => {
1870-
return {
1871-
insertId: uuid.v4(),
1872-
json: Table.encodeValue_(row),
1876+
const encoded: InsertRow = {
1877+
json: Table.encodeValue_(row)!,
18731878
};
1879+
1880+
if (options.createInsertId !== false) {
1881+
encoded.insertId = uuid.v4();
1882+
}
1883+
1884+
return encoded;
18741885
});
18751886
}
18761887

1888+
delete json.createInsertId;
18771889
delete json.raw;
18781890

18791891
let schema: string | {};

handwritten/bigquery/test/table.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,16 @@ describe('BigQuery/Table', () => {
20452045
table.insert([data[0]], done);
20462046
});
20472047

2048+
it('should omit the insertId if createInsertId is false', done => {
2049+
table.request = ({json}: DecorateRequestOptions) => {
2050+
assert.strictEqual(json.rows[0].insertId, undefined);
2051+
assert.strictEqual(json.createInsertId, undefined);
2052+
done();
2053+
};
2054+
2055+
table.insert([data[0]], {createInsertId: false}, done);
2056+
});
2057+
20482058
it('should execute callback with API response', done => {
20492059
const apiResponse = {insertErrors: []};
20502060

0 commit comments

Comments
 (0)