@atlascity/vuex-orm-plugin-lokijs
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

vuex-orm-plugin-loki

npm version vue2

vuex-orm LokiJS persistance (Local Storage) plugin, with optional AES encryption.

Installation

npm install @atlascity/vuex-orm-plugin-lokijs

Why & How

* 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).

Caveats

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!

Examples

$all

$insert

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();

$update

MyModel.$update({
  where: record => record.name === 'Bob',
  data: { name: 'Bobby' },
});

$delete

const bobby = MyModel.query().where('name', 'Bobby').get();
MyModel.$delete(bobby.id);

$update

import MyModel from './models/MyModel';

MyModel.$update({
  where: (record) => { return record.id === id; },
  data: { example: 'data' },
});

Usage

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

Encryption currently only works on $insert().

Setup the model

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(),
    };
  }
}

Insert data with 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.$insert({ data, password }); // pass the password

Update data with 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

Retrieve data and decrypt

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);
});

📜 Changelog

Details changes for each release are documented in the CHANGELOG.md.

Issues

Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

💪 Contribution

Please make sure to read the Contributing Guide before making a pull request.

Contributors

Here are the wonderfull soles who contribute to this project

nshCore
nshCore

💻 💡 🤔 🚇 🔌
Konrad Moskal
Konrad Moskal

💻 🤔 👀

©️ License

GPL-2.0-only

Package Sidebar

Install

npm i @atlascity/vuex-orm-plugin-lokijs

Weekly Downloads

2

Version

0.0.3

License

GPL-2.0-only

Unpacked Size

204 kB

Total Files

39

Last publish

Collaborators

  • nshcore