2021-01-26 12:07:30 +01:00
|
|
|
const mysql = require("mysql")
|
2021-02-15 18:05:53 +01:00
|
|
|
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
|
2021-01-26 12:07:30 +01:00
|
|
|
|
|
|
|
const SCHEMA = {
|
|
|
|
docs: "https://github.com/mysqljs/mysql",
|
2021-02-18 17:58:10 +01:00
|
|
|
friendlyName: "MySQL",
|
2021-02-19 16:07:43 +01:00
|
|
|
description:
|
|
|
|
"MySQL Database Service is a fully managed database service to deploy cloud-native applications. ",
|
2021-01-26 12:07:30 +01:00
|
|
|
datasource: {
|
|
|
|
host: {
|
2021-02-03 09:20:21 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-26 12:07:30 +01:00
|
|
|
default: "localhost",
|
|
|
|
required: true,
|
|
|
|
},
|
2021-02-03 09:20:21 +01:00
|
|
|
port: {
|
|
|
|
type: FIELD_TYPES.NUMBER,
|
2021-02-24 17:31:47 +01:00
|
|
|
default: 3306,
|
2021-02-03 09:20:21 +01:00
|
|
|
required: false,
|
|
|
|
},
|
2021-01-26 12:07:30 +01:00
|
|
|
user: {
|
2021-02-03 09:20:21 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-26 12:07:30 +01:00
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
password: {
|
2021-02-03 09:20:21 +01:00
|
|
|
type: FIELD_TYPES.PASSWORD,
|
2021-01-26 12:07:30 +01:00
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
database: {
|
2021-02-03 09:20:21 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-26 12:07:30 +01:00
|
|
|
required: true,
|
|
|
|
},
|
2021-02-24 17:31:47 +01:00
|
|
|
ssl: {
|
|
|
|
type: FIELD_TYPES.OBJECT,
|
|
|
|
required: false,
|
|
|
|
},
|
2021-01-26 12:07:30 +01:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
2021-02-15 18:05:53 +01:00
|
|
|
type: QUERY_TYPES.SQL,
|
2021-01-26 12:07:30 +01:00
|
|
|
},
|
|
|
|
read: {
|
2021-02-15 18:05:53 +01:00
|
|
|
type: QUERY_TYPES.SQL,
|
2021-01-26 12:07:30 +01:00
|
|
|
},
|
|
|
|
update: {
|
2021-02-15 18:05:53 +01:00
|
|
|
type: QUERY_TYPES.SQL,
|
2021-01-26 12:07:30 +01:00
|
|
|
},
|
|
|
|
delete: {
|
2021-02-15 18:05:53 +01:00
|
|
|
type: QUERY_TYPES.SQL,
|
2021-01-26 12:07:30 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
class MySQLIntegration {
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config
|
2021-02-24 22:58:38 +01:00
|
|
|
if (Object.keys(config.ssl).length === 0) {
|
2021-02-24 17:31:47 +01:00
|
|
|
delete config.ssl
|
|
|
|
}
|
2021-01-26 12:07:30 +01:00
|
|
|
this.client = mysql.createConnection(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
query(query) {
|
|
|
|
// Node MySQL is callback based, so we must wrap our call in a promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.client.connect()
|
2021-03-16 19:43:56 +01:00
|
|
|
console.log(this.client.query())
|
2021-01-26 12:07:30 +01:00
|
|
|
return this.client.query(query.sql, (error, results) => {
|
|
|
|
if (error) return reject(error)
|
|
|
|
resolve(results)
|
|
|
|
this.client.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:43:56 +01:00
|
|
|
async create(query) {
|
|
|
|
const results = await this.query(query)
|
|
|
|
return results.length ? results : { created: true }
|
2021-01-26 12:07:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
read(query) {
|
|
|
|
return this.query(query)
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:43:56 +01:00
|
|
|
async update(query) {
|
|
|
|
const results = await this.query(query)
|
|
|
|
return results.length ? results : { updated: true }
|
2021-01-26 12:07:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 19:43:56 +01:00
|
|
|
async delete(query) {
|
|
|
|
const results = await this.query(query)
|
|
|
|
return results.length ? results : { deleted: true }
|
2021-01-26 12:07:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: MySQLIntegration,
|
|
|
|
}
|