Skip to content

Commit

Permalink
Add versions (explicit incrementation) and beefed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Cummins committed Feb 6, 2019
1 parent e9b48cc commit 6a3b29f
Show file tree
Hide file tree
Showing 20 changed files with 9,178 additions and 731 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ indent_style = spaces
curly_bracket_next_line = false
spaces_around_operators = true
indent_brace_style = 1TBS
spaces_around_brackets = boths
spaces_around_brackets = boths
6 changes: 2 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"parser": "babel-eslint",
"root": true,
// "extends": "airbnb/base",
// "extends": "airbnb/base",
"plugins": [
"flow-vars",
"flowtype"
],
"env": {
Expand All @@ -28,8 +28,6 @@
"quotes": [1, "single"],
"curly": 2,
"semi": [1, "always"],
"flow-vars/define-flow-type": 2,
"flow-vars/use-flow-type": 2,
"flowtype/require-parameter-type": 2,
"flowtype/require-return-type": [
"warn",
Expand Down
3 changes: 0 additions & 3 deletions .eslintstrict
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"root": true,
"extends": "airbnb/base",
"plugins": [
"flow-vars",
"flowtype"
],
"env": {
Expand All @@ -17,8 +16,6 @@
"quotes": [2, "single"],
"curly": 2,
"semi": [2, "always"],
"flow-vars/define-flow-type": 2,
"flow-vars/use-flow-type": 2,
"flowtype/require-parameter-type": 2,
"flowtype/require-return-type": [
2,
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ node_modules

# IDE directory
.idea

# SQLite test db
test.db
5 changes: 4 additions & 1 deletion .npmingore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ npm-debug.log*
node_modules

# Configurations
.gitignore
.gitignore

# SQLite test db
test.db
115 changes: 0 additions & 115 deletions 20160826094924-sequelize-paper-trail-migration.js

This file was deleted.

25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ npm install --save sequelize-paper-trail
```

*Note: the current test suite is very limited in coverage.*
*Although it's getting better...*

## Usage

Expand Down Expand Up @@ -129,6 +130,25 @@ Model.update({
To enable continuation-local-storage set `continuationNamespace` in initialization options.
Additionally, you may also have to call `.run()` or `.bind()` on your cls namespace, as described in the [docs](https://www.npmjs.com/package/continuation-local-storage).

## Versions

PaperTrail also tracks versions, which are explicitly incremented, and reset the revision. Documents start at version 1, revision 0, and every update, the revision increments. But if the version is incremented, the revision is reset to 0.

```javascript
let instance = Model.findOne({where: {[criteria]}).then(() => {
console.log(instance.get( {plain: true} ));
instance.set({[many changes...]});
Model.newVersion(instance).then(() => {
instance.reload().then(() => {
console.log(instance.get( {plain: true} ));
});
});
});

```
In the above example, the final revision should be 0, and the version should be incremented by one from the original value
## Options
Paper Trail supports various options that can be passed into the initialization. The following are the default options:
Expand Down Expand Up @@ -156,7 +176,8 @@ var options = {
underscoredAttributes: false,
defaultAttributes: {
documentId: 'documentId',
revisionId: 'revisionId'
revisionId: 'revisionId'
versionId: 'versionId'
},
enableCompression: false,
enableMigration: false,
Expand All @@ -178,7 +199,7 @@ var options = {
| [UUID] | Boolean | false | The [revisionModel] has id attribute of type UUID for postgresql. |
| [underscored] | Boolean | false | The [revisionModel] and [revisionChangeModel] have 'createdAt' and 'updatedAt' attributes, by default, setting this option to true changes it to 'created_at' and 'updated_at'. |
| [underscoredAttributes] | Boolean | false | The [revisionModel] has a [defaultAttribute] 'documentId', and the [revisionChangeModel] has a [defaultAttribute] 'revisionId, by default, setting this option to true changes it to 'document_id' and 'revision_id'. |
| [defaultAttributes] | Object | { documentId: 'documentId', revisionId: 'revisionId' } | |
| [defaultAttributes] | Object | { documentId: 'documentId', revisionId: 'revisionId', versionId: 'versionId' } | |
| [userModel] | String | | Name of the model that stores users in your. |
| [enableCompression] | Boolean | false | Compresses the revision attribute in the [revisionModel] to only the diff instead of all model attributes. |
| [enableMigration] | Boolean | false | Automatically adds the [revisionAttribute] via a migration to the models that have paper trails enabled. |
Expand Down
10 changes: 10 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"test": {
"username": "root",
"password": null,
"database": "database_test",
"storage": "test.db",
"dialect": "sqlite",
"logging": false
}
}
86 changes: 44 additions & 42 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
Object.defineProperty(exports, '__esModule', {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
const _typeof =
typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
function (obj: Object): String { return typeof obj; } :
function (obj: Object): String {
return obj && typeof Symbol === 'function' && obj.constructor === Symbol ? 'symbol' : typeof obj;
};

var Sequelize = require('sequelize');
var diff = require('deep-diff').diff;
var jsdiff = require('diff');
var _ = require('lodash');
// const Sequelize = require('sequelize');
const diff = require('deep-diff').diff;
// const jsdiff = require('diff');
const _ = require('lodash');

exports.default = {
test: function test() {
return true;
},
calcDelta: function calcDelta(current, next, exclude, strict) {
var DEBUG = false;
calcDelta: function calcDelta (current: Object, next: Object, exclude: Object, strict: Boolean): Object {
const DEBUG = false;
if (DEBUG) {
console.log('current', current);
console.log('next', next);
console.log('exclude', exclude);
}

var di = diff(current, next);
let di = diff(current, next);

if (DEBUG) {
console.log('di', di);

var diffs2 = di ? di.map(function (i) {
var str = JSON.stringify(i).replace("\"__data\",", "");
let diffs2 = di ? di.map(function (i: Object): Object {
let str = JSON.stringify(i).replace('"__data",', '');
return JSON.parse(str);
}) : [];
console.log('diffs2', diffs2);

console.log('filter1', diffs2.filter(function (i) {
return i.path.join(",").indexOf("_") === -1;
console.log('filter1', diffs2.filter(function (i: Object): Boolean {
return i.path.join(',').indexOf('_') === -1;
}));

console.log('filter2', diffs2.filter(function (i) {
return exclude.every(function (x) {
console.log('filter2', diffs2.filter(function (i: Object): Object {
return exclude.every(function (x: Object): Boolean {
return i.path.indexOf(x) === -1;
});
}));
}

var diffs = di ? di.map(function (i) {
var str = JSON.stringify(i).replace("\"__data\",", "");
let diffs = di ? di.map(function (i: Object): Object {
let str = JSON.stringify(i).replace('"__data",', '');
return JSON.parse(str);
}).filter(function (i) {
}).filter(function (i: Object): Object {
// return i.path.join(",").indexOf("_") === -1;
// console.log('i', i)
if (!strict && i.kind === 'E') {
Expand All @@ -60,9 +62,9 @@ exports.default = {
} else {
return i;
}
}).filter(function (i) {
}).filter(function (i: Object): Array {
// console.log('i', i);
return exclude.every(function (x) {
return exclude.every(function (x: Integer): Boolean {
return i.path.indexOf(x) === -1;
});
}) : [];
Expand All @@ -72,34 +74,34 @@ exports.default = {
return null;
}
},
capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
capitalizeFirstLetter (string: String): String {
return string.charAt(0).toUpperCase() + string.slice(1);
},
diffToString: function diffToString(val) {
if (typeof val === "undefined" || val === null) {
return "";
diffToString: function diffToString (val: Object): String {
if (typeof val === 'undefined' || val === null) {
return '';
} else if (val === true) {
return "1";
return '1';
} else if (val === false) {
return "0";
} else if (typeof val === "string") {
return '0';
} else if (typeof val === 'string') {
return val;
} else if (!isNaN(val)) {
return String(val) + "";
} else if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === "object") {
return JSON.stringify(val) + "";
return String(val) + '';
} else if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object') {
return JSON.stringify(val) + '';
} else if (Array.isArray(val)) {
return JSON.stringify(val) + "";
return JSON.stringify(val) + '';
}
return "";
return '';
},
toUnderscored: function toUnderscored(obj) {
_.forEach(obj, function (k, v) {
obj[k] = v.replace(/(?:^|\.?)([A-Z])/g, function (x, y) {
return "_" + y.toLowerCase();
}).replace(/^_/, "");
toUnderscored: function toUnderscored (obj: Object): Object {
_.forEach(obj, function (k: String, v: Object): undefined {
obj[k] = v.replace(/(?:^|\.?)([A-Z])/g, function (x: String, y: String): String {
return '_' + y.toLowerCase();
}).replace(/^_/, '');
});
return obj;
}
};
module.exports = exports['default'];
module.exports = exports.default;
Loading

0 comments on commit 6a3b29f

Please sign in to comment.