const hash = "MDFPHX";
const user = User.find({where: { hash: hash }});
console.log(user.user_id); // 1
function pkHashToNumber(options) {
if (!options.where) {
return;
}
let pk = options.where.user_id;
if (!pk) return;
if (typeof pk === 'string' && isNaN(pk)) {
options.where.user_id= hashids.decode(pk);
}
}
const Model = sequelize.define('task', {
hash: {
type: new DataTypes.VIRTUAL(DataTypes.STRING, ["user_id"]),
get: function() {
return hashids.encode(this.get("user_id"));
}
},
},
hooks: {
beforeFind: pkHashToNumber,
beforeDestroy: pkHashToNumber,
beforeUpdate: pkHashToNumber
})
var hashids = new Hashids('', 0, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); // all UpperCase
console.log(hashids.encode(1, 2, 3)); //MDFPHX