mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
feat(Google Cloud Firestore Node): Add support for service account and document creation with id (#9713)
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
committed by
GitHub
parent
8f970b5d37
commit
cb1bbf5fd3
@@ -109,6 +109,18 @@ export const documentFields: INodeProperties[] = [
|
|||||||
description: 'Collection name',
|
description: 'Collection name',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Document ID',
|
||||||
|
name: 'documentId',
|
||||||
|
type: 'string',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: ['document'],
|
||||||
|
operation: ['create'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
default: '',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Columns / Attributes',
|
displayName: 'Columns / Attributes',
|
||||||
name: 'columns',
|
name: 'columns',
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ import type {
|
|||||||
import { NodeApiError } from 'n8n-workflow';
|
import { NodeApiError } from 'n8n-workflow';
|
||||||
|
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
|
import { getGoogleAccessToken } from '../../GenericFunctions';
|
||||||
|
|
||||||
export async function googleApiRequest(
|
export async function googleApiRequest(
|
||||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||||
method: IHttpRequestMethods,
|
method: IHttpRequestMethods,
|
||||||
resource: string,
|
resource: string,
|
||||||
|
|
||||||
body: any = {},
|
body: any = {},
|
||||||
qs: IDataObject = {},
|
qs: IDataObject = {},
|
||||||
uri: string | null = null,
|
uri: string | null = null,
|
||||||
@@ -37,12 +37,20 @@ export async function googleApiRequest(
|
|||||||
delete options.body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let credentialType = 'googleFirebaseCloudFirestoreOAuth2Api';
|
||||||
|
const authentication = this.getNodeParameter('authentication', 0) as string;
|
||||||
|
|
||||||
|
if (authentication === 'serviceAccount') {
|
||||||
|
const credentials = await this.getCredentials('googleApi');
|
||||||
|
credentialType = 'googleApi';
|
||||||
|
|
||||||
|
const { access_token } = await getGoogleAccessToken.call(this, credentials, 'firestore');
|
||||||
|
|
||||||
|
(options.headers as IDataObject).Authorization = `Bearer ${access_token}`;
|
||||||
|
}
|
||||||
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
return await this.helpers.requestOAuth2.call(
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
||||||
this,
|
|
||||||
'googleFirebaseCloudFirestoreOAuth2Api',
|
|
||||||
options,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,9 +40,40 @@ export class GoogleFirebaseCloudFirestore implements INodeType {
|
|||||||
{
|
{
|
||||||
name: 'googleFirebaseCloudFirestoreOAuth2Api',
|
name: 'googleFirebaseCloudFirestoreOAuth2Api',
|
||||||
required: true,
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: ['googleFirebaseCloudFirestoreOAuth2Api'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'googleApi',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: ['serviceAccount'],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
properties: [
|
||||||
|
{
|
||||||
|
displayName: 'Authentication',
|
||||||
|
name: 'authentication',
|
||||||
|
type: 'options',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
||||||
|
name: 'OAuth2 (recommended)',
|
||||||
|
value: 'googleFirebaseCloudFirestoreOAuth2Api',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Service Account',
|
||||||
|
value: 'serviceAccount',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
default: 'googleFirebaseCloudFirestoreOAuth2Api',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Resource',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
@@ -157,6 +188,7 @@ export class GoogleFirebaseCloudFirestore implements INodeType {
|
|||||||
items.map(async (item: IDataObject, i: number) => {
|
items.map(async (item: IDataObject, i: number) => {
|
||||||
const collection = this.getNodeParameter('collection', i) as string;
|
const collection = this.getNodeParameter('collection', i) as string;
|
||||||
const columns = this.getNodeParameter('columns', i) as string;
|
const columns = this.getNodeParameter('columns', i) as string;
|
||||||
|
const documentId = this.getNodeParameter('documentId', i) as string;
|
||||||
const columnList = columns.split(',').map((column) => column.trim());
|
const columnList = columns.split(',').map((column) => column.trim());
|
||||||
const document = { fields: {} };
|
const document = { fields: {} };
|
||||||
columnList.map((column) => {
|
columnList.map((column) => {
|
||||||
@@ -174,6 +206,7 @@ export class GoogleFirebaseCloudFirestore implements INodeType {
|
|||||||
'POST',
|
'POST',
|
||||||
`/${projectId}/databases/${database}/documents/${collection}`,
|
`/${projectId}/databases/${database}/documents/${collection}`,
|
||||||
document,
|
document,
|
||||||
|
{ documentId },
|
||||||
);
|
);
|
||||||
|
|
||||||
responseData.id = (responseData.name as string).split('/').pop();
|
responseData.id = (responseData.name as string).split('/').pop();
|
||||||
|
|||||||
@@ -52,6 +52,10 @@ const googleServiceAccountScopes = {
|
|||||||
'https://www.googleapis.com/auth/cloud-translation',
|
'https://www.googleapis.com/auth/cloud-translation',
|
||||||
'https://www.googleapis.com/auth/cloud-platform',
|
'https://www.googleapis.com/auth/cloud-platform',
|
||||||
],
|
],
|
||||||
|
firestore: [
|
||||||
|
'https://www.googleapis.com/auth/datastore',
|
||||||
|
'https://www.googleapis.com/auth/firebase',
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
type GoogleServiceAccount = keyof typeof googleServiceAccountScopes;
|
type GoogleServiceAccount = keyof typeof googleServiceAccountScopes;
|
||||||
|
|||||||
Reference in New Issue
Block a user