https://github.com/whitfin/loki-titanium-adapter
Titanium SDK adapter for the LokiJS embedded database
https://github.com/whitfin/loki-titanium-adapter
database json mobile nosql titanium-mobile titanium-sdk
Last synced: about 1 year ago
JSON representation
Titanium SDK adapter for the LokiJS embedded database
- Host: GitHub
- URL: https://github.com/whitfin/loki-titanium-adapter
- Owner: whitfin
- License: mit
- Created: 2019-05-31T00:26:32.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2023-11-14T08:34:11.000Z (over 2 years ago)
- Last Synced: 2024-03-15T10:51:54.767Z (over 2 years ago)
- Topics: database, json, mobile, nosql, titanium-mobile, titanium-sdk
- Language: JavaScript
- Homepage:
- Size: 8.06 MB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# loki-titanium-adapter
[](https://github.com/whitfin/loki-titanium-adapter/actions) [](https://npmjs.com/package/loki-titanium-adapter) [](https://npmjs.com/package/loki-titanium-adapter)
Titanium SDK adapter for the [Loki](https://github.com/techfort/LokiJS)
embedded database.
You can use this library to persist Loki databases inside Titanium applications
using the Titanium API. Everything else operates as it would in any other runtime,
so visit the [Loki documentation](https://github.com/techfort/LokiJS) for further
information.
### Usage
This module is on npm, so feel free to grab from there (as well as Loki):
```shell
$ npm i lokijs loki-titanium-adapter
```
You can then configure it inside your application pretty easily, as the
API is still synchronous for the time being:
```javascript
// Load our modules
const Loki = require('lokijs');
const TitaniumAdapter = require('loki-titanium-adapter');
// Construct our database instance
const db = new Loki('my-database', {
adapter: new TitaniumAdapter({
parent: 'data', // subdirectory in app data
reader: {
buffer: 1024 * 1024 // max buffer during disk reads
},
writer: {
batch: 25 // number of documents to write in batch
}
}),
autoload: true,
autosave: true,
autosaveInterval: 5000,
autoloadCallback: function () {
// called when your database is loaded
},
});
```
This will save your database changes to disk every 5 seconds (configured via
the `autosaveInterval` parameter). In addition to this, I recommend adding a
hook for the `pause` event to flush when the application is closed:
```javascript
// Add a pause listener to ensure flush on background
Ti.App.addEventListener('pause', function (_e) {
db.saveDatabase(function (e) {
// documents should have been flushed
});
});
```
There is also a more complete [example app](example/) available - just keep
in mind that you need to run `npm install` before you try to run it. For any
further use, check out [Loki wiki](https://github.com/techfort/LokiJS/wiki)
as the API is exactly the same.