onlinecode

Ionic 5 SQLite Data Pagination

Ionic 5 SQLite Data Pagination

In this post we will give you information about Ionic 5 SQLite Data Pagination. Hear we will give you detail about Ionic 5 SQLite Data PaginationAnd how to use it also give you demo for it if it is necessary.

In the previous part(s) we have:

  • Created a new Ionic 5 project,
  • Added the Cordova SQLite plugin and its Ionic Native wrapper,
  • Created a data service for interfacing with the SQLite database,
  • And implemented the various CRUD methods to create, read, update and delete data from the SQLite database.

Adding SQLite Pagination

Now let’s implement pagination in our data service so we can get paginated data from our SQLite database instead of returning all rows which can be bad for our application when the database size grows.

Head over to the src/data.service.ts file and start by adding a TypeScript interface for the Pager object which we are going to need later for accessing the pager.

exportinterfacePager{initialPage();nextPage();}

The pager interface declares two methods, the initialPage() method which returns the initial page of data and the nextPage() method which returns the next number of data rows – This method will be called by the application everytime the user requests more data to be displayed.

Next, create the pager service which takes a SQLiteObject object as a parameter:

classPagerService{publicdatabase:SQLiteObject;constructor(database:SQLiteObject){this.database=database;}privategetTotal(tableName:string):Promise<number>{/* ... */}publicexecuteSql(tableName:string,limit:number,offset:number):Promise<any>{/* ... */}publicasyncgetPager(tableName:string,pageSize:number=10){/* ... */}}

Don’t forget to import SQLiteObject from the @ionic-native/sqlite package:

import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

The getTotal(tableName) returns the total size (number of data rows) of a specified table.

The executeSql(tableName : string , limit : number , offset : number ) takes care of executing the SQL queries against a SQLite database table.

The getPager(tableName:string, pageSize: number = 10) returns an object of Pager type for a specified SQL table and a Page size which defaults to 10 rows.

PagerService has one member variable which holds the SQLiteObject instance passed via class constructor.

Let’s begin by implementing the getTotal(tableName) method:

privategetTotal(tableName:string):Promise<number>{lettotal=-1;returnnewPromise<number>((resolve,reject)=>{this.database.executeSql('selectcount(*)assizefrom${tableName}',[]).then((data)=>{resolve(data.rows.item().size);},(err)=>{console.log("error"+JSON.stringify(err));reject(err);});});}

The method returns a Promise which when resolved returns the total size of a table.

The methods builds a query to get the total count of rows using TypeScript template strings and the interpolation operator ${} to dynamically specify the table name:

'select count(*) as size from ${tableName}'

Next, It calls the executeSql() to execute the query against the SQLite database which returns a Promise.

The Promise either resolves with the table size data or rejects with the error originally returned from executeSql().

Next let’s add an implementation of the executeSql() wrapper method:

publicexecuteSql(tableName:string,limit:number,offset:number):Promise<any>{varsql='select*from${tableName}LIMIT${limit}OFFSET${offset};'returnnewPromise((resolve,reject)=>{this.database.executeSql(sql,[]).then((data)=>{resolve(data);}).catch((e)=>{reject(e);});});}
  • tableName holds the table name.
  • limit holds the index of last page row to fetch.
  • offset holds the index of first page row from where to start fetching.

Now, let’s implement the getPager() method which returns a Pager object for a specified SQLite table and Page size:

publicasyncgetPager(tableName:string,pageSize:number=10){letpageSize=pageSize;letoffset=;letlimit=pageSize;letsize=awaitthis.getTotal(tableName);letthat=this;return{initialPage:function(){returnnewPromise((resolve,reject)=>{vard=[];that.executeSql(tableName,limit,offset).then((data)=>{console.log(JSON.stringify(data));for(vari=;i<data.rows.length;i++){d.push(data.rows.item(i));}resolve(d);},(e)=>{reject(e);});});},nextPage:function(){if(that.offset<=that.size-that.pageSize){that.offset+=that.pageSize;}returnnewPromise((resolve,reject)=>{vard=[];that.executeSql(tableName,limit,offset).then((data)=>{for(vari=;i<data.rows.length;i++){d.push(data.rows.item(i));}resolve(d);},(e)=>{reject(e);});});}};}}

After adding the Pager service, It’s time to modify our Data service to use paging when reading data.

So, first add a pagerService member variable:

@Injectable()exportclassDataService{publicdatabase:SQLiteObject;publicpagerService:PagerService;

Next, in the constructor, create an instance of PagerService and assign it to the pagerService variable:

constructor(publicsqlite:SQLite){this.sqlite.create({name:"data.db",location:"default"}).then((db:SQLiteObject)=>{this.database=db;this.pagerService=newPagerService(db);this.createTables();},(error)=>{console.log("ERROR: ",error);});}

Next, modify the list(tableName) method to return a pager object instead of the query result.

publiclist(tableName){returnthis.pager(tableName);}

Conclusion

That’s all we need for adding SQL pagination to our Ionic 5 application.

In the next tutorial, we will see how to use our data service to actually create, update, read and delete data from our SQLite database tables that we have previously created.


Hope this code and post will helped you for implement Ionic 5 SQLite Data Pagination. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version