mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-27 16:37:42 +00:00
* initial commit * feature-2152 bigquery integration * Typo fix * moved bigquery to databse type
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { ConnectionTestResult, QueryService, QueryResult, QueryError } from '@tooljet-plugins/common'
|
|
|
|
import { deleteItem, getItem, listTables, queryTable, scanTable } from './operations';
|
|
const AWS = require('aws-sdk');
|
|
import { SourceOptions, QueryOptions } from './types'
|
|
|
|
export default class DynamodbQueryService implements QueryService {
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions): Promise<QueryResult> {
|
|
const operation = queryOptions.operation;
|
|
const client = await this.getConnection(sourceOptions, { operation });
|
|
let result = {};
|
|
|
|
try {
|
|
switch (operation) {
|
|
case 'list_tables':
|
|
result = await listTables(client);
|
|
break;
|
|
case 'get_item':
|
|
result = await getItem(client, queryOptions.table, JSON.parse(queryOptions.key));
|
|
break;
|
|
case 'delete_item':
|
|
result = await deleteItem(client, queryOptions.table, JSON.parse(queryOptions.key));
|
|
break;
|
|
case 'query_table':
|
|
result = await queryTable(client, JSON.parse(queryOptions.query_condition));
|
|
break;
|
|
case 'scan_table':
|
|
result = await scanTable(client, JSON.parse(queryOptions.scan_condition));
|
|
break;
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw new QueryError('Query could not be completed', err.message, {});
|
|
}
|
|
|
|
return {
|
|
status: 'ok',
|
|
data: result,
|
|
};
|
|
}
|
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
|
const client = await this.getConnection(sourceOptions, { operation: 'list_tables' });
|
|
await listTables(client);
|
|
|
|
return {
|
|
status: 'ok',
|
|
};
|
|
}
|
|
|
|
async getConnection(sourceOptions: SourceOptions, options?: object): Promise<any> {
|
|
const credentials = new AWS.Credentials(sourceOptions['access_key'], sourceOptions['secret_key']);
|
|
const region = sourceOptions['region'];
|
|
|
|
if (options['operation'] == 'list_tables') {
|
|
return new AWS.DynamoDB({ region, credentials });
|
|
} else {
|
|
return new AWS.DynamoDB.DocumentClient({ region, credentials });
|
|
}
|
|
}
|
|
}
|