-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadfice-db.js
210 lines (182 loc) · 5.5 KB
/
adfice-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2021-2024 Stichting Open Electronics Lab
// vim: set sts=4 shiftwidth=4 expandtab :
"use strict";
const fs = require('fs');
const dotenv = require('dotenv');
const mariadb = require('mariadb');
async function init(config, env_file_path) {
let dbconfig = {};
config = config || {};
env_file_path = env_file_path || 'dbconfig.env';
var envfile = {};
try {
envfile = await dotenv.parse(fs.readFileSync(env_file_path));
} catch (error) /* istanbul ignore next */ {
console.log(error);
}
if (config.socketPath || process.env.DB_SOCKET_PATH ||
envfile.DB_SOCKET_PATH) {
dbconfig.socketPath = config.socketPath ||
process.env.DB_SOCKET_PATH ||
envfile.DB_SOCKET_PATH;
} else {
dbconfig.host = config.host ||
process.env.DB_HOST ||
envfile.DB_HOST ||
'127.0.0.1';
dbconfig.port = config.port ||
process.env.DB_PORT ||
envfile.DB_PORT ||
3306;
}
dbconfig.user = config.user ||
process.env.DB_USER ||
envfile.DB_USER ||
null;
dbconfig.database = config.database ||
process.env.DB_NAME ||
envfile.DB_NAME ||
null;
dbconfig.password = config.password ||
process.env.DB_PASSWORD ||
envfile.DB_PASSWORD ||
null;
dbconfig.passwordFile = config.passwordFile ||
process.env.DB_PW_FILE ||
envfile.DB_PW_FILE ||
null;
dbconfig.connectionLimit = config.connectionLimit ||
process.env.DB_CONNECTION_LIMIT ||
envfile.DB_CONNECTION_LIMIT ||
'5';
if (config.acquireTimeout ||
process.env.DB_ACQUIRE_TIMEOUT ||
envfile.DB_ACQUIRE_TIMEOUT) {
dbconfig.acquireTimeout = config.acquireTimeout ||
process.env.DB_ACQUIRE_TIMEOUT ||
envfile.DB_ACQUIRE_TIMEOUT;
}
if (config.connectTimeout ||
process.env.DB_CONNECT_TIMEOUT ||
envfile.DB_CONNECT_TIMEOUT) {
dbconfig.connectTimeout = config.connectTimeout ||
process.env.DB_CONNECT_TIMEOUT ||
envfile.DB_CONNECT_TIMEOUT;
}
if (config.initializationTimeout ||
process.env.DB_INITIALIZATION_TIMEOUT ||
envfile.DB_INITIALIZATION_TIMEOUT) {
dbconfig.initializationTimeout = config.initializationTimeout ||
process.env.DB_INITIALIZATION_TIMEOUT ||
envfile.DB_INITIALIZATION_TIMEOUT;
}
if (dbconfig.passwordFile && !dbconfig.password) {
try {
let passwd = await fs.promises.readFile(dbconfig.passwordFile);
dbconfig.password = String(passwd).trim();
} catch (error) /* istanbul ignore next */ {
console.log(error);
}
}
let db = {
/* private member variables */
pool: null,
dbconfig: dbconfig,
/* "private" member functions */
connection_begin: connection_begin,
connection_end: connection_end,
connection_pool_close: connection_pool_close,
connection_pool: connection_pool,
/* public member functions */
schema_name: schema_name,
sql_query: sql_query,
as_sql_transaction: as_sql_transaction,
close: close,
}
return db;
}
async function close() {
await this.connection_pool_close();
}
async function connection_pool_close() {
try {
/* istanbul ignore else */
if (this.pool) {
await this.pool.end();
}
} finally {
this.pool = null;
}
}
async function connection_pool() {
if (this.pool) {
return this.pool;
}
this.pool = mariadb.createPool(this.dbconfig);
return this.pool;
}
async function connection_begin() {
let pool = await this.connection_pool();
let conn = await pool.getConnection();
return conn;
}
async function connection_end(conn) {
try {
/* istanbul ignore else */
if (conn) {
conn.end();
}
} catch (error) {
/* istanbul ignore next */
this.connection_pool_close();
/* istanbul ignore next */
throw error;
}
}
async function as_sql_transaction(sqls_and_params) {
let rs;
let conn;
try {
conn = await this.connection_begin();
conn.beginTransaction();
for (let i = 0; i < sqls_and_params.length; ++i) {
let sql = sqls_and_params[i][0];
let params = sqls_and_params[i][1];
let rv = await conn.query(sql, params);
}
rs = await conn.commit();
} finally {
await connection_end(conn);
}
return rs;
}
async function sql_query(sql, params) {
let objects = [];
let conn;
try {
conn = await this.connection_begin();
// This version of the driver seems to always place the "meta" in
// with the rows, no matter which calling convention we try.
let result_set;
if (!params || params.length == 0) {
result_set = await conn.query(sql);
} else {
result_set = await conn.query(sql, params);
}
// So, we will filter out anything that is not in the iterator:
for (let i = 0; i < result_set.length; ++i) {
let row = result_set[i];
objects.push(row);
}
} finally {
await this.connection_end(conn);
}
return objects;
}
async function schema_name() {
return this.dbconfig.database;
}
module.exports = {
init: init,
}