ToolJet/server/plugins/datasources/firestore/index.ts
Akshay 5b30aa2007
Chore: Setup pipeline (#1539)
* github actions for PR and push to develop branch

* test workflow

* move to workflows folder

* add setup node action

* modify build

* specify npm version

* config unit test

* specify host postgres

* specify container to run on

* add postgresql dependency

* add specify ws adapter for test

* add e2e test

* fix linting

* only log errors on tests

* update eslint config

* fix linting

* run e2e test in silent mode

* fix library app spec

* dont send email on test env

* fix org scope

* mock env vars

* remove reset modules

* force colors

* explicitly close db connection

* add eslint rule for floating promises

* update workflow

* fix floating promise

* fix lint

* update workflow

* run on all push and pulls

* update lint check files

* simplify workflow

* increase js heap size on env

* separate lint and build

Co-authored-by: arpitnath <arpitnath42@gmail.com>
2021-12-10 08:43:05 +05:30

95 lines
3 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { parseJson } from 'src/helpers/utils.helper';
import { ConnectionTestResult } from 'src/modules/data_sources/connection_test_result.type';
import { QueryError } from 'src/modules/data_sources/query.error';
import { QueryResult } from 'src/modules/data_sources/query_result.type';
import { QueryService } from 'src/modules/data_sources/query_service.interface';
import {
addDocument,
bulkUpdate,
deleteDocument,
getDocument,
queryCollection,
setDocument,
updateDocument,
} from './operations';
const { Firestore } = require('@google-cloud/firestore');
@Injectable()
export default class FirestoreQueryService implements QueryService {
async run(sourceOptions: any, queryOptions: any): Promise<QueryResult> {
const firestore = await this.getConnection(sourceOptions);
const operation = queryOptions.operation;
let result = {};
try {
switch (operation) {
case 'query_collection':
result = await queryCollection(
firestore,
queryOptions.path,
parseInt(queryOptions.limit),
queryOptions.where_operation,
queryOptions.where_field,
queryOptions.where_value,
queryOptions.order_field,
queryOptions.order_type
);
break;
case 'get_document':
result = await getDocument(firestore, queryOptions.path);
break;
case 'set_document':
result = await setDocument(firestore, queryOptions.path, queryOptions.body);
break;
case 'add_document':
result = await addDocument(firestore, queryOptions.path, queryOptions.body);
break;
case 'update_document':
result = await updateDocument(firestore, queryOptions.path, queryOptions.body);
break;
case 'delete_document':
result = await deleteDocument(firestore, queryOptions.path);
break;
case 'bulk_update':
result = await bulkUpdate(
firestore,
queryOptions.collection,
JSON.parse(queryOptions.records),
queryOptions['document_id_key']
);
break;
}
} catch (error) {
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
async testConnection(sourceOptions: object): Promise<ConnectionTestResult> {
const client = await this.getConnection(sourceOptions);
await getDocument(client, 'test/test');
return {
status: 'ok',
};
}
async getConnection(sourceOptions: any): Promise<any> {
const gcpKey = parseJson(sourceOptions['gcp_key'], 'GCP key could not be parsed as a valid JSON object');
const firestore = new Firestore({
projectId: gcpKey['project_id'],
credentials: {
private_key: gcpKey['private_key'],
client_email: gcpKey['client_email'],
},
});
return firestore;
}
}