Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zixiaowang/cordova-angular-sqlcipher

The mid-layer between Cordova Angular project and Cordova Sqlcipher plugin.
https://github.com/zixiaowang/cordova-angular-sqlcipher

Last synced: about 2 months ago
JSON representation

The mid-layer between Cordova Angular project and Cordova Sqlcipher plugin.

Awesome Lists containing this project

README

        

# cordova-angular-sqlcipher
The mid-layer between Cordova Angular project and Cordova Sqlcipher plugin.

### HOW TO INSTALL?
1. Import angular js to your project, if you've already had angular js in your project, ignore this step.
2. Install [liteHelpers/Cordova-sqlcipher-adapter](https://github.com/litehelpers/Cordova-sqlcipher-adapter) by entering cmd
cordova plugin add https://github.com/litehelpers/Cordova-sqlcipher-adapter
3. Bower install angular-sqlcipher by entering cmd bower install angular-sqlcipher

------
### How to use it?
1. Add following script tag into your html.
```html

```
2. Add ngSqlcipher to your Angular dependencies.
```javascript
var app = angular.module('myApp', ['ngSqlcipher']);
```
3. Inject service $sqlcipher to the function which requires it.
```javascript
app.
controller('appCtrl', ['$scope', '$sqlcipher', function($scope, $sqlcipher){
// ... your code here
}];
```

------
### Config and create database
The $sqlcipher services is the object returned after creating database. In original command
```javascript
var db = openDatabase('db.name', 'db.version', 'db.description', 'db.size', callbackFn);
```
db is the object after the database db.name was created. And this operation has to
be configured in angular.config.
```javascript
app
.config(['sqlcipherConfigProvider', function(sqlcipherConfigProvider){
var options = {
name:'my.db',
password:'admin',
location:'default',
iosDatabaseLocation:'defalult'
};
sqlcipherConfigProvider.open(options, callbackFn);
}])
```

------
### APIs
| functions | |
|----|----|
| [trans(_fn_)](#trans) | equivalent to db.transaction(fn) |
|[readTrans(_fn_)](#readTrans)| equivalent to db.readTransaction(fn)|
|[createTable(_table_name_, _options_)](#createTable)|_table_name_ : String,
_options_ : Array, String|
|[dropTable(_table_name_)](#dropTable)| _table_name_ : String |
|[truncate(_table_name_)](#truncate)| _table_name_ : String
NOTE: this will erase all the data in table|
|[select(_table_name_, _col_names_, _conditions_)](#select)|_table_name_ : String
_col_name_ : Array
_conditions_ : Object|
|[selectAll(_table_name_)](#selectAll)|_table_name_ : String
This function is equivalent to
SELECT * FROM table_name|
|[insert(_table_name_, _dataSet_)](#insert)|_table_name_ : String
_dataSet_ : Array|
|[renameTable(_table_name_, _new_table_name_)](#renameTable)|_table_name_ : String
_new_table_name_ : String|
|[addColumns(_table_name_, _cols_)](#addColumns)|_table_name_ : String
_cols_ : String or Array|
|[update(_table_name_, _options_, _conditions_)](#update)|_table_name_ : String
_options_ : Object
_conditions_ : Object |

### Examples
###### trans(_fn_)
$sqlcipher.trans(_fn_).then(_successCallbackFn_).catch(_errorCallbackFn_);
```javascript
$sqlcipher
.trans(function(tx){
tx.executeSql('SELECT * FROM table_name') // Where statements were executed.
}).then(function(result){
console.log(result.rows.item(0)); // {name:'Jacky', age:25}
}).catch(function(err){
console.log(err);
})
```

###### readTrans(_fn_)
Normally, read-transaction are recommended for SELECT execution.
$sqlcipher.readTrans(_fn_).then(_successCallbackFn_).catch(_errorCallbackFn_);
```javascript
$sqlcipher
.readTrans(function(tx){
tx.executeSql('SELECT * FROM table_name') // Where statements were executed.
}).then(function(result){
console.log(result.rows.item(0)); // {name:'Jacky', age:25}
}).catch(function(err){
console.log(err);
})
```

###### createTable(_table_name_, _options_)
options can be Array or String.
$sqlcipher.createTable(_table_name_, _options_).then(_successCallbackFn_).catch(_errorCallbackFn_);
```javascript
// recommended
var options = [
{name:'id', type:'INTEGER', primary:true, autoIncrement:true},
{name:'name', type:'TEXT', notNull:true},
{name:'age', type:'INTEGER'}
];
// or
var options = ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'name TEXT NOT NULL', 'age INTEGER'];
// or
var options = 'id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER';
$sqlcipher
.createTable('my_table', options)
.then((msg) => {console.log(msg);}) // SQLResultSet {rows: SQLResultSetRowList, insertId: 0, rowsAffected: 0}
.catch((err) => {console.log(err); }); // error messages.
```

###### dropTable(_table_name_)
```javascript
$ngSqlcipher.dropTable('table_name'); // table will be deleted
```

###### truncate(_table_name_)
```javascript
$ngSqlcipher.truncate('table_name'); // table will be erased.
```

###### select(_table_name_, _col_names_, _conditions_)
select(_table_name_, _col_names_, _conditions_).then(_successCallbackFn_).catch(_errorCallbackFn_)
```javascript
var conditions = {
where:'id<10&&name=="Jacky"'
// groupby:'name',
// having:'key_word',
// orderby:'key_word',
// sort:'key_word'
}
$sqlcipher
.select('myTable', ['age'], conditions)
.then( (res) => console.log(res.rows.item(0) ) // return {age:25}
.catch( (err) => console.log(err) ); // err
```

###### selectAll(_table_name_)
selectAll(_table_name_).then(_successCallbackFn_).catch(_errorCallbackFn_)
```javascript
$sqlcipher
.selectAll('myTable')
.then( (res) => console.log(res.rows.item(0) ) // return {name:'Jacky', age:25}
.catch( (err) => console.log(err) ); // err
```

###### insert(_table_name_, _dataSet_)
insert('table_name', dataSet).then(_successCallbackFn_).catch(_errorCallbackFn_)
```javascript
var dataSet = [
{name:'Rynn', age:30},
{name:'Amy', age:22}
]
$sqlcipher
.insert('myTable', dataSet)
.then((res)=>{console.log(res)}) // d {$$state: Object} NOTE: this is a promise type
.catch( (err) => console.log(err) ); // err
```

###### renameTable(_table_name_, _new_table_name_)
renameTable('myTable', 'myNewTable')
```javascript
$sqlcipher.renameTable('myTable', 'myNewTable'); // table will be renamed;
```

###### addColumns(_table_name_, _cols_)
addColumns('myTable', _cols_)
```javascript
var cols = [
{name:'gender', type:'TEXT'},
{name:'title', type:'TEXT'}
];
/* or */ cols = ['gender TEXT', 'title TEXT'];
/* or */ cols = 'gender TEXT, title TEXT';
$sqlcipher
.addColumns('myTable', cols)
.then((res) => console.log(res)) // d {$$state: Object} NOTE: this is a promise type
.catch((err) => console.log(err)) // err
```

###### update(_table_name_, _options_, _conditions_)
update(_table_name_, _options_, _conditions_)
```javascript
var options = {
age:23,
title:'Software Developer' // or Ingeter, function
}
var conditions = {
where:'id<10' // where only
}
$sqlcipher
.update('myTable', options, conditions)
.then((res) => console.log(res))
.catch((res) => console.log(res))
```

------
### What if I do not want to use those APIs?
For this situation, use following methods:
var db = $sqlcipher.core;
db is the oringinal object returned by openDatabase() function.