# @bshal/snapdb

> SnapDB is a fast, zero-dependency JSON database for Node.js with MongoDB-style queries, automatic indexing, atomic writes and batched saves.

Latest version **2.0.0** (published 2026-09-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bshal/snapdb
pnpm add @bshal/snapdb
yarn add @bshal/snapdb
bun add @bshal/snapdb
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-09-23 |
| First published | 2024-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 48.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Bishal Chettri |
| Maintainers | bshal |
| Keywords | database, json, nodejs, indexing, async, embedded, mongodb-query, zero-dependency |

## Links

- npm: https://www.npmjs.com/package/@bshal/snapdb
- Repository: https://github.com/bshal/snapdb
- Homepage: https://github.com/bshal/snapdb#readme
- Issues: https://github.com/bshal/snapdb/issues
- npm.io page: https://npm.io/package/@bshal/snapdb

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2026-09-23
- 1.0.0 — 2024-06-18

## README

# SnapDB

> SnapDB is a high-performance, easy-to-use JSON database for Node.js. Featuring asynchronous operations and advanced indexing, it ensures fast and efficient data management.

## Installation

To install the package, use npm:

```bash
npm install @bshal/snapdb
```

## Usage Examples
### Loading and Inserting Data (using FileAdapter)
```javascript
const { Database, FileAdapter } = require('@bshal/snapdb');

(async () => {
  const adapter = new FileAdapter('db.json');
  const db = new Database(adapter);
  
  // Load the database
  await db.load();
  
  // Insert items into the "users" collection
  await db.insert('users', { id: 1, name: 'Alice' });
  await db.insert('users', { id: 2, name: 'Bob' });

  // Find all users
  const allUsers = db.find('users', {});
  console.log(allUsers); // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
})();
```

### Using MemoryAdapter
```javascript
const { Database, MemoryAdapter } = require('@bshal/snapdb');

(async () => {
  const adapter = new MemoryAdapter();
  const db = new Database(adapter);

  // Load the database
  await db.load();

  // Insert items into the "users" collection
  await db.insert('users', { id: 1, name: 'Charlie' });
  await db.insert('users', { id: 2, name: 'David' });

  // Find all users
  const allUsers = db.find('users', {});
  console.log(allUsers); // [{ id: 1, name: 'Charlie' }, { id: 2, name: 'David' }]
})();
```

### Querying Data
```javascript
const { Database, FileAdapter } = require('@bshal/snapdb');

(async () => {
  const adapter = new FileAdapter('db.json');
  const db = new Database(adapter);

  await db.load();
  await db.insert('users', { id: 1, name: 'Alice', age: 28, city: 'New York' });
  await db.insert('users', { id: 2, name: 'Bob', age: 30, city: 'Los Angeles' });
  await db.insert('users', { id: 3, name: 'Charlie', age: 28, city: 'New York' });

  // Find users by age and city
  const usersByAgeAndCity = db.find('users', { age: 28, city: 'New York' });
  console.log(usersByAgeAndCity); // [{ id: 1, name: 'Alice', age: 28, city: 'New York' }, { id: 3, name: 'Charlie', age: 28, city: 'New York' }]
})();

```

### Updating Data
```javascript
const { Database, FileAdapter } = require('@bshal/snapdb');

(async () => {
  const adapter = new FileAdapter('db.json');
  const db = new Database(adapter);

  await db.load();
  await db.insert('users', { id: 1, name: 'Grace' });

  // Find and update user
  const user = db.find('users', { id: 1 })[0];
  user.name = 'Grace Hopper';
  await db.save();

  const updatedUser = db.find('users', { id: 1 });
  console.log(updatedUser); // [{ id: 1, name: 'Grace Hopper' }]
})();
```

#### Removing Data
```javascript
const { Database, FileAdapter } = require('@bshal/snapdb');

(async () => {
  const adapter = new FileAdapter('db.json');
  const db = new Database(adapter);

  await db.load();
  await db.insert('users', { id: 1, name: 'Hank' });
  await db.insert('users', { id: 2, name: 'Ivy' });

  // Remove a user
  await db.remove('users', { name: 'Hank' });
  const remainingUsers = db.find('users', {});
  console.log(remainingUsers); // [{ id: 2, name: 'Ivy' }]
})();
```

#### Complete CRUD Operations
```javascript
const { Database, FileAdapter } = require('@bshal/snapdb');

(async () => {
  const adapter = new FileAdapter('db.json');
  const db = new Database(adapter);

  // Load the database
  await db.load();

  // Create
  await db.insert('users', { id: 1, name: 'Alice' });
  await db.insert('users', { id: 2, name: 'Bob' });

  // Read
  const users = db.find('users', {});
  console.log('All users:', users); // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]

  // Update
  const userToUpdate = db.find('users', { id: 1 })[0];
  userToUpdate.name = 'Alice Updated';
  await db.save();
  const updatedUsers = db.find('users', {});
  console.log('Updated users:', updatedUsers); // [{ id: 1, name: 'Alice Updated' }, { id: 2, name: 'Bob' }]

  // Delete
  await db.remove('users', { id: 2 });
  const finalUsers = db.find('users', {});
  console.log('Final users:', finalUsers); // [{ id: 1, name: 'Alice Updated' }]
})();
```

## API Reference

### Database

- **`load()`**: Loads the database data.
- **`save()`**: Saves the current state of the database.
- **`insert(collection, item)`**: Adds a new item to a collection.
- **`find(collection, query)`**: Finds items in a collection that match the query.
- **`remove(collection, query)`**: Removes items from a collection that match the query.

### Adapters

#### FileAdapter

- **`load()`**: Reads data from a file.
- **`save(data)`**: Writes data to a file.

#### MemoryAdapter

- **`load()`**: Loads data from memory.
- **`save(data)`**: Saves data to memory.


## Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.

---
_Source: https://npm.io/package/@bshal/snapdb · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
