2020-12-12 22:48:51 +01:00
|
|
|
const sqlServer = require("mssql")
|
2021-01-13 17:39:47 +01:00
|
|
|
const { FIELD_TYPES } = require("./Integration")
|
2020-12-12 22:48:51 +01:00
|
|
|
|
2021-01-11 18:18:22 +01:00
|
|
|
const SCHEMA = {
|
2021-01-15 18:29:46 +01:00
|
|
|
docs: "https://github.com/tediousjs/node-mssql",
|
2021-02-19 16:07:43 +01:00
|
|
|
description:
|
|
|
|
"Microsoft SQL Server is a relational database management system developed by Microsoft. ",
|
2021-02-18 17:58:10 +01:00
|
|
|
friendlyName: "MS SQL Server",
|
2021-01-11 18:18:22 +01:00
|
|
|
datasource: {
|
|
|
|
user: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-11 18:18:22 +01:00
|
|
|
required: true,
|
|
|
|
default: "localhost",
|
|
|
|
},
|
|
|
|
password: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.PASSWORD,
|
2021-01-11 18:18:22 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
server: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-11 18:18:22 +01:00
|
|
|
default: "localhost",
|
|
|
|
},
|
2021-02-03 09:20:21 +01:00
|
|
|
port: {
|
|
|
|
type: FIELD_TYPES.NUMBER,
|
|
|
|
required: false,
|
|
|
|
default: 1433,
|
|
|
|
},
|
2021-01-11 18:18:22 +01:00
|
|
|
database: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-11 18:18:22 +01:00
|
|
|
default: "root",
|
|
|
|
},
|
2021-05-28 12:55:28 +02:00
|
|
|
encrypt: {
|
|
|
|
type: FIELD_TYPES.BOOLEAN,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-12-12 22:48:51 +01:00
|
|
|
},
|
|
|
|
query: {
|
2021-01-13 17:39:47 +01:00
|
|
|
create: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: "sql",
|
2021-01-13 17:39:47 +01:00
|
|
|
},
|
|
|
|
read: {
|
2021-01-22 13:22:28 +01:00
|
|
|
type: "sql",
|
2021-01-11 18:18:22 +01:00
|
|
|
},
|
2021-04-12 16:01:52 +02:00
|
|
|
update: {
|
|
|
|
type: "sql",
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: "sql",
|
|
|
|
},
|
2020-12-12 22:48:51 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
class SqlServerIntegration {
|
2021-06-01 16:00:28 +02:00
|
|
|
static pool
|
|
|
|
|
2020-12-12 22:48:51 +01:00
|
|
|
constructor(config) {
|
|
|
|
this.config = config
|
2021-05-28 12:55:28 +02:00
|
|
|
this.config.options = {
|
|
|
|
encrypt: this.config.encrypt,
|
|
|
|
}
|
|
|
|
delete this.config.encrypt
|
2021-06-01 16:00:28 +02:00
|
|
|
if (!this.pool) {
|
|
|
|
this.pool = new sqlServer.ConnectionPool(this.config)
|
2021-05-23 16:06:33 +02:00
|
|
|
}
|
2020-12-12 22:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
2021-06-01 16:00:28 +02:00
|
|
|
const client = await this.pool.connect()
|
2021-05-23 16:06:33 +02:00
|
|
|
this.client = client.request()
|
2020-12-12 22:48:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:39:47 +01:00
|
|
|
async read(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)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(query) {
|
2020-12-12 22:48:51 +01:00
|
|
|
try {
|
|
|
|
await this.connect()
|
2021-01-13 17:39:47 +01:00
|
|
|
const response = await this.client.query(query.sql)
|
2021-03-16 19:43:56 +01:00
|
|
|
return response.recordset || [{ created: true }]
|
2020-12-12 22:48:51 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error querying MS SQL Server", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 17:51:07 +02:00
|
|
|
|
|
|
|
async update(query) {
|
2021-04-12 16:01:52 +02:00
|
|
|
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)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(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)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 22:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2021-01-11 18:18:22 +01:00
|
|
|
schema: SCHEMA,
|
2020-12-12 22:48:51 +01:00
|
|
|
integration: SqlServerIntegration,
|
|
|
|
}
|