2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2021-04-17 06:04:25 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2021-04-17 06:04:25 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { googleApiRequest, googleApiRequestAllItems, simplify } from './GenericFunctions';
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { recordFields, recordOperations } from './RecordDescription';
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2021-06-13 10:17:39 -07:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2021-04-17 06:04:25 -07:00
|
|
|
|
|
|
|
export class GoogleBigQuery implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Google BigQuery',
|
|
|
|
name: 'googleBigQuery',
|
|
|
|
icon: 'file:googleBigQuery.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Google BigQuery API',
|
2021-04-17 06:04:25 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Google BigQuery',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
2022-04-18 09:46:50 -07:00
|
|
|
{
|
|
|
|
name: 'googleApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['serviceAccount'],
|
2022-04-18 09:46:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-04-17 06:04:25 -07:00
|
|
|
{
|
|
|
|
name: 'googleBigQueryOAuth2Api',
|
|
|
|
required: true,
|
2022-04-18 09:46:50 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['oAuth2'],
|
2022-04-18 09:46:50 -07:00
|
|
|
},
|
|
|
|
},
|
2021-04-17 06:04:25 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
2022-04-18 09:46:50 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
noDataExpression: true,
|
|
|
|
options: [
|
|
|
|
{
|
2022-09-08 05:44:34 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
|
|
|
name: 'OAuth2 (recommended)',
|
2022-07-18 01:15:03 -07:00
|
|
|
value: 'oAuth2',
|
2022-04-18 09:46:50 -07:00
|
|
|
},
|
|
|
|
{
|
2022-07-18 01:15:03 -07:00
|
|
|
name: 'Service Account',
|
|
|
|
value: 'serviceAccount',
|
2022-04-18 09:46:50 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'oAuth2',
|
|
|
|
},
|
2021-04-17 06:04:25 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-04-18 09:46:50 -07:00
|
|
|
noDataExpression: true,
|
2021-04-17 06:04:25 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Record',
|
|
|
|
value: 'record',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'record',
|
|
|
|
},
|
|
|
|
...recordOperations,
|
|
|
|
...recordFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2022-08-17 08:50:24 -07:00
|
|
|
async getProjects(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-04-17 06:04:25 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { projects } = await googleApiRequest.call(this, 'GET', '/v2/projects');
|
2021-04-17 06:04:25 -07:00
|
|
|
for (const project of projects) {
|
|
|
|
returnData.push({
|
|
|
|
name: project.friendlyName as string,
|
|
|
|
value: project.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getDatasets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-04-17 06:04:25 -07:00
|
|
|
const projectId = this.getCurrentNodeParameter('projectId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { datasets } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/projects/${projectId}/datasets`,
|
|
|
|
);
|
|
|
|
for (const dataset of datasets) {
|
|
|
|
returnData.push({
|
|
|
|
name: dataset.datasetReference.datasetId as string,
|
|
|
|
value: dataset.datasetReference.datasetId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getTables(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-04-17 06:04:25 -07:00
|
|
|
const projectId = this.getCurrentNodeParameter('projectId');
|
|
|
|
const datasetId = this.getCurrentNodeParameter('datasetId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { tables } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables`,
|
|
|
|
);
|
|
|
|
for (const table of tables) {
|
|
|
|
returnData.push({
|
|
|
|
name: table.tableReference.tableId as string,
|
|
|
|
value: table.tableReference.tableId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-04-17 06:04:25 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-04-17 06:04:25 -07:00
|
|
|
|
|
|
|
if (resource === 'record') {
|
|
|
|
// *********************************************************************
|
|
|
|
// record
|
|
|
|
// *********************************************************************
|
|
|
|
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// record: create
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/insertAll
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', 0) as string;
|
|
|
|
const datasetId = this.getNodeParameter('datasetId', 0) as string;
|
|
|
|
const tableId = this.getNodeParameter('tableId', 0) as string;
|
|
|
|
const rows: IDataObject[] = [];
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-04-17 06:04:25 -07:00
|
|
|
Object.assign(body, options);
|
|
|
|
if (body.traceId === undefined) {
|
|
|
|
body.traceId = uuid();
|
|
|
|
}
|
|
|
|
const columns = this.getNodeParameter('columns', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const columnList = columns.split(',').map((column) => column.trim());
|
2021-04-17 06:04:25 -07:00
|
|
|
const record: IDataObject = {};
|
|
|
|
|
|
|
|
for (const key of Object.keys(items[i].json)) {
|
|
|
|
if (columnList.includes(key)) {
|
|
|
|
record[`${key}`] = items[i].json[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rows.push({ json: record });
|
|
|
|
}
|
|
|
|
|
|
|
|
body.rows = rows;
|
|
|
|
|
2022-04-18 09:46:50 -07:00
|
|
|
try {
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/insertAll`,
|
|
|
|
body,
|
|
|
|
);
|
2022-09-13 00:55:58 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-09-13 00:55:58 -07:00
|
|
|
{ itemData: { item: 0 } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2022-04-18 09:46:50 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-09-13 00:55:58 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: 0 } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2022-04-18 09:46:50 -07:00
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject, { itemIndex: 0 });
|
2022-04-18 09:46:50 -07:00
|
|
|
}
|
2021-04-17 06:04:25 -07:00
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// record: getAll
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/get
|
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-04-17 06:04:25 -07:00
|
|
|
const projectId = this.getNodeParameter('projectId', 0) as string;
|
|
|
|
const datasetId = this.getNodeParameter('datasetId', 0) as string;
|
|
|
|
const tableId = this.getNodeParameter('tableId', 0) as string;
|
|
|
|
const simple = this.getNodeParameter('simple', 0) as boolean;
|
|
|
|
let fields;
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (simple) {
|
2021-04-17 06:04:25 -07:00
|
|
|
const { schema } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}`,
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
fields = schema.fields.map((field: IDataObject) => field.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2022-04-18 09:46:50 -07:00
|
|
|
try {
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-04-18 09:46:50 -07:00
|
|
|
Object.assign(qs, options);
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2022-04-18 09:46:50 -07:00
|
|
|
if (qs.selectedFields) {
|
|
|
|
fields = (qs.selectedFields as string).split(',');
|
|
|
|
}
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2022-04-18 09:46:50 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'rows',
|
|
|
|
'GET',
|
|
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/data`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.maxResults = this.getNodeParameter('limit', i);
|
2022-04-18 09:46:50 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/data`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
2022-09-13 00:55:58 -07:00
|
|
|
if (!returnAll) {
|
|
|
|
responseData = responseData.rows;
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = simple
|
|
|
|
? simplify(responseData as IDataObject[], fields as string[])
|
|
|
|
: responseData;
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2022-04-18 09:46:50 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2022-04-18 09:46:50 -07:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject, { itemIndex: i });
|
2021-04-17 06:04:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-04-17 06:04:25 -07:00
|
|
|
}
|
|
|
|
}
|