DBINFOZ Universal Database Adapter

0.14.0 · active · verified Wed Apr 22

DBINFOZ is a JavaScript/TypeScript library designed to provide a simple and unified interface for interacting with various SQL databases, including PostgreSQL, MySQL, MSSQL, and SQLite. Currently at version 0.14.0, the package aims to abstract away database-specific connection and query methods, allowing developers to list databases, tables, and retrieve table schemas through a consistent API. While there's no explicit release cadence mentioned, the 0.x.x versioning implies active development and potential for non-semver breaking changes. Its key differentiator is offering a single factory function to obtain adapters for multiple database types, reducing the boilerplate of managing individual database client libraries directly in application logic.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a database adapter for SQLite or PostgreSQL, list tables, retrieve a table's schema, and execute a custom query, while also showing proper type usage and environment variable integration for sensitive credentials.

import { getDatabaseAdapter } from 'dbinfoz';
import type { DatabaseConfig, DatabaseAdapter } from 'dbinfoz';

// Configuration for a SQLite database. Replace with your actual database details.
const sqliteConfig: DatabaseConfig = {
  filename: process.env.SQLITE_DB_PATH ?? './mydb.sqlite',
};

// Configuration for a PostgreSQL database. Remember to install 'pg' separately.
const postgresConfig: DatabaseConfig = {
  host: process.env.PG_DB_HOST ?? 'localhost',
  user: process.env.PG_DB_USER ?? 'yourUsername',
  database: process.env.PG_DB_NAME ?? 'yourDatabase',
  password: process.env.PG_DB_PASSWORD ?? 'yourPassword',
  port: parseInt(process.env.PG_DB_PORT ?? '5432', 10),
};

// Choose your database type and config
const type: 'sqlite' | 'postgres' = 'sqlite'; // or 'postgres', 'mysql', 'mssql'
const config = type === 'sqlite' ? sqliteConfig : postgresConfig; // Use appropriate config

(async () => {
  let dbAdapter: DatabaseAdapter | null = null;
  try {
    dbAdapter = getDatabaseAdapter(type, config);
    console.log(`Connected to ${type} database.`);

    // List tables
    const tables = await dbAdapter.listTables();
    console.log('Tables:', tables);

    // Example: Get schema for a specific table (if it exists)
    if (tables.length > 0) {
      const firstTable = tables[0];
      console.log(`Schema for table '${firstTable}':`);
      const schema = await dbAdapter.getTableSchema(firstTable);
      console.log(schema);
    }

    // Run a custom query (example: create a table for sqlite if not exists)
    if (type === 'sqlite') {
      await dbAdapter.runQuery('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT);');
      console.log('Checked/created users table.');
    }

  } catch (error: any) {
    console.error('Error:', error.message);
  } finally {
    // Some adapters (like SQLite) might have a 'close' method
    if (dbAdapter && typeof (dbAdapter as any).close === 'function') {
      await (dbAdapter as any).close();
      console.log('Database connection closed.');
    }
  }
})();

view raw JSON →