Skip to content

Simple and minimal WebSQL and cordova SQLite ORM for ionic and angular

Notifications You must be signed in to change notification settings

lykmapipo/ngData

Repository files navigation

ngData

Build Status

Simple and minimal WebSQL and cordova SQLite ORM for ionic and angular

Istallation

$ bower install ngData --save

Testing

  • Clone this repository

  • Install all development dependencies

$ npm install && bower install

Usage

Configure database and define model

angular
        .module('ngBooks', ['ngData'])
        .config(function($databaseProvider) {
            //configure database
            $databaseProvider.name = 'books';
            $databaseProvider.description = 'Books database';
            $databaseProvider.version = '1.0.0';
            $databaseProvider.size = 4 * 1024 * 1024;

        })
        .run(function($ngData) {
            //define model
            $ngData.model('Book', {
                tableName: 'books',
                properties: {
                    name: String,
                    author: Object,
                    isbn: {
                        type: String,
                        required: true,
                        unique: true
                    }
                },
                methods:{//instance methods
                   getCodedName: function(){
                       return [this.code, this.name].join('-');
                   }
                },
                statics:{//static methods
                   findByCode: function(code){
                       return this.findOne({code:code});
                   }
                }
            });

            //initialize 
            $ngData.initialize().then(function(results) {
                console.log(results);
            }).catch(function(error) {
                console.log(error);
            });

        });

Define factories

angular
    .module('ngBooks')
    .factory('Book', function($ngData) {
        
        var Book = $ngData.model('Book');

        return Book;
    });

Use it

angular
        .module('ngBooks')
        .controller('BookCtrl', function($scope, $q, Book) {

            $scope.index = function(offset, limit) {
                Book.find().limit(limit).offset(offset).then(function(books) {
                    $scope.books = books;
                });
            };

            $scope.book = Book.new();

            $scope.show = function(id) {
                return Book.findById(id);
            };

            $scope.create = function(book) {
                return Book.create(book);
            };

            $scope.update = function(book) {
                return book.save();
            };

            $scope.delete = function(book) {
                return book.delete();
            };
           
            //load books
            $scope.index();

        });
  • Then run test
$ npm test

Development

ngData has set of useful grunt tasks to help you with development. By running

$ grunt

ngData will be able watch your development environment for file changes and apply jshint and karma to the project.

Contribute

Fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.

References

Licence

Copyright (c) 2015 lykmapipo && Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Simple and minimal WebSQL and cordova SQLite ORM for ionic and angular

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages