Replace deprecated hooks with Operation hooks

AccessToken.beforeCreate -> AccessToken.observe('before save')
Application.beforeCreate -> Application.observe('before save')
Checkpoint.beforeSave -> Checkpoint.observe('before save')
This commit is contained in:
Miroslav Bajtoš 2015-03-02 13:09:14 +01:00
parent f857f44cae
commit e20cc66787
3 changed files with 27 additions and 16 deletions

View File

@ -65,20 +65,18 @@ module.exports = function(AccessToken) {
/*! /*!
* Hook to create accessToken id. * Hook to create accessToken id.
*/ */
AccessToken.observe('before save', function(ctx, next) {
AccessToken.beforeCreate = function(next, data) { if (!ctx.instance || ctx.instance.id) {
data = data || {}; // We are running a partial update or the instance already has an id
return next();
}
AccessToken.createAccessTokenId(function(err, id) { AccessToken.createAccessTokenId(function(err, id) {
if (err) { if (err) return next(err);
next(err); ctx.instance.id = id;
} else { next();
data.id = id;
next();
}
}); });
}; });
/** /**
* Find a token for the given `ServerRequest`. * Find a token for the given `ServerRequest`.

View File

@ -81,8 +81,15 @@ module.exports = function(Application) {
* A hook to generate keys before creation * A hook to generate keys before creation
* @param next * @param next
*/ */
Application.beforeCreate = function(next) { Application.observe('before save', function(ctx, next) {
var app = this; 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.created = app.modified = new Date();
app.id = generateKey('id', 'md5'); app.id = generateKey('id', 'md5');
app.clientKey = generateKey('client'); app.clientKey = generateKey('client');
@ -91,7 +98,7 @@ module.exports = function(Application) {
app.windowsKey = generateKey('windows'); app.windowsKey = generateKey('windows');
app.masterKey = generateKey('master'); app.masterKey = generateKey('master');
next(); next();
}; });
/** /**
* Register a new application * Register a new application

View File

@ -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) { if (!model.getId() && model.seq === undefined) {
model.constructor.current(function(err, seq) { model.constructor.current(function(err, seq) {
if (err) return next(err); if (err) return next(err);
@ -59,5 +65,5 @@ module.exports = function(Checkpoint) {
} else { } else {
next(); next();
} }
}; });
}; };