@amanat-qa/utils-backend 1.2.0 → 1.3.1
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 +4 -2
- package/src/DB/baseDB.js +17 -46
- package/src/DB/baseModel.js +23 -0
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amanat-qa/utils-backend",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
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
|
}
|
package/src/DB/baseDB.js
CHANGED
|
@@ -1,58 +1,29 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { Sequelize } = require('sequelize');
|
|
2
2
|
const Logger = require('../log/logger');
|
|
3
3
|
|
|
4
4
|
class BaseDB {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
27
|
-
|
|
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
|
|
38
|
-
await this
|
|
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');
|