Merge pull request #1148 from strongloop/fix/juggler-deprecation-warnings

Replace deprecated hooks with Operation hooks
This commit is contained in:
Miroslav Bajtoš 2015-03-03 18:27:59 +01:00
commit add9304555
3 changed files with 27 additions and 16 deletions

View File

@ -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`.

View File

@ -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

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