Ionic Query Interface!
Hey everyone! This package provide support for old query
method of Ionic Storage, that has deprecated in Ionic 3. In addition, the package provide a fluent interface to work with SQL in Ionic 3+ using the SQLite Native Plugin or WebSQL fallback if you are developing in browser environment. You can work with promises
or async/await
.
Installation
Install the Cordova and Ionic Native plugins: Read the documentation: https://ionicframework.com/docs/native/sqlite/
ionic cordova plugin add cordova-sqlite-storage
npm install --save @ionic-native/sqlite
Then, install the package:
npm install --save ionic-query-interface
Configuration
In your app.module.ts
import the SqlProvider
and SQLite
modules:
;;
Then, add the SqlProvider
and SQLite
on providers
array:
... providers: SQLite SqlProvider ...
In the class that you want to use the SQLite database, just import the SqlProvider
and inject in class constructor. All methods will be available in this.db
property.
;...... {
Methods
First, in constructor, you need to open a database connection using the database name. You need call the open
method, passing the DB name as parameter. Then, this package will create or open a connection with the database name provided. For example:
{ thisdb; this;}
Also, you need to specify the table schema after DB opened. The method createTable
accepts the table name and an object with table scheme. All tables schemes automatically includes a id
field as primary key with auto-increment. Example:
async { await thisdb; }
Insert
To insert new data in table users
, you need to call the insert
method. This method is an interface for SQL insert statement
. Before the use of insert
method, you need to call the table
method to specify the table for insert. Example:
async { let user = name: 'John Doe' age: 10 amount: 16 ; // Fluent API for SQL Insert statement let id = await thisdb; thisusers;}
Update
The update
method is an interface for the SQL update statement
. Before to use update
method, you need to call the table
method to specify the table for update. The data
passed as parameter in update
should contain the id
field, needed in where
statement. Only fields passed in data
will be updated in table. Example:
async { let data = id: 1 name: 'John Doing' ; await thisdb;}
Another option is to use the where
method for explicitly specify the condition for the update
. Example:
async { let data = name: 'John Doing' ; await thisdb;}
Delete
The delete
method is an interface for the SQL delete statement
. Before to use delete
method, you need to call the table
method to specify the table for data delete. The data
passed as parameter in delete
should contain the id
field, needed in where
statement. Example:
async { await thisdb;}
Another option is to use the where
method for explicitly specify the condition for the delete
. Example:
async { await thisdb;}
All
To select all data from a table. Example:
async { thisusers = await thisdball;}
Select
To select data from a table. Example:
async { let results = await thisdb; thisuser = results0name; thisage = results0age; ...}
Where, And and Or
You can use a fluid interface combining where
clausule with and
/ or
methods. It's need to call select
method at end. Example:
async { thisusers = await thisdb ; }
Raw queries
You can use the query
method to run raw SQL queries. Example:
let transaction = await thisdb; thisusers = transactionresrows;
API
-
open(db_name)
: create or open a database connection withdb_name : <string>
provided. -
createTable(table, schema)
: create thetable : <string>
byschema : <object>
provided, if not exists. Theschema
consists of an object that containskey : value
pairs, wherekey
is the column name andvalue
is the column type (integer, real, text and null). -
table(name)
: define the table name for insert | update | select | delete operations. The method return the instance for the chain. -
insert(data)
: method for insert newdata : <object>
in a table. An interface for SQL insert statement. -
update(data)
: method for update adata : <object>
in a table. An interface for SQL update statement. It's required thatdata
contain theid
field used inwhere
clausule. -
delete(data)
: method for delete a data byid : <integer>
in a table. An interface for SQL delete statement. It's required thatdata
contain theid
field used inwhere
clausule. -
select(data)
: method for select data (optional) byid : <integer>
in a table. An interface for SQL select statement with where clausule byid
. This method return an array of table results. Ifdata
is not provided, then the method return all results of table. -
all()
: return an array with all results of a table. -
where(condition)
: sets thewhere
clausule for a SQL method for thecondition
provided. Returns the class instance for the chain. -
and()
: sets the SQLAND
condition to join twowhere
clausule. Returns the class instance for the chain. -
or()
: sets the SQLOR
condition to join twowhere
clausule. Returns the class instance for the chain. -
query(sql)
: run a SQL raw query in the database. Return a SQL Transaction.