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;
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 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
})