JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2783
  • Score
    100M100P100Q113751F
  • License MIT

Get Database Info and Table Schemas

Package Exports

  • dbinfoz
  • dbinfoz/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dbinfoz) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

🌐 DBINFOZ Universal Database Adapter

A simple and unified interface to interact with different types of SQL databases including PostgreSQL, MySQL, MSSQL, and SQLite.

✨ Features

  • 📚 List all databases
  • 📃 List all tables within a database
  • 📄 Get the schema of a table
  • 🗂 Get all tables and their schemas within a database

Supports PostgreSQL, MySQL, MSSQL, and SQLite databases.

🛠 Installation

npm install dbinfoz

🚀 Usage

First, require the package and use the factory function to get an instance of the database adapter based on the type of database you're working with:

const getDatabaseAdapter = require('dbinfo');

// For PostgreSQL
const postgresConfig = {
  user: 'yourUsername',
  host: 'localhost',
  database: 'yourDatabase',
  password: 'yourPassword',
  port: 5432,
};
const postgresAdapter = getDatabaseAdapter('postgres', postgresConfig);

// For MySQL
const mysqlConfig = {
  host: 'localhost',
  user: 'yourUsername',
  database: 'yourDatabase',
  password: 'yourPassword',
  port: 3306,
};
const mysqlAdapter = getDatabaseAdapter('mysql', mysqlConfig);

// For MSSQL
const mssqlConfig = {
  host: 'localhost',
  user: 'yourUsername',
  database: 'yourDatabase',
  password: 'yourPassword',
  port: 3306,
};
const mssqlAdapter = getDatabaseAdapter('mssql', mssqlConfig);

// For SQLite
const sqliteConfig = {
  filename: './path/to/database.sqlite',
};
const sqliteAdapter = getDatabaseAdapter('sqlite', sqliteConfig);

📖 Examples

const getDatabaseAdapter = require('./index');

// Configuration for the required database
const config = {
  host: 'localhost', // For MySQL and PostgreSQL
  user: 'root', // For MySQL
  password: 'password', // For MySQL and PostgreSQL
  database: 'mydb',
  filename: './mydb.sqlite' // For SQLite
};

// Specify the database type ('sqlite', 'mysql', 'postgres')
const type = 'sqlite'; // Change as needed

(async () => {
  try {
    const dbAdapter = getDatabaseAdapter(type, config);

    // List databases
    const databases = await dbAdapter.listDatabases();
    console.log('Databases:', databases);

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

    // Get table schema
    const schema = await dbAdapter.getTableSchema('my_table');
    console.log('Schema:', schema);

    // Run a custom query
    const result = await dbAdapter.runQuery('SELECT * FROM my_table');
    console.log('Query Result:', result);

    // Close the connection (SQLite specific method for example purposes)
    if (dbAdapter.close) {
      await dbAdapter.close();
    }
  } catch (error) {
    console.error('Error:', error.message);
  }
})();

💡 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues page.

📝 License

MIT © Jason Jacobs

📁 Repository

The source code is available at GitHub.