# @burakbey/jsondb

> Simple library for storing datas on JSON files using models.

Latest version **1.1.4** (published 2024-07-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @burakbey/jsondb
pnpm add @burakbey/jsondb
yarn add @burakbey/jsondb
bun add @burakbey/jsondb
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2024-07-14 |
| First published | 2023-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | burakbey |
| Keywords | json, db, jsondb, json-db |

## Links

- npm: https://www.npmjs.com/package/@burakbey/jsondb
- Repository: https://github.com/BUR4KBEY/jsondb
- Homepage: https://github.com/BUR4KBEY/jsondb#readme
- Issues: https://github.com/BUR4KBEY/jsondb/issues
- Funding: https://burakbey.dev
- npm.io page: https://npm.io/package/@burakbey/jsondb

## Dependencies (3)

- [fs](https://npm.io/package/fs.md) ^0.0.1-security
- [path](https://npm.io/package/path.md) ^0.12.7
- [uuid](https://npm.io/package/uuid.md) ^9.0.0

## 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

- 1.1.4 (latest) — 2024-07-14
- 1.1.3 — 2023-03-22
- 1.1.2 — 2023-03-21
- 1.1.1 — 2023-03-21
- 1.1.0 — 2023-02-02
- 1.0.3 — 2023-02-02
- 1.0.2 — 2023-02-02
- 1.0.1 — 2023-02-02
- 1.0.0 — 2023-02-02

## README

# JsonDB

Simple library for storing datas on JSON files using models.

## Installation

Using `npm`:

```
npm install @burakbey/jsondb
```

Using `yarn`:

```
yarn add @burakbey/jsondb
```

## Initialization

Create `data` folder on root path.

```ts
// src/index.ts

import { join } from 'path';
import { JsonDB, Store } from '@burakbey/jsondb';

const store = new Store({
    mainPath: join(__dirname, '../data')
});
new JsonDB(store);
```

## Creating a new model

Create `users.json` in `data` folder with `[]` content.

```json
// data/users.json

[]
```

```ts
// src/models/User.ts

/* eslint-disable no-use-before-define */
import { Collection, Item } from '@burakbey/jsondb';

export class User extends Item<User> {
    username: string;

    password: string;
}

export const UserCollection = new Collection<User>(User, 'users');
```

## Using the methods

```ts
// src/index.ts

import { User, UserCollection } from './models/User';

// Create new users
const admin = new User({
    username: 'admin',
    password: 'password'
});

const user = new User({
    username: 'user',
    password: 'password'
});

UserCollection.save(admin, user);

// Get all users
const users = UserCollection.find();

// Get all users with filter
const users = UserCollection.find(x => x.password === 'password');

// Get one user
const user = UserCollection.findOne(x => x.username === 'user');

// Update user
const user = UserCollection.findOne(x => x.username === 'user');
if (user) {
    user.username = 'user-updated';
    UserCollection.update(user);
}

// Delete user
const user = UserCollection.findOne(x => x.username === 'user-updated');
if (user) {
    UserCollection.delete(user);
}
```

## Usage for FiveM

Create `data` folder on root path.

```ts
// server/index.ts

import { JsonDB, Store } from '@burakbey/jsondb';

const store = new Store({
    mainPath: 'data',
    fetch(collection) {
        const dataStr = LoadResourceFile(
            GetCurrentResourceName(),
            `${this.mainPath}/${collection.name}.json`
        );
        const data = JSON.parse(dataStr);
        return data;
    },
    save(collection) {
        SaveResourceFile(
            GetCurrentResourceName(),
            `${this.mainPath}/${collection.name}.json`,
            JSON.stringify(collection.items, null, 4),
            -1
        );
    }
});
new JsonDB(store);

...
```

## Change unique id generator

JsonDB uses `uuid` package for generating unique ids. If you want to change this to something else, you can use this example:

```ts
// src/index.ts

import { randomBytes } from 'crypto';
import { JsonDB, Store } from '@burakbey/jsondb';

const store = new Store({
    mainPath: 'somePathHere',
    getId() {
        const ms = new Date().getTime().toString();
        const id = crypto.randomBytes(8).toString('hex');

        return `${ms}-${id}`;
    }
});
new JsonDB(store);

...
```

## ☕ Support

If you find this project useful and would like to support [me](https://github.com/BUR4KBEY), you can do so by visiting [my website](https://burakbey.dev).

<a href="https://burakbey.dev" target="_blank"><img src="https://burakbey.dev/github_support_snippet.png" style="height: 56px !important;width: 200px !important;" alt="Buy me a coffee"></img></a>

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