Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Google Cloud Firestore Node): Add support for service account and document creation with id #9713

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) => {
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions packages/nodes-base/nodes/Google/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading