Browse Source

add arangodb integration

gh-pages
sovlookup 6 years ago
parent
commit
8996200d8d
  1. 134
      packages/server/src/integrations/arangodb.js

134
packages/server/src/integrations/arangodb.js

@ -2,84 +2,86 @@ const { Database, aql } = require("arangojs")
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration") const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
const SCHEMA = { const SCHEMA = {
docs: "https://github.com/arangodb/arangojs", docs: "https://github.com/arangodb/arangojs",
datasource: { datasource: {
url: { url: {
type: FIELD_TYPES.STRING, type: FIELD_TYPES.STRING,
default: "http://localhost:8529", default: "http://localhost:8529",
required: true, required: true,
},
username: {
type: FIELD_TYPES.STRING,
default: "root",
required: true,
},
password: {
type: FIELD_TYPES.PASSWORD,
required: true,
},
databaseName: {
type: FIELD_TYPES.STRING,
default: "_system",
required: true,
},
collection: {
type: FIELD_TYPES.STRING,
required: true,
},
}, },
query: { username: {
read: { type: FIELD_TYPES.STRING,
type: QUERY_TYPES.SQL, default: "root",
}, required: true,
create: {
type: QUERY_TYPES.JSON,
},
}, },
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,
},
},
} }
class ArangoDBIntegration { class ArangoDBIntegration {
constructor(config) { constructor(config) {
config.auth = { config.auth = {
username: config.username, username: config.username,
password: config.password, password: config.password,
}
this.config = config
this.client = new Database(config)
} }
async read(query) { this.config = config
try { this.client = new Database(config)
const result = await this.client.query(query.sql) }
let rl = []
await result.forEach((r)=> rl.push(r)) async read(query) {
return rl try {
} catch (err) { const result = await this.client.query(query.sql)
console.error("Error querying arangodb",err.message) let rl = []
throw err await result.forEach(r => rl.push(r))
} finally { return rl
this.client.close() } catch (err) {
} console.error("Error querying arangodb", err.message)
throw err
} finally {
this.client.close()
} }
}
async create(query) { async create(query) {
const clc = this.client.collection(this.config.collection) const clc = this.client.collection(this.config.collection)
try { try {
const result = await this.client.query(aql`INSERT ${query.json} INTO ${clc} LET n = NEW RETURN NEW`) const result = await this.client.query(
let rl = [] aql`INSERT ${query.json} INTO ${clc} LET n = NEW RETURN NEW`
await result.forEach((r)=> rl.push(r)) )
return rl let rl = []
} catch (err) { await result.forEach(r => rl.push(r))
console.error("Error querying arangodb",err.message) return rl
throw err } catch (err) {
} finally { console.error("Error querying arangodb", err.message)
this.client.close() throw err
} } finally {
this.client.close()
} }
}
} }
module.exports = { module.exports = {
schema: SCHEMA, schema: SCHEMA,
integration: ArangoDBIntegration, integration: ArangoDBIntegration,
} }

Loading…
Cancel
Save