"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 CouchDBModule;
(function (CouchDBModule) {
    const PouchDB = require("pouchdb");
    const SCHEMA = {
        docs: "https://docs.couchdb.org/en/stable/",
        friendlyName: "CouchDB",
        type: "Non-relational",
        description: "Apache CouchDB is an open-source document-oriented NoSQL database, implemented in Erlang.",
        datasource: {
            url: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                required: true,
                default: "http://localhost:5984",
            },
            database: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                required: true,
            },
        },
        query: {
            create: {
                type: datasource_1.QueryTypes.JSON,
            },
            read: {
                type: datasource_1.QueryTypes.JSON,
            },
            update: {
                type: datasource_1.QueryTypes.JSON,
            },
            delete: {
                type: datasource_1.QueryTypes.FIELDS,
                fields: {
                    id: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
        },
    };
    class CouchDBIntegration {
        constructor(config) {
            this.config = config;
            this.client = new PouchDB(`${config.url}/${config.database}`);
        }
        query(command, errorMsg, query) {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    const response = yield this.client[command](query.id || query.json);
                    yield this.client.close();
                    return response;
                }
                catch (err) {
                    console.error(errorMsg, err);
                    throw err;
                }
            });
        }
        create(query) {
            return __awaiter(this, void 0, void 0, function* () {
                return this.query("post", "Error writing to couchDB", query);
            });
        }
        read(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const result = yield this.query("allDocs", "Error querying couchDB", {
                    json: Object.assign({ include_docs: true }, query.json),
                });
                return result.rows.map((row) => row.doc);
            });
        }
        update(query) {
            return __awaiter(this, void 0, void 0, function* () {
                return this.query("put", "Error updating couchDB document", query);
            });
        }
        delete(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const doc = yield this.query("get", "Cannot find doc to be deleted", query);
                return this.query("remove", "Error deleting couchDB document", {
                    json: doc,
                });
            });
        }
    }
    module.exports = {
        schema: SCHEMA,
        integration: CouchDBIntegration,
    };
})(CouchDBModule || (CouchDBModule = {}));
