mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08: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:
parent
8f970b5d37
commit
cb1bbf5fd3
|
@ -109,6 +109,18 @@ export const documentFields: INodeProperties[] = [
|
|||
description: 'Collection name',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Document ID',
|
||||
name: 'documentId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['document'],
|
||||
operation: ['create'],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Columns / Attributes',
|
||||
name: 'columns',
|
||||
|
|
|
@ -9,12 +9,12 @@ import type {
|
|||
import { NodeApiError } from 'n8n-workflow';
|
||||
|
||||
import moment from 'moment-timezone';
|
||||
import { getGoogleAccessToken } from '../../GenericFunctions';
|
||||
|
||||
export async function googleApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
resource: string,
|
||||
|
||||
body: any = {},
|
||||
qs: IDataObject = {},
|
||||
uri: string | null = null,
|
||||
|
@ -37,12 +37,20 @@ export async function googleApiRequest(
|
|||
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
|
||||
return await this.helpers.requestOAuth2.call(
|
||||
this,
|
||||
'googleFirebaseCloudFirestoreOAuth2Api',
|
||||
options,
|
||||
);
|
||||
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
|
|
|
@ -40,9 +40,40 @@ export class GoogleFirebaseCloudFirestore implements INodeType {
|
|||
{
|
||||
name: 'googleFirebaseCloudFirestoreOAuth2Api',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: ['googleFirebaseCloudFirestoreOAuth2Api'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'googleApi',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: ['serviceAccount'],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
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',
|
||||
name: 'resource',
|
||||
|
@ -157,6 +188,7 @@ export class GoogleFirebaseCloudFirestore implements INodeType {
|
|||
items.map(async (item: IDataObject, i: number) => {
|
||||
const collection = this.getNodeParameter('collection', 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 document = { fields: {} };
|
||||
columnList.map((column) => {
|
||||
|
@ -174,6 +206,7 @@ export class GoogleFirebaseCloudFirestore implements INodeType {
|
|||
'POST',
|
||||
`/${projectId}/databases/${database}/documents/${collection}`,
|
||||
document,
|
||||
{ documentId },
|
||||
);
|
||||
|
||||
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-platform',
|
||||
],
|
||||
firestore: [
|
||||
'https://www.googleapis.com/auth/datastore',
|
||||
'https://www.googleapis.com/auth/firebase',
|
||||
],
|
||||
};
|
||||
|
||||
type GoogleServiceAccount = keyof typeof googleServiceAccountScopes;
|
||||
|
|
Loading…
Reference in a new issue