2022-02-07 10:02:38 +00:00
|
|
|
import { ConnectionTestResult, QueryService, QueryResult, QueryError } from '@tooljet-plugins/common'
|
2022-01-17 07:08:17 +00:00
|
|
|
|
2021-07-16 13:06:19 +00:00
|
|
|
import { deleteItem, getItem, listTables, queryTable, scanTable } from './operations';
|
2021-09-21 13:48:28 +00:00
|
|
|
const AWS = require('aws-sdk');
|
2022-01-24 13:59:21 +00:00
|
|
|
import { SourceOptions, QueryOptions } from './types'
|
2021-07-16 13:06:19 +00:00
|
|
|
|
|
|
|
|
export default class DynamodbQueryService implements QueryService {
|
2022-01-24 13:59:21 +00:00
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions): Promise<QueryResult> {
|
2021-07-16 13:06:19 +00:00
|
|
|
const operation = queryOptions.operation;
|
2021-07-17 07:59:28 +00:00
|
|
|
const client = await this.getConnection(sourceOptions, { operation });
|
2021-09-21 13:48:28 +00:00
|
|
|
let result = {};
|
2021-07-16 13:06:19 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
switch (operation) {
|
|
|
|
|
case 'list_tables':
|
|
|
|
|
result = await listTables(client);
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
2021-07-16 13:06:19 +00:00
|
|
|
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));
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
2021-07-16 13:06:19 +00:00
|
|
|
case 'query_table':
|
2022-01-24 13:59:21 +00:00
|
|
|
result = await queryTable(client, JSON.parse(queryOptions.query_condition));
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
2021-07-16 13:06:19 +00:00
|
|
|
case 'scan_table':
|
2022-01-24 13:59:21 +00:00
|
|
|
result = await scanTable(client, JSON.parse(queryOptions.scan_condition));
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
2021-07-16 13:06:19 +00:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
2022-02-07 10:02:38 +00:00
|
|
|
throw new QueryError('Query could not be completed', err.message, {});
|
2021-07-16 13:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result,
|
|
|
|
|
};
|
2021-07-16 13:06:19 +00:00
|
|
|
}
|
2021-07-17 07:59:28 +00:00
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
2021-07-18 07:43:19 +00:00
|
|
|
const client = await this.getConnection(sourceOptions, { operation: 'list_tables' });
|
|
|
|
|
await listTables(client);
|
|
|
|
|
|
|
|
|
|
return {
|
2021-09-21 13:48:28 +00:00
|
|
|
status: 'ok',
|
|
|
|
|
};
|
2021-07-18 07:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async getConnection(sourceOptions: SourceOptions, options?: object): Promise<any> {
|
2021-07-17 07:59:28 +00:00
|
|
|
const credentials = new AWS.Credentials(sourceOptions['access_key'], sourceOptions['secret_key']);
|
|
|
|
|
const region = sourceOptions['region'];
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (options['operation'] == 'list_tables') {
|
2021-07-17 07:59:28 +00:00
|
|
|
return new AWS.DynamoDB({ region, credentials });
|
|
|
|
|
} else {
|
2021-09-21 13:48:28 +00:00
|
|
|
return new AWS.DynamoDB.DocumentClient({ region, credentials });
|
2021-07-17 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-07-16 13:06:19 +00:00
|
|
|
}
|