dataBase.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. const { Sequelize, Model, DataTypes } = require("sequelize");
  2. const sequelize = new Sequelize("distrohard", "graukatze", "1234", {
  3. host: "localhost",
  4. dialect: "postgres",
  5. });
  6. class Vendor extends Model {}
  7. Vendor.init(
  8. {
  9. id: {
  10. type: DataTypes.INTEGER,
  11. autoIncrement: true,
  12. primaryKey: true,
  13. },
  14. companyName: DataTypes.STRING(50),
  15. },
  16. {
  17. sequelize,
  18. modelName: "Vendors",
  19. timestamps: false,
  20. freezeTableName: true,
  21. }
  22. );
  23. class Processor extends Model {}
  24. Processor.init(
  25. {
  26. id: {
  27. type: DataTypes.INTEGER,
  28. autoIncrement: true,
  29. primaryKey: true,
  30. },
  31. modelName: DataTypes.STRING(50),
  32. vendor_id: {
  33. type: DataTypes.INTEGER,
  34. references: { model: Vendor, key: "id" },
  35. },
  36. },
  37. {
  38. sequelize,
  39. modelName: "Processors",
  40. timestamps: false,
  41. freezeTableName: true,
  42. }
  43. );
  44. class VideoCard extends Model {}
  45. VideoCard.init(
  46. {
  47. id: {
  48. type: DataTypes.INTEGER,
  49. autoIncrement: true,
  50. primaryKey: true,
  51. },
  52. modelName: DataTypes.STRING(50),
  53. vendor_id: {
  54. type: DataTypes.INTEGER,
  55. references: { model: Vendor, key: "id" },
  56. },
  57. },
  58. {
  59. sequelize,
  60. modelName: "VideoCards",
  61. timestamps: false,
  62. freezeTableName: true,
  63. }
  64. );
  65. class DistroLinux extends Model {}
  66. DistroLinux.init(
  67. {
  68. id: {
  69. type: DataTypes.INTEGER,
  70. autoIncrement: true,
  71. primaryKey: true,
  72. },
  73. vendor: DataTypes.STRING(50),
  74. version: DataTypes.STRING(50),
  75. },
  76. {
  77. sequelize,
  78. modelName: "DistroLinux",
  79. timestamps: false,
  80. freezeTableName: true,
  81. }
  82. );
  83. class Errors extends Model {}
  84. Errors.init(
  85. {
  86. id: {
  87. type: DataTypes.INTEGER,
  88. autoIncrement: true,
  89. primaryKey: true,
  90. },
  91. codeCVE: {
  92. type: DataTypes.STRING(20),
  93. },
  94. },
  95. {
  96. sequelize,
  97. modelName: "Errors",
  98. timestamps: false,
  99. freezeTableName: true,
  100. }
  101. );
  102. class ProcessorStatusOnLinux extends Model {}
  103. ProcessorStatusOnLinux.init(
  104. {
  105. id: {
  106. type: DataTypes.INTEGER,
  107. autoIncrement: true,
  108. primaryKey: true,
  109. },
  110. Processor_id: {
  111. type: DataTypes.INTEGER,
  112. references: { model: Processor, key: "id" },
  113. },
  114. DistLinux_id: {
  115. type: DataTypes.INTEGER,
  116. references: { model: DistroLinux, key: "id" },
  117. },
  118. Status: DataTypes.STRING(25),
  119. },
  120. {
  121. sequelize,
  122. modelName: "ProcessorStatusOnLinux",
  123. timestamps: false,
  124. freezeTableName: true,
  125. }
  126. );
  127. class VideoCardStatusOnLinux extends Model {}
  128. VideoCardStatusOnLinux.init(
  129. {
  130. id: {
  131. type: DataTypes.INTEGER,
  132. autoIncrement: true,
  133. primaryKey: true,
  134. },
  135. VideoCard_id: {
  136. type: DataTypes.INTEGER,
  137. references: { model: VideoCard, key: "id" },
  138. },
  139. DistLinux_id: {
  140. type: DataTypes.INTEGER,
  141. references: { model: DistroLinux, key: "id" },
  142. },
  143. Status: DataTypes.STRING(25),
  144. },
  145. {
  146. sequelize,
  147. modelName: "VideoCardStatusOnLinux",
  148. timestamps: false,
  149. freezeTableName: true,
  150. }
  151. );
  152. class ErrorStatusOnProcessor extends Model {}
  153. ErrorStatusOnProcessor.init(
  154. {
  155. Error_id: {
  156. type: DataTypes.INTEGER,
  157. references: { model: Errors, key: "id" },
  158. },
  159. Processor_id: {
  160. type: DataTypes.INTEGER,
  161. references: { model: Processor, key: "id" },
  162. },
  163. Status: DataTypes.STRING(25),
  164. },
  165. {
  166. sequelize,
  167. modelName: "ErrorStatusOnProcessor",
  168. timestamps: false,
  169. freezeTableName: true,
  170. }
  171. );
  172. class ErrorStatusOnVideoCard extends Model {}
  173. ErrorStatusOnVideoCard.init(
  174. {
  175. Error_id: {
  176. type: DataTypes.INTEGER,
  177. references: { model: Errors, key: "id" },
  178. },
  179. VideoCard_id: {
  180. type: DataTypes.INTEGER,
  181. references: { model: VideoCard, key: "id" },
  182. },
  183. Status: DataTypes.STRING(25),
  184. },
  185. {
  186. sequelize,
  187. modelName: "ErrorStatusOnVideoCard",
  188. timestamps: false,
  189. freezeTableName: true,
  190. }
  191. );
  192. class ErrorStatusOnLinux extends Model {}
  193. ErrorStatusOnLinux.init(
  194. {
  195. Error_id: {
  196. type: DataTypes.INTEGER,
  197. references: { model: Errors, key: "id" },
  198. },
  199. DistroLinux_id: {
  200. type: DataTypes.INTEGER,
  201. references: { model: DistroLinux, key: "id" },
  202. },
  203. Status: DataTypes.STRING(25),
  204. },
  205. {
  206. sequelize,
  207. modelName: "ErrorStatusOnLinux",
  208. timestamps: false,
  209. freezeTableName: true,
  210. }
  211. );
  212. async function syncDataBase() {
  213. await sequelize.sync({ alter: true }).then(() => {
  214. console.log("\n================Basa was sync================");
  215. });
  216. }
  217. //===========================
  218. //-----FUNC FOR DATABASE-----
  219. //===========================
  220. //work
  221. function insertData(Model, jsonData) {
  222. Model.create(jsonData);
  223. }
  224. async function updateData(Model, modelID, jsonData) {
  225. Model.update(jsonData, { where: { id: modelID } });
  226. }
  227. //work
  228. async function deleteData(Model, jsonData) {
  229. Model.destroy({ where: jsonData });
  230. }
  231. //work
  232. async function selectDataAll(Model) {
  233. return Model.findAll({ raw: true });
  234. }
  235. async function selectDataAllFiltr(Model, jsonData) {
  236. return Model.findAll({ raw: true, where: jsonData });
  237. }
  238. async function selectDataOne(Model, jsonData) {
  239. return Model.findOne({ raw: true, where: jsonData });
  240. }
  241. module.exports = {
  242. sequelize,
  243. syncDataBase,
  244. DistroLinux,
  245. selectDataAll,
  246. insertData,
  247. deleteData,
  248. Vendor,
  249. Processor,
  250. VideoCard,
  251. selectDataAllFiltr,
  252. selectDataOne,
  253. ProcessorStatusOnLinux,
  254. VideoCardStatusOnLinux,
  255. Errors,
  256. updateData,
  257. ErrorStatusOnLinux,
  258. ErrorStatusOnVideoCard,
  259. ErrorStatusOnProcessor,
  260. VideoCardStatusOnLinux,
  261. ProcessorStatusOnLinux,
  262. };