Deploy a Database with WeaveDB: A Step-by-Step Guide

Deploy a Database with WeaveDB: A Step-by-Step Guide

Table of contents

Introduction

WeaveDB offers a user-friendly web console that simplifies the process of deploying a database, creating a collection, and integrating it into your code. In this article, we will walk you through the essential steps of deploying a database, creating a collection, and getting started with WeaveDB.

Step 1: Deploying a Database

To begin, follow these simple steps to deploy a database using the WeaveDB web console:

  1. Access the Web Console: Start by logging in to the WeaveDB web console. If you haven't registered yet, you can sign up for an account to get started.

  2. Create a Database: In the web console, navigate to the "Create Database" section. Here, you can define the parameters of your database, such as its name and access controls.

  3. Customize Your Smart Contract: WeaveDB allows you to customize your database by creating a smart contract. You can define the contract's behavior, rules, and logic to suit your project's needs.

  4. Deploy the Database: After configuring your database and smart contract, click the "Deploy" button. WeaveDB will initiate the deployment process, and you will receive a contract transaction ID (CONTRACT_TX_ID) upon successful deployment.

Step 2: Creating a Collection

Once your database is deployed, you can create collections to organize your data. Follow these steps to create a collection using the WeaveDB web console:

  1. Access the Web Console: Log in to the WeaveDB web console, and select the database you just deployed.

  2. Create a Collection: Navigate to the "Collections" section within your database. Click the "Create Collection" button and define the name and schema of your collection. The schema determines the structure of your data within the collection.

  3. Customize Your Collection: You can further customize your collection by specifying access controls, indexing rules, and permissions based on your project's requirements.

  4. Save Your Collection: After configuring your collection, click the "Save" button. Your collection is now ready to store data.

Step 3: Initializing WeaveDB in Your Project

With your database and collection in place, it's time to integrate WeaveDB into your project. Follow these steps to install and initialize WeaveDB in your code:

  1. Install WeaveDB SDK:

    • Using Yarn: Run the following command to install the WeaveDB SDK:

        yarn add weavedb-sdk
      
    • Using NPM: Alternatively, you can use NPM to install the SDK.

        npm install weavedb-sdk
      
  2. Initialize WeaveDB in Your Code:

    • Import the WeaveDB module in your code:

        import WeaveDB from "weavedb-sdk"
      
    • Create a WeaveDB instance with your contract transaction ID (CONTRACT_TX_ID) obtained during deployment:

        const db = new WeaveDB({ contractTxId: CONTRACT_TX_ID })
        await db.init()
      
    • Ensure you replace CONTRACT_TX_ID with the actual transaction ID you received during the deployment of your WeaveDB database.

Step 4: Adding Documents to Your Collection

Now that WeaveDB is integrated into your project, you can start adding documents to your collection. Here's how you can do it:

const personData = { name: "Bob", age: 20 }
const tx = await db.add(personData, collection_name)

Replace collection_name with the name of the collection where you want to store the document. This will add a document with the specified data to your collection.

Step 5: Getting Data from Your Collection

You can easily retrieve data from your collection using WeaveDB. There are two primary methods:

  • Use the get method to read the entire collection:

      const result = await db.get(collection_name)
    
  • Use the cget method to obtain metadata about the documents in your collection:

      const result = await db.cget(collection_name)
    

These methods allow you to retrieve data and metadata from your collection efficiently.

Conclusion

WeaveDB simplifies the process of deploying a decentralized NoSQL database for your web3 project. With a user-friendly web console and straightforward integration into your code, you can use WeaveDB to create, manage, and access your data effortlessly. Start building your database here WeaveDB.

Did you find this article valuable?

Support Gordian Etim by becoming a sponsor. Any amount is appreciated!