https://github.com/lullaby6/jnbase
NPM Package - Manipulates json files easily for use as lightweight databases
https://github.com/lullaby6/jnbase
data-base database db javascript js js-package json json-database json-db node nosql npm npm-package
Last synced: 3 months ago
JSON representation
NPM Package - Manipulates json files easily for use as lightweight databases
- Host: GitHub
- URL: https://github.com/lullaby6/jnbase
- Owner: lullaby6
- Created: 2023-09-25T00:32:59.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-25T20:48:33.000Z (over 2 years ago)
- Last Synced: 2025-03-15T18:06:04.738Z (about 1 year ago)
- Topics: data-base, database, db, javascript, js, js-package, json, json-database, json-db, node, nosql, npm, npm-package
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/jnbase
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JNBase
JNBase is a simple and lightweight and asynchronous file-based JSON database. It features in-memory caching, atomic writes to prevent data corruption, and Mutex locking to ensure concurrency safety.
## Installation
### NPM
```bash
npm i jnbase
```
### Import
#### CommonJS
```js
const { createData, getDataById } = require("jnbase");
```
#### ModuleJS
```js
import { createData, getDataById } from "jnbase";
```
## Usage
```js
const {
createKey,
createData,
getDataByObject,
updateDataById,
deleteDataById,
} = require("jnbase");
(async () => {
await createKey("users");
await createData("users", {
name: "Alice",
age: 25,
role: "admin",
});
const users = await getDataByObject("users", {
name: "Alice",
});
const user = users[0];
console.log(user);
await updateDataById("users", user.id, {
age: 26,
});
await deleteDataById("users", user.id);
})();
```
## API
| Method | Description | Parameters | Returns |
| -------------------- | ----------------------------------------------------------------------------------- | -------------------------------------------------------- | ---------------------------- |
| `createKey` | Creates a new empty array for a key if it does not exist. | `key` (string) | `Promise` |
| `getKeyData` | Retrieves all data associated with a key. | `key` (string) | `Promise` |
| `setKeyData` | Overwrites all data for a specific key. | `key` (string), `value` (array) | `Promise` |
| `renameKey` | Renames an existing key. | `key` (string), `newKey` (string) | `Promise` |
| `deleteKey` | Deletes a key and all its data. | `key` (string) | `Promise` |
| `clearKeyData` | Empties the array for a specific key. | `key` (string) | `Promise` |
| `createData` | Adds a new object to a key array. Generates UUID if missing. Checks for duplicates. | `key` (string), `data` (object) | `Promise` |
| `createMultipleData` | Adds multiple objects to a key array efficiently. | `key` (string), `dataList` (array) | `Promise` |
| `getDataById` | Retrieves a specific item by its ID. | `key` (string), `id` (string) | `Promise` |
| `getDataByObject` | Retrieves items matching a specific set of key-value pairs. | `key` (string), `condition` (object) | `Promise` |
| `updateDataById` | Updates fields of an item identified by ID. | `key` (string), `id` (string), `newData` (object) | `Promise` |
| `updateDataByObject` | Updates items matching a condition. | `key` (string), `condition` (object), `newData` (object) | `Promise` |
| `deleteDataById` | Removes an item by its ID. | `key` (string), `id` (string) | `Promise` |
| `deleteDataByObject` | Removes items matching a condition. | `key` (string), `condition` (object) | `Promise` |
| `hasDataById` | Checks if an item with the given ID exists. | `key` (string), `id` (string) | `Promise` |
| `hasDataByObject` | Checks if any item matches the condition. | `key` (string), `condition` (object) | `Promise` |
| `getJsonData` | Returns the entire database object from memory. | None | `Promise` |
| `setJsonData` | Replaces the entire database. Use with caution. | `jsonData` (object) | `Promise` |
## License
- [MIT](https://github.com/lullaby6/jnbase/blob/main/LICENSE)