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

77 lines
1.4 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 = {
2021-01-15 18:29:46 +01:00
docs: "https://github.com/tediousjs/node-mssql",
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",
},
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",
},
2020-12-12 22:48:51 +01:00
},
query: {
2021-01-13 17:39:47 +01:00
create: {
type: "sql",
2021-01-13 17:39:47 +01:00
},
read: {
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,
}