Skip to content

Commit

Permalink
feat(table): allow opting out of default insert id (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored Dec 5, 2019
1 parent 99aad91 commit 6bf2dbd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type JobMetadataResponse = [JobMetadata];
export type RowMetadata = any;

export type InsertRowsOptions = bigquery.ITableDataInsertAllRequest & {
createInsertId?: boolean;
raw?: boolean;
schema?: string | {};
};
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 | {};
Expand Down
10 changes: 10 additions & 0 deletions test/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: []};

Expand Down

0 comments on commit 6bf2dbd

Please sign in to comment.