Manage SQlLite connection and create tables.
##Get Started
bower install angular-sql-storage --save
Include angular-sql-storage.js
(or angular-sql-storage.min.js
) from the dist directory in your index.html
, after including Angular itself.
Add 'sf.sqlStorage'
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-storage/angular-sql-storage.min.js"></script>
...
<script>
var myApp = angular.module('myApp', ['sf.sqlStorage']);
</script>
...
</body>
</html>
##Configuration
###setDatabaseConfig
You can define your name and database version number
The version number let you manage migrations automatically.
The service compares the current version with the latest and runs all updates accordingly
Default: name: database.db
version: 1
myApp.config(function (sqlStorageServiceProvider) {
sqlStorageServiceProvider
.setDatabaseConfig({
name: 'myapp.db',
version: 0.1
});
});
###setDatabaseSchema
You need to define the table definition to create tables
Params:
defaultFields
[Array] - Define fields which appear in all tablesname
[String] - Sql field nametype
[String] - Sql field typeprimayKey
[Boolean] - Add PRIMARY KEY constraint to field.unique
[Boolean] - Add UNIQUE constraint to field.
tables
[Object] - Define tables with specific structuretable_name
[String] - Name of tableindexed_fields
[Array] - Add indexed fields to table (structure like defaultFields)
myApp.config(function (sqlStorageServiceProvider) {
sqlStorageServiceProvider
.setDatabaseSchema({
defaultFields: [{
name: 'id',
type: 'NVARCHAR(32)',
primayKey: true,
unique: true,
}, {
name: 'payload',
type: 'TEXT',
},
tables: {
users: {
table_name: 'users',
indexed_fields: [{
name: 'age',
type: 'INTEGER',
}],
},
}]
});
});
###setDatabaseInstance
Set the database instance instead of window.openDatabase.
Example, you can use cordova and ngCordova to instantiate your database (Function is invoke by angular $injector.invoke service).
myApp.config(function (sqlStorageServiceProvider) {
sqlStorageServiceProvider
.setDatabaseInstance(databaseInstance);
function databaseInstance($cordovaSQLite) {
return $cordovaSQLite.openDB('myapp.db', 1);
}
});
###addUpdater
You could define migration scripts when you update your database schema. Just need to define verison and script.
You can access to this following instance variable :
this._database
: Database Instance.
Params:
version
[Float] - Version which need to run the script.method
[Function] - Method to run by $injector.inoke().
myApp.config(function (sqlStorageServiceProvider) {
sqlStorageServiceProvider
.addUpdater({
version: 1.1,
method: updateMethod,
});
function updateMethod($cordovaSQLite) {
var _database = this._database;
var query = 'ALTER TABLE users ADD COLUMN name TEXT';
return $cordovaSQLite.execute(_database, query);
});
});
##API Documentation
##initDatabase
Init instance if you want to update earlier. This method is called by getDatabase too.
Returns: Database Instance
###getDatabase
Just get database instance.
Returns: Database Instance
###initTables
Create Sql tables and run migration if needed
Returns: Database Instance
###deleteDatas Drop all tables (of the database).
###execute
Helpers to execute sql queries.
Returns: Query result