Connect to xBase using Node.js

You can connect to xBase from Node.js using Devart ODBC Driver for xBase.

Install the odbc package

Run the following command to install the odbc package.

npm install odbc

Define a connection string

An ODBC connection requires a connection string, which can either use a predefined DSN or be specified through connection string parameters (a DSN-less connection).

For information about available parameters, see Connection string parameters.

DSN connection string

You can use a connection string with a predefined DSN.

const connectionString = 'DSN=your_dsn';

For information on configuring a DSN on specific operating systems, see instructions for Windows, macOS, or Linux.

DSN-less connection string

You can establish a connection without a DSN by specifying all necessary parameters directly in the connection string.

const connectionString = 'DRIVER=Devart ODBC Driver for xBase;Database=your_database_files_location;DBFFormat=your_database_format'

For more information, see DSN-less connections.

Connect to xBase

The following Node.js code demonstrates how to connect to xBase and fetch data using the ODBC driver.

const odbc = require('odbc');

// Define an async function to connect to a data source and execute a SQL query
async function connectAndQuery() {
  // Declare a variable to hold the ODBC connection
  var connection;

  try {
    // Define an ODBC connection string
    const connectionString = 'your_connection_string';

    // Create an object that represents a connection to a data source
    connection = await odbc.connect(connectionString);

    // Execute a SQL query to retrieve data from the data source
    const result = await connection.query('SELECT ID, Name FROM Account');    

    // Print the query results
    console.log('xBase data:', result);

    // You can integrate this data into your app logic here
    // Example: Save the result to your application's database or display it on the UI
  } catch (error) {
    // Handle any errors
    console.error('Error connecting to xBase via ODBC:', error);
  } finally {
    // Ensure the connection is closed to free resources
    if (connection) {
      await connection.close();
    }
  }
}

// Call the function as part of your application's workflow
connectAndQuery();