I have tried the sample website code (documentation) as several I found. But I did not function the event capture either with constructor () or with initialize ().
If there is an application that uses the events or if you have a detailed example of capturing each event I would be of help.
//My Model usuario.js
var bookshelf = require("./ormConf"); //instancia de bookshelf con confiraciones de bd y plugins
var Usuario = module.exports = bookshelf.Model.extend({
tableName: 'usuarios',
//idAttribute:'id',
hasTimestamps: ['createdat', 'updatedat', 'deletedat'],
//Declarar constructor para manejo de eventos
//Initialize produces error: Maximum call stack size exceeded
constructor: function() {
bookshelf.Model.apply(this, arguments);
this.on('fetching', function(model, attributes, options) {
console.log("\nEvento fetching!!!\n");
})
});
...
//My controller usuario.js
var usuarioModel = require("../models/usuarios"); //importando modelo de usuario
module.exports = {
list: function list(req, res) {
return usuarioModel.fetchAll().then(function (registros) {
res.status(200).json(registros);
}).catch(function (err) {
res.status(400).json(err);
})
}
...
My route usuario.js
var express = require('express');
var app = express();
var router = express.Router();
var usuarioModel = require("../models/usuarios");
...
router.route('/usuarios').get(function (req, res) {
//Metodo GET para obtener informaicon de varios usuarios
console.log("\nEjecucion de metodo get() para ruta: /usuarios\n");
var query = req.query;
var page = query.page;
var limit = query.limit;
var visibleFields = query.visibleFields;
// Query DB para usuarios y envio de resultados
var usuarios = usuarioController.list(req,res);
});
...
I am interested in capturing all events involving CRUD events. Including one triggered by fetchAll() or fetch() but I do not know if I have create collection for fetchAll() or catch up with running it from a model.
Node.js v6.9.5
Express v4.13.4
Bookhself.js 0.10.3
Knex CLI version: 0.12.6
Local Knex version: 0.12.8
Postgres 9.5
I would like this to be added to the documentation. There is the list of events, but I don't (or didn't) know what to return or how to manipulate the flow of the events.
So far, I've used promises, I don't know if this is the best practice, or if it's the only way to do it, but I saw it somewhere, I think:
initialize: function () {
this.on('updating', (model, affectedRows, options) => {
return new Promise((resolve, reject) => {
// do some stuff
// ...
// call resolve() if you want to continue with the event, in this case,
// it would update the model; or call reject() to stop it, leaving the model
// as it was.
})
})
}
At some point it seems a good idea to generate audit trails / logs: #1535, but do not take that path through models. It has some possible uses as previous validations to save, apply filters in case of retrieving information from a model or generate updates / eliminations in cascade or apply restrictions. But it would be interesting to consider the transaction object in these events.
var Promise = require('bluebird');
var bookshelf = require("../config/ormConf");
var Menu = module.exports = bookshelf.Model.extend({
tableName: 'menues',
hasTimestamps: ['created_at', 'updated_at', 'deleted_at'],
constructor: function() {
bookshelf.Model.apply(this, arguments);
this.on("fetching", function(model, attributes, options) {
//options.query.where('type', '=', 'menu');
console.log('\nSe intenta consultar informacion de menu...\n');
})
this.on("fetched", function(model, attributes, options) {
//options.query.where('type', '=', 'menu');
console.log('\nSe consulto informacion de menu exitosamente\n');
})
this.on("creating", function(model, attrs, options) {
//options.query.where('type', '=', 'book');
console.log('\nSe intenta agregar un nuevo...\n');
});
this.on("created", function(model, attrs, options) {
//options.query.where('type', '=', 'book');
console.log('\nSe agrego un nuevo menu con exito...\n');
});
this.on('updating', function(model, attrs, options) {
//options.query.where('type', '=', 'book');
console.log('\nSe modificara informacion de Menu\n');
console.log('Campos a modificar:');
console.log(attrs);
});
this.on("updated", function(model, affectedRows, options) {
//options.query.where('type', '=', 'book');
console.log('\nSe modifico informacion de Menu con exito...\n');
console.log('\nNumero de Filas Afectadas: ', affectedRows);
// console.log('\nAtributos:\n', model.get('attributes'));
console.log('\nAtributos previos:\n', this._previousAttributes);
console.log('\nAtributos cambiados:\n', this.get('changed'));
})
this.on("saving", function(model, attrs, options) {
//options.query.where('type', '=', 'book');
if (options.method == 'update') {
if (options.patch == true) {
console.log('\nSe intenta actualizar solo algunos campos...\n');
} else {
console.log('\nSe intenta actualizar todos los campos...\n');
}
} else {
console.log('\nSe intenta agregar...\n');
//VALIDACION DE USUARIO A REGISTRAR
return Menu.validar(this);
}
});
this.on("saved", function(model, resp, options) {
//options.query.where('type', '=', 'book');
if (options.method == 'update') {
if (options.patch == true) {
console.log('\nSe actualizaron solo algunos campos...\n');
} else {
console.log('\nSe actualizaron todos los campos...\n');
}
} else {
console.log('\nSe intenta agregar...\n');
//VALIDACION DE USUARIO A REGISTRAR
return Menu.validar(this);
}
if (options.transacting) {
console.log('\nOperacion dentro de una transaccion ...\n');
}
console.log('\nRespuesta de la base de datos: ', resp, '\n');
});
this.on('destroying', function (model, attrs, options) {
//options.query.where('type', '=', 'book');
console.log('se elimino registro de un menu...');
});
this.on('destroyed', function (model, attrs, options) {
//options.query.where('type', '=', 'book');
//Auditar "Hard" DELETE en Model
console.log('se elimino registro de un menu...');
});
},
{
orderBy: function(column, order){
return this.query(function(qb){
return qb.orderBy(column, order);
});
}
});
I don't understand what you're trying to get at. Is that an example on how to solve #1535?
Most helpful comment
I would like this to be added to the documentation. There is the list of events, but I don't (or didn't) know what to return or how to manipulate the flow of the events.
So far, I've used promises, I don't know if this is the best practice, or if it's the only way to do it, but I saw it somewhere, I think: