Database-js INI File Driver

1.0.1 · maintenance · verified Wed Apr 22

database-js-ini is a JavaScript package that functions as a driver for the database-js unified database interface, enabling SQL-like querying of INI configuration files. It allows developers to establish a connection to an INI file using a database-js connection string (e.g., `database-js-ini:///path/to/file.ini`) and execute `SELECT` statements, treating INI sections as tables and properties as columns. The package is currently at version 1.0.1. Given its specific utility as a wrapper and the age of its latest significant update, it operates under a maintenance status, implying stability rather than active feature development. Its core differentiation is providing a consistent, Promise-based `database-js` API for interacting with the simple, structured data of INI files, abstracting file parsing and offering a familiar query paradigm for configuration management.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `database-js-ini` to connect to and query data from a simple INI file. It creates a temporary `config.ini` file, establishes a `database-js` connection, executes `SELECT` queries against different INI sections, and logs the results before cleaning up.

import { Connection } from 'database-js';
import * as fs from 'fs';

const iniContent = `
[SERVER]
port=8080
host=localhost

[DATABASE]
type=sqlite
filepath=/tmp/data.db
`;

const iniFilePath = './config.ini';

// Create a temporary INI file for the example
fs.writeFileSync(iniFilePath, iniContent, 'utf8');

(async () => {
    let connection: Connection | null = null;
    try {
        // Connect using the database-js-ini protocol and specify the INI file path
        connection = new Connection(`database-js-ini:///${iniFilePath}`);
        
        // Query all properties from the [SERVER] section
        let statement = await connection.prepareStatement("SELECT port, host FROM SERVER");
        let serverSettings = await statement.query();
        console.log('Server Settings:', serverSettings);

        // Query all properties from the [DATABASE] section
        statement = await connection.prepareStatement("SELECT type, filepath FROM DATABASE");
        let databaseSettings = await statement.query();
        console.log('Database Settings:', databaseSettings);

    } catch (error) {
        console.error('An error occurred during INI file access:', error);
    } finally {
        if (connection) {
            await connection.close();
        }
        // Clean up the temporary INI file
        if (fs.existsSync(iniFilePath)) {
            fs.unlinkSync(iniFilePath);
        }
    }
})();

view raw JSON →