如何在创建钩子 CALL a mysql 函数之前进行 FEATHERS-sequelize
How to FEATHERS-sequelize before create hook CALL a mysql function
我正在尝试使用 feathersjs-sequelize 和 mysql 数据库重现 Java/Hibernate/TABLE 序列功能 https://dzone.com/articles/hibernate-identity-sequence。
因此,我在 mysql
中创建了以下函数
FUNCTION `generate_pessoa_seq`() RETURNS bigint(16)
BEGIN
DECLARE R_ID BIGINT(16);
UPDATE pessoa_seq SET next_value = next_value + 1;
SELECT next_value INTO R_ID FROM pessoa_seq;
RETURN R_ID;
之后我在 mysql cli 中测试:
mysql> select generate_pessoa_seq();
+-----------------------+
| generate_pessoa_seq() |
+-----------------------+
| 11 |
+-----------------------+
1 row in set (0,00 sec)
好的。现在我可以使用 11 作为我的 table 的 ID 值 "pessoa".
所以我试着按照下面的想法(pessoa hook)
const pessoaBeforeCreateHook = options => { // always wrap in a function so you can pass options and for consistency.
return hook => {
console.log('Pessoa hook ' + hook);
sequelize = hook.app.get('sequelize');
var newId = -1; //forcing an error
sequelize.query('select generate_pessoa_seq();',{ type: sequelize.QueryTypes.SELECT}).then(function(response){
//getNewId();
newId = response;
}).error(function(err){
console.log(err);
newId=-1;
});
hook.data.IdiPessoa=newId;
return Promise.resolve(hook); // A good convention is to always return a promise.
};
};
exports.before = {
all: [],
find: [],
get: [],
create: [pessoaBeforeCreateHook()],
update: [],
patch: [],
remove: []
};
感谢您的帮助。
daffl 说:
Sequelize 查询 return 是一个 promise 并且 运行 是异步的,因此您想要 运行 一旦它解析的任何逻辑都需要在 .then 处理程序中。您还必须 return 钩子函数的承诺,以便它会等到它完成:
所以,最终代码在这里:
pessoaService.hooks({
before: {
create(hook) {
const sequelize = app.get('sequelize');
var newId = -1; //forcing an error
return sequelize.query('select generate_pessoa_seq() as nextId;',{
nest: true,
raw: true }).then(function(response){
//getNewId();
var stringId = response[0];
newId = parseInt(stringId.nextId, 10);
hook.data.IdiPessoa=newId;
return hook;
}).error(function(err){
console.log(err);
return hook;
});
}
}
});
我正在尝试使用 feathersjs-sequelize 和 mysql 数据库重现 Java/Hibernate/TABLE 序列功能 https://dzone.com/articles/hibernate-identity-sequence。
因此,我在 mysql
中创建了以下函数FUNCTION `generate_pessoa_seq`() RETURNS bigint(16)
BEGIN
DECLARE R_ID BIGINT(16);
UPDATE pessoa_seq SET next_value = next_value + 1;
SELECT next_value INTO R_ID FROM pessoa_seq;
RETURN R_ID;
之后我在 mysql cli 中测试:
mysql> select generate_pessoa_seq();
+-----------------------+
| generate_pessoa_seq() |
+-----------------------+
| 11 |
+-----------------------+
1 row in set (0,00 sec)
好的。现在我可以使用 11 作为我的 table 的 ID 值 "pessoa".
所以我试着按照下面的想法(pessoa hook)
const pessoaBeforeCreateHook = options => { // always wrap in a function so you can pass options and for consistency.
return hook => {
console.log('Pessoa hook ' + hook);
sequelize = hook.app.get('sequelize');
var newId = -1; //forcing an error
sequelize.query('select generate_pessoa_seq();',{ type: sequelize.QueryTypes.SELECT}).then(function(response){
//getNewId();
newId = response;
}).error(function(err){
console.log(err);
newId=-1;
});
hook.data.IdiPessoa=newId;
return Promise.resolve(hook); // A good convention is to always return a promise.
};
};
exports.before = {
all: [],
find: [],
get: [],
create: [pessoaBeforeCreateHook()],
update: [],
patch: [],
remove: []
};
感谢您的帮助。
daffl 说:
Sequelize 查询 return 是一个 promise 并且 运行 是异步的,因此您想要 运行 一旦它解析的任何逻辑都需要在 .then 处理程序中。您还必须 return 钩子函数的承诺,以便它会等到它完成:
所以,最终代码在这里:
pessoaService.hooks({
before: {
create(hook) {
const sequelize = app.get('sequelize');
var newId = -1; //forcing an error
return sequelize.query('select generate_pessoa_seq() as nextId;',{
nest: true,
raw: true }).then(function(response){
//getNewId();
var stringId = response[0];
newId = parseInt(stringId.nextId, 10);
hook.data.IdiPessoa=newId;
return hook;
}).error(function(err){
console.log(err);
return hook;
});
}
}
});