budibase/packages/server/src/integrations/microsoftSqlServer.js

75 lines
1.3 KiB
JavaScript
Raw Normal View History

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 = {
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",
},
database: {
2021-01-13 17:39:47 +01:00
type: FIELD_TYPES.STRING,
2021-01-11 18:18:22 +01:00
default: "root",
},
2020-12-12 22:48:51 +01:00
},
query: {
2021-01-13 17:39:47 +01:00
create: {
SQL: {
type: "sql",
},
},
read: {
SQL: {
type: "sql",
},
2021-01-11 18:18:22 +01:00
},
2020-12-12 22:48:51 +01:00
},
}
class SqlServerIntegration {
constructor(config) {
this.config = config
this.client = sqlServer
}
async connect() {
return await this.client.connect(this.config)
}
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)
2020-12-12 22:48:51 +01:00
return response.recordset
} catch (err) {
console.error("Error querying MS SQL Server", err)
throw err
}
}
}
module.exports = {
2021-01-11 18:18:22 +01:00
schema: SCHEMA,
2020-12-12 22:48:51 +01:00
integration: SqlServerIntegration,
}