1.0.0 • Published 1 year ago

dbf-node v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

dbf-node

To get started, simply install the module using npm:

$ npm install dbf-node

and then import it:

const { Dbf } = require("dbf-node")

API

The module exports the Dbf class, which has the following shape:

/** Represents a DBF file. */
class Dbf {

    /** Opens an existing DBF file. */
    static open(path, options);

    /** Creates a new DBF file with no records. */
    static create(path, fields, options);

  	/** Read the specified records to this DBF file. */
    static readRecords(startIndex, endIndex);
  
  	/** Write the specified records to this DBF file. */
    static write(records);
  
  	/** Update the specified records to this DBF file. */
    static update(records);
}

Example: read all records in a .dbf file using for-await-of

import {DBFFile} from 'dbffile';

async function iterativeRead() {
    let dbf = await DBFFile.open('<full path to .dbf file>');
    console.log(`DBF file contains ${dbf.recordCount} records.`);
    console.log(`Field names: ${dbf.fields.map(f => f.name).join(', ')}`);
    for await (const record of dbf) console.log(record);
}

Example: reading a batch of records from a .dbf file

import {DBFFile} from 'dbffile';

async function batchRead() {
    let dbf = await DBFFile.open('<full path to .dbf file>');
    console.log(`DBF file contains ${dbf.recordCount} records.`);
    console.log(`Field names: ${dbf.fields.map(f => f.name).join(', ')}`);
    let records = await dbf.readRecords(100); // batch-reads up to 100 records, returned as an array
    for (let record of records) console.log(record);
}

Example: writing a .dbf file

import {DBFFile} from 'dbffile';

async function batchWrite() {
    let fieldDescriptors = [
        { name: 'fname', type: 'C', size: 255 },
        { name: 'lname', type: 'C', size: 255 }
    ];

    let records = [
        { fname: 'Joe', lname: 'Bloggs' },
        { fname: 'Mary', lname: 'Smith' }
    ];

    let dbf = await DBFFile.create('<full path to .dbf file>', fieldDescriptors);
    console.log('DBF file created.');
    await dbf.appendRecords(records);
    console.log(`${records.length} records added.`);
}

****

1.0.0

1 year ago