npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ngSqlite

v0.0.3

Published

Sqlite cordova wrapper for angularjs

Downloads

6

Readme

ngSqlite

This library is a simple angular wrapper for cordova-sqlite-storage cordova plugin. With this library you dont have to write sql queries and do transactions. This wrapper provides convinence methods for common CRUD operations like createTable, upsert, delete, etc. Also you can use plain POJO's( Plain old javascript objects ) as inputs for these operations.

Note: Though this wrapper is designed to be used in cordova based applications, this wrapper will also support HTML5 WebSQL (which is deprecated) which is still available in some browsers like chrome. So if you are using this library in a browser based application, it will fallback to use WebSQL instead of Sqlite.

How to install

bower install ngSqlite --save

Dependencies

As this library is a angular wrapper on top of cordova-sqlite-storage, obviously we need angular and cordova-sqlite-storage plugin. Apart from these two the obly dependency needed is lodash. If you install this package through bower, then angular and lodash will be installed along with this package.

Getting Started

  • Inject ngSqlite module as a dependency in your app module
    angular.module('app', ['ngSqlite']);
  • Inject cacheProvider in the config block of your angular app and configure the name of the DB and its location
angular.module('app')
        .config(appConfig);

    function appConfig(cacheProvider) {
        var cfg = {
            dbName: 'app',
            dbLocation:'default'
        }
        cacheProvider.config(cfg);
    }
  • Ininitalize the DB either in run block or in any of your controllers by injecting the cache servic and calling its init method
angular.module('app')
        .run(appRun);

    function appRun(cache) {
        cache.init();
    }
  • Next, you can inject cache service in any controller or service and start using the convinience methods provided by the wrapper. All the methods of this service returns an angularjs promise that you can resolve using then.
angular
    .module('app')
    .controller('DashboardController', DashboardController);

    function DashboardController(cache) {
        var vm = this;
        var tableName = 'demo';
        //Create Table
        vm.createTable = function() {
          // 'Id text primary key, FirstName text, LastName text, Email text'
          var fieldSpec = {
            Id: {
              type: 'TEXT',
              primary: true,
              notnull: true
            },
            FirstName: {
              type: 'TEXT'
            },
            LastName: {
              type: 'TEXT'
            },
            Email: {
              type: 'TEXT'
            }
          }
          cache.createTable(tableName, fieldSpec).then(function(result) {
            console.log(result);
          }, function(error) {
            console.log(error);
          });

        };
        
        // Create/Insert record
        vm.createRecord = function() {
            var testData = {
                Id: 'ave',
                FirstName: 'Tony',
                LastName: 'Stark',
                Email: '[email protected]'
            };
            cache.insert(tableName, testData).then(function(result) {
                console.log(result);
            }, function(error) {
                console.log(error);
            });

        };
        
        //Retrieve record with where clause
        vm.retrieve = function() {
            var testData = {
                Id: null,
                FirstName: 'Tony',
                LastName: 'Stark',
                Email: null
            };

            cache.select(tableName, testData, ['FirstName', 'LastName']).then(function(result) {
                console.log(result);
            }, function(error) {
                console.log(error);
            });
        };
        
        // Retrieve all records
        vm.retrieveAll = function() {
            cache.selectAll(tableName).then(function(result) {
                console.log(result);
            }, function(error) {
                console.log(error);
            });
        };
        
        // Update record - keyFields parameter is mandatory as it is used in where clause of the query for udpate
        vm.update = function() {
            var testData = {
                Id: 'ave',
                FirstName: 'Tony Iron Man',
                LastName: 'Stark',
                Email: '[email protected]'
            };
            cache.upsert(tableName, testData, ['Id']).then(function(result) {
                console.log(result);
            }, function(error) {
                console.log(error);
            });
        };

        // Upsert record - Inserts a new record or replaces existing record with the new values (Note: Upsert deletes the matching row and inserts a new row with the provided new values. Also upsert uses primary or unique fields for upsert transaction)
        vm.update = function() {
            var testData = {
                Id: 'ave',
                FirstName: 'Tony Iron Man',
                LastName: 'Stark',
                Email: '[email protected]'
            };
            cache.upsert(tableName, testData, ['Id']).then(function(result) {
                console.log(result);
            }, function(error) {
                console.log(error);
            });
        };

        // Delete record by its Id
        vm.delete = function() {
            var testData = {
                Id: 'ave'
            };
            cache.delete(tableName, testData, ['Id']).then(function(result) {
                console.log(result);
            }, function(error) {
                console.log(error);
            });
        };
        
        // Bulk upsert
        vm.bulkUpsert = function() {
            var testData = [{
                Id: 'cde',
                FirstName: 'Peter',
                LastName: 'Parker',
                Email: '[email protected]'
            },{
                Id: 'adf',
                FirstName: 'Steve',
                LastName: 'Rogers',
                Email: '[email protected]'
            }];
            cache.bulkUpsert(tableName, testData, ['Id']).then(function(result){
                console.log(result);
            }, function(error) {
                console.log(error);
            });
        };

        // Raw query - Exploit full power of raw queries
        vm.query = function() {
            var query = 'SELECT * FROM demo ORDER BY LastName';
            
            cache.query(query, []).then(function(result) {
                    console.log(result);
                }, function(error) {
                    console.log(error);
                });

        };

        // sql batch transactions - A wrapper around sqlBatch method
        vm.sqlBatch = function() {
            var queryList = [
                'CREATE TABLE IF NOT EXISTS DemoTable (name, core)', 
                [ 'INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101] ], 
                [ 'INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202] ]
            ];

            cache.sqlBatch(queryList).then(function(result) {
                    console.log(result);
                }, function(error) {
                    console.log(error);
                });
        };

    }

Sample App

Refer the example folder in this repo for a fully functioning browser app. The same code can be used in cordova app as well :)

API Reference

TODO