antoniopresto / react-native-local-mongodb

react-native local mongodb storage.
MIT License
304 stars 66 forks source link

Data is not persisted - Android #48

Closed getavinashm closed 5 years ago

getavinashm commented 5 years ago

Hi, I am having issues with the data not persisting after the app crashes or is force closed for some reason. I have seen issue #24 but that is related to chrome debugging and my problem is not with chrome debugging. I have working code that adds data to the db and fetches it perfectly fine. However, when the app crashes or I force close it and relaunch the app, the data is all gone. Here is the code snippet for how the db is created...... I have a wrapper function called 'createCollection' and the 'config' constant has the configuration as per documentation.

import DataStore from 'react-native-local-mongodb';
import CryptoJS from 'crypto-js'

const createCollection = ({ name, enableEncryption, encryptionKey }) => {
    const config = {
        filename: name,
        autoload: true
    }
    if (enableEncryption && encryptionKey) {
        config.afterSerialization = function (doc) {
            if (doc) {
                var encryptedData = CryptoJS.AES.encrypt(JSON.stringify(doc), encryptionKey);
                return encryptedData;
            }
        };
        config.beforeDeserialization = function (doc) {
            if (doc) {
                const decipher = CryptoJS.AES.decrypt(doc.toString(), encryptionKey);
                const decryptedData = JSON.parse(decipher.toString(CryptoJS.enc.Utf8));
                return decryptedData;
            }
        }
    }
    const db = new DataStore({ filename: name, ...config });
    return db;
};
antoniopresto commented 5 years ago

Too busy right now.; just guessing. Maybe is related with append-only format. https://github.com/antoniopresto/react-native-local-mongodb#persistence

You can manually call the compaction function with yourDatabase.persistence.compactDatafilewhich takes no argument. It queues a compaction of the datafile in the executor, to be executed sequentially after all pending operations. The datastore will fire a compaction.done event once compaction is finished.

antoniopresto commented 5 years ago

Feel free to reopen if you believe it is a bug in this lib.