iot sensor finished
1)with validation on front and backend 2)fixed seed route generating value 0 for data
This commit is contained in:
75
IoT-sensor/Database/locationModel.js
Normal file
75
IoT-sensor/Database/locationModel.js
Normal file
@ -0,0 +1,75 @@
|
||||
"use strict";
|
||||
const { Sequelize, DataTypes } = require("sequelize");
|
||||
const { sequelize } = require("./mySQL");
|
||||
const { isAlphaNumericwithSpaces } = require('../../Web-Server/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,
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = { locationModel };
|
34
IoT-sensor/Database/mySQL.js
Normal file
34
IoT-sensor/Database/mySQL.js
Normal file
@ -0,0 +1,34 @@
|
||||
const dotenv = require("dotenv");
|
||||
const path = require('path')
|
||||
require('dotenv').config({ path: path.resolve(__dirname, '../../.env') })
|
||||
const Sequelize = require("sequelize");
|
||||
const fs = require('fs');
|
||||
|
||||
const sequelize = new Sequelize(
|
||||
"eco_saver",
|
||||
process.env.DB_USER,
|
||||
process.env.DB_PASS,
|
||||
{
|
||||
host: "mpsqldatabase.mysql.database.azure.com",
|
||||
dialect: 'mysql',
|
||||
// attributeBehavior?: 'escape' | 'throw' | 'unsafe-legacy';
|
||||
attributeBehavior: 'escape',
|
||||
dialectOptions: {
|
||||
ssl: {
|
||||
ca: fs.readFileSync(path.resolve(__dirname, '../../cert/DigiCertGlobalRootCA.crt.pem')),
|
||||
},
|
||||
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
);
|
||||
|
||||
sequelize.authenticate().then(() => {
|
||||
console.log('Connection has been established successfully.');
|
||||
}).catch((error) => {
|
||||
console.error('Unable to connect to the database: ', error);
|
||||
});
|
||||
|
||||
module.exports = { sequelize };
|
||||
|
110
IoT-sensor/Database/sensorModel.js
Normal file
110
IoT-sensor/Database/sensorModel.js
Normal 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("../../Web-Server/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 };
|
Reference in New Issue
Block a user