Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
sets validateUpsert to false with tests for upsert
Browse files Browse the repository at this point in the history
This fixes #10 by adding in the tests from @0ff which then required
that we cannot yet support the upsert validation.  Upsert is causing
validation to happen before our observer can run which makes the
required timestamps to fail the presence validation.
  • Loading branch information
clarkbw committed Jul 22, 2015
1 parent 2c5b89c commit 93e8df0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
6 changes: 6 additions & 0 deletions es6/time-stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export default (Model, options = {}) => {

debug('options', options);

debug('Model.settings.validateUpsert', Model.settings.validateUpsert);
if (Model.settings.validateUpsert && options.required) {
console.warn('TimeStamp mixin requires validateUpsert be false. See @clarkbw/loopback-ds-timestamp-mixin#10');
}
Model.settings.validateUpsert = false;

Model.defineProperty(options.createdAt, {type: Date, required: options.required, defaultFn: 'now'});
Model.defineProperty(options.updatedAt, {type: Date, required: options.required});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"debug": "2.x"
},
"peerDependencies": {
"loopback-datasource-juggler": ">=2.23.0"
"loopback-datasource-juggler": ">=2.33.2"
},
"devDependencies": {
"babel-core": "latest",
Expand Down
24 changes: 16 additions & 8 deletions test/test-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ test('loopback datasource timestamps', function(tap) {
});

t.test('should not change on save', function(tt) {
var createdAt;
Book.destroyAll(function() {
Book.create({name:'book 1', type:'fiction'}, function(err, book) {
tt.error(err);
tt.ok(book.createdAt);
createdAt = book.createdAt;
book.name = 'book inf';
book.save(function(err, b) {
tt.equal(book.createdAt, b.createdAt);
Expand All @@ -51,7 +49,6 @@ test('loopback datasource timestamps', function(tap) {
});

t.test('should not change on update', function(tt) {
var createdAt;
Book.destroyAll(function() {
Book.create({name:'book 1', type:'fiction'}, function(err, book) {
tt.error(err);
Expand All @@ -65,6 +62,20 @@ test('loopback datasource timestamps', function(tap) {
});
});

t.test('should not change on upsert', function(tt) {
Book.destroyAll(function() {
Book.create({name:'book 1', type:'fiction'}, function(err, book) {
tt.error(err);
tt.ok(book.createdAt);
Book.upsert({id: book.id, name:'book inf'}, function(err, b) {
tt.error(err);
tt.equal(book.createdAt.getTime(), b.createdAt.getTime());
tt.end();
});
});
});
});

t.test('should not change with bulk updates', function(tt) {
var createdAt;
Book.destroyAll(function() {
Expand Down Expand Up @@ -224,22 +235,19 @@ test('loopback datasource timestamps', function(tap) {
);

t.test('should skip changing updatedAt when option passed', function(tt) {
var updated, book;
Book.destroyAll(function() {
Book.create({name:'book 1', type:'fiction'}, function(err, book1) {
tt.error(err);

tt.ok(book1.updatedAt);

updated = book1.updatedAt;
book = book1.toObject();
book.name = 'book 2';
var book = {id: book1.id, name:'book 2'};

Book.updateOrCreate(book, {skipUpdatedAt: true}, function(err, book2) {
tt.error(err);

tt.ok(book2.updatedAt);
tt.equal(updated.getTime(), book2.updatedAt.getTime());
tt.equal(book1.updatedAt.getTime(), book2.updatedAt.getTime());
tt.end();
});

Expand Down
24 changes: 16 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ test('loopback datasource timestamps', function(tap) {
});

t.test('should not change on save', function(tt) {
var createdAt;
Widget.destroyAll(function() {
Widget.create({name:'book 1', type:'fiction'}, function(err, book) {
tt.error(err);
tt.ok(book.createdAt);
createdAt = book.createdAt;
book.name = 'book inf';
book.save(function(err, b) {
tt.equal(book.createdAt, b.createdAt);
Expand All @@ -39,7 +37,6 @@ test('loopback datasource timestamps', function(tap) {
});

t.test('should not change on update', function(tt) {
var createdAt;
Widget.destroyAll(function() {
Widget.create({name:'book 1', type:'fiction'}, function(err, book) {
tt.error(err);
Expand All @@ -53,6 +50,20 @@ test('loopback datasource timestamps', function(tap) {
});
});

t.test('should not change on upsert', function(tt) {
Widget.destroyAll(function() {
Widget.create({name:'book 1', type:'fiction'}, function(err, book) {
tt.error(err);
tt.ok(book.createdAt);
Widget.upsert({id: book.id, name:'book inf'}, function(err, b) {
tt.error(err);
tt.equal(book.createdAt.getTime(), b.createdAt.getTime());
tt.end();
});
});
});
});

t.test('should not change with bulk updates', function(tt) {
var createdAt;
Widget.destroyAll(function() {
Expand Down Expand Up @@ -205,22 +216,19 @@ test('loopback datasource timestamps', function(tap) {
tap.test('operation hook options', function(t) {

t.test('should skip changing updatedAt when option passed', function(tt) {
var updated, book;
Widget.destroyAll(function() {
Widget.create({name:'book 1', type:'fiction'}, function(err, book1) {
tt.error(err);

tt.ok(book1.updatedAt);

updated = book1.updatedAt;
book = book1.toObject();
book.name = 'book 2';
var book = {id: book1.id, name:'book 2'};

Widget.updateOrCreate(book, {skipUpdatedAt: true}, function(err, book2) {
tt.error(err);

tt.ok(book2.updatedAt);
tt.equal(updated.getTime(), book2.updatedAt.getTime());
tt.equal(book1.updatedAt.getTime(), book2.updatedAt.getTime());
tt.end();
});

Expand Down
6 changes: 6 additions & 0 deletions time-stamp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion time-stamp.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93e8df0

Please sign in to comment.