42 lines
1 KiB
JavaScript
42 lines
1 KiB
JavaScript
|
|
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;
|