A simple and unified interface to interact with different types of SQL databases including PostgreSQL, MySQL, and SQLite.
- 📚 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, and SQLite databases.
npm install dbinfoz
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 SQLite
const sqliteConfig = {
filename: './path/to/database.sqlite',
};
const sqliteAdapter = getDatabaseAdapter('sqlite', sqliteConfig);
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);
}
})();
Contributions, issues, and feature requests are welcome! Feel free to check issues page.
MIT © Jason Jacobs
The source code is available at GitHub.