Advanced Usage Scenarios

This guide provides an extensive overview of the many ways you can use Jsonverse in your projects, along with step-by-step explanations.

1. Installation

To start using Jsonverse, you need to install it in your Node.js project. Follow these steps:

  1. Create a new Node.js project if you haven’t already.

  2. Open your project’s terminal.

  3. Run the following command to install Jsonverse:

npm install jsonverse
  1. Import Jsonverse in your code:
const { jsonverse } = require("jsonverse");

Now you have Jsonverse set up in your project.

2. Initializing Jsonverse

Before working with data, you need to initialize Jsonverse. Here’s how:

  1. Create a new Jsonverse instance:
const db = new jsonverse();
  1. Optionally, configure Jsonverse with custom settings like database connection details or caching: this is still Beta only beta users can try it
db.configure({
  database: "https://localhost/mydatabase", // or you can use a local folder and i suggest that for now we didn't test data oprations with 
  cache: true,
});

Your Jsonverse instance is now ready to use.

3. Data Models

Jsonverse allows you to define data models using schemas. Let’s create a simple “User” schema:

const { Schema } = require("jsonverse");

const userSchema = new Schema({
  name: String,
  email: { type: String, unique: true },
  age: Number,
});
  1. Define your schema as shown above.

  2. Create a model based on the schema:

const userModel = db.model("User", userSchema);

Now you have a “User” model ready to interact with the database.

4. CRUD Operations

Performing CRUD (Create, Read, Update, Delete) operations with Jsonverse is straightforward.

4.1. Creating Data

To create a new user:

const newUser = {
  name: "John Doe",
  email: "john@example.com",
  age: 30,
}; // example data

userModel.save(newUser).then((result) => {
  // handel response logic
}).catch((error) => {
  // handel error logic here
})

4.2. Reading Data

To find a user by email:

userModel.find(id).then((result) => {
  // handel response here
}).catch((error) => {
  // handel error logic here
});

4.3. Updating Data

To update a user’s age:

const updatedData = {
  name: "Johnathan Doe",
  email: "johnathan@example.com",
  age: 35,
}
userModel.update(id, updatedData).then((result) => {
  // response logic here
}).catch((error) => {
  // Error handel logic here
});

4.4. Deleting Data

To delete a user:

userModel.delete(id).then((result) => {
  // response logic here
}).catch((error) => {
  // error handel logic here
});

5. Utilities

Jsonverse offers various utility functions, including random ID generation and file operations.

5.1. Random ID Generation

Generating random IDs:

const randomId = db.generateRandomId();
console.log("Random ID:", randomId);

6. Error Handling

Proper error handling is crucial in any application. Jsonverse provides error handling mechanisms to handle potential issues:

try {
  // Jsonverse code that might throw an error
} catch (error) {
  console.error("Jsonverse error:", error);
}

With this comprehensive guide, you have a solid foundation for using Jsonverse in a wide range of scenarios. Explore further, experiment, and build robust applications with confidence.