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-02-18 17:58:10 +01:00
|
|
|
friendlyName: "DynamoDB",
|
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,
|
|
|
|
},
|
2021-02-02 17:26:15 +01:00
|
|
|
endpoint: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: false,
|
|
|
|
default: "https://dynamodb.us-east-1.amazonaws.com",
|
|
|
|
},
|
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,
|
2021-02-02 17:26:15 +01:00
|
|
|
customisable: true,
|
2021-01-22 13:22:28 +01:00
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: 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,
|
2021-02-02 17:26:15 +01:00
|
|
|
customisable: true,
|
2021-02-02 18:28:11 +01:00
|
|
|
readable: true,
|
2021-01-22 13:22:28 +01:00
|
|
|
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,
|
|
|
|
},
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
|
|
|
},
|
2021-02-02 17:26:15 +01:00
|
|
|
scan: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
2021-02-02 17:26:15 +01:00
|
|
|
customisable: true,
|
2021-02-02 18:28:11 +01:00
|
|
|
readable: true,
|
2021-01-22 13:22:28 +01:00
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
2021-02-02 17:26:15 +01:00
|
|
|
index: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
},
|
2021-01-15 14:24:36 +01:00
|
|
|
},
|
|
|
|
},
|
2021-02-02 17:26:15 +01:00
|
|
|
get: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
2021-02-02 17:26:15 +01:00
|
|
|
customisable: true,
|
2021-02-02 18:28:11 +01:00
|
|
|
readable: true,
|
2021-01-22 13:22:28 +01:00
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
2021-02-02 17:26:15 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QUERY_TYPES.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: FIELD_TYPES.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QUERY_TYPES.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2021-01-22 13:22:28 +01:00
|
|
|
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()
|
2021-02-02 17:26:15 +01:00
|
|
|
let options = {
|
|
|
|
correctClockSkew: true,
|
|
|
|
}
|
|
|
|
if (config.endpoint) {
|
|
|
|
options.endpoint = config.endpoint
|
|
|
|
}
|
|
|
|
this.client = new AWS.DynamoDB.DocumentClient({
|
|
|
|
correctClockSkew: true,
|
|
|
|
})
|
2020-11-26 17:46:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
AWS.config.update(this.config)
|
|
|
|
}
|
|
|
|
|
2021-01-15 14:24:36 +01:00
|
|
|
async create(query) {
|
2021-02-02 17:26:15 +01:00
|
|
|
const params = {
|
2021-01-15 14:24:36 +01:00
|
|
|
TableName: query.table,
|
2021-02-02 17:26:15 +01:00
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.put(params).promise()
|
2021-01-15 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:39:47 +01:00
|
|
|
async read(query) {
|
2021-02-02 17:26:15 +01:00
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
2021-01-15 14:24:36 +01:00
|
|
|
...query.json,
|
2021-02-02 17:26:15 +01:00
|
|
|
}
|
|
|
|
if (query.index) {
|
|
|
|
params.IndexName = query.index
|
|
|
|
}
|
|
|
|
const response = await this.client.query(params).promise()
|
|
|
|
if (response.Items) {
|
|
|
|
return response.Items
|
|
|
|
}
|
2021-01-15 14:24:36 +01:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:26:15 +01:00
|
|
|
async scan(query) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
if (query.index) {
|
|
|
|
params.IndexName = query.index
|
|
|
|
}
|
|
|
|
const response = await this.client.scan(params).promise()
|
|
|
|
if (response.Items) {
|
|
|
|
return response.Items
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(query) {
|
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.get(params).promise()
|
|
|
|
}
|
|
|
|
|
2021-01-15 14:24:36 +01:00
|
|
|
async update(query) {
|
2021-02-02 17:26:15 +01:00
|
|
|
const params = {
|
2021-01-15 14:24:36 +01:00
|
|
|
TableName: query.Table,
|
|
|
|
...query.json,
|
2021-02-02 17:26:15 +01:00
|
|
|
}
|
|
|
|
return this.client.update(params).promise()
|
2021-01-15 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete(query) {
|
2021-02-02 17:26:15 +01:00
|
|
|
const params = {
|
|
|
|
TableName: query.table,
|
|
|
|
...query.json,
|
|
|
|
}
|
|
|
|
return this.client.delete(params).promise()
|
2020-11-26 17:46:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2021-01-11 18:18:22 +01:00
|
|
|
schema: SCHEMA,
|
2020-11-26 17:46:36 +01:00
|
|
|
integration: DynamoDBIntegration,
|
|
|
|
}
|