UNPKG

@pujansrt/data-genie

Version:

High performant ETL engine written in TypeScript

57 lines (56 loc) 2.79 kB
import { DataWriter, DataRecord, SqlConnection } from '../core/interfaces'; /** * SQLWriter class for writing data records to a SQL database. * It uses a provided SqlConnection interface, allowing the user to supply * their own database client (e.g., pg.Pool, mysql2.Connection) without * this library needing direct npm dependencies on specific database drivers. */ export declare class SQLWriter implements DataWriter { private readonly tableName; private dbClient; private fieldNames; private initializedFieldNames; /** * Constructs a new SQLWriter. * @param dbClient An object implementing the SqlConnection interface, provided by the user. * This is typically a database client or connection pool from an npm package * (e.g., new Pool() from 'pg', createConnection() from 'mysql2'). * @param tableName The name of the database table to write to. */ constructor(dbClient: SqlConnection, tableName: string); /** * Sets the names of the fields. These names correspond to the column names in the database table * and define the order of data extraction from `DataRecord` objects. * @param names A list of string names for the fields (column names). * @returns The current SQLWriter instance for chaining. */ setFieldNames(...names: string[]): this; /** * Writes a single data record to the SQL database. * It constructs an `INSERT` statement dynamically based on field names and record data. * @param record The DataRecord object to write. * @returns A Promise that resolves when the record has been inserted. * @throws Error if field names are not defined. */ write(record: DataRecord): Promise<void>; /** * Writes all data records from an asynchronous iterable to the SQL database. * This method performs individual inserts. For batch inserts, consider a dedicated * batching mechanism or a transactional approach if the underlying dbClient supports it. * * @param records An AsyncIterableIterator of DataRecord objects. * @returns A Promise that resolves when all records have been written. */ writeAll(records: AsyncIterableIterator<DataRecord>): Promise<void>; /** * In this design, the SQLWriter does not own the lifecycle of the dbClient. * The user who provides the dbClient is responsible for closing it. * This method is implemented to satisfy the DataWriter interface but does nothing. * If you want the SQLWriter to manage connection closing, the SqlConnection interface * would need an `end()` or `close()` method, and the user would pass a client * that can be closed. * * @returns A Promise that resolves immediately. */ close(): Promise<void>; }