Home
Cakebase provides a simplistic way to handle data storage using JSON files within your Node.js applications. It allows you to perform essential CRUD operations: Create, Read, Update, and Delete, with the bonus of ensuring type safety and straightforward data retrieval.
To utilize Cakebase, ensure you've got Node.js (version 14 or above) installed in your project. Install Cakebase into your project with:
npm install cakebase --save
Here’s a detailed breakdown of the core functionalities provided by Cakebase:
Integrate Cakebase into your project by requiring it and specifying your JSON file path:
const users = require('cakebase')('./users.json')
//Alternatively you can manage multiple JSON files effortlessly:
const collection = require('cakebase');
const users = collection('./users.json');
const planets = collection('./planets.json');
Attribute | Description |
---|---|
Description | Adds new objects to the JSON data. Does not overwrite existing data. |
Returns | A promise that resolves once the operation is complete. |
await users.set(obj => obj.location === "Mars")
Attribute | Description |
---|---|
Description | Retrieves objects that match the specified condition. |
Returns | A promise that resolves to an array of objects that meet the condition. |
const onMars = await users.get(user => user.location === "Mars")
console.log(onMars) // [{ id: '...' location: 'Mars', ... }, ... ]
Attribute | Description |
---|---|
Description | Updates objects that match the given condition with specified data. |
Returns | A promise that resolves once the update is complete. |
await users.update(user => user.location === "earth", { takeoff: '1988146800' })
Attribute | Description |
---|---|
Description | Removes objects that match the given predicate. |
Returns | A promise that resolves to an array of objects that meet the condition. |
await users.remove(user => user.age < 18)
Attribute | Description |
---|---|
Description | Clears all objects from the database. |
Returns | A promise that resolves once the data is cleared. |
await users.clear()
Cakebase provides a clean and minimalistic approach to handling JSON data, ensuring developers can quickly set up, retrieve, and manage their data without the complexities of a full-fledged database solution.
Note: Ensure to include error handling in production implementations to manage I/O and asynchronous operation issues effectively.