From 01ff661f17ed9967cd5fa7fbce59ca6d6c4e5922 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 13 Jan 2021 16:39:47 +0000 Subject: [PATCH] schema updates, custom fields --- .../IntegrationConfigForm.svelte | 2 +- .../integration/QueryFieldsBuilder.svelte | 3 +- .../components/integration/QueryViewer.svelte | 40 +++++------ .../src/components/integration/index.svelte | 34 ++++----- .../[selectedDatasource]/index.svelte | 4 +- .../server/src/integrations/Integration.js | 17 +++-- packages/server/src/integrations/airtable.js | 19 ++--- packages/server/src/integrations/dynamodb.js | 69 +++++++++---------- .../server/src/integrations/elasticsearch.js | 6 +- .../src/integrations/microsoftSqlServer.js | 35 +++++++--- packages/server/src/integrations/mongodb.js | 39 ++++++++--- packages/server/src/integrations/s3.js | 26 ++++--- 12 files changed, 170 insertions(+), 124 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte index e7ce7aea49..79bb951c01 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte @@ -8,7 +8,7 @@ {#each Object.keys(integration) as configKey} diff --git a/packages/builder/src/components/integration/QueryFieldsBuilder.svelte b/packages/builder/src/components/integration/QueryFieldsBuilder.svelte index cf067dfb93..993dc43c0e 100644 --- a/packages/builder/src/components/integration/QueryFieldsBuilder.svelte +++ b/packages/builder/src/components/integration/QueryFieldsBuilder.svelte @@ -35,6 +35,7 @@ {/each} {#if schema.customisable} + {#each Object.keys(customSchema) as field} @@ -54,7 +55,7 @@ - + {/if} diff --git a/packages/builder/src/components/integration/QueryViewer.svelte b/packages/builder/src/components/integration/QueryViewer.svelte index 48eda4016b..01be849d0d 100644 --- a/packages/builder/src/components/integration/QueryViewer.svelte +++ b/packages/builder/src/components/integration/QueryViewer.svelte @@ -32,8 +32,6 @@ }, ] - const QueryVerb = ["create", "read", "update", "delete"] - export let query export let fields = [] @@ -128,25 +126,27 @@
{query.name} -
- {#each QueryVerb as queryVerb} -
{ - query.queryVerb = queryVerb - }}> - {queryVerb} -
- {/each} -
- {#if config && query.queryVerb} - + + {#if query.queryVerb} + + {/if} {/if}
diff --git a/packages/builder/src/components/integration/index.svelte b/packages/builder/src/components/integration/index.svelte index b0003ef2ef..ad6ec7b761 100644 --- a/packages/builder/src/components/integration/index.svelte +++ b/packages/builder/src/components/integration/index.svelte @@ -14,7 +14,7 @@ export let schema function updateQuery({ detail }) { - query.fields.sql = detail.value + query.fields[schema.type] = detail.value } @@ -24,19 +24,21 @@ Query -{#if schema.type === QueryTypes.SQL} - -{:else if schema.type === QueryTypes.JSON} - - -{:else if schema.type === QueryTypes.FIELDS} - +{#if schema} + {#if schema.type === QueryTypes.SQL} + + {:else if schema.type === QueryTypes.JSON} + + + {:else if schema.type === QueryTypes.FIELDS} + + {/if} {/if} diff --git a/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte b/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte index 9f4f2d1aaa..1c5696a552 100644 --- a/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte +++ b/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte @@ -26,9 +26,7 @@ {/if} diff --git a/packages/server/src/integrations/Integration.js b/packages/server/src/integrations/Integration.js index 2305f0ea55..f24080853f 100644 --- a/packages/server/src/integrations/Integration.js +++ b/packages/server/src/integrations/Integration.js @@ -1,7 +1,12 @@ -class Field { - constructor(type, defaultValue, required) { - this.type = type - this.default = defaultValue - this.required = required - } +exports.QUERY_TYPES = { + SQL: "sql", + JSON: "json", + FIELDS: "fields", +} + +exports.FIELD_TYPES = { + STRING: "string", + NUMBER: "number", + PASSWORD: "password", + LIST: "list", } diff --git a/packages/server/src/integrations/airtable.js b/packages/server/src/integrations/airtable.js index b7300ac446..a0d56bac9d 100644 --- a/packages/server/src/integrations/airtable.js +++ b/packages/server/src/integrations/airtable.js @@ -1,14 +1,15 @@ const Airtable = require("airtable") +const { FIELD_TYPES, QUERY_TYPES } = require("./Integration") const SCHEMA = { datasource: { apiKey: { - type: "string", + type: FIELD_TYPES.STRING, default: "enter api key", required: true, }, base: { - type: "string", + type: FIELD_TYPES.STRING, default: "mybase", required: true, }, @@ -20,7 +21,7 @@ const SCHEMA = { customisable: true, fields: { table: { - type: "string", + type: FIELD_TYPES.STRING, required: true, }, }, @@ -28,14 +29,14 @@ const SCHEMA = { }, read: { Table: { - type: "fields", + type: QUERY_TYPES.FIELDS, fields: { table: { - type: "string", + type: FIELD_TYPES.STRING, required: true, }, view: { - type: "string", + type: FIELD_TYPES.STRING, required: true, }, }, @@ -43,11 +44,11 @@ const SCHEMA = { }, update: { Fields: { - type: "fields", + type: QUERY_TYPES.FIELDS, customisable: true, fields: { id: { - type: "string", + type: FIELD_TYPES.STRING, required: true, }, }, @@ -55,7 +56,7 @@ const SCHEMA = { }, delete: { "Airtable Ids": { - type: "list", + type: FIELD_TYPES.LIST, }, }, }, diff --git a/packages/server/src/integrations/dynamodb.js b/packages/server/src/integrations/dynamodb.js index aec93e0933..196f7fd97f 100644 --- a/packages/server/src/integrations/dynamodb.js +++ b/packages/server/src/integrations/dynamodb.js @@ -1,53 +1,46 @@ const AWS = require("aws-sdk") +const { FIELD_TYPES, QUERY_TYPES } = require("./Integration") const SCHEMA = { datasource: { - table: { - type: "string", - required: true, - }, region: { - type: "string", + type: FIELD_TYPES.STRING, required: true, default: "us-east-1", }, accessKeyId: { - type: "string", + type: FIELD_TYPES.PASSWORD, required: true, }, secretKey: { - type: "secretKey", + type: FIELD_TYPES.PASSWORD, required: true, - default: 5432, - }, - indexName: { - type: "string", }, }, query: { - type: "fields", - fields: [ - { - name: "Index", - key: "Index", - type: "string", + read: { + DynamoConfig: { + type: QUERY_TYPES.FIELDS, + fields: { + Table: { + type: FIELD_TYPES.STRING, + required: true, + }, + Index: { + type: FIELD_TYPES.STRING, + }, + KeyConditionExpression: { + type: FIELD_TYPES.STRING, + }, + ExpressionAttributeNames: { + type: FIELD_TYPES.STRING, + }, + ExpressionAttributeValues: { + type: FIELD_TYPES.STRING, + }, + }, }, - { - name: "Key Condition Expression", - key: "KeyConditionExpression", - type: "string", - }, - { - name: "Attribute Names", - key: "ExpressionAttributeNames", - type: "string", - }, - { - name: "Attribute Values", - key: "ExpressionAttributeValues", - type: "string", - }, - ], + }, }, } @@ -62,12 +55,12 @@ class DynamoDBIntegration { AWS.config.update(this.config) } - async read() { + async read(query) { const response = await this.client.query({ - TableName: this.config.table, - KeyConditionExpression: this.config.KeyConditionExpression, - ExpressionAttributeNames: this.config.ExpressionAttributeNames, - ExpressionAttributeValues: this.config.ExpressionAttributeValues, + TableName: query.Table, + KeyConditionExpression: query.KeyConditionExpression, + ExpressionAttributeNames: query.ExpressionAttributeNames, + ExpressionAttributeValues: query.ExpressionAttributeValues, }) return response } diff --git a/packages/server/src/integrations/elasticsearch.js b/packages/server/src/integrations/elasticsearch.js index 8e9e92be42..610fa99041 100644 --- a/packages/server/src/integrations/elasticsearch.js +++ b/packages/server/src/integrations/elasticsearch.js @@ -26,11 +26,11 @@ class ElasticSearchIntegration { this.client = new Client({ node: config.url }) } - async create(document) { + async create(query) { try { const result = await this.client.index({ index: this.config.index, - body: JSON.parse(document), + body: JSON.parse(query.json), }) return [result] } catch (err) { @@ -45,7 +45,7 @@ class ElasticSearchIntegration { try { const result = await this.client.search({ index: this.config.index, - body: JSON.parse(query), + body: JSON.parse(query.json), }) return result.body.hits.hits.map(({ _source }) => _source) } catch (err) { diff --git a/packages/server/src/integrations/microsoftSqlServer.js b/packages/server/src/integrations/microsoftSqlServer.js index d8e7949aad..cabf70c2bf 100644 --- a/packages/server/src/integrations/microsoftSqlServer.js +++ b/packages/server/src/integrations/microsoftSqlServer.js @@ -1,28 +1,36 @@ const sqlServer = require("mssql") +const { FIELD_TYPES } = require("./Integration") const SCHEMA = { datasource: { user: { - type: "string", + type: FIELD_TYPES.STRING, required: true, default: "localhost", }, password: { - type: "password", + type: FIELD_TYPES.PASSWORD, required: true, }, server: { - type: "string", + type: FIELD_TYPES.STRING, default: "localhost", }, database: { - type: "string", + type: FIELD_TYPES.STRING, default: "root", }, }, query: { - sql: { - type: "sql", + create: { + SQL: { + type: "sql", + }, + }, + read: { + SQL: { + type: "sql", + }, }, }, } @@ -37,10 +45,21 @@ class SqlServerIntegration { return await this.client.connect(this.config) } - async read() { + async read(query) { try { await this.connect() - const response = await this.client.query(this.config.query) + const response = await this.client.query(query.sql) + return response.recordset + } catch (err) { + console.error("Error querying MS SQL Server", err) + throw err + } + } + + async create(query) { + try { + await this.connect() + const response = await this.client.query(query.sql) return response.recordset } catch (err) { console.error("Error querying MS SQL Server", err) diff --git a/packages/server/src/integrations/mongodb.js b/packages/server/src/integrations/mongodb.js index 984ed11068..69a49c43d3 100644 --- a/packages/server/src/integrations/mongodb.js +++ b/packages/server/src/integrations/mongodb.js @@ -17,8 +17,15 @@ const SCHEMA = { }, }, query: { - JSON: { - type: "json", + create: { + JSON: { + type: "json", + }, + }, + read: { + JSON: { + type: "json", + }, }, }, } @@ -27,23 +34,35 @@ class MongoIntegration { constructor(config) { this.config = config this.client = new MongoClient(config.connectionString) - try { - this.config.query = JSON.parse(this.config.query) - } catch (err) { - this.config.query = {} - } - this.connect() } async connect() { return this.client.connect() } - async read() { + async create(query) { try { + const mongoQuery = query.json ? JSON.parse(query.json) : {} + await this.connect() const db = this.client.db(this.config.db) const collection = db.collection(this.config.collection) - const result = await collection.find(this.config.query).toArray() + const result = await collection.insertOne(mongoQuery) + return result + } catch (err) { + console.error("Error querying mongodb", err) + throw err + } finally { + await this.client.close() + } + } + + async read(query) { + try { + const mongoQuery = query.json ? JSON.parse(query.json) : {} + await this.connect() + const db = this.client.db(this.config.db) + const collection = db.collection(this.config.collection) + const result = await collection.find(mongoQuery).toArray() return result } catch (err) { console.error("Error querying mongodb", err) diff --git a/packages/server/src/integrations/s3.js b/packages/server/src/integrations/s3.js index fd1e079bee..e01c45227d 100644 --- a/packages/server/src/integrations/s3.js +++ b/packages/server/src/integrations/s3.js @@ -2,25 +2,33 @@ const AWS = require("aws-sdk") const SCHEMA = { datasource: { - bucket: { - type: "string", - required: true, - }, region: { type: "string", required: true, default: "us-east-1", }, accessKeyId: { - type: "string", + type: "password", required: true, }, secretAccessKey: { - type: "string", + type: "password", required: true, }, }, - query: {}, + query: { + read: { + Bucket: { + type: "fields", + fields: { + bucket: { + type: "string", + required: true, + }, + }, + }, + }, + }, } class S3Integration { @@ -34,10 +42,10 @@ class S3Integration { AWS.config.update(this.config) } - async query() { + async read(query) { const response = await this.client .listObjects({ - Bucket: this.config.bucket, + Bucket: query.bucket, }) .promise() return response.Contents