LevelGraph - Graph Database
LevelGraph is a graph database implemented for both Node.js and browser environments, leveraging the high-performance LevelDB key-value store through the `level` library. Currently stable at version 4.0.0, its release cadence appears to be tied to significant updates in its underlying `level` dependency, as seen with the v4.0.0 release updating to `level` v8. A key differentiator is its implementation of the Hexastore approach, employing six indices for each triple (subject, predicate, object) to ensure rapid data access and pattern matching. It supports standard graph database operations like triple insertion, retrieval, and deletion, with capabilities for advanced searches and filtering. It offers a consistent API across Node.js and browser contexts, though initialization differs slightly.
Common errors
-
Error: Cannot find module 'level-browserify'
cause This error typically occurs when using LevelGraph versions prior to 3.0.0 with newer Node.js environments, where the deprecated `level-browserify` package had installation issues.fixUpgrade `levelgraph` to version 3.0.0 or higher. This version replaces `level-browserify` with the more compatible `level` package. -
TypeError: Object.keys called on non-object
cause Reported in older versions (fixed in v1.1.3), this error indicates that a non-object value was passed where an object (e.g., a triple pattern) was expected, causing `Object.keys()` to fail.fixEnsure that all arguments expected to be objects (like triple patterns for `db.get` or `db.put`) are indeed valid JavaScript objects. If you are on an older version, upgrade to `levelgraph` v1.1.3 or newer. -
Error: The 'path' argument must be of type string. Received undefined
cause This is a common error from the underlying `level` library when its constructor `new Level()` is called without a valid string path for the database directory.fixAlways provide a valid, non-empty string path when initializing `new Level('your-database-path')`. Ensure your database path variable is correctly defined and not `null` or `undefined`.
Warnings
- breaking LevelGraph v4.0.0 updates its internal `level` dependency to v8. This update can introduce breaking changes from the `level` library itself, particularly regarding its asynchronous API and potential Node.js version requirements. Ensure compatibility with your existing `level` usage.
- breaking In v3.0.0, `level-browserify` was replaced by `level` as the underlying store for browser environments. `level-browserify` was noted to have installation issues on newer Node.js versions, making this a critical upgrade for compatibility.
- gotcha Stream closing behavior for internal tests in v3.0.0 was changed from listening to the `close` event to the `end` event due to double-triggering. While this primarily affected internal tests, developers relying on specific stream lifecycle events in their own code might experience different behavior.
- gotcha LevelGraph officially tests only against Node.js versions 18 and 20. While other versions may function, their support is not guaranteed, and compatibility issues might arise.
Install
-
npm install levelgraph -
yarn add levelgraph -
pnpm add levelgraph
Imports
- levelgraph
import levelgraph from 'levelgraph';
const levelgraph = require('levelgraph'); - Level
import { Level } from 'level';const { Level } = require('level'); - db.put
db.put('s', 'p', 'o', callback);db.put({ subject: 's', predicate: 'p', object: 'o' }, callback);
Quickstart
const { Level } = require('level');
const levelgraph = require('levelgraph');
const path = require('path');
const os = require('os');
const fs = require('fs');
// Create a temporary directory for the database
const dbPath = path.join(os.tmpdir(), `levelgraph-test-${Date.now()}`);
// Initialize the database with a LevelDB store
const db = levelgraph(new Level(dbPath));
const triple = { subject: 'book', predicate: 'hasAuthor', object: 'Alice' };
db.put(triple, function(err) {
if (err) {
console.error('Error putting triple:', err);
return;
}
console.log('Triple inserted:', triple);
// Retrieve triples matching a pattern
db.get({ subject: 'book', predicate: 'hasAuthor' }, function(err, triples) {
if (err) {
console.error('Error getting triples:', err);
return;
}
console.log('Retrieved triples:', triples);
// Expected output: [{ subject: 'book', predicate: 'hasAuthor', object: 'Alice' }]
// Clean up: close the database and remove the directory
db.close(function(closeErr) {
if (closeErr) console.error('Error closing db:', closeErr);
else console.log('Database closed.');
fs.rm(dbPath, { recursive: true, force: true }, (rmErr) => {
if (rmErr) console.error('Error removing database directory:', rmErr);
else console.log('Database directory removed.');
});
});
});
});