2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
2022-08-11 14:50:05 +02:00
|
|
|
DatasourceFieldType,
|
|
|
|
QueryType,
|
2022-08-11 12:48:58 +02:00
|
|
|
IntegrationBase,
|
|
|
|
} from "@budibase/types"
|
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",
|
2022-06-23 12:35:57 +02:00
|
|
|
type: "Non-relational",
|
2021-06-24 19:16:48 +02:00
|
|
|
datasource: {
|
|
|
|
region: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
default: "us-east-1",
|
|
|
|
},
|
|
|
|
accessKeyId: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.PASSWORD,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
secretAccessKey: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.PASSWORD,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
endpoint: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: false,
|
|
|
|
default: "https://dynamodb.us-east-1.amazonaws.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
read: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
index: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scan: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
index: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-29 16:46:33 +01:00
|
|
|
describe: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-12-29 16:46:33 +01:00
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-12-29 16:46:33 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
get: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
customisable: true,
|
|
|
|
readable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
delete: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
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
|
2022-04-12 19:49:46 +02:00
|
|
|
if (this.config.endpoint && !this.config.endpoint.includes("localhost")) {
|
2022-03-25 11:35:59 +01:00
|
|
|
this.connect()
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
let options = {
|
|
|
|
correctClockSkew: true,
|
2022-04-12 19:49:46 +02:00
|
|
|
region: this.config.region || AWS_REGION,
|
2021-06-24 19:16:48 +02:00
|
|
|
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,
|
|
|
|
}
|
2022-01-26 15:25:11 +01:00
|
|
|
const response = await this.client.query(params).promise()
|
|
|
|
if (response.Items) {
|
|
|
|
return response.Items
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-01-26 15:25:11 +01:00
|
|
|
return response
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|