mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 00:24:07 -08:00
6dcdb30bf4
* ✏️ Alphabetize lint rules * 🔥 Remove duplicates * ⚡ Update `lintfix` script * 👕 Apply `node-param-operation-without-no-data-expression` (#3329) * 👕 Apply `node-param-operation-without-no-data-expression` * 👕 Add exceptions * 👕 Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-value-duplicate` (#3331) * 👕 Apply `node-param-description-miscased-json` (#3337) * 👕 Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * ✏️ Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-description-wrong-for-simplify` (#3334) * ⚡ fix * ⚡ exceptions * ⚡ changed rule ignoring from file to line * 👕 Apply `node-param-resource-without-no-data-expression` (#3339) * 👕 Apply `node-param-display-name-untrimmed` (#3341) * 👕 Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-resource-with-plural-option` (#3342) * 👕 Apply `node-param-description-wrong-for-upsert` (#3333) * ⚡ fix * ⚡ replaced record with contact in description * ⚡ fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-description-identical-to-name` (#3343) * 👕 Apply `node-param-option-name-containing-star` (#3347) * 👕 Apply `node-param-display-name-wrong-for-update-fields` (#3348) * 👕 Apply `node-param-option-name-wrong-for-get-all` (#3345) * ⚡ fix * ⚡ exceptions * 👕 Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * 👕 Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * ⚡ Alphabetize lint rules * ⚡ Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
302 lines
7.7 KiB
TypeScript
302 lines
7.7 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeApiError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
googleApiRequest,
|
|
googleApiRequestAllItems,
|
|
simplify,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
recordFields,
|
|
recordOperations,
|
|
} from './RecordDescription';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
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"]}}',
|
|
description: 'Consume Google BigQuery API',
|
|
defaults: {
|
|
name: 'Google BigQuery',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'googleApi',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'serviceAccount',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'googleBigQueryOAuth2Api',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'oAuth2',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Authentication',
|
|
name: 'authentication',
|
|
type: 'options',
|
|
noDataExpression: true,
|
|
options: [
|
|
{
|
|
name: 'Service Account',
|
|
value: 'serviceAccount',
|
|
},
|
|
{
|
|
name: 'OAuth2',
|
|
value: 'oAuth2',
|
|
},
|
|
],
|
|
default: 'oAuth2',
|
|
},
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
noDataExpression: true,
|
|
options: [
|
|
{
|
|
name: 'Record',
|
|
value: 'record',
|
|
},
|
|
],
|
|
default: 'record',
|
|
},
|
|
...recordOperations,
|
|
...recordFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
async getProjects(
|
|
this: ILoadOptionsFunctions,
|
|
): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const { projects } = await googleApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/v2/projects',
|
|
);
|
|
for (const project of projects) {
|
|
returnData.push({
|
|
name: project.friendlyName as string,
|
|
value: project.id,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
async getDatasets(
|
|
this: ILoadOptionsFunctions,
|
|
): Promise<INodePropertyOptions[]> {
|
|
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;
|
|
},
|
|
async getTables(
|
|
this: ILoadOptionsFunctions,
|
|
): Promise<INodePropertyOptions[]> {
|
|
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();
|
|
const returnData: IDataObject[] = [];
|
|
const length = items.length;
|
|
const qs: IDataObject = {};
|
|
let responseData;
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
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++) {
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
Object.assign(body, options);
|
|
if (body.traceId === undefined) {
|
|
body.traceId = uuid();
|
|
}
|
|
const columns = this.getNodeParameter('columns', i) as string;
|
|
const columnList = columns.split(',').map(column => column.trim());
|
|
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;
|
|
|
|
try {
|
|
responseData = await googleApiRequest.call(
|
|
this,
|
|
'POST',
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/insertAll`,
|
|
body,
|
|
);
|
|
returnData.push(responseData);
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: error.message });
|
|
} else {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
// record: getAll
|
|
// ----------------------------------
|
|
|
|
// https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/get
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
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;
|
|
|
|
if (simple === true) {
|
|
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++) {
|
|
try {
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
Object.assign(qs, options);
|
|
|
|
// if (qs.useInt64Timestamp !== undefined) {
|
|
// qs.formatOptions = {
|
|
// useInt64Timestamp: qs.useInt64Timestamp,
|
|
// };
|
|
// delete qs.useInt64Timestamp;
|
|
// }
|
|
|
|
if (qs.selectedFields) {
|
|
fields = (qs.selectedFields as string).split(',');
|
|
}
|
|
|
|
if (returnAll) {
|
|
responseData = await googleApiRequestAllItems.call(
|
|
this,
|
|
'rows',
|
|
'GET',
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/data`,
|
|
{},
|
|
qs,
|
|
);
|
|
returnData.push.apply(returnData, (simple) ? simplify(responseData, fields) : responseData);
|
|
} else {
|
|
qs.maxResults = this.getNodeParameter('limit', i) as number;
|
|
responseData = await googleApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/data`,
|
|
{},
|
|
qs,
|
|
);
|
|
returnData.push.apply(returnData, (simple) ? simplify(responseData.rows, fields) : responseData.rows);
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: error.message });
|
|
continue;
|
|
}
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|