@coderundebug/dbase
v1.0.0
Published
Read and write to dBase 3 files in Node JS.
Readme
dbase
Read and write to dBase 3 files in Node JS. You may be able to read from newer versions, but the file format can be too different for this to be possible.
Important: This is an ESM NodeJS only package.
Installation
npm install @coderundebug/dbaseContents
Introduction
The dBase file format is somewhat old and was used by many different applications a long time ago. There are still some places where these files are used and so you may find this package useful. It should be able to read dBase version 3, 4 and 5 versions. It can only write dBase 3 version files however. It comes in two classes, DBaseReader and DBaseWriter.
import { DBaseReader } from "@coderundebug/dbase";
// Create dBase reader object
const dBaseReader = new DBaseReader();
// Open the database file
await dBaseReader.open('c:\\database\\DBASE3.DBF');
// Loop for each record
while (true) {
// Get next record
const record = await dBaseReader.read();
// If no more records
if (record === null) break;
// Log record
console.log(record);
}
// Close the database file
await dBaseReader.close();In this example we are opening the database file, read in each available record, then closing it.
import { DBaseWriter } from "@coderundebug/dbase";
// Create dBase writer object
const dBaseWriter = new DBaseWriter();
// Create column list
const columnList = [
{ property: 'id', name: 'ID', type: 'N', length: 19, decimalPlaces: 5 },
{ property: 'text', name: 'TEXT', type: 'C', length: 254 },
{ property: 'memo', name: 'MEMO', type: 'M' },
{ property: 'number', name: 'NUMBER', type: 'N', length: 19, decimalPlaces: 5 },
{ property: 'date', name: 'DATE', type: 'D' },
{ property: 'boolean', name: 'BOOLEAN', type: 'L' },
{ property: 'float', name: 'FLOAT', type: 'F', length: 19, decimalPlaces: 5 }
];
// Create record list
const recordList = [
{ id: 1, text: 'text1', memo: 'memo1', number: 1, date: new Date(2026, 2, 6), boolean: false, float: 1.2345 },
{ id: 2, text: 'text2', memo: 'memo2', number: 2, date: new Date(2026, 2, 7), boolean: true, float: 2.3456 },
{ id: 3, text: 'text3', memo: 'memo3', number: 3, date: new Date(1974, 0, 2), boolean: true, float: 3.4567 },
{ id: 4, text: 'text4', memo: 'memo4', number: 4, date: new Date(2000, 11, 23), boolean: false, float: 4.5678 }
];
// Open database file (this creates a new file)
await dBaseWriter.open('c:\\database\\DBASE3.DBF', columnList);
// Write out the records
await dBaseWriter.write(recordList[0]);
await dBaseWriter.write(recordList[1]);
await dBaseWriter.write(recordList[2]);
await dBaseWriter.write(recordList[3]);
// Close the database file
await dBaseWriter.close();In this example we create a list of columns, then a list of records, open the new file, write each record to the database, and then close it.
Quick Functions
There are some static functions that can help you perform some quick tasks.
Load
Load a dBase database file, and read in all the records and return them.
DBaseReader.load(dbfPath, [options]);
Arguments
dbfPath - The full path of the database file to open. This will be the file that ends with *.DBF. If memo fields are used then it will try to load either the *.DBT or *.FBT (for FoxPro).
options - The options you can give. [cacheSize=10000] - Is used for set the buffer cache size. [encoding='ascii'] - The encoding used when reading in column names and text data.
Returns
A promise that resolves with a list of record objects.
Example
import { DBaseReader } from "@coderundebug/dbase";
// Load in the list of records from the database file
const recordList = await DBaseReader.load('c:\\database\\DBASE3.DBF');Create Column List
Creates a list of columns using the properties within the list of records. It checks the size of the text fields and sees if they need to be memos.
DBaseWriter.createColumnList(recordList);
Arguments
- recordList - The list of records what will later be writen to the database file.
Returns
A promise that resolves with a list of column objects.
Example
import { DBaseWriter } from "@coderundebug/dbase";
// Get the list of records from somewhere
const recordList = [
{ id: 1, text: 'Hello world', number: 123 },
];
// Create a list of columns that contains all the properties used within the records
const columnList = DBaseWriter.createColumnList(recordList);Save
Save all the records into a dBase database file.
DBaseWrite.save(dbfPath, recordList, columnList, [options]);
Arguments
dbfPath - The full path of the database file to create. This will be a file that ends with *.DBF. If memo fields are used then it will create the *.DBT file too.
recordList - The list of records to save to the database.
columnList - The list of columns (record fields).
options - The options you can give. [cacheSize=10000] - Is used for set the buffer cache size. [encoding='ascii'] - The encoding used when writing out column names and text data. [memoBlockHeader=false] - Do the memo blocks have 8 byte headers?
Example
import { DBaseWriter } from "@coderundebug/dbase";
// Get the list of records from somewhere
const recordList = [
{ id: 1, text: 'Hello world', number: 123 },
];
// Create a list of columns that contains all the properties used within the records
const columnList = DBaseWriter.createColumnList(recordList);
// Save the list of records to the database file
await DBaseWriter.save('c:\\database\\DBASE3.DBF', recordList, columnList);DBaseReader
This class is used to read in different versions of dBase database files. All functions return a promise.
Open
Open the dBase database file.
open(dbfPath, [options]);
Arguments
dbfPath - The full path of the database file to open. This will be the file that ends with *.DBF. If memo fields are used then it will try to load either the *.DBT or *.FBT (for FoxPro).
options - The options you can give. [cacheSize=10000] - Is used for set the buffer cache size. [encoding='ascii'] - The encoding used when reading in column names and text data.
Close
Close the database file.
close();
Read
Reads in the next record.
read();
Returns
Returns a promise that resolves to the a record object. If the record is null then it has reached the end of the database file.
Properties
After opening the database file you will be able to access a number of useful properties.
version lastUpdated count columnList
DBaseWriter
This is used to create dBase 3 database files.
Open
Open the dBase database file.
open(dbfPath, columnList, [options]);
Arguments
dbfPath - The full path of the database file to create. This will be a file that ends with *.DBF. If memo fields are used then it will create the *.DBT file too.
columnList - The list of columns (record fields).
options - The options you can give. [cacheSize=10000] - Is used for set the buffer cache size. [encoding='ascii'] - The encoding used when writing out column names and text data. [memoBlockHeader=false] - Do the memo blocks have 8 byte headers?
Close
Close the database file.
close();
Write
Write a record to the database file.
write(record);
Arguments
- record - The record that contains all the fields to be written to the database file.
Column Details
The information about the columns used when reading or writing data are as follows.
property
The name of the record's property (only used with writing).
name
The name of the column. This can only be 10 characters long.
type
The field type of the column. This can be one of the following.
| Code | Description | |-------|-------------| | C | Character/text (ASCII, padded with spaces, max 254 chars) | | F | Floating point number (stored as text, padded with spaces, max 20 digits, 18 decimal places) | | D | Date (no time) (YYYYMMDD) | | L | Logical/boolean (T/t/Y/y/F/f/N/n or ? for null) | | M | Memo | | N | Numeric (same as F) |
length
The field's data length (in characters).
decimalPlaces
When used with a numeric or float, this gives the number of digits after the decimal point.
