@amanat-qa/utils-backend 1.1.5 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amanat-qa/utils-backend",
3
- "version": "1.1.5",
3
+ "version": "1.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -15,6 +15,7 @@
15
15
  ".": "./src/index.js",
16
16
  "./baseAPI": "./src/API/baseAPI.js",
17
17
  "./baseDB": "./src/DB/baseDB.js",
18
+ "./baseModel": "./src/DB/baseModel.js",
18
19
  "./JSONLoader": "./src/data/JSONLoader.js",
19
20
  "./JSONMapper": "./src/data/JSONMapper.js",
20
21
  "./dataUtils": "./src/data/dataUtils.js",
@@ -31,6 +32,7 @@
31
32
  "dotenv": "*",
32
33
  "js-yaml": "*",
33
34
  "mysql2": "*",
34
- "allure-mocha": "*"
35
+ "allure-mocha": "*",
36
+ "sequelize": "*"
35
37
  }
36
38
  }
@@ -67,6 +67,32 @@ class BaseAPI {
67
67
  return error.response;
68
68
  }
69
69
  }
70
+
71
+ async put(endpoint, params) {
72
+ Logger.log(`[req] ▶ put ${JSON.stringify(params || {})} to ${endpoint}:`);
73
+ try {
74
+ const response = await this.#axiosInstance.put(`/${endpoint}`, params);
75
+ Logger.log(`[res] status code: ${response.status}`);
76
+ return response;
77
+ } catch (error) {
78
+ Logger.log(`[res] status code: ${error.response.status}`);
79
+ Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
80
+ return error.response;
81
+ }
82
+ }
83
+
84
+ async delete(endpoint, params) {
85
+ Logger.log(`[req] ▶ delete ${JSON.stringify(params || {})} to ${endpoint}:`);
86
+ try {
87
+ const response = await this.#axiosInstance.delete(`/${endpoint}`, params);
88
+ Logger.log(`[res] status code: ${response.status}`);
89
+ return response;
90
+ } catch (error) {
91
+ Logger.log(`[res] status code: ${error.response.status}`);
92
+ Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
93
+ return error.response;
94
+ }
95
+ }
70
96
  }
71
97
 
72
98
  module.exports = BaseAPI; // test commentary... don't mind me
package/src/DB/baseDB.js CHANGED
@@ -1,58 +1,29 @@
1
- const mysql = require('mysql2/promise');
1
+ const { Sequelize } = require('sequelize');
2
2
  const Logger = require('../log/logger');
3
3
 
4
4
  class BaseDB {
5
- #host;
6
-
7
- #user;
8
-
9
- #port;
10
-
11
- #password;
12
-
13
- #database;
14
-
15
- #connection;
16
-
17
- constructor(host, user, password, database, port) {
18
- this.#host = host;
19
- this.#user = user;
20
- this.#password = password;
21
- this.#database = database;
22
- this.#port = port;
5
+ constructor(database) {
6
+ this.sequelize = new Sequelize(
7
+ database,
8
+ process.env.DB_USERNAME,
9
+ process.env.DB_PASSWORD,
10
+ {
11
+ host: process.env.DB_HOST,
12
+ port: process.env.DB_PORT,
13
+ dialect: 'mysql',
14
+ logging: (msg) => Logger.log(`[sql] ▶ ${msg}`),
15
+ },
16
+ );
23
17
  }
24
18
 
25
19
  async createConnection() {
26
- Logger.log(`[inf] ▶ connect to ${this.#database} database`);
27
- this.#connection = await mysql.createConnection({
28
- host: this.#host,
29
- user: this.#user,
30
- password: this.#password,
31
- database: this.#database,
32
- port: this.#port,
33
- });
20
+ Logger.log(`[inf] ▶ connect to ${this.sequelize.getDatabaseName()} database`);
21
+ await this.sequelize.authenticate();
34
22
  }
35
23
 
36
24
  async closeConnection() {
37
- Logger.log(`[inf] ▶ close connection to ${this.#database} database`);
38
- await this.#connection.end();
39
- }
40
-
41
- async sqlQuery(query, values) {
42
- const [rows] = await this.#connection.query(query, values);
43
- return rows;
44
- }
45
-
46
- async sqlSelect(
47
- tableName,
48
- target = '*',
49
- conditions = '',
50
- values = [],
51
- options = { hasLogger: true },
52
- ) {
53
- if (options.hasLogger) Logger.log(`[inf] ▶ select ${target} from ${tableName} table`);
54
- const query = `SELECT ${target} FROM ${tableName} ${conditions};`;
55
- return this.sqlQuery(query, values);
25
+ Logger.log(`[inf] ▶ close connection to ${this.sequelize.getDatabaseName()} database`);
26
+ await this.sequelize.close();
56
27
  }
57
28
  }
58
29
 
@@ -0,0 +1,23 @@
1
+ const { DataTypes } = require('sequelize');
2
+
3
+ module.exports = {
4
+ attributes: {
5
+ id: { type: DataTypes.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true },
6
+ },
7
+ getOptions: ({ tableName, withSoftDelete = false } = {}) => {
8
+ const options = {
9
+ timestamps: true,
10
+ underscored: true,
11
+ createdAt: 'created_at',
12
+ updatedAt: 'updated_at',
13
+ tableName,
14
+ };
15
+
16
+ if (withSoftDelete) {
17
+ options.paranoid = true;
18
+ options.deletedAt = 'deleted_at';
19
+ }
20
+
21
+ return options;
22
+ },
23
+ };
package/src/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  exports.BaseAPI = require('./API/baseAPI');
4
4
  exports.BaseDB = require('./DB/baseDB');
5
+ exports.baseModel = require('./DB/baseModel');
5
6
  exports.JSONLoader = require('./data/JSONLoader');
6
7
  exports.JSONMapper = require('./data/JSONMapper');
7
8
  exports.DataUtils = require('./data/dataUtils');