DBFFile
Summary
Read and write .dbf (dBase III) files in Node.js:
- Supports
C
(string) , N
(numeric) , I
(integer) , L
(logical) and D
(date) field types - Can open an existing .dbf file
- Can access all field descriptors
- Can access total record count
- Can read records in arbitrary-sized batches
- Supports very large files
- Can create a new .dbf file
- Can use field descriptors from a user-specified object of from another instance
- Can append records to an existing .dbf file
- Supports very large files
- Can specify character encodings either per-file or per-field.
- the default encoding is
'ISO-8859-1'
(also known as latin 1) - example per-file encoding:
DBFFile.open(<path>, {encoding: 'EUC-JP'})
- example per-field encoding:
DBFFile.open(<path>, {encoding: {default: 'latin1', FIELD_XYZ: 'EUC-JP'}})
- supported encodings are listed here.
- All operations are asynchronous and return a promise
Installation
npm install dbffile
or yarn add dbffile
Example: reading a .dbf file
import {DBFFile} from 'dbffile';
async function testRead() {
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);
for (let record of records) console.log(records);
}
Example: writing a .dbf file
import {DBFFile} from 'dbffile';
async function testWrite() {
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.`);
}
API
The module exports the DBFFile
class, which has the following shape:
class DBFFile {
static open(path: string, options?: Options): Promise<DBFFile>;
static create(path: string, fields: Field[], options?: Options): Promise<DBFFile>;
path: string;
recordCount: number;
fields: Array<{
name: string;
type: string;
size: number;
decimalPlaces?: number;
}>;
readRecords(maxCount?: number): Promise<object[]>;
appendRecords(records: object[]): Promise<DBFFile>;
}