131 lines
2.3 KiB
JavaScript
131 lines
2.3 KiB
JavaScript
"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,
|
|
};
|