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 = {
|
2021-01-15 18:29:46 +01:00
|
|
|
docs: "https://github.com/dabit3/dynamodb-documentclient-cheat-sheet",
|
2021-01-11 18:18:22 +01:00
|
|
|
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: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
2021-01-22 13:22:28 +01:00
|
|
|
customisable: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
|
|
|
},
|
2021-01-13 17:39:47 +01:00
|
|
|
read: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
2021-01-22 13:22:28 +01:00
|
|
|
index: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
},
|
|
|
|
customisable: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
2021-01-22 13:22:28 +01:00
|
|
|
customisable: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
delete: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
key: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
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,
|
|
|
|
}
|