corefwnode 3.0.2 → 3.0.4

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/lib/Language.js CHANGED
@@ -1,139 +1,139 @@
1
- const GeneralHandling = require('./GeneralHandling.js');
2
- const Translator = require('../objects/Translator.js');
3
-
4
- /**
5
- * Language class
6
- *
7
- * module.exports = class providing language methods, translation and language cache filling
8
- *
9
- * @author Dario Filkovic <dfilkovi@gmail.com>
10
- *
11
- * @since 1.0
12
- *
13
- * @package CoreFw
14
- */
15
- module.exports = class Language
16
- {
17
- /**
18
- * constructor()
19
- *
20
- * Constructor initializing Translator object, Cache and filling language variables
21
- *
22
- * @author Dario Filkovic <dfilkovi@gmail.com>
23
- *
24
- * @since 1.0
25
- *
26
- * @package CoreFw
27
- *
28
- */
29
- constructor()
30
- {
31
- const me = this;
32
-
33
- /**
34
- * Translator object
35
- * @type Translator
36
- */
37
- me.transObj = new Translator();
38
- }
39
-
40
- /**
41
- * fillTrans()
42
- *
43
- * Check cache for translation, if it doesn't exist, ask Translator object for translation and fill Cache
44
- *
45
- * @author Dario Filkovic <dfilkovi@gmail.com>
46
- *
47
- * @since 1.0
48
- *
49
- * @package CoreFw
50
- *
51
- * @return void
52
- */
53
- async fillTrans()
54
- {
55
- const me = this;
56
-
57
- me.trans = await me.transObj.getTranslation();
58
-
59
- const data = await me.transObj.getLangDefs();
60
- me.langDefs = data.langDefs;
61
- me.langCodes = data.langCodes;
62
-
63
- return { error: false, notice: 'Success' };
64
- }
65
-
66
- /**
67
- * translate()
68
- *
69
- * Translate variable by key, if key was not found check translation
70
- *
71
- * @author Dario Filkovic <dfilkovi@gmail.com>
72
- *
73
- * @since 1.0
74
- *
75
- * @package CoreFw
76
- *
77
- * @param String $key Key for translating
78
- * @return String Returns translated key
79
- */
80
- translate(keyIn, langId)
81
- {
82
- const me = this;
83
- const key = GeneralHandling.safeTrans(keyIn);
84
- if (typeof me.trans[langId] !== 'undefined' && typeof me.trans[langId][key] !== 'undefined')
85
- {
86
- if (me.trans[langId][key] === '')
87
- {
88
- return `#${key}`;
89
- }
90
- return me.trans[langId][key];
91
- }
92
-
93
- if (key === '')
94
- {
95
- return '#EMPTYKEY';
96
- }
97
- me.checkTrans(key);
98
- return `#${key}`;
99
- }
100
-
101
- /**
102
- * _()
103
- *
104
- * @see translate
105
- *
106
- * @since 1.0
107
- *
108
- * @package CoreFw
109
- *
110
- * @param String $key Key for translating
111
- * @return String Returns translated key
112
- */
113
- _(key)
114
- {
115
- return this.translate(key);
116
- }
117
-
118
- /**
119
- * checkTrans()
120
- *
121
- * Check translation in Translator object,
122
- * Translator object should handle insertion to database if key is untranslated
123
- *
124
- * @author Dario Filkovic <dfilkovi@gmail.com>
125
- *
126
- * @since 1.0
127
- *
128
- * @package CoreFw
129
- *
130
- * @param mixed[] $key Translation key
131
- * @return void
132
- */
133
- checkTrans(keyIn)
134
- {
135
- const me = this;
136
- const key = GeneralHandling.safeTrans(keyIn);
137
- me.transObj.checkTrans({ key });
138
- }
139
- };
1
+ const GeneralHandling = require('./GeneralHandling.js');
2
+ const Translator = require('../objects/Translator.js');
3
+
4
+ /**
5
+ * Language class
6
+ *
7
+ * module.exports = class providing language methods, translation and language cache filling
8
+ *
9
+ * @author Dario Filkovic <dfilkovi@gmail.com>
10
+ *
11
+ * @since 1.0
12
+ *
13
+ * @package CoreFw
14
+ */
15
+ module.exports = class Language
16
+ {
17
+ /**
18
+ * constructor()
19
+ *
20
+ * Constructor initializing Translator object, Cache and filling language variables
21
+ *
22
+ * @author Dario Filkovic <dfilkovi@gmail.com>
23
+ *
24
+ * @since 1.0
25
+ *
26
+ * @package CoreFw
27
+ *
28
+ */
29
+ constructor()
30
+ {
31
+ const me = this;
32
+
33
+ /**
34
+ * Translator object
35
+ * @type Translator
36
+ */
37
+ me.transObj = new Translator();
38
+ }
39
+
40
+ /**
41
+ * fillTrans()
42
+ *
43
+ * Check cache for translation, if it doesn't exist, ask Translator object for translation and fill Cache
44
+ *
45
+ * @author Dario Filkovic <dfilkovi@gmail.com>
46
+ *
47
+ * @since 1.0
48
+ *
49
+ * @package CoreFw
50
+ *
51
+ * @return void
52
+ */
53
+ async fillTrans()
54
+ {
55
+ const me = this;
56
+
57
+ me.trans = await me.transObj.getTranslation();
58
+
59
+ const data = await me.transObj.getLangDefs();
60
+ me.langDefs = data.langDefs;
61
+ me.langCodes = data.langCodes;
62
+
63
+ return { error: false, notice: 'Success' };
64
+ }
65
+
66
+ /**
67
+ * translate()
68
+ *
69
+ * Translate variable by key, if key was not found check translation
70
+ *
71
+ * @author Dario Filkovic <dfilkovi@gmail.com>
72
+ *
73
+ * @since 1.0
74
+ *
75
+ * @package CoreFw
76
+ *
77
+ * @param String $key Key for translating
78
+ * @return String Returns translated key
79
+ */
80
+ translate(keyIn, langId)
81
+ {
82
+ const me = this;
83
+ const key = GeneralHandling.safeTrans(keyIn);
84
+ if (typeof me.trans[langId] !== 'undefined' && typeof me.trans[langId][key] !== 'undefined')
85
+ {
86
+ if (me.trans[langId][key] === '')
87
+ {
88
+ return `#${key}`;
89
+ }
90
+ return me.trans[langId][key];
91
+ }
92
+
93
+ if (key === '')
94
+ {
95
+ return '#EMPTYKEY';
96
+ }
97
+ me.checkTrans(key);
98
+ return `#${key}`;
99
+ }
100
+
101
+ /**
102
+ * _()
103
+ *
104
+ * @see translate
105
+ *
106
+ * @since 1.0
107
+ *
108
+ * @package CoreFw
109
+ *
110
+ * @param String $key Key for translating
111
+ * @return String Returns translated key
112
+ */
113
+ _(key)
114
+ {
115
+ return this.translate(key);
116
+ }
117
+
118
+ /**
119
+ * checkTrans()
120
+ *
121
+ * Check translation in Translator object,
122
+ * Translator object should handle insertion to database if key is untranslated
123
+ *
124
+ * @author Dario Filkovic <dfilkovi@gmail.com>
125
+ *
126
+ * @since 1.0
127
+ *
128
+ * @package CoreFw
129
+ *
130
+ * @param mixed[] $key Translation key
131
+ * @return void
132
+ */
133
+ checkTrans(keyIn)
134
+ {
135
+ const me = this;
136
+ const key = GeneralHandling.safeTrans(keyIn);
137
+ me.transObj.checkTrans({ key });
138
+ }
139
+ };
package/lib/MySql.js CHANGED
@@ -1,142 +1,141 @@
1
-
2
- const mysql = require('promise-mysql');
3
- const SqlString = require('sqlstring');
4
- const CoreError = require('./errors/CoreError');
5
- const SQLError = require('./errors/SQLError');
6
-
7
- module.exports = class MySQLConn
8
- {
9
- static async init(config)
10
- {
11
- try
12
- {
13
- const db = await mysql.createPool(
14
- {
15
- connectionLimit: config.connectionLimit,
16
- host: config.host,
17
- user: config.username,
18
- password: config.password,
19
- database: config.dbname,
20
- port: config.port,
21
- dateStrings: true, // if we want dates as strings
22
- connectTimeout: 20000,
23
- acquireTimeout: 20000,
24
- queryFormat(query, values)
25
- {
26
- let data = query;
27
- if (!values)
28
- {
29
- data = query;
30
- }
31
- else
32
- if (query.indexOf('?') !== -1)
33
- {
34
- data = SqlString.format(query, values, this.config.stringifyObjects, this.config.timezone);
35
- }
36
- else
37
- {
38
- data = query.replace(/:(\w+)/g, (txt, key) =>
39
- {
40
- if (Object.prototype.hasOwnProperty.call(values, key))
41
- {
42
- return this.escape(values[key]);
43
- }
44
- return txt;
45
- });
46
- }
47
- console.log(data, `connected as id ${this.threadId}`);
48
- return data;
49
- },
50
- },
51
- );
52
-
53
- db.dbname = config.dbname;
54
-
55
- db.query = async function query(...args)
56
- {
57
- const me = this;
58
- try
59
- {
60
- const res = await Object.getPrototypeOf(me).query.call(me, ...args);
61
- return res;
62
- }
63
- catch (e)
64
- {
65
- throw (new SQLError(e));
66
- }
67
- };
68
-
69
- db.begin = async (session) =>
70
- {
71
- if (typeof session === 'undefined')
72
- {
73
- throw (new CoreError({
74
- error: true,
75
- notice: 'begin excepts session',
76
- }));
77
- }
78
-
79
- const connection = await db.getConnection();
80
-
81
- connection.commitInstance = 0;
82
-
83
- connection.dbname = db.dbname;
84
- connection.query = db.query;
85
-
86
- session.tranDb = connection;
87
-
88
- connection.begin = () =>
89
- {
90
- // dummy function, as begin was already called and
91
- // mysql does not support nested transactions, return existing connection
92
- connection.commitInstance += 1;
93
- return connection;
94
- };
95
-
96
- connection.rollback = async () =>
97
- {
98
- if (connection.commitInstance > 0)
99
- {
100
- connection.commitInstance -= 1;
101
- return db;
102
- }
103
- await connection.query('ROLLBACK');
104
- try
105
- {
106
- connection.release();
107
- }
108
- catch (error)
109
- {
110
- // ignore already released connections
111
- }
112
- session.tranDb = null;
113
- return db;
114
- };
115
-
116
- connection.commit = async () =>
117
- {
118
- if (connection.commitInstance > 0)
119
- {
120
- connection.commitInstance -= 1;
121
- return db;
122
- }
123
- await connection.query('COMMIT');
124
- connection.release();
125
- session.tranDb = null;
126
- return db;
127
- };
128
-
129
- await connection.query('START TRANSACTION');
130
-
131
- return connection;
132
- };
133
-
134
- return db;
135
- }
136
- catch (error)
137
- {
138
- console.error(error);
139
- return null;
140
- }
141
- }
142
- };
1
+
2
+ const mysql = require('mysql2/promise');
3
+ const SqlString = require('sqlstring');
4
+ const CoreError = require('./errors/CoreError');
5
+ const SQLError = require('./errors/SQLError');
6
+
7
+ module.exports = class MySQLConn
8
+ {
9
+ static async init(config)
10
+ {
11
+ try
12
+ {
13
+ const db = await mysql.createPool(
14
+ {
15
+ connectionLimit: config.connectionLimit,
16
+ host: config.host,
17
+ user: config.username,
18
+ password: config.password,
19
+ database: config.dbname,
20
+ port: config.port,
21
+ dateStrings: true, // if we want dates as strings
22
+ connectTimeout: 20000,
23
+ queryFormat(query, values)
24
+ {
25
+ let data = query;
26
+ if (!values)
27
+ {
28
+ data = query;
29
+ }
30
+ else
31
+ if (query.indexOf('?') !== -1)
32
+ {
33
+ data = SqlString.format(query, values, this.config.stringifyObjects, this.config.timezone);
34
+ }
35
+ else
36
+ {
37
+ data = query.replace(/:(\w+)/g, (txt, key) =>
38
+ {
39
+ if (Object.prototype.hasOwnProperty.call(values, key))
40
+ {
41
+ return this.escape(values[key]);
42
+ }
43
+ return txt;
44
+ });
45
+ }
46
+ console.log(data, `connected as id ${this.threadId}`);
47
+ return data;
48
+ },
49
+ },
50
+ );
51
+
52
+ db.dbname = config.dbname;
53
+
54
+ db.query = async function query(...args)
55
+ {
56
+ const me = this;
57
+ try
58
+ {
59
+ const res = await Object.getPrototypeOf(me).query.call(me, ...args);
60
+ return res[0];
61
+ }
62
+ catch (e)
63
+ {
64
+ throw (new SQLError(e));
65
+ }
66
+ };
67
+
68
+ db.begin = async (session) =>
69
+ {
70
+ if (typeof session === 'undefined')
71
+ {
72
+ throw (new CoreError({
73
+ error: true,
74
+ notice: 'begin excepts session',
75
+ }));
76
+ }
77
+
78
+ const connection = await db.getConnection();
79
+
80
+ connection.commitInstance = 0;
81
+
82
+ connection.dbname = db.dbname;
83
+ connection.query = db.query;
84
+
85
+ session.tranDb = connection;
86
+
87
+ connection.begin = () =>
88
+ {
89
+ // dummy function, as begin was already called and
90
+ // mysql does not support nested transactions, return existing connection
91
+ connection.commitInstance += 1;
92
+ return connection;
93
+ };
94
+
95
+ connection.rollback = async () =>
96
+ {
97
+ if (connection.commitInstance > 0)
98
+ {
99
+ connection.commitInstance -= 1;
100
+ return db;
101
+ }
102
+ await connection.query('ROLLBACK');
103
+ try
104
+ {
105
+ connection.release();
106
+ }
107
+ catch (error)
108
+ {
109
+ // ignore already released connections
110
+ }
111
+ session.tranDb = null;
112
+ return db;
113
+ };
114
+
115
+ connection.commit = async () =>
116
+ {
117
+ if (connection.commitInstance > 0)
118
+ {
119
+ connection.commitInstance -= 1;
120
+ return db;
121
+ }
122
+ await connection.query('COMMIT');
123
+ connection.release();
124
+ session.tranDb = null;
125
+ return db;
126
+ };
127
+
128
+ await connection.query('START TRANSACTION');
129
+
130
+ return connection;
131
+ };
132
+
133
+ return db;
134
+ }
135
+ catch (error)
136
+ {
137
+ console.error(error);
138
+ return null;
139
+ }
140
+ }
141
+ };