Cap-go/capacitor-data-storage-sqlite

Based on your definitions get should return false if no item found

Closed this issue · 9 comments

 @objc func get(_ call: CAPPluginCall) {
        guard let key = call.options["key"] as? String else {
            call.reject("Must provide a key")
            return
        }
        let data: Data = mDb.get(name:key)!
        if data.id != nil {
            call.resolve([
                "value": data.value!
            ])

        } else {
            call.resolve([
                "value": data.id!
            ])
        }
    }

This seems wrong. Your interface says it will return false but if the item is not found the app just crashes because it is trying to unwrap an optional value that is in fact nil.

I think it should be

@objc func get(_ call: CAPPluginCall) {
     guard let key = call.options["key"] as? String else {
         call.reject("Must provide a key")
         return
     }
     let data: Data = mDb.get(name:key)!
     if data.id != nil {
         call.resolve([
             "value": data.value!
         ])

     } else {
         call.resolve(["result":false])
     }
 }

i do not understand why you need to clean the values in the set function ? anyhow if it is required it must be the same for the android and the web plugin (please give me an example so i can understand). In the get function i think the code should be ```let data: Data = mDb.get(name:key)!
if data.id != nil {
call.resolve([
"value": data.value!
])

    } else {
        call.resolve([
            "value": nil
        ])
    } ``` 

The reason for cleaning the string is that when writing to sqllite a plain json string adds extra slashes which basically makes the object unreadable by redux-persist when trying to rehydrate state. May not be relevant to your use cases.

i am looking to update the plugin to capacitor 1, so i am not familiar with redux-persist can you share some code so i can understand your request as if i implemented it for IOS, i must also implemented it in Android and Web for consistency

import { Plugins } from '@capacitor/core';
import * as CapacitorSQLPlugin from 'capacitor-data-storage-sqlite';

const { CapacitorDataStorageSqlite, Device } = Plugins;

export const CapacitorEngineStorage  = {

  async setItem(key, value, callback) {
    return new Promise(function (resolve, reject) {
      
      const info = Device.getInfo();
      info.then((response)=>{
        if(response.platform === 'web'){
          CapacitorSQLPlugin.CapacitorDataStorageSqlite.set({key:key,value:value})
          .then((response) => {
            resolve(true);
          })
        }else{
          CapacitorDataStorageSqlite.set({key:key,value:value})
          .then((response) => {
            resolve(true);
          })
        } 
      });
    }); 
  },

  async getItem(key) {
    return new Promise(function (resolve, reject) {
      const info = Device.getInfo();
      info.then((response)=>{
        if(response.platform === 'web'){
          CapacitorSQLPlugin.CapacitorDataStorageSqlite.get({key:key})
          .then((response) => {
            //console.log('retrieved store',response.value);
            resolve(response.value);
          })
        }else{
          CapacitorDataStorageSqlite.get({key:key})
          .then((response) => {
            //console.log('retrieved store',response.value);
            resolve(response.value);
          })
        } 
      });
    });
  },

  async removeItem(key, callback) {
    return new Promise(function (resolve, reject) {
      const info = Device.getInfo();
      info.then((response)=>{
        if(response.platform === 'web'){
          CapacitorSQLPlugin.CapacitorDataStorageSqlite.remove({key:key})
          .then((response) => {
            resolve(response.result);
          })
        }else{
          CapacitorDataStorageSqlite.remove({key:key})
          .then((response) => {
            resolve(response.result);
          })
        } 
      });
    }); 
  }
}



Thats my integration

This where I tie this class into redux-persist

const persistConfig = {
  key: 'root',
  storage: CapacitorEngineStorage,
  whitelist: ['auth'],
};

const persistedReducer = persistReducer(persistConfig, createReducer());

const store = createStore(persistedReducer, enhancer);

i come back to you, i have updated capacitor-data-storage-sqlite to capacitor One taking and solve the issue one. Now i start looking at Redux but cannot figure out how to make it running in a pwa-app using stencil one. As i am willing to take into account your query with redux-persist can you share on github a small pwa-app showing the issue so i can play with it to find the solution. you propose let cleanValue = value.replacingOccurrences(of: "\\", with: "\\"); in ios plugin would you mean let cleanValue = value.replacingOccurrences(of: "\\", with: "");

You did not come back to me. but having look a bit deeper without having makes an app, i can say that it is nothing to do with the capacitor-data-storage-sqlite. It seems that if you have an object {a:2,b:"Hello World"} redux-persit store it as ""{a:2,b:\"Hello World\"}"" so for me you have to deal with this in your CapacitorEngineStorage wrapper and i suggest to do it at the setItem method async setItem(key, value, callback) { return new Promise(function (resolve, reject) { value.replace(/[\\]/g, ''); const info = Device.getInfo(); info.then((response)=>{ if(response.platform === 'web'){ CapacitorSQLPlugin.CapacitorDataStorageSqlite.set({key:key,value:value}) .then((response) => { resolve(true); }) }else{ CapacitorDataStorageSqlite.set({key:key,value:value}) .then((response) => { resolve(true); }) } }); }); }, . This should make it. tell me if it did so we can close the issue

    return new Promise(function (resolve, reject) {

      value.replace(/[\\]/g,'');

      const info = Device.getInfo();
      info.then((response)=>{
        if(response.platform === 'web'){
          CapacitorSQLPlugin.CapacitorDataStorageSqlite.set({key:key,value:value})
          .then((response) => {
            resolve(true);
          })
        }else{
          CapacitorDataStorageSqlite.set({key:key,value:value})
          .then((response) => {
            resolve(true);
          })
        } 
      });
    }); 
  },