startupDB

raw JSON →
3.0.19 verified Sat Apr 25 auth: no javascript

A file-based database for Express.js applications. Its current stable version is 3.0.19. It stores data in JSON files on the filesystem, making it suitable for small projects or prototyping where a full database is unnecessary. Key differentiators include zero configuration, automatic file management, and tight integration with Express routing. It is released as needed on npm with no fixed cadence. Compared to alternatives like lowdb, startupDB offers simpler file handling and is specifically designed to work as middleware for Express.

error Cannot find module 'startupdb'
cause Package not installed in node_modules.
fix
Run npm install startupdb.
error TypeError: startupDB is not a function
cause Using ES module import syntax with CommonJS package.
fix
Use const startupDB = require('startupdb') instead of import.
error TypeError: Cannot read properties of undefined (reading 'get')
cause StartupDB middleware not initialized or called incorrectly.
fix
Ensure app.use(startupDB()) is called with parentheses.
error Error: ENOENT: no such file or directory, open './data/startupdb.json'
cause The default data directory does not exist.
fix
Create the directory or configure a custom dataPath in startupDB options.
gotcha The database file operation is synchronous and blocking. Avoid using startupDB in production with high concurrency.
fix Use a proper database like MongoDB or PostgreSQL for scalable applications.
gotcha Data is not automatically persisted after each write; you may need to call `req.db.save()` after modifications.
fix Upgrade to version 3.0.0 or later, or call `req.db.save()` after pushes.
breaking In v2, `req.startupDB` was used instead of `req.db`. This is a breaking change.
fix Replace `req.startupDB` with `req.db` as of v3.
deprecated The `startupDB` function without arguments uses a default data directory './data'. This may be deprecated in future versions.
fix Explicitly pass a configuration object with a `dataPath` property.
npm install startupdb
yarn add startupdb
pnpm add startupdb

Sets up Express with startupDB middleware, demonstrating GET and POST routes to interact with a file-based 'users' collection.

const express = require('express');
const startupDB = require('startupdb');

const app = express();
const PORT = process.env.PORT || 3000;

app.use(startupDB());

app.get('/users', (req, res) => {
  const users = req.db.get('users') || [];
  res.json(users);
});

app.post('/users', (req, res) => {
  const user = req.body;
  req.db.push('users', user);
  res.status(201).json(user);
});

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});