diff --git a/Changelog.md b/Changelog.md index 70d84a99..fe2a89c7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,6 @@ +### v7.1.0 +- Add `isDirty` function to instances ([859](../../pull/859)) + ### v7.0.0 - Update `async` package from v2 to v3 to resolve security vulnerabilities ([858](../../pull/858)) - Drop support for node < 6 (due to `async` package update) diff --git a/lib/Instance.js b/lib/Instance.js index 2ad70443..24da4724 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -670,6 +670,12 @@ function Instance(Model, opts) { get: function () { return opts.changes; }, enumerable: false }); + Object.defineProperty(instance, "isDirty", { + value: function () { + return opts.changes.length > 0; + }, + enumerable: false + }); Object.defineProperty(instance, "isInstance", { value: true, enumerable: false diff --git a/package-lock.json b/package-lock.json index 7439e127..66013c86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "orm", - "version": "7.0.0", + "version": "7.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7c38938a..b2fa2785 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "sqlite", "mongodb" ], - "version": "7.0.0", + "version": "7.1.0", "license": "MIT", "homepage": "http://dresende.github.io/node-orm2", "repository": "http://github.com/dresende/node-orm2.git", diff --git a/test/integration/instance.js b/test/integration/instance.js index 363d7dee..eb49a30d 100644 --- a/test/integration/instance.js +++ b/test/integration/instance.js @@ -303,6 +303,35 @@ describe("Model instance", function() { }); }); + describe("#isDirty", function () { + var person = null; + + beforeEach(function (done) { + Person.create({ name: 'John', age: 44, data: { a: 1 } }, function (err, p) { + if (err) return cb(err); + + person = p; + done(); + }); + }); + + it("should return false by default", function () { + should.equal(person.isDirty(), false); + }); + + it("should return false when property is set to same value", function () { + should.equal(person.isDirty(), false); + person.name = 'John'; + should.equal(person.isDirty(), false); + }); + + it("should return true when property is changed", function () { + should.equal(person.isDirty(), false); + person.name = 'Bob'; + should.equal(person.isDirty(), true); + }); + }); + describe("#isShell", function () { it("should return true for shell models", function () { should.equal(Person(4).isShell(), true);