返回带有格式化日期的额外列
returning extra column with formatted date
我有一个模式(table),就像在 seqluelize 中一样:
const schema = sequelize.define("order_entry_header", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
date: {
type: DataTypes.TEXT,
},
sub_total: {
type: DataTypes.DOUBLE,
},
...
});
我的要求是每当我调用或使用或包含此模式时 order_entry_header
在我的应用程序中的任何地方,我希望 date
列在名为 date_format
的不同列中格式化为可读文本
在简单的 sql 中应该如下所示:
SELECT
id,
date,
DATE_FORMAT(date, "%d/%m/%Y") as date_format
...
FROM order_entry_header;
我 joining/fetching 这个 table 数据在很多地方,每次我都必须添加额外的列来专门获取格式化日期。我希望这是自动化的。
。
.
用instanceMethod
可以执行。但我每次获取数据时都必须调用该方法,这有时有点混乱。
。
.
有什么办法吗?
您可以尝试使用Default scope
(参见Scopes)
const schema = sequelize.define("order_entry_header", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
date: {
type: DataTypes.TEXT,
},
sub_total: {
type: DataTypes.DOUBLE,
},
...
}, {
defaultScope: {
attributes: [{ include: [[Sequelize.fn('DATE_FORMAT', Sequelize.col('date')), 'date_format']]}]
active: true
}
}
});
我有一个模式(table),就像在 seqluelize 中一样:
const schema = sequelize.define("order_entry_header", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
date: {
type: DataTypes.TEXT,
},
sub_total: {
type: DataTypes.DOUBLE,
},
...
});
我的要求是每当我调用或使用或包含此模式时 order_entry_header
在我的应用程序中的任何地方,我希望 date
列在名为 date_format
在简单的 sql 中应该如下所示:
SELECT
id,
date,
DATE_FORMAT(date, "%d/%m/%Y") as date_format
...
FROM order_entry_header;
我 joining/fetching 这个 table 数据在很多地方,每次我都必须添加额外的列来专门获取格式化日期。我希望这是自动化的。
。 .
用instanceMethod
可以执行。但我每次获取数据时都必须调用该方法,这有时有点混乱。
。 .
有什么办法吗?
您可以尝试使用Default scope
(参见Scopes)
const schema = sequelize.define("order_entry_header", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
date: {
type: DataTypes.TEXT,
},
sub_total: {
type: DataTypes.DOUBLE,
},
...
}, {
defaultScope: {
attributes: [{ include: [[Sequelize.fn('DATE_FORMAT', Sequelize.col('date')), 'date_format']]}]
active: true
}
}
});