mailbox/backend/models/SystemConfig.js

42 lines
1 KiB
JavaScript
Raw Permalink Normal View History

2025-12-16 08:18:15 +09:00
const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");
const SystemConfig = sequelize.define(
"SystemConfig",
{
key: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
value: {
type: DataTypes.TEXT, // Using TEXT to store JSON or long strings
allowNull: true,
get() {
// Automatically parse JSON if possible
const rawValue = this.getDataValue("value");
try {
return JSON.parse(rawValue);
} catch (e) {
return rawValue;
}
},
set(value) {
// Automatically stringify objects
if (typeof value === "object" && value !== null) {
this.setDataValue("value", JSON.stringify(value));
} else {
this.setDataValue("value", value);
}
},
},
},
{
tableName: "system_configs",
timestamps: false, // No need for created/updatedAt for config
}
);
module.exports = SystemConfig;