Simple database package for Node.js.
npm i @jnode/db
// Import from the main entry
const { jdb, dble, ljson } = require('@jnode/db');
// Or import from subpaths
const { JDBFile } = require('@jnode/db/jdb');
const { DBLEFile, DBLEUInt32Field, DBLEStringField } = require('@jnode/db/dble');
const { LJSONFile } = require('@jnode/db/ljson');LJSON is perfect for small, append-only or simple text-based data.
const { LJSONFile } = require('@jnode/db/ljson');
(async () => {
// Load or create a database
const db = await LJSONFile.load('./data.ljson');
// Push new data
await db.push({ name: 'Alice', score: 10 });
await db.push({ name: 'Bob', score: 15 });
// Update data (adds to numbers/strings, merges objects, etc.)
await db.update(0, { score: 5 }); // Alice's score becomes 15
// Overwrite or delete
await db.overwrite(1, { name: 'Bob', score: 20 });
await db.delete(0); // Deletes Alice
})();DBLE is a fast, structured, binary-based database with schema definitions.
const { DBLEFile, DBLEUInt32Field, DBLEStringField } = require('@jnode/db/dble');
(async () => {
// Create a new DBLE database with a defined schema
const db = await DBLEFile.create('./users.dble', {
fields:[
new DBLEUInt32Field('id', true), // 'id' is a key (indexed)
new DBLEStringField(255, 'username')
]
});
// Append lines (rows)
await db.appendLine({ id: 1, username: 'justapple' });
await db.appendLine({ id: 2, username: 'nick' });
// Fast query using indexed keys
const nickData = await db.readLineByField('id', 2);
console.log(nickData.fields.username); // 'nick'
// Safely close the database
await db.close();
})();The @jnode/db package provides three core components to handle different data persistence needs seamlessly:
- JDB (
.jdb): The base binary file format manager. It handles basic file magic headers, versions, and provides raw file access wrappers. - DBLE (
.dble): Stands for JDB Lite Extended. It is a simple, fast, schema-defined binary database built on top of JDB. It supports fixed-length fields, typed records, automatic indexing, deleted space reuse, and dynamic extended fields. - LJSON (
.ljson): Stands for LineJSON. It follows theJLD(JustLineData) text-based format. It stores an array of JSON objects line-by-line and processes append-only mutations like updates, deletions, and overwrites, making it extremely durable for logging or small configs.
Advanced Usage (DBLE): Because DBLE queues its async file operations internally to ensure data integrity, executing multiple interdependent queries could sometimes face race conditions if another script block appends data concurrently. If you want to run complex logic that involves multiple operations, you can use the internal task queue method ._doTask(async () => { ... }) and pass true to the skipQueue argument of any method called inside. This bypasses the queue check internally and ensures your complex block runs uninterruptedly and safely!
The base JDB file manager for handling binary file headers.
path<string> Path to the file.- Returns: <Promise> Fulfills with a<jdb.JDBFile>.
Opens an existing JDB file and verifies its format and magic string.
path<string> Path to the file.options<Object>- Returns: <Promise> Fulfills with a <jdb.JDBFile>.
Creates a new JDB file (fails if the file already exists).
path<string> Path to the file.options<Object> Same asJDBFile.create().- Returns: <Promise> Fulfills with a <jdb.JDBFile>.
Forces creation of a new JDB file, overwriting if it already exists.
- Returns: <Promise>
Closes the internal file handle safely.
- Returns: <Promise>
Synchronizes the file's data and metadata to the storage device.
- Returns: <Promise>
Synchronizes only the file's data to the storage device.
Internal methods to initialize and load file headers.
The main manager for DBLE files, which extends JDBFile.
path<string> Path to the file.options<Object>- Returns: <Promise> Fulfills with a<dble.DBLEFile>.
path<string> Path to the file.options<Object>fields<DBLEField[]> Required. An array of field instances defining the table structure.extBaseLength<number> Optional override to calculate extension field base length manually.- Plus
DBLEFile.loadoptions.
- Returns: <Promise> Fulfills with a<dble.DBLEFile>.
Creates (or forcefully overwrites) a DBLE database file.
line<number> The index of the line/row.field<string> Field name.skipQueue<boolean> Skip the internal task queue execution lock.- Returns: <Promise> Resolves with the parsed field value.
Retrieves a specific field from a specific line.
Retrieves the concatenated binary extended data for a line. Returns a <Buffer>.
Finds the numeric line index where the specified field matches the query. If the field was declared with isKey: true, this operation is O(1) via index map. Otherwise, it executes a sequential scan.
Reads a full line structure at the target index. The resolved object looks like: { type, nextLine, fields: { ... } }.
Combination of getLineByField and readLine. Returns the parsed line object.
Iterates over all lines and returns the line index when the async callback fn(parsedLine) returns true.
Executes the callback fn(parsedLine, lineIndex) on every valid database line starting from start up to to.
Updates specific fields on an existing line. Pass {} to fields to just touch it, or pass an object with partial properties. Also updates indexes seamlessly. If line is not a number, it behaves like appendLine.
Finds the line via field/query matching, and updates it.
Appends a new record to the database, or overwrites an empty (deleted) line dynamically for disk space reuse. Returns the new line index.
Flags a line as deleted. If releaseSpace is true, it dynamically links the block to the empty line manager to allow future appendLine calls to overwrite this spot.
Finds a line by a query and deletes it.
Loops over the DB sequentially, completely clearing up lines marked as deleted and moving them to the empty reusable line linked list.
Saves an arbitrary binary<Buffer> to a line's extension block. If the buffer is large, it automatically chunks it across multiple linked empty lines.
Finds a line by field query and sets its extended buffer data.
File handle operations natively routed to the base JDB class.
Advanced tool. Runs your func exclusively inside the DB task queue, locking other async modifications from messing with your state.
Inner methods: _init, _loadHead, _loadIndices, _getLineBuffer, _writeLineBuffer, _appendLineBuffer, _getLastEmptyLine, _clearLinesFrom, _readlines, _parseLine, etc
Internal lifecycle and binary chunking methods.
The base interface for field typings in DBLE. You instantiate these classes in the fields array when creating a new database.
Access these via require('@jnode/db/dble') or dble.defaultDBLETypes:
DBLEInt8Field(name, isKey, isRelative)/DBLEUInt8FieldDBLEInt16Field(name, isKey, isRelative)/DBLEUInt16FieldDBLEInt32Field(name, isKey, isRelative)/DBLEUInt32FieldDBLEBigInt64Field(name, isKey, isRelative)/DBLEBigUInt64FieldDBLEFloatField(name, isKey, isRelative)/DBLEDoubleFieldDBLEStringField(maxLength, name, isKey, isRelative)DBLEBufferField(maxLength, name, isKey, isRelative)DBLEAnyField(length, name, isKey, isRelative)
Note: The maxLength argument is required for String and Buffer types to enforce DBLE's fixed-length binary schema standard.
Manages text-based LJSON files. This works by keeping a stateful memory representation of the JSON array, and appending modification operations (like +, =, -) to the file stream iteratively.
file<string> Target LJSON file path.options<Object>disableCleanupOnStart<boolean> Stop LJSON from compacting its mutation logs on load.
- Returns: <Promise> Fulfills with a <ljson.LJSONFile>.
Loads the JLD formatted LJSON file. If it doesn't exist, an empty file will be created.
Overwrites a file with a fresh, empty LJSON database.
Rewrites the entire file on disk, collapsing all mutation lines (updates, overwrites, deletions) into a clean, flat list of final JSON representations.
json[<any>] Valid JSON.- Returns:<Promise>
Appends a new line with the object directly to the database.
index<number> The array index to modify.json[<any>] The mutation payload.
Applies a mutation diff based on the original data type:
- String / Number:
original += update - Boolean:
original = update !== original - Object:
Object.assign(original, update) - Array:
original.push(...update)
Overwrites the target index entirely with the new json payload.
Removes the index element from the database. Note that this shifts subsequent indexes.
Parses textual JLD lines and applies all operators into the memory array.
---- File Head
[4 Byte] "JDBD"
[UInt8 ] Version, 1
[3 Byte] RSV
[4 Byte] DB Type, "DBLE"
[UInt8 ] DB Version, 1
[3 Byte] RSV
---- DB Head
[Array ] Definition[16 Bit] Flag (isLast, isKey, isRelative RSV...)
[UInt16] Field Length
[UInt8 ] Type Name Length
[String] Type Name
[UInt8 ] Field Name Length
[String] Field Name
[n Byte] (OPTIONAL) Field Relative Data[UInt16] Extend Field Base Length (making each line 2 ** N bytes)
[UInt32] Last Empty Line (0xFFFFFFFF for null)
[n Byte] Padding (making 'File Head' + 'DB Head' N * Line Length)
---- DB Body
[Array ] Lines
[2 Bit ] Type (Normal Start, Extended, Empty, Deleted Start)
[14 Bit] Flag[UInt32] Extended Line At/Last Empty Line (if type is 'Empty', 0xFFFFFFFF for null)
[Array ] Line Data
[n Byte] Field Data
This is a simple text-based, line-based data format.
JLDstands for JustLineData, a subtype of JDB but is text-and-line-based while a.jdbfile is binary based.
@ JLD 01 <DATA-TYPE (4 ASCII Character)> <DATA-VERSION (2 HEX)>
[@ <OTHER-HEADERS...>]...
<ACTUAL-DATA>...
For LJSON, we use:
@ JLD 01 LJSO 01
<JSON>JSON data line. E.G.{"name":"nick"}./ <CONTENT>Comment. Only for temporary use, we won't keep the comment lines.+ <INDEX> <JSON>Update line. Update JSON's data type must be same as original JSON's data type. Works in different ways according to the data type:- String:
original += update. - Number:
original += update. - Boolean:
original = update !== original. - Object:
Object.assign(original, update) - Array:
original.push(...update)
- String:
= <INDEX> <JSON>Overwrite line.- <INDEX>Delete line.