This commit is contained in:
newtbot
2024-01-04 15:32:46 +08:00
parent db1b513ad5
commit 1819956bd0
21 changed files with 1541 additions and 642 deletions

View File

@ -1,9 +1,6 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const {
isValidDateString,
} = require("../../Web-Server/functions/validateData");
//sequelize.sync();
const api_log_Model = sequelize.define(
@ -14,41 +11,23 @@ const api_log_Model = sequelize.define(
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
validate: {
isNumeric: true,
},
autoIncrement: true,
},
ip: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
validate: {
isIP: true,
},
},
time: {
type: DataTypes.STRING,
allowNull: false,
length: 20,
validate: {
//validate time: new Date().toUTCString(),
isValidDateString(value) {
if (!isValidDateString(value)) {
throw new Error("Time must be a valid date string");
}
},
notEmpty: { msg: "Time cannot be empty" },
len: [1, 20],
},
},
method: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validate: {
isIn: [["GET", "POST", "PUT", "DELETE"]],
},
},
host: {
type: DataTypes.STRING,
@ -59,47 +38,21 @@ const api_log_Model = sequelize.define(
type: DataTypes.STRING,
allowNull: false,
length: 10,
validate: {
isNumeric: true,
len: [1, 3],
},
},
Responsesize: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validate: {
isNumeric: true,
len: [1, 100],
},
},
referrer: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
validate: {
isString(value) {
if (typeof value !== "string") {
throw new Error("Referrer must be a string");
}
},
notEmpty: { msg: "Referrer cannot be empty" },
len: [1, 45], // Length between 1 and 45 characters
},
},
userAgent: {
type: DataTypes.STRING,
allowNull: false,
length: 100,
validate: {
isString(value) {
if (typeof value !== "string") {
throw new Error("UserAgent must be a string");
}
},
notEmpty: { msg: "UserAgent cannot be empty" },
len: [1, 100], // Length between 1 and 100 characters
},
},
createdAt: {
type: DataTypes.DATE,
@ -110,7 +63,6 @@ const api_log_Model = sequelize.define(
allowNull: true,
},
},
{
timestamps: true,
}

View File

@ -1,6 +1,7 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { isAlphaNumericwithSpaces } = require('../../Web-Server/functions/validateData')
//sequelize.sync();
const locationModel = sequelize.define(
@ -19,28 +20,27 @@ const locationModel = sequelize.define(
type: DataTypes.STRING,
allowNull: false,
length: 10,
unique: true,
validate: {
notEmpty: { msg: "Name cannot be empty" },
notEmpty: true,
len: [1, 20],
/*
//will not validate this and fail it
"hello world" (contains a space)
"hello@123" (contains a symbol)
"" (empty string)
is: /^[a-z]+$/i, // matches this RegExp
is: ["^[a-z]+$",'i'],
*/
is: ["^[a-z0-9]+$", "i"]
},
//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: { msg: "Added by cannot be empty" },
notEmpty: true,
len: [1, 20],
is: ["^[a-z0-9]+$", "i"]
is: ["^[a-z0-9]+$", "i"],
isIn: [['admin', 'system' , 'Admin', 'System']],
},
},
description: {
@ -48,13 +48,12 @@ const locationModel = sequelize.define(
allowNull: true,
length: 100,
validate: {
notEmpty: { msg: "Description cannot be empty" },
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"]
},

View File

@ -3,6 +3,7 @@ const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { locationModel } = require("./locationModel");
const { sensorModel } = require("./sensorModel");
const { isJson } = require("../../Web-Server/functions/validateData");
//sequelize.sync();
const sensorDataModel = sequelize.define(
@ -13,6 +14,11 @@ const sensorDataModel = sequelize.define(
allowNull: true,
primaryKey: true,
autoIncrement: true,
unique: true,
validate: {
isNumeric: true,
notEmpty: true,
},
},
id_sensor: {
type: DataTypes.INTEGER,
@ -23,6 +29,10 @@ const sensorDataModel = sequelize.define(
model: sensorModel,
key: "id",
},
validate: {
isNumeric: true,
notEmpty: true,
},
},
id_location: {
type: DataTypes.INTEGER,
@ -33,10 +43,22 @@ const sensorDataModel = sequelize.define(
model: locationModel,
key: "id",
},
validate: {
isNumeric: true,
notEmpty: true,
},
},
sensordata: {
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,

View File

@ -2,46 +2,97 @@
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const { locationModel } = require("./locationModel");
const {
isAlphaNumericwithSpaces,
isAlphaNumericWithSpacesAndDash,
isMacAddress,
} = require("../../Web-Server/functions/validateData");
//sequelize.sync();
const sensorModel = sequelize.define("sensors",
const sensorModel = sequelize.define(
"sensors",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
autoIncrement: true,
unique: true,
validate: {
notEmpty: true,
isNumeric: true,
},
},
sensorname: {
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");
}
},
},
},
location: {
type: DataTypes.INTEGER,
location: {
type: DataTypes.INTEGER,
allowNull: true,
length: 100,
//one to many relationship
references: {
model: locationModel,
key: 'id'
}
},
//one to many relationship
references: {
model: locationModel,
key: "id",
},
validate: {
notEmpty: true,
isNumeric: true,
},
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,