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A2B,EAA2B,GAAK,KAAK,oBAC/D,EACA,EAA2B,EAEjC,KAAK,gBAAkB,KAAK,eAAe,KAAK,0BAChD,QAAe,KAAK,gBAAgB,MAAM,EAAK,EAIvD,CACA,OAAO,CAAM,EAKrB,OAAO,QAAU","file":"/npm/patroni-switcher@1.0.7/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/patroni-switcher@1.0.7/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const {Sequelize} = require(\"sequelize\");\n\nclass PatroniSwitcher extends Sequelize {\n\n    sequelizeMaster = undefined;\n    patroniServers = [];\n    patroniCountServers = 0;\n    patroniNowServer = 0;\n    patroniMasterServerIndex;\n\n    constructor(database, username, password, config) {\n        super(database, username, password, config);\n        this.sequelizeMaster = new Sequelize(database, username, password, config);\n\n        let DB_PATRONI = [\n            {\n                host: config.host,\n                port: config.port\n            }\n        ];\n        if ((config.patroni) && (Array.isArray(config.patroni))) {\n            DB_PATRONI = config.patroni;\n        }\n        this.patroniCountServers = DB_PATRONI.length;\n        DB_PATRONI.forEach(server => {\n            let configSlave = {...config};\n            configSlave.host = server.host;\n            configSlave.port = server.port;\n            let sequelizeSlave = new Sequelize(database, username, password, configSlave);\n            sequelizeSlave.addHook('afterQuery', () => {\n                console.log(`Send Query to ${server.host}:${server.port}`);\n            });\n            this.patroniServers.push(sequelizeSlave);\n        })\n\n        this.patroniMasterServerIndex = this.patroniServers.findIndex((server) => server.options.host === this.sequelizeMaster.options.host);\n\n        return this;\n    }\n\n    query = async (sql, options) => {\n        let result;\n        const currentMasterServerIndex = this.patroniMasterServerIndex;\n        try {\n            if ((typeof sql === \"string\") && (sql.indexOf(\"SELECT\") === 0)) {\n                result = await this.patroniServers[this.patroniNowServer].query(sql, options);\n                //Следующий запрос через следующий сервер\n                this.patroniNowServer = (this.patroniNowServer + 1 >= this.patroniCountServers) ? 0 : this.patroniNowServer + 1;\n            } else {\n                //Это не select, поэтому выполним на мастере\n                result = await this.sequelizeMaster.query(sql, options);\n            }\n        } catch (e) {\n            console.error(e);\n            if ((e.original.routine === \"PreventCommandIfReadOnly\") && (this.patroniCountServers > 1)) {\n\n                this.patroniMasterServerIndex = currentMasterServerIndex + 1 >= this.patroniCountServers\n                    ? 0\n                    : currentMasterServerIndex + 1;\n\n                this.sequelizeMaster = this.patroniServers[this.patroniMasterServerIndex];\n                result = await this.sequelizeMaster.query(sql, options);\n            } else {\n                throw e;\n            }\n        }\n        return result;\n    }\n\n}\n\nmodule.exports = PatroniSwitcher;"]}