"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const datasource_1 = require("../definitions/datasource");
var ArangoModule;
(function (ArangoModule) {
    const { Database, aql } = require("arangojs");
    const SCHEMA = {
        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: datasource_1.DatasourceFieldTypes.STRING,
                default: "http://localhost:8529",
                required: true,
            },
            username: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                default: "root",
                required: true,
            },
            password: {
                type: datasource_1.DatasourceFieldTypes.PASSWORD,
                required: true,
            },
            databaseName: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                default: "_system",
                required: true,
            },
            collection: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                required: true,
            },
        },
        query: {
            read: {
                type: datasource_1.QueryTypes.SQL,
            },
            create: {
                type: datasource_1.QueryTypes.JSON,
            },
        },
    };
    class ArangoDBIntegration {
        constructor(config) {
            const newConfig = {
                auth: {
                    username: config.username,
                    password: config.password,
                },
            };
            this.config = config;
            this.client = new Database(newConfig);
        }
        read(query) {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    const result = yield 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();
                }
            });
        }
        create(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const clc = this.client.collection(this.config.collection);
                try {
                    const result = yield 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();
                }
            });
        }
    }
    module.exports = {
        schema: SCHEMA,
        integration: ArangoDBIntegration,
    };
})(ArangoModule || (ArangoModule = {}));
