From 8e53caf50e440be4d21d5db8593c18d7d60cc370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dvo=C5=99=C3=A1k?= Date: Sun, 29 Oct 2023 20:20:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Handle=20connection=20to=20exter?= =?UTF-8?q?nal=20database?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 62 +++++++++++++++++++++++++++++++++++++-------------- tests/test.js | 2 +- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 21b9e29..ee5be0c 100644 --- a/src/index.js +++ b/src/index.js @@ -10,10 +10,29 @@ import Transaction from "./transaction"; import SchemaCompiler from "./schema/compiler"; import Firebird_Formatter from "./formatter"; import Firebird_DDL from "./schema/ddl"; -import { Blob } from 'node-firebird-driver-native' -import * as fs from "fs"; class Client_Firebird extends Client { + constructor(config = {}, ...args) { + if (!config.connection) { + throw new Error('Missing "connection" property in configuration!') + } + + const customConfig = {...config, connection: { ...config.connection}} + if (customConfig.connection.user) { + customConfig.connection.username = customConfig.connection.user + delete customConfig.connection.user + } + + if (!customConfig.connection.database) { + throw new Error('Database path is missing!') + } + if (customConfig.connection.database[0] !== '/') { + customConfig.connection.database = `/${customConfig.connection.database}` + } + + super(customConfig, ...args); + } + _driver() { return require("node-firebird-driver-native"); } @@ -53,25 +72,34 @@ class Client_Firebird extends Client { async acquireRawConnection() { assert(!this._connectionForTransactions); - // FIREBIRD_HOST="fsdm.farmsoft.cz" - // FIREBIRD_PORT="3050" - // FIREBIRD_USER="SYSDBA" - // FIREBIRD_PASSWORD="AgrosofT" - // FIREBIRD_POOL_SIZE="1" - // FIREBIRD_KRONOS_PATH="/var/lib/firebird/3.0/data/kronos/kronos.fdb" - - /** @type {import('node-firebird-driver-native').Client} client */ const client = this.driver.createNativeClient(this.config.libraryPath || this._driver().getDefaultLibraryFilename()) - const databasePath = this.config.connection.database || this.config.connection.path + + const databasePath = this.config.connection.database + const getConnectionString = () => { + const { + host, + port + } = this.config.connection + + const target = [host, port].filter(Boolean).join('/') + return [target, databasePath].filter(Boolean).join(':') + } + + const uri = getConnectionString() + const connect = () => client.connect(uri, this.config.connection) + const connectWithCreate = () => client.createDatabase(uri, this.config.connection) if (this.config.createDatabaseIfNotExists) { - const dbExists = await fs.promises.stat(databasePath).then(() => false).catch(() => true) - if (dbExists) { - return await client.createDatabase(databasePath, this.config.connection) + try { + return await connectWithCreate() + } catch (e) { + if (String(e).includes('I/O error during "open O_CREAT"')) { + return await connect() + } + throw e } } - - return await client.connect(databasePath, this.config.connection) + return await connect() } /** @@ -181,7 +209,7 @@ class Client_Firebird extends Client { const row = {} fields.forEach((key, index) => { const value = rows[i][index] - if (value instanceof Blob) { + if (value instanceof this._driver().Blob) { blobs.push( value.attachment.openBlob(transaction, value).then(async (stream) => { const buffer = Buffer.alloc(await stream.length) diff --git a/tests/test.js b/tests/test.js index adeb1db..3f7a94b 100644 --- a/tests/test.js +++ b/tests/test.js @@ -9,7 +9,7 @@ const generateConfig = () => ({ connection: { host: "127.0.0.1", port: 3050, - user: process.env.ISC_USER || "SYSDBA", + username: process.env.ISC_USER || "SYSDBA", password: process.env.ISC_PASSWORD || "masterkey", database: path.join( os.tmpdir(),