### Install kasdb.js Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Install the kasdb.js library using npm. ```bash npm install @kasperenok/kasdb ``` -------------------------------- ### Get Full Data with DB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Retrieve all data from the database file using the synchronous DB instance. ```javascript const data = db.getData(); console.log(data.money.count); ``` -------------------------------- ### Get Data by Key with DB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Retrieve specific data by its key from the database file using the synchronous DB instance. ```javascript const data = db.getData("money"); console.log(data.count); ``` -------------------------------- ### Get Full Data with AsyncDB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Retrieve all data asynchronously from the database file using the AsyncDB instance. This method returns a Promise. ```javascript const data = await db.getData(); console.log(data.money.count); ``` -------------------------------- ### Get Data by Key with AsyncDB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Retrieve specific data by its key asynchronously using the AsyncDB instance. This method returns a Promise. ```javascript const data = await db.getData("money"); console.log(data.count); ``` -------------------------------- ### Initialize DB Instance Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Initialize a new DB instance with a specified filename and extension for data storage. ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/example", extension: ".db" }); ``` -------------------------------- ### new DB(args) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Constructs a synchronous DB instance bound to a file on disk. Initializes the file with provided data if specified. ```APIDOC ## new DB(args) — Create a synchronous database instance ### Description Constructs a synchronous `DB` instance bound to a file on disk. The `args.filename` and optional `args.extension` (defaults to `.db`) determine the file path. If `args.data` is provided, the file is immediately initialized with that data (overwriting any existing content). All subsequent `saveData` and `getData` calls are synchronous and block the event loop. ### Parameters #### Path Parameters - **filename** (string) - Required - The base filename for the database. - **extension** (string) - Optional - The file extension for the database file (defaults to ".db"). - **data** (object) - Optional - Initial data to populate the database with. ### Request Example ```javascript const { DB } = require("@kasperenok/kasdb"); // Basic instantiation — creates or opens "database/users.db" const db = new DB({ filename: "database/users", extension: ".db" }); // Instantiation with seed data — writes initial data to file immediately const db2 = new DB({ filename: "database/config", extension: ".db", data: { appName: "MyApp", version: "1.0.0", debugMode: false } }); ``` ``` -------------------------------- ### Instantiate Synchronous DB Instance Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Use `new DB()` to create a synchronous database instance. Specify the filename and optionally an extension. Seed data can be provided to initialize the file. ```javascript const { DB } = require("@kasperenok/kasdb"); // Basic instantiation — creates or opens "database/users.db" const db = new DB({ filename: "database/users", extension: ".db" }); // Instantiation with seed data — writes initial data to file immediately const db2 = new DB({ filename: "database/config", extension: ".db", data: { appName: "MyApp", version: "1.0.0", debugMode: false } }); ``` -------------------------------- ### Create AsyncDB Instance Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Construct an AsyncDB instance with the same signature as DB. File path resolution is identical. If args.data is provided, the initial write is synchronous. All save/getData calls return Promises. ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); // Basic async instantiation const db = new AsyncDB({ filename: "database/orders", extension: ".db" }); // With seed data const db2 = new AsyncDB({ filename: "database/inventory", data: { itemCount: 0, lastUpdated: null } }); ``` -------------------------------- ### Listen for Data Retrieval Event with DB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Set up a listener for the 'getData' event to log details about data retrieval. The event provides key, data, and filename. ```javascript db.on("getData", (eventData) => { console.log(`Data retrieved from key ${eventData.key} in file ${eventData.filename}`); }); ``` -------------------------------- ### Initialize AsyncDB Instance Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Initialize a new AsyncDB instance, which provides an asynchronous API for database operations. ```javascript const { AsyncDB } = require("kasperdb"); const db = new AsyncDB({ filename: "database/example", extension: ".db" }); ``` -------------------------------- ### new AsyncDB(args) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Constructs an AsyncDB instance. It accepts the same arguments as the synchronous DB constructor. Subsequent operations like saveData and getData return Promises, making them suitable for async/await usage. ```APIDOC ## `new AsyncDB(args)` — Create an async database instance Constructs an `AsyncDB` instance with the same constructor signature as `DB`. The file path resolution logic is identical. If `args.data` is provided, the initial write is performed synchronously (using `fs.writeFileSync`) even though subsequent operations are async. All `saveData` and `getData` calls return Promises and are safe to use in async/await contexts. ### Parameters - **args** (object) - Configuration object for the database instance. - **filename** (string) - The base name for the database file. - **extension** (string) - The file extension for the database file (e.g., ".db"). - **data** (object, optional) - Initial data to seed the database with. ### Example ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); // Basic async instantiation const db = new AsyncDB({ filename: "database/orders", extension: ".db" }); // With seed data const db2 = new AsyncDB({ filename: "database/inventory", data: { itemCount: 0, lastUpdated: null } }); ``` ``` -------------------------------- ### Event Handling with AsyncDB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Set up event listeners for 'getData' and 'saveData' events with the AsyncDB instance. The event object 'e' contains key and filename. ```javascript db.on("getData", (e) => console.log(`Got key ${e.key} from ${e.filename}`)); db.on("saveData", (e) => console.log(`Saved key ${e.key} to ${e.filename}`)); ``` -------------------------------- ### Listen for Data Save Event with DB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Set up a listener for the 'saveData' event to log details about data saving. The event provides key, value, and filename. ```javascript db.on("saveData", (eventData) => { console.log(`Data saved with key ${eventData.key} in file ${eventData.filename}`); }); ``` -------------------------------- ### Save Data with DB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Save data to a file using a specified key with the synchronous DB instance. ```javascript db.saveData("money", { username: "example username", count: 0 }); ``` -------------------------------- ### db.on(event, callback) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Registers a callback function for 'getData' or 'saveData' events. Only one listener per event type is supported, and subsequent calls will replace the previous listener. The callback receives an eventData object containing contextual metadata. ```APIDOC ## `db.on(event, callback)` — Register event listeners Registers a callback for either `"getData"` or `"saveData"` events. Only one listener per event type is supported; calling `on` again for the same event replaces the previous listener. The callback receives an `eventData` object with contextual metadata about the operation. ### Parameters - **event** (string) - The event to listen for. Supported values: `"getData"`, `"saveData"`. - **callback** (function) - The function to execute when the event is triggered. It receives an `eventData` object. ### `eventData` Object - **key** (string | null) - The key associated with the operation. - **value** (any) - The data being saved (for `saveData` event). - **data** (any) - The data retrieved (for `getData` event). - **filename** (string) - The database file name. ### Example ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/app", extension: ".db" }); db.on("saveData", (eventData) => { console.log(`[WRITE] key="${eventData.key}" → file="${eventData.filename}"`); console.log("Value written:", eventData.value); }); db.on("getData", (eventData) => { console.log(`[READ] key="${eventData.key}" from "${eventData.filename}"`); }); db.saveData("session", { token: "abc123", expires: 1700000000 }); db.getData("session"); db.getData(); ``` ``` -------------------------------- ### Save Data with AsyncDB Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Save data asynchronously using the AsyncDB instance. This method returns a Promise. ```javascript await db.saveData("money", { username: "example", count: 0 }); ``` -------------------------------- ### DB.on Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Listens for specific events emitted by the DB instance. Supports 'getData' and 'saveData' events. ```APIDOC ## DB.on ### Description Listens for specific events emitted by the DB instance. Supports 'getData' and 'saveData' events. ### Method ```javascript db.on(eventName, callback) ``` ### Parameters #### Path Parameters - **eventName** (string) - Required - The name of the event to listen for ('getData' or 'saveData'). - **callback** (function) - Required - The function to execute when the event is triggered. ### Event Attributes #### getData eventData - **key** (string) - The key associated with the data retrieval. - **data** (any) - The data that was retrieved. - **filename** (string) - The name of the file from which data was retrieved. #### saveData eventData - **key** (string) - The key under which data was saved. - **value** (any) - The data that was saved. - **filename** (string) - The name of the file to which data was saved. ### Request Example ```javascript db.on("getData", (eventData) => { console.log(`Data retrieved from key ${eventData.key} in file ${eventData.filename}`); }); db.on("saveData", (eventData) => { console.log(`Data saved with key ${eventData.key} in file ${eventData.filename}`); }); ``` ``` -------------------------------- ### db.getData([key]) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Retrieves data from the database synchronously. Can fetch a specific key's value or the entire database object. ```APIDOC ## db.getData([key]) — Retrieve data from file (synchronous) ### Description Reads and decodes the entire binary file from disk. If `key` is provided, returns only `data[key]`; otherwise returns the full decoded object. Triggers the `getData` event listener with metadata about the access. Returns `undefined` if the key does not exist. ### Method GET ### Endpoint `/getData` ### Parameters #### Query Parameters - **key** (string) - Optional - The key of the data to retrieve. If omitted, the entire database object is returned. ### Response #### Success Response (200) - **data** (any) - The retrieved data, or the full database object if no key was specified. ### Response Example ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/app", extension: ".db" }); db.saveData("player", { username: "alice", score: 99 }); db.saveData("settings", { theme: "dark", lang: "en" }); // Retrieve a specific key const player = db.getData("player"); console.log(player.username); // "alice" console.log(player.score); // 99 // Retrieve the full database object const all = db.getData(); console.log(all); // { player: { username: "alice", score: 99 }, settings: { theme: "dark", lang: "en" } } // Non-existent key returns undefined const missing = db.getData("nonexistent"); console.log(missing); // undefined ``` ``` -------------------------------- ### asyncDb.getData([key]) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt The asynchronous version of getData. It reads data from the database file using `fs.promises.readFile`, ensuring non-blocking I/O. It returns a Promise that resolves to either a specific value by key or the entire database content if no key is provided. ```APIDOC ## `asyncDb.getData([key])` — Retrieve data from file (async) The async equivalent of `DB.getData`. Returns a Promise resolving to either a single keyed value or the full decoded object. Non-blocking; uses `fs.promises.readFile` internally. ### Parameters - **key** (string, optional) - The key of the data to retrieve. If omitted, the entire database content is returned. ### Returns - Promise - A Promise that resolves with the requested data. ### Example ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); async function main() { const db = new AsyncDB({ filename: "database/orders", extension: ".db" }); await db.saveData("order_001", { product: "Widget", qty: 3, total: 29.97 }); await db.saveData("order_002", { product: "Gadget", qty: 1, total: 49.99 }); // Get a specific order by key const order = await db.getData("order_001"); console.log(order.product); // "Widget" console.log(order.total); // 29.97 // Get all data const allOrders = await db.getData(); console.log(Object.keys(allOrders)); // ["order_001", "order_002"] // Process all entries for (const [id, details] of Object.entries(allOrders)) { console.log(`${id}: ${details.product} — $${details.total}`); } } main(); ``` ``` -------------------------------- ### AsyncDB.getData Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Asynchronously retrieves data from the database. Can retrieve all data or data by a specific key. ```APIDOC ## AsyncDB.getData ### Description Asynchronously retrieves data from the database. Can retrieve all data or data by a specific key. ### Method ```javascript await db.getData(key?) ``` ### Parameters #### Path Parameters - **key** (string) - Optional - The key of the data to retrieve. If omitted, all data is returned. ### Request Example 1. Get full data: ```javascript const data = await db.getData(); console.log(data.money.count); ``` 2. Get data by key: ```javascript const data = await db.getData("money"); console.log(data.count); ``` ### Response #### Success Response (200) - **data** (object) - The retrieved data. Structure depends on whether a key was provided. #### Response Example 1. Full data: ```json { "money": { "username": "example", "count": 0 } } ``` 2. Data by key: ```json { "username": "example", "count": 0 } ``` ``` -------------------------------- ### AsyncDB.on Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Listens for specific events emitted by the AsyncDB instance. Supports 'getData' and 'saveData' events. ```APIDOC ## AsyncDB.on ### Description Listens for specific events emitted by the AsyncDB instance. Supports 'getData' and 'saveData' events. ### Method ```javascript db.on(eventName, callback) ``` ### Parameters #### Path Parameters - **eventName** (string) - Required - The name of the event to listen for ('getData' or 'saveData'). - **callback** (function) - Required - The function to execute when the event is triggered. ### Event Attributes #### getData eventData - **key** (string) - The key associated with the data retrieval. - **filename** (string) - The name of the file from which data was retrieved. #### saveData eventData - **key** (string) - The key under which data was saved. - **filename** (string) - The name of the file to which data was saved. ### Request Example ```javascript db.on("getData", (e) => console.log(`Got key ${e.key} from ${e.filename}`)); db.on("saveData", (e) => console.log(`Saved key ${e.key} to ${e.filename}`)); ``` ``` -------------------------------- ### DB.getData Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Retrieves data from the database. Can retrieve all data or data by a specific key. This is a synchronous operation. ```APIDOC ## DB.getData ### Description Retrieves data from the database. Can retrieve all data or data by a specific key. This is a synchronous operation. ### Method ```javascript db.getData(key?) ``` ### Parameters #### Path Parameters - **key** (string) - Optional - The key of the data to retrieve. If omitted, all data is returned. ### Request Example 1. Get full data: ```javascript const data = db.getData(); console.log(data.money.count); ``` 2. Get data by key: ```javascript const data = db.getData("money"); console.log(data.count); ``` ### Response #### Success Response (200) - **data** (object) - The retrieved data. Structure depends on whether a key was provided. #### Response Example 1. Full data: ```json { "money": { "username": "example username", "count": 0 } } ``` 2. Data by key: ```json { "username": "example username", "count": 0 } ``` ``` -------------------------------- ### AsyncDB.saveData Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Asynchronously saves data to a specified file with a given key. ```APIDOC ## AsyncDB.saveData ### Description Asynchronously saves data to a specified file with a given key. ### Method ```javascript await db.saveData(key, value) ``` ### Parameters #### Path Parameters - **key** (string) - Required - The key under which to save the data. - **value** (any) - Required - The data to be saved. ### Request Example ```javascript await db.saveData("money", { username: "example", count: 0 }); ``` ``` -------------------------------- ### Register Event Listeners with DB Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Register callbacks for 'getData' or 'saveData' events. Only one listener per event type is supported; subsequent calls replace previous ones. Callbacks receive eventData with operation metadata. ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/app", extension: ".db" }); // Listen for data saves db.on("saveData", (eventData) => { // eventData: { key, value, filename } console.log(`[WRITE] key="${eventData.key}" → file="${eventData.filename}"`); console.log("Value written:", eventData.value); }); // Listen for data reads db.on("getData", (eventData) => { // eventData: { key, data, filename } // key is "[null]" when getData() is called with no argument console.log(`[READ] key="${eventData.key}" from "${eventData.filename}"`); }); db.saveData("session", { token: "abc123", expires: 1700000000 }); // Console: [WRITE] key="session" → file="database/app.db" db.getData("session"); // Console: [READ] key="session" from "database/app.db" db.getData(); // Console: [READ] key="[null]" from "database/app.db" ``` -------------------------------- ### asyncDb.on(event, callback) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Registers event listeners for 'saveData' or 'getData' events on an AsyncDB instance. This method functions identically to DB.on, providing synchronous callbacks after each operation completes, and receiving the same eventData structure. ```APIDOC ## `asyncDb.on(event, callback)` — Register event listeners (AsyncDB) Identical in signature and behavior to `DB.on`. Registers a `"getData"` or `"saveData"` listener on the `AsyncDB` instance. The callbacks are synchronous functions called after each operation completes, and receive the same `eventData` shape as the synchronous `DB`. ### Parameters - **event** (string) - The event to listen for. Supported values: `"getData"`, `"saveData"`. - **callback** (function) - The function to execute when the event is triggered. It receives an `eventData` object. ### `eventData` Object - **key** (string | null) - The key associated with the operation. - **value** (any) - The data being saved (for `saveData` event). - **data** (any) - The data retrieved (for `getData` event). - **filename** (string) - The database file name. ### Example ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); async function main() { const db = new AsyncDB({ filename: "database/audit", extension: ".db" }); db.on("saveData", ({ key, value, filename }) => { console.log(`[AUDIT] Saved key="${key}" to "${filename}" at ${new Date().toISOString()}`); }); db.on("getData", ({ key, data, filename }) => { const label = key === "[null]" ? "full DB" : `key="${key}"`; console.log(`[AUDIT] Read ${label} from "${filename}"`); }); await db.saveData("event_001", { type: "login", user: "alice" }); await db.getData("event_001"); } main(); ``` ``` -------------------------------- ### Retrieve Data Synchronously with DB Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Use `db.getData([key])` to retrieve data. If a key is provided, it returns the value associated with that key. Without a key, it returns the entire decoded object. Returns `undefined` for non-existent keys. ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/app", extension: ".db" }); db.saveData("player", { username: "alice", score: 99 }); db.saveData("settings", { theme: "dark", lang: "en" }); // Retrieve a specific key const player = db.getData("player"); console.log(player.username); // "alice" console.log(player.score); // 99 // Retrieve the full database object const all = db.getData(); console.log(all); // { player: { username: "alice", score: 99 }, settings: { theme: "dark", lang: "en" } } // Non-existent key returns undefined const missing = db.getData("nonexistent"); console.log(missing); // undefined ``` -------------------------------- ### Save Data Synchronously with DB Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Use `db.saveData(key, value)` to persist data. This method reads the entire file, updates the specified key, and writes the full object back to disk. The value can be any JSON-serializable type. ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/app", extension: ".db" }); // Save a simple object under the key "user" db.saveData("user", { username: "alice", score: 42, active: true }); // Save an array under the key "leaderboard" db.saveData("leaderboard", [ { username: "alice", score: 42 }, { username: "bob", score: 37 } ]); // Update a nested key — overwrites the entire "user" entry const existing = db.getData("user"); existing.score += 10; db.saveData("user", existing); // "user" entry now has score: 52 ``` -------------------------------- ### Retrieve Data Asynchronously with AsyncDB Source: https://context7.com/kasper-studios/kasdb.js/llms.txt The async equivalent of DB.getData. Returns a Promise resolving to a single keyed value or the full decoded object. Non-blocking; uses fs.promises.readFile internally. ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); async function main() { const db = new AsyncDB({ filename: "database/orders", extension: ".db" }); await db.saveData("order_001", { product: "Widget", qty: 3, total: 29.97 }); await db.saveData("order_002", { product: "Gadget", qty: 1, total: 49.99 }); // Get a specific order by key const order = await db.getData("order_001"); console.log(order.product); // "Widget" console.log(order.total); // 29.97 // Get all data const allOrders = await db.getData(); console.log(Object.keys(allOrders)); // ["order_001", "order_002"] // Process all entries for (const [id, details] of Object.entries(allOrders)) { console.log(`${id}: ${details.product} — $${details.total}`); } // order_001: Widget — $29.97 // order_002: Gadget — $49.99 } main(); ``` -------------------------------- ### DB.saveData Source: https://github.com/kasper-studios/kasdb.js/blob/main/README.md Saves data to a specified file with a given key. This is a synchronous operation. ```APIDOC ## DB.saveData ### Description Saves data to a specified file with a given key. This is a synchronous operation. ### Method ```javascript db.saveData(key, value) ``` ### Parameters #### Path Parameters - **key** (string) - Required - The key under which to save the data. - **value** (any) - Required - The data to be saved. ### Request Example ```javascript db.saveData("money", { username: "example username", count: 0 }); ``` ``` -------------------------------- ### Register Event Listeners with AsyncDB Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Identical in signature and behavior to DB.on. Registers a 'getData' or 'saveData' listener on the AsyncDB instance. Callbacks are synchronous and called after each operation completes, receiving the same eventData shape. ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); async function main() { const db = new AsyncDB({ filename: "database/audit", extension: ".db" }); db.on("saveData", ({ key, value, filename }) => { console.log(`[AUDIT] Saved key="${key}" to "${filename}" at ${new Date().toISOString()}`); }); db.on("getData", ({ key, data, filename }) => { const label = key === "[null]" ? "full DB" : `key="${key}"`; console.log(`[AUDIT] Read ${label} from "${filename}"`); }); await db.saveData("event_001", { type: "login", user: "alice" }); // [AUDIT] Saved key="event_001" to "database/audit.db" at 2024-01-15T10:30:00.000Z await db.getData("event_001"); // [AUDIT] Read key="event_001" from "database/audit.db" } main(); ``` -------------------------------- ### Persist Data Asynchronously with AsyncDB Source: https://context7.com/kasper-studios/kasdb.js/llms.txt The async equivalent of DB.saveData. Reads the current file, merges the new key-value pair, and writes back using fs.promises. Returns a Promise resolving upon completion and triggers the 'saveData' event. ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); async function main() { const db = new AsyncDB({ filename: "database/orders", extension: ".db" }); await db.saveData("order_001", { product: "Widget", qty: 3, total: 29.97, status: "pending" }); await db.saveData("order_002", { product: "Gadget", qty: 1, total: 49.99, status: "shipped" }); console.log("Both orders saved."); } main(); ``` -------------------------------- ### asyncDb.saveData(key, value) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt The asynchronous version of saveData. It reads the current file, merges the new key-value pair, and writes the data back using Node.js's `fs.promises` to avoid blocking the event loop. Returns a Promise that resolves upon completion and triggers the 'saveData' event. ```APIDOC ## `asyncDb.saveData(key, value)` — Persist a value under a key (async) The async equivalent of `DB.saveData`. Reads the current file, merges the new key-value pair, and writes back — all using `fs.promises` so the event loop is not blocked. Returns a Promise that resolves when the write is complete. Triggers the `saveData` event listener after a successful write. ### Parameters - **key** (string) - The key under which to store the value. - **value** (any) - The data to persist. ### Returns - Promise - A Promise that resolves when the data has been successfully saved. ### Example ```javascript const { AsyncDB } = require("@kasperenok/kasdb"); async function main() { const db = new AsyncDB({ filename: "database/orders", extension: ".db" }); await db.saveData("order_001", { product: "Widget", qty: 3, total: 29.97, status: "pending" }); await db.saveData("order_002", { product: "Gadget", qty: 1, total: 49.99, status: "shipped" }); console.log("Both orders saved."); } main(); ``` ``` -------------------------------- ### db.saveData(key, value) Source: https://context7.com/kasper-studios/kasdb.js/llms.txt Persists a value under a specified key in the database synchronously. Overwrites existing data for the key. ```APIDOC ## db.saveData(key, value) — Persist a value under a key (synchronous) ### Description Reads the entire current file into memory, sets `data[key] = value`, re-encodes the full object with MessagePack, and writes it back to disk synchronously. The `value` can be any JSON-serializable JavaScript object, array, string, or number. Triggers the `saveData` event listener if one is registered. ### Method POST ### Endpoint `/saveData` ### Parameters #### Path Parameters - **key** (string) - Required - The key under which to store the value. - **value** (any) - Required - The data to store. Must be JSON-serializable. ### Request Example ```javascript const { DB } = require("@kasperenok/kasdb"); const db = new DB({ filename: "database/app", extension: ".db" }); // Save a simple object under the key "user" db.saveData("user", { username: "alice", score: 42, active: true }); // Save an array under the key "leaderboard" db.saveData("leaderboard", [ { username: "alice", score: 42 }, { username: "bob", score: 37 } ]); // Update a nested key — overwrites the entire "user" entry const existing = db.getData("user"); existing.score += 10; db.saveData("user", existing); // "user" entry now has score: 52 ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.