Browse Source

Some fixes for integrations, making Dynamo work correctly and allow local usage as well as using the API to try and make responses that are objects work better.

pull/1070/head
mike12345567 6 years ago
parent
commit
c74f1011d0
  1. 33
      packages/server/src/api/controllers/query.js
  2. 109
      packages/server/src/integrations/dynamodb.js
  3. 903
      packages/server/yarn.lock

33
packages/server/src/api/controllers/query.js

@ -1,8 +1,18 @@
const handlebars = require("handlebars")
const { processString } = require("@budibase/string-templates")
const CouchDB = require("../../db")
const { generateQueryID, getQueryParams } = require("../../db/utils")
const { integrations } = require("../../integrations")
function formatResponse(resp) {
if (typeof resp === "string") {
resp = JSON.parse(resp)
}
if (!Array.isArray(resp)) {
resp = [resp]
}
return resp
}
exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
@ -29,13 +39,12 @@ exports.save = async function(ctx) {
ctx.message = `Query ${query.name} saved successfully.`
}
function enrichQueryFields(fields, parameters) {
async function enrichQueryFields(fields, parameters) {
const enrichedQuery = {}
// enrich the fields with dynamic parameters
for (let key in fields) {
const template = handlebars.compile(fields[key])
enrichedQuery[key] = template(parameters)
for (let key of Object.keys(fields)) {
enrichedQuery[key] = await processString(fields[key], parameters)
}
if (enrichedQuery.json || enrichedQuery.customData) {
@ -62,9 +71,11 @@ exports.preview = async function(ctx) {
const { fields, parameters, queryVerb } = ctx.request.body
const enrichedQuery = enrichQueryFields(fields, parameters)
const enrichedQuery = await enrichQueryFields(fields, parameters)
ctx.body = await new Integration(datasource.config)[queryVerb](enrichedQuery)
ctx.body = formatResponse(
await new Integration(datasource.config)[queryVerb](enrichedQuery)
)
}
exports.execute = async function(ctx) {
@ -80,17 +91,15 @@ exports.execute = async function(ctx) {
return
}
const enrichedQuery = enrichQueryFields(
const enrichedQuery = await enrichQueryFields(
query.fields,
ctx.request.body.parameters
)
// call the relevant CRUD method on the integration class
const response = await new Integration(datasource.config)[query.queryVerb](
enrichedQuery
ctx.body = formatResponse(
await new Integration(datasource.config)[query.queryVerb](enrichedQuery)
)
ctx.body = response
}
exports.destroy = async function(ctx) {

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

@ -17,20 +17,26 @@ const SCHEMA = {
type: FIELD_TYPES.PASSWORD,
required: true,
},
endpoint: {
type: FIELD_TYPES.STRING,
required: false,
default: "https://dynamodb.us-east-1.amazonaws.com",
},
},
query: {
create: {
type: QUERY_TYPES.FIELDS,
customisable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
},
customisable: true,
},
},
read: {
type: QUERY_TYPES.FIELDS,
customisable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
@ -39,27 +45,46 @@ const SCHEMA = {
index: {
type: FIELD_TYPES.STRING,
},
customisable: true,
},
},
update: {
scan: {
type: QUERY_TYPES.FIELDS,
customisable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
},
customisable: true,
index: {
type: FIELD_TYPES.STRING,
},
},
},
delete: {
get: {
type: QUERY_TYPES.FIELDS,
customisable: true,
fields: {
table: {
type: FIELD_TYPES.STRING,
required: true,
},
key: {
},
},
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,
},
@ -72,7 +97,15 @@ class DynamoDBIntegration {
constructor(config) {
this.config = config
this.connect()
this.client = new AWS.DynamoDB.DocumentClient()
let options = {
correctClockSkew: true,
}
if (config.endpoint) {
options.endpoint = config.endpoint
}
this.client = new AWS.DynamoDB.DocumentClient({
correctClockSkew: true,
})
}
async connect() {
@ -80,37 +113,65 @@ class DynamoDBIntegration {
}
async create(query) {
const response = await this.client.query({
const params = {
TableName: query.table,
Item: query.json,
})
return response
...query.json,
}
return this.client.put(params).promise()
}
async read(query) {
const response = await this.client.query({
TableName: query.Table,
const params = {
TableName: query.table,
...query.json,
})
}
if (query.index) {
params.IndexName = query.index
}
const response = await this.client.query(params).promise()
if (response.Items) {
return response.Items
}
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()
}
async update(query) {
const response = await this.client.query({
const params = {
TableName: query.Table,
...query.json,
})
return response
}
return this.client.update(params).promise()
}
async delete(query) {
const response = await this.client.query({
TableName: query.Table,
Key: {
id: query.key,
},
})
return response
const params = {
TableName: query.table,
...query.json,
}
return this.client.delete(params).promise()
}
}

903
packages/server/yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save