Skip to content

Commit

Permalink
feat(Google Cloud Realtime Database Node): Make it possible to select…
Browse files Browse the repository at this point in the history
… region (#3096)

* upstream merge

* 🔨 fixed bug, replaced icon with svg, added ability to get whole db object

* 🔨 optimization

* 🔨 option for region in credentials

* 🐛 Fix region default

* ⚡ Remove dot

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
  • Loading branch information
3 people committed Apr 14, 2022
1 parent 3e5d981 commit 176538e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,25 @@ export class GoogleFirebaseRealtimeDatabaseOAuth2Api implements ICredentialType
type: 'hidden',
default: scopes.join(' '),
},
{
displayName: 'Region',
name: 'region',
type: 'options',
default: 'firebaseio.com',
options: [
{
name: 'us-central1',
value: 'firebaseio.com',
},
{
name: 'europe-west1',
value: 'europe-west1.firebasedatabase.app',
},
{
name: 'asia-southeast1',
value: 'asia-southeast1.firebasedatabase.app',
},
],
},
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ import {
} from 'n8n-core';

import {
IDataObject, NodeApiError,
IDataObject, JsonObject, NodeApiError,
} from 'n8n-workflow';

export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, projectId: string, method: string, resource: string, body: any = {}, qs: IDataObject = {}, headers: IDataObject = {}, uri: string | null = null): Promise<any> { // tslint:disable-line:no-any

const { region } = await this.getCredentials('googleFirebaseRealtimeDatabaseOAuth2Api') as IDataObject;

const options: OptionsWithUrl = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
url: uri || `https://${projectId}.firebaseio.com/${resource}.json`,
url: uri || `https://${projectId}.${region}/${resource}.json`,
json: true,
};

try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
Expand All @@ -34,7 +37,7 @@ export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleF

return await this.helpers.requestOAuth2!.call(this, 'googleFirebaseRealtimeDatabaseOAuth2Api', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
Expand All @@ -22,7 +23,7 @@ export class RealtimeDatabase implements INodeType {
description: INodeTypeDescription = {
displayName: 'Google Cloud Realtime Database',
name: 'googleFirebaseRealtimeDatabase',
icon: 'file:googleFirebaseRealtimeDatabase.png',
icon: 'file:googleFirebaseRealtimeDatabase.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
Expand Down Expand Up @@ -53,6 +54,7 @@ export class RealtimeDatabase implements INodeType {
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Create',
Expand Down Expand Up @@ -81,17 +83,35 @@ export class RealtimeDatabase implements INodeType {
},
],
default: 'create',
description: 'The operation to perform.',
required: true,
},
{
displayName: 'Object Path',
name: 'path',
type: 'string',
default: '',
placeholder: '/app/users',
description: 'Object path on database. With leading slash. Do not append .json.',
placeholder: 'e.g. /app/users',
description: 'Object path on database. Do not append .json.',
required: true,
displayOptions: {
hide: {
'operation': [ 'get' ],
},
},
},
{
displayName: 'Object Path',
name: 'path',
type: 'string',
default: '',
placeholder: 'e.g. /app/users',
description: 'Object path on database. Do not append .json.',
hint: 'Leave blank to get a whole database object',
displayOptions: {
show: {
'operation': [ 'get' ],
},
},
},
{
displayName: 'Columns / Attributes',
Expand Down Expand Up @@ -121,36 +141,47 @@ export class RealtimeDatabase implements INodeType {
): Promise<INodePropertyOptions[]> {
const projects = await googleApiRequestAllItems.call(
this,
'projects',
'',
'GET',
'results',
{},
{},
{},
'https://firebase.googleapis.com/v1beta1/projects',
);
const returnData = projects.map((o: IDataObject) => ({ name: o.projectId, value: o.projectId })) as INodePropertyOptions[];

const returnData = projects
// select only realtime database projects
.filter((project: IDataObject) => (project.resources as IDataObject).realtimeDatabaseInstance )
.map((project: IDataObject) => (
{
name: project.projectId,
value: (project.resources as IDataObject).realtimeDatabaseInstance,
}
)) as INodePropertyOptions[];

return returnData;
},
},
};

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {

const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
let responseData;
const operation = this.getNodeParameter('operation', 0) as string;
//https://firebase.google.com/docs/reference/rest/database


if (['push', 'create', 'update'].includes(operation) && items.length === 1 && Object.keys(items[0].json).length === 0) {
throw new NodeOperationError(this.getNode(), `The ${operation} operation needs input data`);
}

for (let i = 0; i < length; i++) {
try {
const projectId = this.getNodeParameter('projectId', i) as string;

let method = 'GET', attributes = '';
const document: IDataObject = {};
if (operation === 'create') {
Expand Down Expand Up @@ -194,7 +225,7 @@ export class RealtimeDatabase implements INodeType {
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
returnData.push({ error: (error as JsonObject).message });
continue;
}
throw error;
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 176538e

Please sign in to comment.