This commit is contained in:
2024-01-23 17:07:19 -05:00
parent 4aea6a8e4c
commit 173277cc8b
21 changed files with 5312 additions and 70 deletions

View File

@ -1,7 +1,9 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { Sequelize, DataTypes, Op } = require("sequelize");
const { sequelize } = require("../mySql");
const { userModel } = require("./userModel");
const { generateUUID } = require("../../functions/generateUUID.js");
const { hash, compareHash } = require("../../functions/bcrypt.js");
//sequelize.sync();
const apikeyModel = sequelize.define(
@ -16,7 +18,7 @@ const apikeyModel = sequelize.define(
isNumeric: true,
},
},
userid: {
userId: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
@ -63,6 +65,8 @@ const apikeyModel = sequelize.define(
);
apikeyModel.belongsTo(userModel);
apikeyModel.sync()
module.exports = { apikeyModel };
/*

View File

@ -0,0 +1,71 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
//sequelize.sync();
const api_log_Model = sequelize.define(
"api-logs",
{
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
},
ip: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
},
time: {
type: DataTypes.STRING,
allowNull: false,
length: 20,
},
method: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
host: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
},
statusCode: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
Responsesize: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
referrer: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
},
userAgent: {
type: DataTypes.STRING,
allowNull: false,
length: 100,
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: true,
}
);
module.exports = { api_log_Model };

View File

@ -0,0 +1,130 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { isAlphaNumericwithSpaces } = require('../../functions/validateData')
//sequelize.sync();
const locationModel = sequelize.define(
"location",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
validate: {
isNumeric: true,
},
},
name: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
unique: true,
validate: {
notEmpty: true,
len: [1, 20],
//accept only alphanumeric and spaces
isAlphaNumericwithSpaces(value){
if(!isAlphaNumericwithSpaces(value)){
throw new Error('Invalid characters in name')
}
}
},
},
added_by: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validate: {
notEmpty: true,
len: [1, 20],
is: ["^[a-z0-9]+$", "i"],
isIn: [['admin', 'system' , 'Admin', 'System']],
},
},
description: {
type: DataTypes.STRING,
allowNull: true,
length: 100,
validate: {
notEmpty: true,
len: [1, 100],
/*
//will not validate this and fail it
"hello@123" (contains a symbol)
"" (empty string)
*/
is: ["^[a-zA-Z0-9 ]+$", "i"]
},
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: true,
}
);
async function getLocation() {
const location = await locationModel.findAll();
return location;
}
async function addLocation(name, added_by, description) {
const location = await locationModel.create({
name: name,
added_by: added_by,
description: description,
});
}
async function updateLocation(id, name, added_by, description) {
const location = await locationModel.update(
{
name: name,
added_by: added_by,
description: description,
},
{
where: {
id: id,
},
}
);
}
async function deleteLocation(id) {
//delete by id
const location = await locationModel.destroy({
where: {
id: id,
},
});
}
async function getLocationById(id) {
const location = await locationModel.findAll({
where: {
id: id,
},
});
return location;
}
module.exports = {
locationModel,
getLocation,
addLocation,
updateLocation,
deleteLocation,
getLocationById,
};

View File

@ -0,0 +1,77 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { locationModel } = require("./locationModel");
const { sensorModel } = require("./sensorModel");
const { isJson } = require('../../functions/validateData');
//sequelize.sync();
const sensorDataModel = sequelize.define(
"sensorData",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
unique: true,
validate: {
isNumeric: true,
notEmpty: true,
},
},
sensorid: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: sensorModel,
key: "id",
},
validate: {
isNumeric: true,
notEmpty: true,
},
},
locationid: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: locationModel,
key: "id",
},
validate: {
isNumeric: true,
notEmpty: true,
},
},
measurement: {
type: DataTypes.JSON,
allowNull: false,
validate: {
notEmpty: true,
isJson(value)
{
if (isJson(value) !== true)
throw new Error("sensordata must be a JSON");
},
},
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: true,
}
);
module.exports = { sensorDataModel };

View File

@ -0,0 +1,110 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { locationModel } = require("./locationModel");
const {
isAlphaNumericwithSpaces,
isAlphaNumericWithSpacesAndDash,
isMacAddress,
} = require('../../functions/validateData');
//sequelize.sync();
const sensorModel = sequelize.define(
"sensors",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
unique: true,
validate: {
notEmpty: true,
isNumeric: true,
},
},
name: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
unique: true,
validate: {
notEmpty: true,
len: [1, 30],
//accept only alphanumeric and spaces
isAlphaNumericWithSpacesAndDash(value) {
if (!isAlphaNumericWithSpacesAndDash(value)) {
throw new Error("Invalid characters in name");
}
},
},
},
added_by: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validate: {
notEmpty: { msg: "Added by cannot be empty" },
len: [1, 20],
is: ["^[a-z0-9]+$", "i"],
isIn: [["admin", "system", "Admin", "System"]],
},
},
mac_address: {
type: DataTypes.STRING,
allowNull: false,
length: 12,
unique: true,
validate: {
notEmpty: true,
len: [12, 18],
isMacAddress(value) {
if (!isMacAddress(value)) {
throw new Error("Invalid Mac Address");
}
},
},
},
description: {
type: DataTypes.STRING,
allowNull: true,
length: 100,
validate: {
notEmpty: true,
len: [1, 100],
isAlphaNumericwithSpaces(value) {
if (!isAlphaNumericwithSpaces(value)) {
throw new Error("Invalid characters in name");
}
},
},
},
locationid: {
type: DataTypes.INTEGER,
allowNull: true,
length: 100,
//one to many relationship
references: {
model: locationModel,
key: "id",
},
validate: {
notEmpty: true,
isNumeric: true,
},
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: true,
}
);
module.exports = { sensorModel };

View File

@ -1,6 +1,6 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { sequelize } = require("../mySql");
const {
isAlphaNumericWithSpacesAndDash,
isAddress,
@ -111,4 +111,5 @@ const userModel = sequelize.define(
timestamps: true,
}
);
module.exports = { userModel };