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.
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.
In this example we are opening the database file, read in each available record, then closing it.
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.
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.
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
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.
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
Save
Save all the records into a dBase database file.
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
This class is used to read in different versions of dBase database files.
All functions return a promise.
Open
Open the dBase database file.
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.
Read
Reads in the next record.
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.
The version of the dBase file. This is normally 3, 4 or 5.
The data the database was last updated. This is only the year, month and day (no time).
The number of records within the dasebase. No sure if this includes the deleted ones.
A list of column objects with detailed information about each one.
This is used to create dBase 3 database files.
Open
Open the dBase database file.
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.
Write
Write a record to the database file.
Arguments
-
record
- The record that contains all the fields to be written to the database file.
The information about the columns used when reading or writing data are as follows.
Code
Description
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. Normally upper case.
type
The field type of the column (see below).
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.
These are the different column types.
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, max 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).