mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
0448feec56
* ⚡ Initial setup
* 👕 Update `.eslintignore`
* 👕 Autofix node-param-default-missing (#3173)
* 🔥 Remove duplicate key
* 👕 Add exceptions
* 📦 Update package-lock.json
* 👕 Apply `node-class-description-inputs-wrong-trigger-node` (#3176)
* 👕 Apply `node-class-description-inputs-wrong-regular-node` (#3177)
* 👕 Apply `node-class-description-outputs-wrong` (#3178)
* 👕 Apply `node-execute-block-double-assertion-for-items` (#3179)
* 👕 Apply `node-param-default-wrong-for-collection` (#3180)
* 👕 Apply node-param-default-wrong-for-boolean (#3181)
* Autofixed default missing
* Autofixed booleans, worked well
* ⚡ Fix params
* ⏪ Undo exempted autofixes
* 📦 Update package-lock.json
* 👕 Apply node-class-description-missing-subtitle (#3182)
* ⚡ Fix missing comma
* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3184)
* 👕 Add exception for `node-class-description-missing-subtitle`
* 👕 Apply `node-param-default-wrong-for-multi-options` (#3185)
* 👕 Apply `node-param-collection-type-unsorted-items` (#3186)
* Missing coma
* 👕 Apply `node-param-default-wrong-for-simplify` (#3187)
* 👕 Apply `node-param-description-comma-separated-hyphen` (#3190)
* 👕 Apply `node-param-description-empty-string` (#3189)
* 👕 Apply `node-param-description-excess-inner-whitespace` (#3191)
* Rule looks good
* Add whitespace rule in eslint config
* :zao: fix
* 👕 Apply `node-param-description-identical-to-display-name` (#3193)
* 👕 Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195)
* ⏪ Revert ":zao: fix"
This reverts commit ef8a76f3df
.
* 👕 Apply `node-param-description-missing-for-simplify` (#3196)
* 👕 Apply `node-param-description-missing-final-period` (#3194)
* Rule working as intended
* Add rule to eslint
* 👕 Apply node-param-description-missing-for-return-all (#3197)
* ⚡ Restore `lintfix` command
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
244 lines
6 KiB
TypeScript
244 lines
6 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
JsonObject,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
googleApiRequest,
|
|
googleApiRequestAllItems,
|
|
} from './GenericFunctions';
|
|
|
|
export class RealtimeDatabase implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Google Cloud Realtime Database',
|
|
name: 'googleFirebaseRealtimeDatabase',
|
|
icon: 'file:googleFirebaseRealtimeDatabase.svg',
|
|
group: ['input'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"]}}',
|
|
description: 'Interact with Google Firebase - Realtime Database API',
|
|
defaults: {
|
|
name: 'Google Cloud Realtime Database',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'googleFirebaseRealtimeDatabaseOAuth2Api',
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Project ID',
|
|
name: 'projectId',
|
|
type: 'options',
|
|
default: '',
|
|
typeOptions: {
|
|
loadOptionsMethod: 'getProjects',
|
|
},
|
|
description: 'As displayed in firebase console URL',
|
|
required: true,
|
|
},
|
|
{
|
|
displayName: 'Operation',
|
|
name: 'operation',
|
|
type: 'options',
|
|
noDataExpression: true,
|
|
options: [
|
|
{
|
|
name: 'Create',
|
|
value: 'create',
|
|
description: 'Write data to a database',
|
|
},
|
|
{
|
|
name: 'Delete',
|
|
value: 'delete',
|
|
description: 'Delete data from a database',
|
|
},
|
|
{
|
|
name: 'Get',
|
|
value: 'get',
|
|
description: 'Get a record from a database',
|
|
},
|
|
{
|
|
name: 'Push',
|
|
value: 'push',
|
|
description: 'Append to a list of data',
|
|
},
|
|
{
|
|
name: 'Update',
|
|
value: 'update',
|
|
description: 'Update item on a database',
|
|
},
|
|
],
|
|
default: 'create',
|
|
required: true,
|
|
},
|
|
{
|
|
displayName: 'Object Path',
|
|
name: 'path',
|
|
type: 'string',
|
|
default: '',
|
|
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',
|
|
name: 'attributes',
|
|
type: 'string',
|
|
default: '',
|
|
displayOptions: {
|
|
show: {
|
|
operation: [
|
|
'create',
|
|
'push',
|
|
'update',
|
|
],
|
|
},
|
|
},
|
|
description: 'Attributes to save',
|
|
required: true,
|
|
placeholder: 'age, name, city',
|
|
},
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
async getProjects(
|
|
this: ILoadOptionsFunctions,
|
|
): Promise<INodePropertyOptions[]> {
|
|
const projects = await googleApiRequestAllItems.call(
|
|
this,
|
|
'',
|
|
'GET',
|
|
'results',
|
|
{},
|
|
{},
|
|
{},
|
|
'https://firebase.googleapis.com/v1beta1/projects',
|
|
);
|
|
|
|
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;
|
|
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') {
|
|
method = 'PUT';
|
|
attributes = this.getNodeParameter('attributes', i) as string;
|
|
} else if (operation === 'delete') {
|
|
method = 'DELETE';
|
|
} else if (operation === 'get') {
|
|
method = 'GET';
|
|
} else if (operation === 'push') {
|
|
method = 'POST';
|
|
attributes = this.getNodeParameter('attributes', i) as string;
|
|
} else if (operation === 'update') {
|
|
method = 'PATCH';
|
|
attributes = this.getNodeParameter('attributes', i) as string;
|
|
}
|
|
|
|
if (attributes) {
|
|
const attributeList = attributes.split(',').map(el => el.trim());
|
|
attributeList.map((attribute: string) => {
|
|
if (items[i].json.hasOwnProperty(attribute)) {
|
|
document[attribute] = items[i].json[attribute];
|
|
}
|
|
});
|
|
}
|
|
|
|
responseData = await googleApiRequest.call(
|
|
this,
|
|
projectId,
|
|
method,
|
|
this.getNodeParameter('path', i) as string,
|
|
document,
|
|
);
|
|
|
|
if (responseData === null) {
|
|
if (operation === 'get') {
|
|
throw new NodeApiError(this.getNode(), responseData, { message: `Requested entity was not found.` });
|
|
} else if (method === 'DELETE') {
|
|
responseData = { success: true };
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: (error as JsonObject).message });
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
if (Array.isArray(responseData)) {
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
} else if (typeof responseData === 'string' || typeof responseData === 'number') {
|
|
returnData.push({ [this.getNodeParameter('path', i) as string]: responseData } as IDataObject);
|
|
} else {
|
|
returnData.push(responseData as IDataObject);
|
|
}
|
|
}
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|