n8n-ikaue-nodes
Version:
IKAUE nodes for N8N, such as a custom BigQuery node or a Google Search Console node.
242 lines • 12.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleBigQueryIk = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("./GenericFunctions");
const RecordDescription_1 = require("./RecordDescription");
const uuid_1 = require("uuid");
class GoogleBigQueryIk {
constructor() {
this.description = {
displayName: 'Google BigQuery IK',
name: 'googleBigQueryIk',
icon: 'file:googleBigQuery.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Google BigQuery API (added support for query operation)',
defaults: {
name: 'Google BigQuery IK',
},
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: 'OAuth2 (Recommended)',
value: 'oAuth2',
},
{
name: 'Service Account',
value: 'serviceAccount',
},
],
default: 'oAuth2',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Record',
value: 'record',
},
],
default: 'record',
},
...RecordDescription_1.recordOperations,
...RecordDescription_1.recordFields,
],
};
this.methods = {
loadOptions: {
async getProjects() {
const returnData = [];
const { projects } = await GenericFunctions_1.googleApiRequest.call(this, 'GET', '/v2/projects');
for (const project of projects) {
returnData.push({
name: project.friendlyName,
value: project.id,
});
}
return returnData;
},
async getDatasets() {
const projectId = this.getCurrentNodeParameter('projectId');
const returnData = [];
const { datasets } = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/v2/projects/${projectId}/datasets`);
for (const dataset of datasets) {
returnData.push({
name: dataset.datasetReference.datasetId,
value: dataset.datasetReference.datasetId,
});
}
return returnData;
},
async getTables() {
const projectId = this.getCurrentNodeParameter('projectId');
const datasetId = this.getCurrentNodeParameter('datasetId');
const returnData = [];
const { tables } = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/v2/projects/${projectId}/datasets/${datasetId}/tables`);
for (const table of tables) {
returnData.push({
name: table.tableReference.tableId,
value: table.tableReference.tableId,
});
}
return returnData;
},
},
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const length = items.length;
const qs = {};
let responseData;
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
if (resource === 'record') {
if (operation === 'create') {
const projectId = this.getNodeParameter('projectId', 0);
const datasetId = this.getNodeParameter('datasetId', 0);
const tableId = this.getNodeParameter('tableId', 0);
const rows = [];
const body = {};
for (let i = 0; i < length; i++) {
const options = this.getNodeParameter('options', i);
Object.assign(body, options);
if (body.traceId === undefined) {
body.traceId = (0, uuid_1.v4)();
}
const columns = this.getNodeParameter('columns', i);
const columnList = columns.split(',').map((column) => column.trim());
const record = {};
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 GenericFunctions_1.googleApiRequest.call(this, 'POST', `/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/insertAll`, body);
returnData.push(responseData);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message } });
}
else {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
}
else if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', 0);
const projectId = this.getNodeParameter('projectId', 0);
const datasetId = this.getNodeParameter('datasetId', 0);
const tableId = this.getNodeParameter('tableId', 0);
const simple = this.getNodeParameter('simple', 0);
let fields;
if (simple === true) {
const { schema } = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}`, {});
fields = schema.fields.map((field) => field.name);
}
for (let i = 0; i < length; i++) {
try {
const options = this.getNodeParameter('options', i);
Object.assign(qs, options);
if (qs.selectedFields) {
fields = qs.selectedFields.split(',');
}
if (returnAll) {
responseData = await GenericFunctions_1.googleApiRequestAllItems.call(this, 'rows', 'GET', `/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/data`, {}, qs);
}
else {
qs.maxResults = this.getNodeParameter('limit', i);
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/data`, {}, qs);
}
responseData = simple ? (0, GenericFunctions_1.simplify)(responseData.rows, fields) : responseData.rows;
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } });
returnData.push(...executionErrorData);
continue;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { itemIndex: i });
}
}
}
else if (operation === 'query') {
const fallbackValue = 'Not set';
const projectId = this.getNodeParameter('projectId', 0);
const datasetId = this.getNodeParameter('datasetId', 0, fallbackValue);
const tableId = this.getNodeParameter('tableId', 0, fallbackValue);
const query = this.getNodeParameter('query', 0);
const simple = this.getNodeParameter('simple', 0);
let fields;
if (simple === true) {
const { schema } = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}`, {});
fields = schema.fields.map((field) => field.name);
}
for (let i = 0; i < length; i++) {
try {
const options = this.getNodeParameter('options', i);
const body = {};
Object.assign(body, options);
body.query = query;
body.useLegacySql = false;
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', `/v2/projects/${projectId}/queries`, body);
responseData = simple ? (0, GenericFunctions_1.simplify)(responseData.rows, fields) : responseData.rows;
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } });
returnData.push(...executionErrorData);
continue;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { itemIndex: i });
}
}
}
}
return this.prepareOutputData(returnData);
}
}
exports.GoogleBigQueryIk = GoogleBigQueryIk;
//# sourceMappingURL=GoogleBigQueryIk.node.js.map