CouchDB and Ionic 3
Let's start by creating a new Ionic 3 project, using the Ionic CLI. So go ahead and open the terminal on Mac/Linux or the command prompt in Windows the run the following command to generate a new project.
ionic start ionic3-pouchdb-sqlite blank
Wait for the project to setup the dependencies then navigate inside the root folder:
Installing SQLite Cordova Plugin and PouchDB
On Ionic 3, the native SQLite database is the most adequate choice when it comes to storing data locally, because it allows to have unlimited storage which is not the case of local storage or IndexedDB. Also SQLite is a file based database which makes very portable.
You can add SQLite support to your Ionic application with many Cordova plugins such as:
- Cordova-sqlite-storage: the original Cordova plugin for SQLite
- cordova-plugin-sqlite-2: a fork of the previous plugin with extra features
Let's install the fork with :
ionic cordova plugin add cordova-plugin-sqlite-2
To be able to use PouchDB, you need to install a third party library available from npm :
npm install pouchdb --save
Next you'll need to install another third part adapter to use SQLite with PouchDB :
npm install pouchdb-adapter-cordova-sqlite --save
These are all the dependencies you need to install in order to start using PouchDB with SQLite in your Ionic app.
We will build a small application that allows to do CRUD operations i.e create, read, update and delete employees data from a PouchDB + SQLite database.
The application has many screens:
- employees screen: list employees with delete buttons
- create an employee screen
- update an employee screen