This commit is contained in:
Jan Prochazka
2024-11-15 16:48:33 +01:00
parent c5d23410f4
commit 81e4b947b6
9 changed files with 58 additions and 11 deletions

View File

@@ -5,6 +5,15 @@ const { getLogger, extendDatabaseInfo } = require('dbgate-tools');
const logger = getLogger('dropAllDbObjects');
/**
* Drops all database objects
* @param {object} options
* @param {connectionType} options.connection - connection object
* @param {object} options.systemConnection - system connection (result of driver.connect). If not provided, new connection will be created
* @param {object} options.driver - driver object. If not provided, it will be loaded from connection
* @param {object} options.analysedStructure - analysed structure of the database. If not provided, it will be loaded
* @returns {Promise}
*/
async function dropAllDbObjects({ connection, systemConnection, driver, analysedStructure }) {
if (!driver) driver = requireEngineDriver(connection);

View File

@@ -8,12 +8,12 @@ const logger = getLogger('execQuery');
/**
* Executes SQL query
* @param {object} options
* @param {connectionType} options.connection - connection object
* @param {object} options.systemConnection - system connection (result of driver.connect)
* @param {object} options.driver - driver object
* @param {string} options.sql - SQL query
* @param {string} options.sqlFile - SQL file
* @param {boolean} options.logScriptItems - whether to log script items instead of whole script
* @param {connectionType} [options.connection] - connection object
* @param {object} [options.systemConnection] - system connection (result of driver.connect). If not provided, new connection will be created
* @param {object} [options.driver] - driver object. If not provided, it will be loaded from connection
* @param {string} [options.sql] - SQL query
* @param {string} [options.sqlFile] - SQL file
* @param {boolean} [options.logScriptItems] - whether to log script items instead of whole script
*/
async function executeQuery({
connection = undefined,

View File

@@ -24,6 +24,14 @@ class StringifyStream extends stream.Transform {
}
}
/**
* Returns writer object for {@link copyStream} function. This writer object writes data to JSONL file. JSONL format - text file, every line is JSON encoded row, used eg. by MongoDB.
* @param {object} options
* @param {string} options.fileName - file name
* @param {string} [options.encoding] - encoding of the file
* @param {boolean} [options.header] - whether to write header. Header is JSON describing source table structure. Header is specific to DbGate, if you want eg. to import data to MongoDB, you should not write header.
* @returns {Promise<writerType>} - writer object
*/
async function jsonLinesWriter({ fileName, encoding = 'utf-8', header = true }) {
logger.info(`Writing file ${fileName}`);
const stringify = new StringifyStream({ header });

View File

@@ -45,6 +45,17 @@ class ParseStream extends stream.Transform {
}
}
/**
* Creates reader object for JSON file for {@link copyStream} function.
* @param {object} options
* @param {string} options.fileName - file name or URL
* @param {string} options.jsonStyle - 'object' or 'array'
* @param {string} [options.keyField] - key field for object style
* @param {string} [options.rootField] - root field for object style
* @param {string} [options.encoding] - encoding of the file
* @param {number} [options.limitRows] - maximum number of rows to read
* @returns {Promise<readerType>} - reader object
*/
async function jsonReader({
fileName,
jsonStyle,

View File

@@ -85,6 +85,16 @@ class StringifyStream extends stream.Transform {
}
}
/**
* Returns writer object for {@link copyStream} function. This writer object writes data to JSON file.
* @param {object} options
* @param {string} options.fileName - file name
* @param {string} [options.jsonStyle] - 'object' or 'array'
* @param {string} [options.keyField] - key field for object style
* @param {string} [options.rootField] - root field for object style
* @param {string} [options.encoding] - encoding of the file
* @returns {Promise<writerType>} - writer object
*/
async function jsonWriter({ fileName, jsonStyle, keyField = '_key', rootField, encoding = 'utf-8' }) {
logger.info(`Writing file ${fileName}`);
const stringify = new StringifyStream({ jsonStyle, keyField, rootField });

View File

@@ -3,6 +3,15 @@ const connectUtility = require('../utility/connectUtility');
const { getLogger } = require('dbgate-tools');
const logger = getLogger('queryReader');
/**
* Returns reader object for {@link copyStream} function. This reader object reads data from query.
* @param {object} options
* @param {connectionType} options.connection - connection object
* @param {string} options.query - SQL query
* @param {string} [options.queryType] - query type
* @param {string} [options.sql] - SQL query. obsolete; use query instead
* @returns {Promise<readerType>} - reader object
*/
async function queryReader({
connection,
query,

View File

@@ -7,7 +7,7 @@ const logger = getLogger('tableReader');
* Creates reader object for {@link copyStream} function. This reader object reads data from table or view.
* @param {object} options
* @param {connectionType} options.connection - connection object
* @param {object} options.systemConnection - system connection (result of driver.connect)
* @param {object} options.systemConnection - system connection (result of driver.connect). If not provided, new connection will be created
* @param {string} options.pureName - table name
* @param {string} options.schemaName - schema name
* @returns {Promise<readerType>} - reader object

View File

@@ -7,10 +7,10 @@ const logger = getLogger('tableWriter');
* Creates writer object for {@link copyStream} function. This writer object writes data to table. Table could be created if not exists.
* @param {object} options
* @param {connectionType} options.connection - connection object
* @param {object} options.systemConnection - system connection (result of driver.connect)
* @param {object} options.systemConnection - system connection (result of driver.connect). If not provided, new connection will be created
* @param {object} options.driver - driver object. If not provided, it will be loaded from connection
* @param {string} options.pureName - table name
* @param {string} options.schemaName - schema name
* @param {object} options.driver - driver object
* @param {boolean} options.dropIfExists - drop table if exists
* @param {boolean} options.truncate - truncate table before insert
* @param {boolean} options.createIfNotExists - create table if not exists

View File

@@ -1,11 +1,11 @@
/**
* Reader (input) object for copyStream function
* Reader (input) object for {@link copyStream} function
* @typedef {Object} readerType
*
*/
/**
* Writer (output) object for copyStream function
* Writer (output) object for {@link copyStream} function
* @typedef {Object} writerType
*
*/