Best Local Storage option?


#1

What is the current ‘state of the art’ local storage option for Nativescript?

In researching, it looks like the two leading options are:

  1. Couchbase, using the couchbase plugin. Like here: https://github.com/couchbaselabs/nativescript-couchbase . Further detail here: https://github.com/couchbase-guides/nativescript-angular ;
    and also here: https://blog.couchbase.com/couchbase-meetup-project-nativescript-angular-nosql-development-available/

  2. Local Storage API plugin for Nativescript. Like here: https://github.com/NathanaelA/nativescript-localstorage

In both cases, the plugin hasn’t been updated for about 9 months to a year, which seems a little long for a regularly updated / newish tech like Nativescript (Edit: Correction–looks like couchbase plugin was updated 5 months ago). That being said, both of these look good and user-friendly to me. I especially like the Couchbase option. Are these still the best out there?

(Edit: I see there is also an sqlite plugin for Nativescript, and previously people used “Application Settings”. I would still prefer the couchbase or local storage apis noted above (personal preference over sqlite), unless they are not sufficiently being updated.)


#2

Couchbase keeps changing and seems to have issues, you may try SQLite plugin.

Or even the local storage plugin, it stores data in plain JSON files so it doesn’t need any constant updates or maintenance at least until there is any breaking change in NativeScript FileSystem APIs.

A choice between these always depends on how much you store and how sensitive the information is, if you need better performance and security, opt for the paid version of SQLite plugin which offers you multi threading and encrypted storages.


#3

Personally I use @NathanaelA’s LocalStorage plugin because I need to share code with web and this plugin shims window.localstorage perfectly.

The reason it has not been updated in a while is probably that it’s pretty solid. I’ve never run into trouble with it.


#4

Thanks. I liked Couchbase over sql because in my previous experience using an sql database was tedious during development–extra effort involved if I wanted to change / add database tables.

The local storage api is the easiest out there, so that would be great, but it seems like it is tricky if you have multiple tables to store. For example, I don’t think my data will be overly complicated, but there will be several different types of “databases” (ie, “tables” in sql speak, I think), and each entry within each database will have a few attributes. I can follow up shortly with a quick example of what I mean.

Do you use the local storage api to store data that is moderately complex?


#5

So for local storage, following up to the above post, my issue is I believe you can only store key/value pairs, so it gets tricky if you want to store data that has different groups of key/value pairs. Seems like every time I want to add a new entry to an existing group, I need to get back all prior entries and parse them–and this seems inefficient.

For example, let’s say I want users to be able to store three types of items: restaurants, parks, and plays. And within each group, items would have a few attributes, like name, description, and date visited:

Group 1: Restaurants
each entry:
–name, --description, --date visited

Group 2: Parks
each entry:
–name, --description, --date visited

Group 3: Plays
each entry:
–name, --description, date

So, roughly speaking, if I want to create the restaurant group, I suppose I would do something like the following, for two new entries:

  1. Put the entries into an array:
restaurantArray = []
newItem1 = {
    name: 'McDonalds',
    description: 'Fast Food',
    date_visited: 'December'
}
restaurantArray.push(newItem1)

newItem2 = {
    name: 'KFC',
    description: 'Fried Chicken',
    date_visited: 'August'
}
restaurantArray.push(newItem2)
  1. Store them:
restaurantString = JSON.stringify(restaurantArray)
localStorage.setItem('Restaurants', restaurantString)
  1. Retrieve them for use:
retrievedRestaurantString = localStorage.getItem('Restaurants')
arrayThatICanUse = JSON.parse(retrievedRestaurantString)
  1. Add a new entry:

NOTE: this is where I have the biggest issue. Seems like in order to add a single entry to the group, I need to first retrieve all of the entries, and parse them. If there are a lot of entries and/or I need to add a lot of entries, I would think this could have negative performance impacts:

newEntry = {
    name: 'Joe's Restaurant',
    description: 'local favorite',
    date_visited: 'January'
}

retrievedRestaurantString = localStorage.getItem('Restaurants')

arrayThatICanUse = JSON.parse(retrievedRestaurantString) //this is where I would have to be retrieving/parsing all the entries EVERY time I want to add a new one

arrayThatICanUse.push(newEntry)

So, fairly easy to use, but seems very inefficient.

For retrieving entries and especially adding new entries, is there a different way to do it?

Edit: and then there is also the issue of trying to get back a single item from a group. In order to retrieve an item for modification–say the “McDonalds” restaurant from the Restaurant group, I believe I would have to call back all entries in the group, parse them, and then loop through all of them, checking each one to see if its name is “McDonalds” or not. Again that seems really inefficient. The one caveat is: is this perhaps the action that couchbase and sql do also, they just do it behind the scenes?


#6

Hi All:

Does anyone have a working example of using the local storage plugin with Nativescript Angular? I keep getting various errors, and must be doing something basic incorrectly.

Here’s what I’ve done:

I downloaded the plugin ($ tns plugin add nativescript-localstorage)

I rebuilt ios just to be sure, and reran $ tns run ios

And have code like the following:

myFile.component.ts:

import { LocalStorage } from "nativescript-localstorage";
...
constructor( private localStorage: LocalStorage ) {...}

ngOnInit(): void {
   
    if (this.localStorage.getItem('myStorageKey') !=  null ){
        this.itemToShow = this.localStorage.getItem('myStorageKey')
     }
 }

I get the following error:

JS ERROR Error: Can't resolve all parameters for MyFileComponent: ([object Object], ?).


#7

I have used SQLLite and it works perfectly. The only thing it lacks is the typings which is for paid version But it works very well for me.


LocalStorage plugin with nativescript angular?