dataBase.js 6.5 KB

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