id: {
type: Sequelize.UUID,
primaryKey: true,
autoIncrement: true
}
var hashids = new Hashids('', 0, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); // all UpperCase
console.log(hashids.encode(1, 2, 3)); //MDFPHX
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
})
const hash = "MDFPHX";
const user = User.find({where: { hash: hash }});
console.log(user.user_id); // 1
const Sequelize = require('sequelize');
const ads = sequelize.define('ads', {
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
return hashids.encode(this.get("id"));
}
},
user_panel_id: {
type: Sequelize.INTEGER,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
},
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
timestamps: true
})
const Sequelize = require('sequelize');
const config = require('../../config')
const Hashids = require('hashids');
const hashids = new Hashids('', 0, config.hashids.string);
const ads = sequelize.define('ads', {
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
return hashids.encode(this.get("id"));
}
},
user_panel_id: {
type: Sequelize.INTEGER,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
},
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
timestamps: true
})
ads.sync({force: false})
module.exports = ads;
const Sequelize = require('sequelize');
const config = require('../../config')
const Hashids = require('hashids');
const hashids = new Hashids('', 0, config.hashids.string);
const ads = sequelize.define('ads', {
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
return hashids.encode(this.get("id"));
}
},
user_panel_id: {
type: Sequelize.INTEGER,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
},
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
timestamps: true,
hooks: {
beforeFind: pkHashToNumber,
beforeDestroy: pkHashToNumber,
beforeUpdate: pkHashToNumber
}
})
ads.sync({force: false})
module.exports = ads;
const hashids = new Hashids(config.hashids.secret, 0, config.hashids.string);
function pkHashToNumber(options) {
if (!options.where) {
return;
}
let pk = options.where.hash;
// Если убрать эту строчку, то возникает ошибка "column ads.hash does not exist"
delete options.where.hash
if (!pk) {
return;
}
if (typeof pk === 'string' && isNaN(pk)) {
console.log(pk)
options.where.id = hashids.decode(pk)[0];
console.log(options.where.id)
}
}
const ads = sequelize.define('ads', {
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
console.log(this.get("id"))
return hashids.encode(this.get("id"));
}
},
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
timestamps: true,
hooks: {
beforeFind: pkHashToNumber,
beforeDestroy: pkHashToNumber,
beforeUpdate: pkHashToNumber
}
})
ads.sync({force: false})
module.exports = ads;
из-за чего может быть это?- это виртуальная колонка. Её следует удалять перед запросом в бд, у вас вероятно нет колонки hash в бд
И если я буду искать не по хешу, а по обыкновенному id, то хеш я не получу, мне придётся делать функцию, которая будет преобразовывать id в хеш?
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
console.log(this.get("id"))
return hashids.encode(this.get("id"));
}
},