-
-
Save cergey-obr/e8d5ceff7df925b45fcb to your computer and use it in GitHub Desktop.
SQLite plugin with ngCordova in Ionic Framework => Using service pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
controllers.controller('ProductsController', ['$scope', '$ionicPlatform', 'Products' function ($scope, $ionicPlatform, Products) { | |
$scope.products = []; | |
$scope.product = { | |
IdProduct: 1, | |
Name: 'Pizza', | |
Icon: 'ion-pizza', | |
Color: '#F056C2' | |
}; | |
$ionicPlatform.ready(function () { | |
Products.createTable().then(function(){ | |
return Products.addProduct($scope.product); //Insert a new product | |
}).then(function () { | |
return Products.getProducts(); //Get Products | |
}).then(function (products) { | |
$scope.products = products; | |
}).catch(function (err) { | |
console.log(err); | |
}); | |
}); | |
} ]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.factory('Products', ['$q', 'sqliteService', function ($q, sqliteService) { | |
return { | |
getProducts: function () { | |
var query = 'SELECT * FROM Products'; | |
return $q.when(sqliteService.getItems(query)); | |
}, | |
addProduct: function (product) { | |
var query = "INSERT INTO Products (IdProduct, Name, Icon, Color) VALUES (?,?,?,?)"; | |
return $q.when(sqliteService.executeSql(query, [product.IdProduct, product.Name, product.Icon, product.Color])); | |
}, | |
dropTable: function () { | |
var query = 'DROP TABLE IF EXISTS Products'; | |
return $q.when(sqliteService.executeSql(query)); | |
}, | |
createTable: function () { | |
var query = 'CREATE TABLE IF NOT EXISTS Products (IdProduct integer primary key, Name text, Icon text, Color text)'; | |
return $q.when(sqliteService.executeSql(query)); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.service('sqliteService', ['$q', '$cordovaSQLite', function ($q, $cordovaSQLite) { | |
var self = this; | |
var _db; | |
self.db = function () { | |
if (!_db) { | |
if (window.sqlitePlugin !== undefined) { | |
_db = window.sqlitePlugin.openDatabase({ name: "demo.db", location: 2, createFromLocation: 1 }); | |
} else { | |
// For debugging in the browser | |
_db = window.openDatabase("demo.db", "1.0", "Demo", 200000); | |
} | |
} | |
return _db; | |
}; | |
self.getItems = function (query, parameters) { | |
var deferred = $q.defer(); | |
self.executeSql(query, parameters).then(function (res) { | |
var items = []; | |
for (var i = 0; i < res.rows.length; i++) { | |
items.push(res.rows.item(i)); | |
} | |
return deferred.resolve(items); | |
}, function (err) { | |
return deferred.reject(err); | |
}); | |
return deferred.promise; | |
}; | |
self.executeSql = function (query, parameters) { | |
return $cordovaSQLite.execute(self.db(), query, parameters); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment