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

185 lines
3.6 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 = {
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,
},
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: {
type: QUERY_TYPES.FIELDS,
customisable: true,
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: {
type: QUERY_TYPES.FIELDS,
customisable: true,
readable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
2021-01-15 14:24:36 +01:00
},
index: {
type: FIELD_TYPES.STRING,
},
2021-01-15 14:24:36 +01:00
},
},
scan: {
type: QUERY_TYPES.FIELDS,
customisable: true,
readable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
2021-01-15 14:24:36 +01:00
},
index: {
type: FIELD_TYPES.STRING,
},
2021-01-15 14:24:36 +01:00
},
},
get: {
type: QUERY_TYPES.FIELDS,
customisable: true,
readable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
},
},
},
update: {
type: QUERY_TYPES.FIELDS,
customisable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
},
},
},
delete: {
type: QUERY_TYPES.FIELDS,
customisable: true,
fields: {
table: {
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()
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) {
const params = {
2021-01-15 14:24:36 +01:00
TableName: query.table,
...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) {
const params = {
TableName: query.table,
2021-01-15 14:24:36 +01:00
...query.json,
}
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
}
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) {
const params = {
2021-01-15 14:24:36 +01:00
TableName: query.Table,
...query.json,
}
return this.client.update(params).promise()
2021-01-15 14:24:36 +01:00
}
async delete(query) {
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,
}