Working With Shared Providers In A NativeScript Angular 2 App

When building a NativeScript application with Angular 2, there are certain scenarios where you might want to share functions and variables across the pages of the application.  There are other scenarios where you might want to pull similar functions into a class for code cleanliness.  Both of these scenarios would find value in using Angular 2 shared providers.

Shared providers can be injected into the constructor methods of each page that you wish to use them.  The providers can act as a singleton where the data and functions are global to the application rather than local to any specific page.

We’re going to see how to create a provider for managing interactions with a database in an Angular 2 NativeScript application.

If you’ve been keeping up you’ll remember that I wrote a tutorial for using a SQLite database in a NativeScript Angular 2 application as well as using a pre-populated SQLite database.  In both these examples the database interactions were mashed into the components.  We’re going to make it slick in a sense that these database interactions will happen from a single location which is more ideal in a larger application.

Let’s start by creating a fresh NativeScript project.  From the Terminal (Mac and Linux) or Command Prompt (Windows), execute the following commands:

In the above, notice the --ng flag.  This indicates that we are creating an Angular 2 TypeScript application.  While we’re adding the iOS build platform, we cannot build for iOS unless we’re using a Mac with Xcode installed.

Since SQLite will be the basis of this particular tutorial, we need to install the SQLite plugin for NativeScript.  This can be done by executing the following:

Since the focus here is around creating an Angular 2 provider, the application we build will only have a single page.  Within the new project, create the following directories and files:

If you don’t have access to mkdir and touch in your command line, just create the files and directories manually.

With the provider file created, we need to populate it with some code.  Open the project’s app/providers/database/database.ts file and include the following TypeScript code:

There is a lot of code in the above file, so it is probably a good idea to break it down so it is easier to understand.

Since we’re going to be injecting this provider in each of our pages, we need to import the appropriate Injectable component.  As mentioned in my previous SQLite tutorials, the free version of this plugin doesn’t include type definitions for TypeScript.  This doesn’t limit what we can do with the library, it only makes things slightly inconvenient, nothing more.  This is why we include the plugin library differently than a standard TypeScript library.

There are two variables that we maintain in this provider.  The db variable will hold the open database instance that will be shared globally in the application.  The isInstantiated variable will let us know if the database has already been instantiated.  After all, we don’t want to keep opening the database if it is already open.

The above constructor method will trigger every time the provider is injected.  The isInstantiated check happens to prevent the database from being opened every time.  When we try to open the database, we will also try to create a table if it doesn’t already exist.  When this initialization is finished, we store the open database instance and say that it is stored.

Next we have an insert method:

From each of the pages we would pass a data object which would be inserted into the database.  It may not be a good idea to work with database queries in every page of your application.  Working with JavaScript objects might make more sense.

Finally we have the fetch method, responsible for gathering data from the database:

Notice in this method we’re not just returning the query result like in the insert method.  This is because we want to do something useful and parse the data into an object before returning it to the page.

So the provider is now complete and really isn’t any different than what we saw in the first SQLite tutorial that I wrote.

Now how do we use this provider?

The first thing we need to do is import the provider in the applications @NgModule block.  For NativeScript, this block is found in the app/main.ts file.  Open the project’s app/main.ts file and include the following TypeScript:

In the above code, we really only added the following line:

Let’s take a look at our application page now.  The goal here isn’t very complex, just to show us using the provider that we just created.  The rest is up to your imagination.

Open the project’s app/app.component.ts file and include the following code:

We’re going to break down this file since it is quite large.

The first thing we want to do is import the Database provider that we created:

Yes we had already imported the provider into the @NgModule, but it is not the same.  It needs to be done in both places.

In the constructor method, we’re not only initializing our data array, but we’re injecting the provider into our component.  This allows us to use it throughout our particular component.

It is frowned upon to load data in the constructor method, so instead we make use of the ngOnInit method.  This however, is a special scenario.  Because the database load process is asynchronous, we really have to wait a moment before we can start using the database.  This is why I added a short timeout.  Again, this is a unique scenario because of the particular provider goals.  If you’re not doing anything asynchronous in the constructor, don’t worry about it.

Using the provider functions are as easy as calling them in the functions as seen above.  You can ignore that I’ve called my component functions the same name as my provider functions.  They don’t need to be named the same.

To wrap things up, the corresponding UI can be found in the project’s app/app.component.html file.  Open this file and include the following markup:

The UI is just a simple ListView and an ActionBar.  Nothing too complicated, right?


You just saw how to create an Angular 2 provider in a NativeScript application.  While this was only one usage scenario, there are plenty of great things you can do with providers.  Another good example is using a provider for passing complex data between pages of an application.

If you want to learn more about SQLite in a NativeScript application, check out one of my many other tutorials.

A video version of this article can be found below.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

  • Jarrod Chesney

    Hi Nic
    On line 7/8 of app/providers/database/database.ts, I think what you wanted to do was declare these “private static …” and then refer to them with “Database.” instead of “this.”

    Each instance of the class Database will get fresh new (undefined) values for “isInstantiated” and “db”

    just add a line to the top of the constructor to demonstrate.

    console.log(Database instantiated, isInstantiated=${this.isInstantiated});