2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
2021-06-27 00:09:46 +02:00
|
|
|
} from "../definitions/datasource"
|
2021-11-10 20:35:09 +01:00
|
|
|
import { IntegrationBase } from "./base/IntegrationBase"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
module DynamoModule {
|
|
|
|
const AWS = require("aws-sdk")
|
|
|
|
const { AWS_REGION } = require("../db/dynamoClient")
|
|
|
|
|
|
|
|
interface DynamoDBConfig {
|
|
|
|
region: string
|
|
|
|
accessKeyId: string
|
|
|
|
secretAccessKey: string
|
|
|
|
endpoint: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://github.com/dabit3/dynamodb-documentclient-cheat-sheet",
|
|
|
|
description:
|
|
|
|
"Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale.",
|
|
|
|
friendlyName: "DynamoDB",
|
|
|
|
datasource: {
|
|
|
|
region: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
default: "us-east-1",
|
|
|
|
},
|
|
|
|
accessKeyId: {
|
|
|
|
type: DatasourceFieldTypes.PASSWORD,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
secretAccessKey: {
|
|
|
|
type: DatasourceFieldTypes.PASSWORD,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
endpoint: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: false,
|
|
|
|
default: "https://dynamodb.us-east-1.amazonaws.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
index: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scan: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
index: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-29 16:46:33 +01:00
|
|
|
describe: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
get: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:35:09 +01:00
|
|
|
class DynamoDBIntegration implements IntegrationBase {
|
2021-06-24 19:16:48 +02:00
|
|
|
private config: DynamoDBConfig
|
|
|
|
private client: any
|
|
|
|
|
|
|
|
constructor(config: DynamoDBConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.connect()
|
|
|
|
let options = {
|
|
|
|
correctClockSkew: true,
|
|
|
|
endpoint: config.endpoint ? config.endpoint : undefined,
|
|
|
|
}
|
|
|
|
this.client = new AWS.DynamoDB.DocumentClient(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
this.disconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
AWS.config.update(this.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
AWS.config.update({
|
|
|
|
secretAccessKey: undefined,
|
|
|
|
accessKeyId: undefined,
|
|
|
|
region: AWS_REGION,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(query: { table: string; json: object }) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.put(params).promise()
|
|
|
|
}
|
|
|
|
|
|
|
|
async read(query: { table: string; json: object; index: null | string }) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
IndexName: query.index ? query.index : undefined,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
if (query.index) {
|
|
|
|
const response = await this.client.query(params).promise()
|
|
|
|
if (response.Items) {
|
|
|
|
return response.Items
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async scan(query: { table: string; json: object; index: null | string }) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
IndexName: query.index ? query.index : undefined,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
const response = await this.client.scan(params).promise()
|
|
|
|
if (response.Items) {
|
|
|
|
return response.Items
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:44:27 +01:00
|
|
|
async describe(query: { table: string }) {
|
2021-12-29 16:46:33 +01:00
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
}
|
|
|
|
return new AWS.DynamoDB().describeTable(params).promise()
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
async get(query: { table: string; json: object }) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.get(params).promise()
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(query: { table: string; json: object }) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.update(params).promise()
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(query: { table: string; json: object }) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.delete(params).promise()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: DynamoDBIntegration,
|
|
|
|
}
|
|
|
|
}
|