我想将 LokiJS 用于我的 AngularJS v1 应用程序。我认为编写自己的持久性工厂是个好主意,这样我就可以轻松更改数据库系统。
LokiJS 有一个特殊的 Angular 实现 (loki-angular.js),并且包含一个 Lokiwork 服务。
你会怎么做?使用 Lokiwork,还是改进我自己的系统?
谢谢,克里斯蒂安。
您可以在初始化 loki 时创建工厂,也可以在同一个工厂中编写方法来创建表、填充表、从表中检索数据。
通过这样做,所有与数据库相关的代码都在一个地方。
以下是您可以在应用程序中实现 lokijs 的一些操作。
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;
}]);
你可以在你的应用程序中实现这样的事情。
希望这对您有所帮助。