diff --git a/common/models/access-token.js b/common/models/access-token.js index c9fbe95a..73137125 100644 --- a/common/models/access-token.js +++ b/common/models/access-token.js @@ -65,20 +65,18 @@ module.exports = function(AccessToken) { /*! * Hook to create accessToken id. */ - - AccessToken.beforeCreate = function(next, data) { - data = data || {}; + AccessToken.observe('before save', function(ctx, next) { + if (!ctx.instance || ctx.instance.id) { + // We are running a partial update or the instance already has an id + return next(); + } AccessToken.createAccessTokenId(function(err, id) { - if (err) { - next(err); - } else { - data.id = id; - - next(); - } + if (err) return next(err); + ctx.instance.id = id; + next(); }); - }; + }); /** * Find a token for the given `ServerRequest`. diff --git a/common/models/application.js b/common/models/application.js index 1f6148ff..a7f0dfa8 100644 --- a/common/models/application.js +++ b/common/models/application.js @@ -81,8 +81,15 @@ module.exports = function(Application) { * A hook to generate keys before creation * @param next */ - Application.beforeCreate = function(next) { - var app = this; + Application.observe('before save', function(ctx, next) { + if (!ctx.instance) { + // Partial update - don't generate new keys + // NOTE(bajtos) This also means that an atomic updateOrCreate + // will not generate keys when a new record is creatd + return next(); + } + + var app = ctx.instance; app.created = app.modified = new Date(); app.id = generateKey('id', 'md5'); app.clientKey = generateKey('client'); @@ -91,7 +98,7 @@ module.exports = function(Application) { app.windowsKey = generateKey('windows'); app.masterKey = generateKey('master'); next(); - }; + }); /** * Register a new application diff --git a/common/models/checkpoint.js b/common/models/checkpoint.js index 2bba736a..baec3bd8 100644 --- a/common/models/checkpoint.js +++ b/common/models/checkpoint.js @@ -49,7 +49,13 @@ module.exports = function(Checkpoint) { }); }; - Checkpoint.beforeSave = function(next, model) { + Checkpoint.observe('before save', function(ctx, next) { + if (!ctx.instance) { + // Example: Checkpoint.updateAll() and Checkpoint.updateOrCreate() + return next(new Error('Checkpoint does not support partial updates.')); + } + + var model = ctx.instance; if (!model.getId() && model.seq === undefined) { model.constructor.current(function(err, seq) { if (err) return next(err); @@ -59,5 +65,5 @@ module.exports = function(Checkpoint) { } else { next(); } - }; + }); };