Registry / database / level-orm

level-orm

JSON →
library2.0.0jsnpmunverified

Simple ORM for leveldb/levelup providing getters, setters, and streaming for models backed by sublevels. Current stable version is 2.0.0. Allows defining models with a primary key, supports compound keys via bytewise encoding, and auto-incrementing keys via a keyfn callback. Designed for use with level-sublevel for shared database state across multiple models. Uses an inheritable base class pattern. Lightweight and minimal compared to full ODM solutions like mongoose.

npm install level-orm
INSTALL
IMPORT
SIG · LEVEL-ORM
L
level-orm
databasejavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Model
import Model from 'level-orm'
const Model = require('level-orm').default
Default export is the constructor. No named exports.
default
const Model = require('level-orm')
const { Model } = require('level-orm')
CommonJS usage is straightforward; no destructuring needed.
Model
const Model = require('level-orm')
import { Model } from 'level-orm'
ESM default import is the correct way; named import will return undefined.
Model as BaseModel
import BaseModel from 'level-orm'
import { default as BaseModel } from 'level-orm'
Renaming default import is fine, but avoid named import syntax.

Creates a leveldb-backed ORM model for 'users', saves a user, retrieves it, and deletes it.

const level = require('level'); const Model = require('level-orm'); const util = require('util'); // Create a leveldb instance const db = level('./mydb', { valueEncoding: 'json' }); // Define a User model with primary key 'handle' function Users(container) { Model.call(this, container, 'users', 'handle'); } util.inherits(Users, Model); const users = new Users({ db: db }); // Save a user users.save({ handle: 'johndoe', name: 'John Doe' }, (err, id) => { if (err) return console.error(err); console.log('Saved user with id:', id); // Retrieve the user users.get('johndoe', (err, user) => { if (err) return console.error(err); console.log('Retrieved user:', user); // Delete the user users.del('johndoe', (err) => { if (err) return console.error(err); console.log('User deleted'); }); }); });
Debug
Known issues
gotchaDefault key encoding in leveldb is 'utf8' which sorts lexicographically; numbers sort as strings (e.g., '10' before '2'). Use bytewise encoding for proper numerical or compound key ordering.
fix
Use bytewise as keyEncoding when opening the database: level(path, { keyEncoding: bytewise, valueEncoding: 'json' })
affects: >=0.0.0
gotchaThe container object passed to the constructor must have a 'db' property containing a levelup instance; otherwise the model will not function.
fix
Ensure you pass an object like { db: levelInstance } (or a sublevel) as the first argument.
affects: >=0.0.0
deprecatedThe package uses util.inherits which is deprecated in modern Node.js; use ES6 class extends instead for new code.
fix
Replace with 'class Users extends Model { constructor(container) { super(container, 'users', 'handle'); } }'
affects: >=2.0.0
gotchaThe key function (keyfn) must be synchronous and return a new key; if it returns an existing key, save will overwrite that record.
fix
Implement keyfn as a function that generates a unique value, e.g., using monotonic-timestamp.
affects: >=0.0.0
breakingVersion 2.0.0 changed the constructor signature to require a container object instead of a raw db instance.
fix
Ver 1.x: new Model(db, sublevelName, key). In 2.x: new Model({ db: db }, sublevelName, key).
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: container.db is not a function
The container object passed to the model constructor does not have a 'db' property that is a levelup instance.
fix
Ensure you pass an object with a 'db' property: new Model({ db: yourLevelInstance }, 'users', 'handle'); instead of new Model(yourLevelInstance, ...).
Error: key '10' does not sort as expected
Default utf8 encoding sorts keys lexicographically, so '10' comes before '2'
fix
Use bytewise encoding for keys: require('bytewise/hex') and set keyEncoding: bytewise in level options.
TypeError: Class extends value #<Object> is not a constructor or null
Using ES6 class extends on the imported Model default without proper constructor handling.
fix
Define a constructor that calls super with the container, sublevel name, and key, then export the class.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
levelrequiredRequires a leveldb instance to function
subleveloptionalOften used for namespacing models within the same database
bytewiseoptionalNeeded for compound key encoding to get proper sorting
Agent activity
2 hits · last 30 days
node
2
Resources
level-orm — npm install level-orm · libregistry