Get watermelonDB size with size limitation check for store data

September 19, 2023

Get watermelonDB size with size limitation check for store data

WatermelonDB is a client-side database designed for React Native applications. To get the size of the database and create a small blog-like document about it, you can follow these steps:

Title: Understanding WatermelonDB Database Size

Introduction: WatermelonDB is a popular database solution for React Native applications, known for its speed and efficiency. One important aspect to consider when using any database is its size, as it directly impacts the performance and storage requirements of your app. In this blog post, we will explore how to get the size of a WatermelonDB database and discuss some tips on managing it efficiently.

Getting Database Size: To determine the size of your WatermelonDB database, you can use the following steps:

  1. Install the ‘react-native-fs’ library: This library allows you to interact with the device’s file system and access the database file.

    npm install react-native-fs --save
  2. Import the library in your code:

    import RNFS from 'react-native-fs';
  3. Get the database file path:

    You can obtain the path to your WatermelonDB database file using the following code:

    export const dbFilePath = `${RNFS.DocumentDirectoryPath}/OfflineSync.sqlite`;

    Replace your-database-file.db with the actual name of your database file.

  4. Get the file size:

    Use the stat method from react-native-fs to get the file size in bytes:

    const GetDbSize = async () => {
     try {
       const fileInfo = await RNFS.stat(dbFilePath);
       const sizeInBytes = fileInfo.size;
       const sizeInMB = (sizeInBytes / (1024 * 1024)).toFixed(2);
       console.info({ dbFilePath }, { sizeInMB });
       const LocalDbSize = Constants?.LocalDbSize;
       if (LocalDbSize < sizeInMB) {
         return LocalDbSize;
       }
       return sizeInMB;
     } catch (error) {
       throw new Error(error);
     }
    }
  5. Database size limit check
    const CheckDbSizeAvailable = async () => {
     try {
       let sizeInMB = await GetDbSize();
       const LocalDbSize = Constants?.LocalDbSize;
       sizeInMB = Number(sizeInMB).toFixed();
       if (LocalDbSize <= sizeInMB) {
         throw Error("Local DB Size Limit is Exceed");
       }
       return true;
     } catch (error) {
       throw new Error(error);
     }
    };

Tips for Managing Database Size:

  1. Prune Unused Data: Regularly remove data that is no longer needed in your app to keep the database size in check.
  2. Optimize Queries: Write efficient database queries to minimize the amount of data retrieved and stored.
  3. Use Indexed Fields: Index frequently used fields to speed up queries and reduce the size of the database.
  4. Consider Data Archiving: If your app deals with large amounts of historical data, consider archiving older records to a separate database or file.
  5. Compact the Database: WatermelonDB provides a method to compact the database, which can help reduce its size.

Conclusion: Understanding and managing the size of your WatermelonDB database is crucial for maintaining the performance and storage efficiency of your React Native application. By following the steps outlined above and implementing best practices for data management, you can ensure that your app runs smoothly while keeping its database size in check.


Written by Manoj Bhardwaj who lives and works in Dharamshala Himachal Pradesh (India). My stackoverflow