Sequelize Notes
Contents
Connection
import { Sequelize } from 'sequelize';
export const db = new Sequelize({
host: 'localhost',
dialect: 'mysql',
port: 3306,
database: 'seq',
username: 'root',
password: '123456',
define: {
timestamps: true, // 是否自动添加 createdAt 和 updatedAt 字段,默认为true
freezeTableName: true, // 防止Sequelize自动复数化表名
underscored: false, // 使用下划线命名风格
},
logging: false,
});
(async () => {
try {
await db.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
})();
Join table
const signatures: (Pick<ISignature, 'tokenId' | 'status' | 'hash'> & { dataValues: any })[] = await Signature.findAll({
where: {
status: 1,
},
attributes: ['tokenId', 'status', 'hash'],
limit: pageSize,
offset: (page - 1) * pageSize,
order: [['id', 'DESC']],
include: [
{
model: Wallet,
attributes: ['ESID', 'address'],
association: new BelongsTo(SignatureMint, Wallet, {
foreignKey: 'address',
targetKey: 'address',
constraints: false,
as: 'wallet',
}),
},
],
});