2021-06-25 05:17:26 +12:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
|
|
|
Operation,
|
|
|
|
QueryJson,
|
2021-06-26 00:46:02 +12:00
|
|
|
SqlQuery,
|
2021-06-27 10:09:46 +12:00
|
|
|
} from "../definitions/datasource"
|
2021-06-28 21:21:37 +12:00
|
|
|
import { Table, TableSchema } from "../definitions/common"
|
2021-06-26 00:46:02 +12:00
|
|
|
import { getSqlQuery } from "./utils"
|
2021-06-25 05:16:48 +12:00
|
|
|
|
|
|
|
module MySQLModule {
|
|
|
|
const mysql = require("mysql")
|
|
|
|
const Sql = require("./base/sql")
|
|
|
|
const { buildExternalTableId, convertType } = require("./utils")
|
|
|
|
const { FieldTypes } = require("../constants")
|
|
|
|
|
|
|
|
interface MySQLConfig {
|
|
|
|
host: string
|
|
|
|
port: number
|
|
|
|
user: string
|
|
|
|
password: string
|
|
|
|
database: string
|
|
|
|
ssl?: object
|
|
|
|
}
|
|
|
|
|
|
|
|
const TYPE_MAP = {
|
|
|
|
text: FieldTypes.LONGFORM,
|
|
|
|
blob: FieldTypes.LONGFORM,
|
|
|
|
enum: FieldTypes.STRING,
|
|
|
|
varchar: FieldTypes.STRING,
|
|
|
|
int: FieldTypes.NUMBER,
|
|
|
|
numeric: FieldTypes.NUMBER,
|
|
|
|
bigint: FieldTypes.NUMBER,
|
|
|
|
mediumint: FieldTypes.NUMBER,
|
|
|
|
decimal: FieldTypes.NUMBER,
|
|
|
|
dec: FieldTypes.NUMBER,
|
|
|
|
double: FieldTypes.NUMBER,
|
|
|
|
real: FieldTypes.NUMBER,
|
|
|
|
fixed: FieldTypes.NUMBER,
|
|
|
|
smallint: FieldTypes.NUMBER,
|
|
|
|
timestamp: FieldTypes.DATETIME,
|
|
|
|
date: FieldTypes.DATETIME,
|
|
|
|
datetime: FieldTypes.DATETIME,
|
|
|
|
time: FieldTypes.DATETIME,
|
|
|
|
tinyint: FieldTypes.BOOLEAN,
|
|
|
|
json: DatasourceFieldTypes.JSON,
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://github.com/mysqljs/mysql",
|
|
|
|
plus: true,
|
|
|
|
friendlyName: "MySQL",
|
|
|
|
description:
|
|
|
|
"MySQL Database Service is a fully managed database service to deploy cloud-native applications. ",
|
|
|
|
datasource: {
|
|
|
|
host: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "localhost",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
port: {
|
|
|
|
type: DatasourceFieldTypes.NUMBER,
|
|
|
|
default: 3306,
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type: DatasourceFieldTypes.PASSWORD,
|
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
database: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
ssl: {
|
|
|
|
type: DatasourceFieldTypes.OBJECT,
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
function internalQuery(
|
|
|
|
client: any,
|
2021-06-26 00:46:02 +12:00
|
|
|
query: SqlQuery,
|
2021-06-25 05:16:48 +12:00
|
|
|
connect: boolean = true
|
|
|
|
): Promise<any[]> {
|
|
|
|
// Node MySQL is callback based, so we must wrap our call in a promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (connect) {
|
|
|
|
client.connect()
|
|
|
|
}
|
|
|
|
return client.query(
|
|
|
|
query.sql,
|
|
|
|
query.bindings || {},
|
|
|
|
(error: any, results: object[]) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve(results)
|
|
|
|
}
|
|
|
|
if (connect) {
|
|
|
|
client.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
class MySQLIntegration extends Sql {
|
|
|
|
private config: MySQLConfig
|
|
|
|
private readonly client: any
|
|
|
|
|
|
|
|
constructor(config: MySQLConfig) {
|
|
|
|
super("mysql")
|
|
|
|
this.config = config
|
|
|
|
if (config.ssl && Object.keys(config.ssl).length === 0) {
|
|
|
|
delete config.ssl
|
|
|
|
}
|
|
|
|
this.client = mysql.createConnection(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
async buildSchema(datasourceId: string) {
|
2021-06-27 10:09:46 +12:00
|
|
|
const tables: { [key: string]: Table } = {}
|
2021-06-25 05:16:48 +12:00
|
|
|
const database = this.config.database
|
|
|
|
this.client.connect()
|
|
|
|
|
|
|
|
// get the tables first
|
|
|
|
const tablesResp = await internalQuery(
|
|
|
|
this.client,
|
|
|
|
{ sql: "SHOW TABLES;" },
|
|
|
|
false
|
|
|
|
)
|
2021-06-25 05:17:26 +12:00
|
|
|
const tableNames = tablesResp.map(
|
|
|
|
(obj: any) => obj[`Tables_in_${database}`]
|
|
|
|
)
|
2021-06-25 05:16:48 +12:00
|
|
|
for (let tableName of tableNames) {
|
|
|
|
const primaryKeys = []
|
2021-06-27 10:09:46 +12:00
|
|
|
const schema: TableSchema = {}
|
2021-06-25 05:16:48 +12:00
|
|
|
const descResp = await internalQuery(
|
|
|
|
this.client,
|
|
|
|
{ sql: `DESCRIBE ${tableName};` },
|
|
|
|
false
|
|
|
|
)
|
|
|
|
for (let column of descResp) {
|
|
|
|
const columnName = column.Field
|
|
|
|
if (column.Key === "PRI") {
|
|
|
|
primaryKeys.push(columnName)
|
|
|
|
}
|
|
|
|
const constraints = {
|
2021-06-27 10:09:46 +12:00
|
|
|
presence: column.Null !== "YES",
|
2021-06-25 05:16:48 +12:00
|
|
|
}
|
2021-07-06 05:16:04 +12:00
|
|
|
const isAuto: boolean =
|
|
|
|
typeof column.Extra === "string" &&
|
|
|
|
(column.Extra === "auto_increment" ||
|
|
|
|
column.Extra.toLowerCase().includes("generated"))
|
2021-06-25 05:16:48 +12:00
|
|
|
schema[columnName] = {
|
|
|
|
name: columnName,
|
2021-07-06 05:11:23 +12:00
|
|
|
autocolumn: isAuto,
|
2021-06-25 05:16:48 +12:00
|
|
|
type: convertType(column.Type, TYPE_MAP),
|
|
|
|
constraints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// for now just default to first column
|
|
|
|
if (primaryKeys.length === 0) {
|
|
|
|
primaryKeys.push(descResp[0].Field)
|
|
|
|
}
|
|
|
|
if (!tables[tableName]) {
|
|
|
|
tables[tableName] = {
|
|
|
|
_id: buildExternalTableId(datasourceId, tableName),
|
|
|
|
primary: primaryKeys,
|
|
|
|
name: tableName,
|
|
|
|
schema,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client.end()
|
|
|
|
this.tables = tables
|
|
|
|
}
|
|
|
|
|
2021-06-26 00:46:02 +12:00
|
|
|
async create(query: SqlQuery | string) {
|
|
|
|
const results = await internalQuery(this.client, getSqlQuery(query))
|
2021-06-25 05:16:48 +12:00
|
|
|
return results.length ? results : [{ created: true }]
|
|
|
|
}
|
|
|
|
|
2021-06-26 00:46:02 +12:00
|
|
|
read(query: SqlQuery | string) {
|
|
|
|
return internalQuery(this.client, getSqlQuery(query))
|
2021-06-25 05:16:48 +12:00
|
|
|
}
|
|
|
|
|
2021-06-26 00:46:02 +12:00
|
|
|
async update(query: SqlQuery | string) {
|
|
|
|
const results = await internalQuery(this.client, getSqlQuery(query))
|
2021-06-25 05:16:48 +12:00
|
|
|
return results.length ? results : [{ updated: true }]
|
|
|
|
}
|
|
|
|
|
2021-06-26 00:46:02 +12:00
|
|
|
async delete(query: SqlQuery | string) {
|
|
|
|
const results = await internalQuery(this.client, getSqlQuery(query))
|
2021-06-25 05:16:48 +12:00
|
|
|
return results.length ? results : [{ deleted: true }]
|
|
|
|
}
|
|
|
|
|
|
|
|
async getReturningRow(json: QueryJson) {
|
2021-06-27 10:09:46 +12:00
|
|
|
if (!json.extra || !json.extra.idFilter) {
|
2021-06-25 05:16:48 +12:00
|
|
|
return {}
|
|
|
|
}
|
|
|
|
const input = this._query({
|
|
|
|
endpoint: {
|
|
|
|
...json.endpoint,
|
|
|
|
operation: Operation.READ,
|
|
|
|
},
|
|
|
|
fields: [],
|
|
|
|
filters: json.extra.idFilter,
|
|
|
|
paginate: {
|
|
|
|
limit: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return internalQuery(this.client, input, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
async query(json: QueryJson) {
|
|
|
|
const operation = this._operation(json)
|
|
|
|
this.client.connect()
|
|
|
|
const input = this._query(json, { disableReturning: true })
|
|
|
|
let row
|
|
|
|
// need to manage returning, a feature mySQL can't do
|
|
|
|
if (operation === "awdawd") {
|
|
|
|
row = this.getReturningRow(json)
|
|
|
|
}
|
|
|
|
const results = await internalQuery(this.client, input, false)
|
|
|
|
// same as delete, manage returning
|
|
|
|
if (operation === Operation.CREATE || operation === Operation.UPDATE) {
|
|
|
|
row = this.getReturningRow(json)
|
|
|
|
}
|
|
|
|
this.client.end()
|
|
|
|
if (operation !== Operation.READ) {
|
|
|
|
return row
|
|
|
|
}
|
|
|
|
return results.length ? results : [{ [operation.toLowerCase()]: true }]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: MySQLIntegration,
|
|
|
|
}
|
|
|
|
}
|