budibase/packages/server/src/integrations/dynamodb.js

128 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-11-26 17:46:36 +01:00
const AWS = require("aws-sdk")
2021-01-13 17:39:47 +01:00
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
2020-11-26 17:46:36 +01:00
2021-01-11 18:18:22 +01:00
const SCHEMA = {
datasource: {
region: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
2021-01-11 18:18:22 +01:00
required: true,
default: "us-east-1",
},
accessKeyId: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.PASSWORD,
2021-01-11 18:18:22 +01:00
required: true,
},
secretKey: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.PASSWORD,
2021-01-11 18:18:22 +01:00
required: true,
},
2020-11-26 17:46:36 +01:00
},
2021-01-11 18:18:22 +01:00
query: {
2021-01-15 14:24:36 +01:00
create: {
DynamoConfig: {
type: QUERY_TYPES.FIELDS,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
},
customisable: true,
},
},
},
2021-01-13 17:39:47 +01:00
read: {
DynamoConfig: {
type: QUERY_TYPES.FIELDS,
fields: {
2021-01-15 14:24:36 +01:00
table: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
required: true,
},
2021-01-15 14:24:36 +01:00
index: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
},
2021-01-15 14:24:36 +01:00
customisable: true,
},
},
},
update: {
DynamoConfig: {
type: QUERY_TYPES.FIELDS,
fields: {
table: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
2021-01-15 14:24:36 +01:00
required: true,
2021-01-13 17:39:47 +01:00
},
2021-01-15 14:24:36 +01:00
customisable: true,
},
},
},
delete: {
"Dynamo Partition Key": {
type: QUERY_TYPES.FIELDS,
fields: {
table: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
2021-01-15 14:24:36 +01:00
required: true,
2021-01-13 17:39:47 +01:00
},
2021-01-15 14:24:36 +01:00
key: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
2021-01-15 14:24:36 +01:00
required: true,
2021-01-13 17:39:47 +01:00
},
},
2021-01-11 18:18:22 +01:00
},
2021-01-13 17:39:47 +01:00
},
2020-11-26 17:46:36 +01:00
},
}
class DynamoDBIntegration {
constructor(config) {
this.config = config
this.connect()
this.client = new AWS.DynamoDB.DocumentClient()
}
async connect() {
AWS.config.update(this.config)
}
2021-01-15 14:24:36 +01:00
async create(query) {
const response = await this.client.query({
TableName: query.table,
Item: query.json,
})
return response
}
2021-01-13 17:39:47 +01:00
async read(query) {
2020-11-26 17:46:36 +01:00
const response = await this.client.query({
2021-01-13 17:39:47 +01:00
TableName: query.Table,
2021-01-15 14:24:36 +01:00
...query.json,
})
return response
}
async update(query) {
const response = await this.client.query({
TableName: query.Table,
...query.json,
})
return response
}
async delete(query) {
const response = await this.client.query({
TableName: query.Table,
Key: {
id: query.key,
},
2020-11-26 17:46:36 +01:00
})
return response
}
}
module.exports = {
2021-01-11 18:18:22 +01:00
schema: SCHEMA,
2020-11-26 17:46:36 +01:00
integration: DynamoDBIntegration,
}