Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

2‐ saveData Command

Marco edited this page Sep 8, 2023 · 2 revisions

saveData Command

The saveData command is used to add new data to a database managed by the jsonverse package. It is a method of the jsonverse class and is responsible for saving data to a specified data store.

Syntax

jsonverse.saveData(dataName, newData)
  • dataName (string): The name of the data store to which you want to add the new data.
  • newData (object): The data you want to add to the data store.

Example Usage (Express with Promises)

const Jsonverse = require('jsonverse'); // Import the jsonverse package
// Initialize the JSONDatabase instance
const db = new Jsonverse({
  dataFolderPath: "./MyData", // data directory
  logFolderPath: "./MyLogs", // logs directory
  activateLogs: true, // to enable the logs set this value to true
});

// Define a sample data object to add
const newData = {
  name: 'John Doe',
  age: 30,
  email: '[email protected]'
};

// Add the data to the 'users' data store
jsonverse.saveData('users', newData)
  .then(() => {
    console.log('Data added successfully.');
  })
  .catch((error) => {
    console.error('Error adding data:', error);
  });

In this example, we create an instance of the jsonverse class and use the saveData method to add new user data to the 'users' data store. We handle the result using promises for asynchronous operation.

Example Usage (Express without Promises)

const Jsonverse = require('jsonverse'); // Import the jsonverse package
const jsonverse = new Jsonverse('./data'); // Initialize jsonverse with data folder path

// Define a sample data object to add
const newData = {
  name: 'Jane Smith',
  age: 25,
  email: '[email protected]'
};

// Add the data to the 'users' data store
jsonverse.saveData('users', newData, (error) => {
  if (error) {
    console.error('Error adding data:', error);
  } else {
    console.log('Data added successfully.');
  }
});

In this example, we create an instance of the jsonverse class and use the saveData method to add new user data to the 'users' data store. We handle the result using a callback function for asynchronous operation.

This documentation provides an overview of the saveData command in the jsonverse package and demonstrates how to use it in Express applications, both with and without promises.