Warehouse JSON Database
Warehouse is a simple JSON-based database library for Node.js, providing a robust interface for managing data through Models, Schemas, and a flexible querying system. It is notably the underlying data store for the Hexo static site generator. The current stable version is 6.0.0, released recently in 2024, indicating active development. The project maintains a consistent release cadence with significant updates, including performance improvements and type refinements in recent major versions. Key differentiators include its focus on a structured schema-driven approach for JSON data, a powerful query engine, and strong TypeScript support, making it suitable for applications requiring local data persistence and structured access without a full-fledged SQL or NoSQL server. It has dropped support for older Node.js versions, currently requiring Node.js 18 or higher.
Common errors
-
RangeError: Invalid string length
cause An internal issue with JSONStream re-implementation in early 4.x versions could lead to this error when handling large data sets.fixUpgrade to Warehouse v4.0.2 or higher to resolve the underlying bug related to JSONStream handling. -
TypeError: Database is not a constructor
cause This error typically occurs if you attempt to use `Database` as a function or if the import mechanism (CommonJS vs. ESM) is incorrectly applied, especially after v3.0.0's switch to class declarations.fixEnsure you are importing `Database` correctly for your module system (`require('warehouse')` or `import Database from 'warehouse'`) and that you are instantiating it with the `new` keyword (e.g., `const db = new Database();`). -
Error: Node.js version not supported
cause The installed version of Warehouse requires a newer Node.js runtime than the one currently in use. For example, v6.0.0 requires Node.js 18+.fixUpgrade your Node.js environment to meet the minimum version specified in the `engines` field of the `warehouse` package's `package.json` file.
Warnings
- breaking Warehouse v6.0.0 dropped support for Node.js 16.
- breaking In Warehouse v3.0.0, the constructor for `Database` and custom `SchemaType` classes changed from a function declaration to a class declaration.
- breaking Warehouse v4.0.0 replaced the `exportAsync()` method.
- gotcha Warehouse frequently updates its minimum Node.js version requirement between major releases, potentially breaking CI/CD pipelines or deployments if not monitored.
Install
-
npm install warehouse -
yarn add warehouse -
pnpm add warehouse
Imports
- Database
import { Database } from 'warehouse'; // Incorrect for default export const { Database } = require('warehouse'); // Incorrect for default exportimport Database from 'warehouse'; // ESM const Database = require('warehouse'); // CommonJS - SchemaType
import { SchemaType } from 'warehouse'; // Incorrect import path const SchemaType = require('warehouse'); // Incorrect import pathimport SchemaType from 'warehouse/schematype'; // ESM const SchemaType = require('warehouse/schematype'); // CommonJS - Model, Schema
import type { Model, Schema, Database } from 'warehouse';
Quickstart
const Database = require('warehouse');
const db = new Database();
const Post = db.model('posts', {
title: String,
created: {type: Date, default: Date.now}
});
Post.insert({
title: 'Hello world'
}).then(function(post){
console.log('New post created:', post);
}).catch(err => {
console.error('Error creating post:', err.message);
});