angularjslokijs

What is the best way to use LokiJS in my Angular 1 app?


I want to use LokiJS for my AngularJS v1 app. I thought it would be a good idea to write my own persistence factory, so that I would be able to change the database system easiely.

LokiJS has a special implemantation for Angular (loki-angular.js), and there is a Lokiwork service included.

What would you do? Use Lokiwork, or better my own system?

Thanks, Christian.


Solution

  • You can create a factory when the loki is initialized, and also in the same factory you can write methods to create a table, populate the table, retrieve the data from the table.

    By doing this all you DB related code is in one place.

    Below is some thing which you can do to implement lokijs in your application.

    app.factory('InAppStorageUtility', ['Loki', function (Loki) {
        var db = {
            initilizeLoki: function() {
                var thisScope = this;
                var inappDb = new loki("DBName", { autosave: false });
                inappDb.loadDatabase({}, function () {
                    var tableInititalized = offlineDb.getCollection('testTable');
    
                    if (tableInititalized === null) {
                        thisScope.createTables();
                    }
                });
            },
            createTables: function() {
                // Table creation logic. Give a structure of the table
                // var tableCreation = offlineDb.addCollection('testTable');
                // Provide the table structure 
            },
            insertValuesToTable: function() {
                // Insert logic for all you tables
            },
            getValuesStoredInDB: function() {
                // Retrieving values from the DB logic goes here.
                // var getTableValue = offlineDb.getCollection('testTable');
            }
        }
        return db;
    }]);
    

    Some thing like this you can implement in your application.

    Hope this helps you.