angular-sql-query
Get Started
bower install angular-sql-query --save
Include angular-sql-query.js
(or angular-sql-query.min.js
) from the dist directory in your index.html
, after including Angular itself.
Add 'sf.sqlQuery'
to your main module's list of dependencies.
When you're done, your setup should look similar to the following:
<!doctype html>
<html ng-app="myApp">
<head>
</head>
<body>
...
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script src="bower_components/angular-sql-query/angular-sql-query.min.js"></script>
...
<script>
var myApp = angular.module('myApp', ['sf.sqlQuery']);
</script>
...
</body>
</html>
Configuration
For using this module, your database need to be architectured with these fields:
id
: Unique key for data.payload
: Object data stringify with angular.toJson.
For some extra helpers regarding storage, you can look at this module angular-sql-storage.
Example
var user = new SqlQueryService(name, databaseFn, options);
Params
name
[String] - Table namedatabase
[Function] - Function that return SQL database instance.options
[Function] - Query options
indexed_fields
[Array] - Reference field by adding a column in the table.
function databaseInstance() {
return $q.when($window.openDatabase('test', '1', 'database', 200000));
}
var user = new SqlQueryService('user', databaseInstance, {
indexed_fields: ['name'],
});
API Documentation
.getBackUp()
Get data by its id
Params
Returns: payload
user.getBackUp(1);
.listBackUp()
All datas
Returns: [Array] payload
user.listBackUp();
.queryBackUp()
All datas corresponding to query.
If field is referenced in options, query can be set directly in SQl Query. Also, a javascript filter is used.
You need to pass an object; the key is the field name and the value is the query value.
- You can pass an Array to make a
IN
query. - You can pass a Boolean for a 1 or 0 query.
- You can pass an RegExp to make a
LIKE
query.
Params:
params
: [Object] Filter datas
Returns: [Array] payload
user.queryBackUp({
name: ['Jean', 'Paul'],
connected: true
});
.saveBackUp()
Save new object data
Params:
id
: Data keydatas
: Data object
Returns: [Object] Data saved
user.saveBackUp(1, { name: 'Jean', connected: false });
.updateBackUp()
Update database object
Params:
data
: Object datas (with id).
Returns: [Object] Data updated
user.updateBackUp({ id: 1, name: 'Paul', connected: false });
.removeBackUp()
Remove database object
Params:
Returns: SQL remove result
user.removeBackUp(1);
.bulkDocsBackUp()
Modify multiple datas
It's possible to update or remove datas with one method called.
You can delete a data by setting an the object key _delete
to true.
Params:
datas
: Array of objects to update.
Returns: SQL update result
user.bulkDocsBackUp([{
id: 1, name: 'Jean', connected: true,
id: 2, name: 'Paul', connected: false, _deleted: true
}]);
.execute()
Directly make an SQL query.
Params:
query
: SQL query.datas
: SQL params.
Returns: SQL result
user.execute('SELECT * FROM user WHERE id=?', [1]);