37860-vm/backend/src/db/models/production_readings.js
2026-01-27 03:30:24 +00:00

133 lines
1.7 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const production_readings = sequelize.define(
'production_readings',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
reading_label: {
type: DataTypes.TEXT,
},
timestamp: {
type: DataTypes.DATE,
},
energy_kwh: {
type: DataTypes.DECIMAL,
},
power_kw: {
type: DataTypes.DECIMAL,
},
source: {
type: DataTypes.ENUM,
values: [
"inverter",
"meter",
"estimate"
],
},
verified: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
production_readings.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.production_readings.belongsTo(db.solar_arrays, {
as: 'array',
foreignKey: {
name: 'arrayId',
},
constraints: false,
});
db.production_readings.belongsTo(db.users, {
as: 'createdBy',
});
db.production_readings.belongsTo(db.users, {
as: 'updatedBy',
});
};
return production_readings;
};