![](/img/trans.png)
[英]Knex.js with Express, How can I knex.commit followed by a knex.select query?
[英]Knex.js: Can I put function in the raw Knex query?
我想用 knex 遷移加密我的 Postgres 數據庫中的所有密碼。 第一步是將 varchar 從 64 擴展到更大的數字,這部分有效。 加密部分應該使用從項目導入的 function 來完成。 這是我試過的:
const {encrypt} = require("/path/to/function")
exports.up = function(knex) {
return knex.schema.table("users", function(table) {
table.string("passhash", 500).alter(); # this is done no problem
}).then(() => {
return knex.raw(`UPDATE users SET passhash = encrypt(passhash)`) # this is the idea I had, but it doesn't work
});
};
所以想法是先改變列類型,然后用function加密所有密碼。這樣可以實現嗎? 我會很感激任何想法,因為我找不到 knex raw 的類似用法,或者也許有一種完全不同的方法來解決這個問題?
您不能將 js function 作為數據庫運行...
您需要拉取用戶表,對密碼字段進行更改,然后將其保存回去。
const {encrypt} = require('/path/to/function');
exports.up = function (knex) {
return knex.schema
.table('users', function (table) {
table.string('passhash', 500).alter();
})
.then(async () => {
const users = await knex('users');
const promises = users.map((user) => {
const passhash = encrypt(user.passhash);
return knex.update('users').set({passhash}).where('id', user.id);
});
return Promise.all(promises);
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.