2021-06-24 19:17:26 +02:00
import {
Integration ,
2022-08-11 14:50:05 +02:00
DatasourceFieldType ,
QueryType ,
2022-08-11 12:48:58 +02:00
IntegrationBase ,
} from "@budibase/types"
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
const { Database , aql } = require ( "arangojs" )
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
interface ArangodbConfig {
url : string
username : string
password : string
databaseName : string
collection : string
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
const SCHEMA : Integration = {
docs : "https://github.com/arangodb/arangojs" ,
friendlyName : "ArangoDB" ,
type : "Non-relational" ,
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. " ,
datasource : {
url : {
type : DatasourceFieldType . STRING ,
default : "http://localhost:8529" ,
required : true ,
2021-06-24 19:16:48 +02:00
} ,
2022-08-12 18:03:06 +02:00
username : {
type : DatasourceFieldType . STRING ,
default : "root" ,
required : true ,
2021-06-24 19:16:48 +02:00
} ,
2022-08-12 18:03:06 +02:00
password : {
type : DatasourceFieldType . PASSWORD ,
required : true ,
} ,
databaseName : {
type : DatasourceFieldType . STRING ,
default : "_system" ,
required : true ,
} ,
collection : {
type : DatasourceFieldType . STRING ,
required : true ,
} ,
} ,
query : {
read : {
type : QueryType . SQL ,
} ,
create : {
type : QueryType . JSON ,
} ,
} ,
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
class ArangoDBIntegration implements IntegrationBase {
private config : ArangodbConfig
private client : any
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
constructor ( config : ArangodbConfig ) {
const newConfig = {
2022-10-17 20:56:30 +02:00
url : config.url ,
databaseName : config.databaseName ,
2022-08-12 18:03:06 +02:00
auth : {
username : config.username ,
password : config.password ,
} ,
2021-06-24 19:16:48 +02:00
}
2022-08-12 18:03:06 +02:00
this . config = config
this . client = new Database ( newConfig )
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
async read ( query : { sql : any } ) {
try {
const result = await this . client . query ( query . sql )
return result . all ( )
} catch ( err ) {
// @ts-ignore
console . error ( "Error querying arangodb" , err . message )
throw err
} finally {
this . client . close ( )
2021-06-24 19:16:48 +02:00
}
}
2022-08-12 18:03:06 +02:00
async create ( query : { json : any } ) {
const clc = this . client . collection ( this . config . collection )
try {
const result = await this . client . query (
aql ` INSERT ${ query . json } INTO ${ clc } RETURN NEW `
)
return result . all ( )
} catch ( err ) {
// @ts-ignore
console . error ( "Error querying arangodb" , err . message )
throw err
} finally {
this . client . close ( )
}
2021-06-24 19:16:48 +02:00
}
}
2022-08-12 18:03:06 +02:00
export default {
schema : SCHEMA ,
integration : ArangoDBIntegration ,
}