dbgate/packages/api
Jan Prochazka 6f32e27eec fix
2024-09-12 15:53:56 +02:00
..
.vscode introduced yarn workspace 2020-02-03 19:43:11 +01:00
env fixed singledb docker connections 2024-06-17 17:06:00 +02:00
src fix 2024-09-12 15:53:56 +02:00
.env upgraded tedious driver 2022-06-02 16:47:46 +02:00
.npmignore reduced API package size 2020-11-17 08:34:15 +01:00
.yarnrc api package docs 2020-11-30 20:17:52 +01:00
package.json upgraded dbgate-query-splitter fixes #880 2024-08-30 10:26:34 +02:00
README.md fixed repo links 2021-02-11 10:11:34 +01:00
tsconfig.json refactor 2020-02-03 19:52:02 +01:00
webpack.config.js oracledb docker install 2024-07-31 14:07:49 +02:00

dbgate-api

Allows run DbGate data-manipulation scripts.

Installation

yarn add dbgate-api

Usage

This example exports table Customer info CSV file.

const dbgateApi = require('dbgate-api');
const dbgatePluginMssql = require("dbgate-plugin-mssql");
const dbgatePluginCsv = require("dbgate-plugin-csv");

dbgateApi.registerPlugins(dbgatePluginMssql);

async function run() {
  const reader = await dbgateApi.tableReader({
    connection: { server: 'localhost', engine: 'mssql', user: 'sa', password: 'xxxx', database: 'Chinook' },
    schemaName: 'dbo',
    pureName: 'Customer',
  });
  const writer = await dbgatePluginCsv.shellApi.writer({ fileName: 'Customer.csv' });
  await dbgateApi.copyStream(reader, writer);

  console.log('Finished job script');
}
dbgateApi.runScript(run);

Silly example, runs without any dependencies. Copy fakeObjectReader to consoleObjectWriter .


const dbgateApi = require('dbgate-api');
async function run() {
  const reader = await dbgateApi.fakeObjectReader();
  const writer = await dbgateApi.consoleObjectWriter();
  await dbgateApi.copyStream(reader, writer);
  console.log('Finished job script');
}
dbgateApi.runScript(run);

dbgateApi functions

dbgateApi.copyStream

Copies data from reader into writer. Reader and writer should be created from functions listed below.

  await dbgateApi.copyStream(reader, writer);

dbgateApi.tableReader

Reads table or view.

  const reader = await dbgateApi.tableReader({
    connection: { server: 'localhost', engine: 'mssql' | 'postgres' | 'mysql', user: 'root', password: 'xxxx', database: 'DB_NAME' },
    schemaName: 'dbo',
    pureName: 'Customer',
  });

dbgateApi.queryReader

Executes query and reads its result.

  const reader = await dbgateApi.tableReader({
    connection: { server: 'localhost', engine: 'mssql' | 'postgres' | 'mysql', user: 'root', password: 'xxxx', database: 'DB_NAME' },
    sql: 'SELECT * FROM Album',
  });

dbgateApi.tableWriter

Imports data into table. Options are optional, default values are false.

  • dropIfExists - if table already exists, it is dropped before import
  • truncate - delete table content before import
  • createIfNotExists - create table, if not exists
  const reader = await dbgateApi.tableWriter({
    connection: { server: 'localhost', engine: 'mssql' | 'postgres' | 'mysql', user: 'root', password: 'xxxx', database: 'DB_NAME' },
    schemaName: 'dbo',
    pureName: 'Customer',
    options: {
      dropIfExists: false,
      truncate: false,
      createIfNotExists: false,
    }
  });

dbgateApi.jsonLinesReader

Reads JSON lines data file. On first line could be structure. Every line contains one row as JSON serialized object.

  const reader = await dbgateApi.jsonLinesReader({
    fileName: 'test.jsonl',
    encoding: 'utf-8',
    header: true,
    limitRows: null
  });

dbgateApi.jsonLinesWriter

Writes JSON lines data file. On first line could be structure. Every line contains one row as JSON serialized object.

  const reader = await dbgateApi.jsonLinesWriter({
    fileName: 'test.jsonl',
    encoding: 'utf-8',
    header: true
  });