1
0
Fork 0
mirror of synced 2024-09-18 02:08:34 +12:00
budibase/packages/server/src/integrations/arangodb.js

87 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-01-29 02:48:29 +13:00
const { Database, aql } = require("arangojs")
2021-01-29 02:42:40 +13:00
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
const SCHEMA = {
2021-01-29 03:17:39 +13:00
docs: "https://github.com/arangodb/arangojs",
friendlyName: "ArangoDB",
description:
"ArangoDB is a scalable open-source multi-model database natively supporting graph, document and search. All supported data models & access patterns can be combined in queries allowing for maximal flexibility. ",
2021-01-29 03:17:39 +13:00
datasource: {
url: {
type: FIELD_TYPES.STRING,
default: "http://localhost:8529",
required: true,
2021-01-29 02:42:40 +13:00
},
2021-01-29 03:17:39 +13:00
username: {
type: FIELD_TYPES.STRING,
default: "root",
required: true,
2021-01-29 02:42:40 +13:00
},
2021-01-29 03:17:39 +13:00
password: {
type: FIELD_TYPES.PASSWORD,
required: true,
},
databaseName: {
type: FIELD_TYPES.STRING,
default: "_system",
required: true,
},
collection: {
type: FIELD_TYPES.STRING,
required: true,
},
},
query: {
read: {
type: QUERY_TYPES.SQL,
},
create: {
type: QUERY_TYPES.JSON,
},
},
2021-01-29 02:42:40 +13:00
}
class ArangoDBIntegration {
2021-01-29 03:17:39 +13:00
constructor(config) {
config.auth = {
username: config.username,
password: config.password,
2021-01-29 02:42:40 +13:00
}
2021-01-29 03:17:39 +13:00
this.config = config
this.client = new Database(config)
}
async read(query) {
try {
const result = await this.client.query(query.sql)
2021-01-29 04:08:53 +13:00
return result.all()
2021-01-29 03:17:39 +13:00
} catch (err) {
console.error("Error querying arangodb", err.message)
throw err
} finally {
this.client.close()
2021-01-29 02:42:40 +13:00
}
2021-01-29 03:17:39 +13:00
}
2021-01-29 02:42:40 +13:00
2021-01-29 03:17:39 +13:00
async create(query) {
const clc = this.client.collection(this.config.collection)
try {
const result = await this.client.query(
2021-01-29 04:08:53 +13:00
aql`INSERT ${query.json} INTO ${clc} RETURN NEW`
2021-01-29 03:17:39 +13:00
)
2021-01-29 04:08:53 +13:00
return result.all()
2021-01-29 03:17:39 +13:00
} catch (err) {
console.error("Error querying arangodb", err.message)
throw err
} finally {
this.client.close()
2021-01-29 02:42:40 +13:00
}
2021-01-29 03:17:39 +13:00
}
2021-01-29 02:42:40 +13:00
}
module.exports = {
schema: SCHEMA,
integration: ArangoDBIntegration,
2021-01-29 03:17:39 +13:00
}