vuex-orm LokiJS persistance (Local Storage) plugin, with optional AES encryption.
npm install @nshcore/vuex-orm-plugin-lokijs
* We needed a persistent Local Storage based database.
* VuexORM is a ORM tool for Vuex, this plugin adds the ability to use LokiJS as a persistant store. When you initialize your VuexORM database, the plugin will hydrate VuexORM with data stored in Local Storage (preserving original IDs).
Only some of VuexORM model methods are currently implemented. There is no support for VuexORM relationships, you have to manage that yourself. Feel free to contribute!
import MyModel from './models/MyModel';
const data = { name: 'Bob' };
MyModel.$insert({ data }); // insert data using $insert() instead of regular insert()
// retrieve data using regular VuexORM query()
const bob = MyModel.query().where('name', 'Bob').get();
MyModel.$update({
where: record => record.name === 'Bob',
data: { name: 'Bobby' },
});
const bobby = MyModel.query().where('name', 'Bobby').get();
MyModel.$delete(bobby.id);
import MyModel from './models/MyModel';
MyModel.$update({
where: (record) => { return record.id === id; },
data: { example: 'data' },
});
import Vue from 'vue';
import Vuex from 'vuex';
import VuexORM from '@vuex-orm/core';
import VuexORMLoki from 'vuex-orm-lokijs';
import { Model } from '@vuex-orm/core';
// define your VuexORM model
class MyModel extends Model {
static entity = 'MyModel';
static fields() {
return {
id: this.increment(),
data: this.attr(''),
};
}
}
Vue.use(Vuex);
const database = new VuexORM.Database();
database.register(MyModel, {});
const options = {
env: 'browser',
};
function hydrationCompletedCallback() {
// data from LocalStorage has been fully loaded into VuexORM
}
VuexORM.use(VuexORMLoki, { database, options, hydrationCompletedCallback });
const store = new Vuex.Store({
plugins: [VuexORM.install(database)],
});
Encryption currently only works on $insert()
.
import { Model } from '@vuex-orm/core';
export default MyModel extends Model {
static entity = 'MyModel';
static AES = ['secretProperty1', 'secretProperty2']; // tell the plugin what to encrypt
static fields() {
return {
id: this.increment(),
name: this.attr(),
secretProperty1: this.attr(),
secretProperty2: this.attr(),
};
}
}
import MyModel from './models/MyModel';
const data = {
name: 'Bob',
secretProperty1: 'Very sensitive data',
secretProperty2: 'Even more',
};
const password = "your password";
/*
* before being put into the database, the data
* will be encrypted with AES using the password
*/
MyModel.$insert({ data, password }); // pass the password
import MyModel from './models/MyModel';
const data = {
name: 'Bob',
secretProperty1: 'Very sensitive data',
secretProperty2: 'Even more',
};
const password = "your password";
/*
* before being put into the database, the data
* will be encrypted with AES using the password
*/
MyModel.$update({ data, password }); // pass the password
import AES from 'crypto-js/aes';
import encUTF8 from 'crypto-js/enc-utf8';
import MyModel from './models/MyModel';
function decrypt(data, password) {
const bytes = AES.decrypt(data, password);
return JSON.parse(bytes.toString(encUTF8));
}
const bob = MyModel.query().where('name', 'Bob').get();
MyModel.AES.forEach((key) => {
bob[key] = decrypt(bob[key], password);
});
Details changes for each release are documented in the CHANGELOG.md.
Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.
Please make sure to read the Contributing Guide before making a pull request.
Here are the wonderfull soles who contribute to this project
nshCore 💻 💡 🤔 🚇 🔌 |