@onurege3467/zerohelper 9.0.0 → 9.2.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/README.md +152 -254
- package/dist/bin/zero.d.ts +2 -0
- package/dist/bin/zero.js +141 -0
- package/dist/database/IDatabase.d.ts +25 -31
- package/dist/database/IDatabase.js +38 -0
- package/dist/database/cacheWrapper.d.ts +5 -2
- package/dist/database/cacheWrapper.js +36 -50
- package/dist/database/index.d.ts +3 -2
- package/dist/database/index.js +13 -9
- package/dist/database/json.d.ts +4 -4
- package/dist/database/json.js +85 -87
- package/dist/database/mongodb.d.ts +12 -12
- package/dist/database/mongodb.js +49 -82
- package/dist/database/mysql.d.ts +7 -9
- package/dist/database/mysql.js +149 -270
- package/dist/database/pg.d.ts +12 -14
- package/dist/database/pg.js +113 -222
- package/dist/database/redis.d.ts +5 -3
- package/dist/database/redis.js +81 -107
- package/dist/database/seeder.d.ts +20 -0
- package/dist/database/seeder.js +37 -0
- package/dist/database/sqlite.d.ts +12 -15
- package/dist/database/sqlite.js +108 -223
- package/dist/database/telemetry.d.ts +35 -0
- package/dist/database/telemetry.js +41 -0
- package/dist/database/toon.d.ts +32 -0
- package/dist/database/toon.js +209 -0
- package/dist/database/types.d.ts +28 -34
- package/dist/database/zpack.d.ts +10 -4
- package/dist/database/zpack.js +151 -71
- package/dist/functions/index.d.ts +16 -0
- package/dist/functions/index.js +49 -3
- package/dist/functions/security.d.ts +15 -0
- package/dist/functions/security.js +46 -0
- package/dist/functions/toon.d.ts +7 -0
- package/dist/functions/toon.js +118 -0
- package/dist/functions/worker.d.ts +5 -0
- package/dist/functions/worker.js +35 -0
- package/dist/test_v91_advanced.d.ts +1 -0
- package/dist/test_v91_advanced.js +48 -0
- package/dist/test_v91_basics.d.ts +1 -0
- package/dist/test_v91_basics.js +54 -0
- package/dist/test_v91_performance.d.ts +1 -0
- package/dist/test_v91_performance.js +54 -0
- package/package.json +16 -3
package/dist/database/mysql.js
CHANGED
|
@@ -16,124 +16,44 @@ class MySQLDatabase extends IDatabase_1.IDatabase {
|
|
|
16
16
|
this._connectionPromise = new Promise(async (resolve, reject) => {
|
|
17
17
|
try {
|
|
18
18
|
const connection = await promise_1.default.createConnection({
|
|
19
|
-
host: config.host,
|
|
19
|
+
host: config.host || 'localhost',
|
|
20
20
|
port: config.port || 3306,
|
|
21
|
-
user: config.
|
|
21
|
+
user: config.username,
|
|
22
22
|
password: config.password,
|
|
23
|
-
typeCast: (field, next) => {
|
|
24
|
-
if (field.type === 'TINY' && field.length === 1) {
|
|
25
|
-
return (field.string() === '1');
|
|
26
|
-
}
|
|
27
|
-
if (['INT', 'DECIMAL', 'NEWDECIMAL', 'FLOAT', 'DOUBLE'].includes(field.type)) {
|
|
28
|
-
return Number(field.string());
|
|
29
|
-
}
|
|
30
|
-
return next();
|
|
31
|
-
}
|
|
32
23
|
});
|
|
33
|
-
await connection.query(`CREATE DATABASE IF NOT EXISTS
|
|
24
|
+
await connection.query(`CREATE DATABASE IF NOT EXISTS
|
|
34
25
|
${config.database}
|
|
35
26
|
`);
|
|
36
27
|
await connection.end();
|
|
37
28
|
this.pool = promise_1.default.createPool({
|
|
38
|
-
host: config.host,
|
|
29
|
+
host: config.host || 'localhost',
|
|
39
30
|
port: config.port || 3306,
|
|
40
|
-
user: config.
|
|
31
|
+
user: config.username,
|
|
41
32
|
password: config.password,
|
|
42
33
|
database: config.database,
|
|
43
34
|
waitForConnections: true,
|
|
44
|
-
connectionLimit: config.
|
|
35
|
+
connectionLimit: config.poolSize || 10,
|
|
45
36
|
queueLimit: 0,
|
|
46
|
-
typeCast: (field, next) => {
|
|
47
|
-
if (field.type === 'TINY' && field.length === 1) {
|
|
48
|
-
return (field.string() === '1');
|
|
49
|
-
}
|
|
50
|
-
if (['INT', 'DECIMAL', 'NEWDECIMAL', 'FLOAT', 'DOUBLE'].includes(field.type)) {
|
|
51
|
-
return Number(field.string());
|
|
52
|
-
}
|
|
53
|
-
return next();
|
|
54
|
-
}
|
|
55
37
|
});
|
|
56
38
|
this._connected = true;
|
|
57
39
|
resolve(this.pool);
|
|
58
40
|
this._processQueue();
|
|
59
41
|
}
|
|
60
42
|
catch (error) {
|
|
61
|
-
console.error("MySQL connection error:", error);
|
|
62
43
|
reject(error);
|
|
63
44
|
}
|
|
64
45
|
});
|
|
65
46
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (Number.isInteger(value)) {
|
|
73
|
-
if (value >= -128 && value <= 127)
|
|
74
|
-
return 'TINYINT';
|
|
75
|
-
if (value >= -32768 && value <= 32767)
|
|
76
|
-
return 'SMALLINT';
|
|
77
|
-
if (value >= -2147483648 && value <= 2147483647)
|
|
78
|
-
return 'INT';
|
|
79
|
-
return 'BIGINT';
|
|
80
|
-
}
|
|
81
|
-
return 'DOUBLE';
|
|
82
|
-
}
|
|
83
|
-
if (typeof value === 'string') {
|
|
84
|
-
const length = value.length;
|
|
85
|
-
if (length <= 255)
|
|
86
|
-
return 'VARCHAR(255)';
|
|
87
|
-
if (length <= 65535)
|
|
88
|
-
return 'TEXT';
|
|
89
|
-
if (length <= 16777215)
|
|
90
|
-
return 'MEDIUMTEXT';
|
|
91
|
-
return 'LONGTEXT';
|
|
92
|
-
}
|
|
93
|
-
if (typeof value === 'object') {
|
|
94
|
-
const jsonString = JSON.stringify(value);
|
|
95
|
-
return jsonString.length <= 65535 ? 'JSON' : 'LONGTEXT';
|
|
96
|
-
}
|
|
97
|
-
if (value instanceof Date)
|
|
98
|
-
return 'DATETIME';
|
|
99
|
-
return 'TEXT';
|
|
100
|
-
}
|
|
101
|
-
_getBestColumnType(values) {
|
|
102
|
-
const types = values.map(val => this._getColumnType(val));
|
|
103
|
-
const uniqueTypes = [...new Set(types)];
|
|
104
|
-
if (uniqueTypes.length === 1)
|
|
105
|
-
return uniqueTypes[0];
|
|
106
|
-
const typePriority = {
|
|
107
|
-
'LONGTEXT': 10, 'MEDIUMTEXT': 9, 'TEXT': 8, 'JSON': 7, 'VARCHAR(255)': 6,
|
|
108
|
-
'DATETIME': 5, 'DOUBLE': 4, 'BIGINT': 3, 'INT': 2, 'SMALLINT': 1, 'TINYINT': 0, 'BOOLEAN': -1
|
|
47
|
+
async _execute(op, table, fn) {
|
|
48
|
+
const start = Date.now();
|
|
49
|
+
const execute = async () => {
|
|
50
|
+
const res = await fn();
|
|
51
|
+
this.recordMetric(op, table, Date.now() - start);
|
|
52
|
+
return res;
|
|
109
53
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const existingColumns = await this.query(`DESCRIBE
|
|
114
|
-
${table}
|
|
115
|
-
`).catch(() => []);
|
|
116
|
-
const existingColumnNames = existingColumns.map((col) => col.Field);
|
|
117
|
-
for (const key of Object.keys(data)) {
|
|
118
|
-
if (!existingColumnNames.includes(key)) {
|
|
119
|
-
const columnType = this._getColumnType(data[key]);
|
|
120
|
-
await this.query(`ALTER TABLE
|
|
121
|
-
${table}
|
|
122
|
-
ADD COLUMN
|
|
123
|
-
${key}
|
|
124
|
-
${columnType}`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
async _queueRequest(operation) {
|
|
129
|
-
if (this._connected) {
|
|
130
|
-
return operation();
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
return new Promise((resolve, reject) => {
|
|
134
|
-
this._queue.push({ operation, resolve, reject });
|
|
135
|
-
});
|
|
136
|
-
}
|
|
54
|
+
if (this._connected)
|
|
55
|
+
return execute();
|
|
56
|
+
return new Promise((resolve, reject) => this._queue.push({ operation: execute, resolve, reject }));
|
|
137
57
|
}
|
|
138
58
|
async _processQueue() {
|
|
139
59
|
if (!this._connected)
|
|
@@ -142,8 +62,7 @@ ${key}
|
|
|
142
62
|
const item = this._queue.shift();
|
|
143
63
|
if (item) {
|
|
144
64
|
try {
|
|
145
|
-
|
|
146
|
-
item.resolve(result);
|
|
65
|
+
item.resolve(await item.operation());
|
|
147
66
|
}
|
|
148
67
|
catch (error) {
|
|
149
68
|
item.reject(error);
|
|
@@ -152,162 +71,129 @@ ${key}
|
|
|
152
71
|
}
|
|
153
72
|
}
|
|
154
73
|
async query(sql, params = []) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return rows;
|
|
159
|
-
});
|
|
74
|
+
const pool = await this._connectionPromise;
|
|
75
|
+
const [rows] = await pool.execute(sql, params);
|
|
76
|
+
return rows;
|
|
160
77
|
}
|
|
161
|
-
async
|
|
162
|
-
|
|
163
|
-
const escapedTable = promise_1.default.escape(table);
|
|
164
|
-
const tables = await this.query(`SHOW TABLES LIKE ${escapedTable}`);
|
|
165
|
-
if (tables.length === 0) {
|
|
166
|
-
const columnDefinitions = Object.keys(data).map(col => {
|
|
167
|
-
const columnType = this._getColumnType(data[col]);
|
|
168
|
-
return `
|
|
169
|
-
${col}
|
|
170
|
-
${columnType}`;
|
|
171
|
-
});
|
|
172
|
-
const columnsPart = columnDefinitions.length > 0 ? ', ' + columnDefinitions.join(", ") : '';
|
|
173
|
-
const createTableSQL = `
|
|
174
|
-
CREATE TABLE
|
|
78
|
+
async _ensureMissingColumns(table, data) {
|
|
79
|
+
const existingColumns = await this.query(`DESCRIBE
|
|
175
80
|
${table}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
81
|
+
`).catch(() => []);
|
|
82
|
+
const columnNames = existingColumns.map(col => col.Field);
|
|
83
|
+
for (const key of Object.keys(data)) {
|
|
84
|
+
if (key !== '_id' && !columnNames.includes(key)) {
|
|
85
|
+
await this.query(`ALTER TABLE
|
|
86
|
+
${table}
|
|
87
|
+
ADD COLUMN
|
|
88
|
+
${key}
|
|
89
|
+
TEXT`);
|
|
182
90
|
}
|
|
183
|
-
}
|
|
91
|
+
}
|
|
184
92
|
}
|
|
185
|
-
async
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
93
|
+
async ensureTable(table, data = {}) {
|
|
94
|
+
const escapedTable = promise_1.default.escape(table);
|
|
95
|
+
const tables = await this.query(`SHOW TABLES LIKE ${escapedTable}`);
|
|
96
|
+
if (tables.length === 0) {
|
|
97
|
+
const defs = Object.keys(data).map(k => `
|
|
98
|
+
${k}
|
|
99
|
+
TEXT`);
|
|
100
|
+
const columnsPart = defs.length > 0 ? ', ' + defs.join(", ") : '';
|
|
101
|
+
await this.query(`CREATE TABLE
|
|
191
102
|
${table}
|
|
192
|
-
`);
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
103
|
+
(_id INT PRIMARY KEY AUTO_INCREMENT ${columnsPart}) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
await this._ensureMissingColumns(table, data);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async insert(table, data) {
|
|
110
|
+
await this.runHooks('beforeInsert', table, data);
|
|
111
|
+
return this._execute('insert', table, async () => {
|
|
112
|
+
await this.ensureTable(table, data);
|
|
113
|
+
const keys = Object.keys(data);
|
|
114
|
+
const sql = `INSERT INTO
|
|
201
115
|
${table}
|
|
202
116
|
(${keys.map(k => `
|
|
203
117
|
${k}
|
|
204
|
-
`).join(",")}) VALUES (${
|
|
205
|
-
const result = await this.query(sql, values);
|
|
118
|
+
`).join(",")}) VALUES (${keys.map(() => '?').join(",")})`;
|
|
119
|
+
const result = await this.query(sql, Object.values(data).map(v => typeof v === 'object' ? JSON.stringify(v) : v));
|
|
120
|
+
const finalData = { _id: result.insertId, ...data };
|
|
121
|
+
await this.runHooks('afterInsert', table, finalData);
|
|
206
122
|
return result.insertId;
|
|
207
123
|
});
|
|
208
124
|
}
|
|
209
125
|
async update(table, data, where) {
|
|
210
|
-
|
|
126
|
+
await this.runHooks('beforeUpdate', table, { data, where });
|
|
127
|
+
return this._execute('update', table, async () => {
|
|
211
128
|
await this.ensureTable(table, { ...data, ...where });
|
|
212
|
-
|
|
213
|
-
const setString = Object.keys(data).map(k => `
|
|
129
|
+
const set = Object.keys(data).map(k => `
|
|
214
130
|
${k}
|
|
215
131
|
= ?`).join(", ");
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
= ?`).join(" AND ");
|
|
219
|
-
const sql = `UPDATE
|
|
132
|
+
const wKeys = Object.keys(where);
|
|
133
|
+
const sql = `UPDATE
|
|
220
134
|
${table}
|
|
221
|
-
SET ${
|
|
222
|
-
|
|
223
|
-
|
|
135
|
+
SET ${set} WHERE ${wKeys.map(k => `
|
|
136
|
+
${k}
|
|
137
|
+
= ?`).join(" AND ")}`;
|
|
138
|
+
const result = await this.query(sql, [...Object.values(data).map(v => typeof v === 'object' ? JSON.stringify(v) : v), ...Object.values(where).map(v => typeof v === 'object' ? JSON.stringify(v) : v)]);
|
|
224
139
|
return result.affectedRows;
|
|
225
140
|
});
|
|
226
141
|
}
|
|
227
142
|
async delete(table, where) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const whereString = Object.keys(where).map(k => `
|
|
234
|
-
${k}
|
|
235
|
-
= ?`).join(" AND ");
|
|
236
|
-
const sql = `DELETE FROM
|
|
143
|
+
await this.runHooks('beforeDelete', table, where);
|
|
144
|
+
return this._execute('delete', table, async () => {
|
|
145
|
+
await this.ensureTable(table, where);
|
|
146
|
+
const keys = Object.keys(where);
|
|
147
|
+
const sql = `DELETE FROM
|
|
237
148
|
${table}
|
|
238
|
-
WHERE ${
|
|
239
|
-
|
|
149
|
+
WHERE ${keys.map(k => `
|
|
150
|
+
${k}
|
|
151
|
+
= ?`).join(" AND ")}`;
|
|
152
|
+
const result = await this.query(sql, Object.values(where).map(v => typeof v === 'object' ? JSON.stringify(v) : v));
|
|
240
153
|
return result.affectedRows;
|
|
241
154
|
});
|
|
242
155
|
}
|
|
243
156
|
async select(table, where = null) {
|
|
244
|
-
return this.
|
|
157
|
+
return this._execute('select', table, async () => {
|
|
245
158
|
await this.ensureTable(table, where || {});
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
let sql = `SELECT * FROM
|
|
159
|
+
const keys = where ? Object.keys(where) : [];
|
|
160
|
+
const sql = `SELECT * FROM
|
|
250
161
|
${table}
|
|
251
|
-
|
|
252
|
-
let params = [];
|
|
253
|
-
if (where && Object.keys(where).length > 0) {
|
|
254
|
-
const whereString = Object.keys(where).map(k => `
|
|
162
|
+
` + (keys.length ? ' WHERE ' + keys.map(k => `
|
|
255
163
|
${k}
|
|
256
|
-
= ?`).join(" AND ");
|
|
257
|
-
|
|
258
|
-
params = Object.values(where).map(v => this._serializeValue(v));
|
|
259
|
-
}
|
|
260
|
-
const rows = await this.query(sql, params);
|
|
164
|
+
= ?`).join(" AND ") : '');
|
|
165
|
+
const rows = await this.query(sql, keys.map(k => typeof where[k] === 'object' ? JSON.stringify(where[k]) : where[k]));
|
|
261
166
|
return rows.map((row) => {
|
|
262
|
-
const
|
|
263
|
-
for (const
|
|
264
|
-
|
|
167
|
+
const nr = {};
|
|
168
|
+
for (const k in row) {
|
|
169
|
+
try {
|
|
170
|
+
nr[k] = JSON.parse(row[k]);
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
nr[k] = row[k];
|
|
174
|
+
}
|
|
265
175
|
}
|
|
266
|
-
return
|
|
176
|
+
return nr;
|
|
267
177
|
});
|
|
268
178
|
});
|
|
269
179
|
}
|
|
270
|
-
async set(table, data, where) {
|
|
271
|
-
return this._queueRequest(async () => {
|
|
272
|
-
const existing = await this.select(table, where);
|
|
273
|
-
if (existing.length === 0) {
|
|
274
|
-
return await this.insert(table, { ...where, ...data });
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
return await this.update(table, data, where);
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
180
|
async selectOne(table, where = null) {
|
|
282
|
-
const
|
|
283
|
-
return
|
|
181
|
+
const res = await this.select(table, where);
|
|
182
|
+
return res[0] || null;
|
|
183
|
+
}
|
|
184
|
+
async set(table, data, where) {
|
|
185
|
+
const existing = await this.selectOne(table, where);
|
|
186
|
+
return existing ? this.update(table, data, where) : this.insert(table, { ...where, ...data });
|
|
284
187
|
}
|
|
285
188
|
async bulkInsert(table, dataArray) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
189
|
+
if (!dataArray.length)
|
|
190
|
+
return 0;
|
|
191
|
+
return this._execute('bulkInsert', table, async () => {
|
|
289
192
|
await this.ensureTable(table, dataArray[0]);
|
|
290
|
-
const
|
|
291
|
-
${table}
|
|
292
|
-
`);
|
|
293
|
-
const existingColumnNames = existingColumns.map((col) => col.Field);
|
|
294
|
-
const allKeys = new Set();
|
|
295
|
-
dataArray.forEach(obj => Object.keys(obj).forEach(key => allKeys.add(key)));
|
|
296
|
-
for (const key of allKeys) {
|
|
297
|
-
if (!existingColumnNames.includes(key)) {
|
|
298
|
-
const columnValues = dataArray.map(obj => obj[key]).filter(val => val !== undefined && val !== null);
|
|
299
|
-
const columnType = columnValues.length > 0 ? this._getBestColumnType(columnValues) : 'TEXT';
|
|
300
|
-
await this.query(`ALTER TABLE
|
|
301
|
-
${table}
|
|
302
|
-
ADD COLUMN
|
|
303
|
-
${key}
|
|
304
|
-
${columnType}`);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
const keys = Array.from(allKeys);
|
|
193
|
+
const keys = Object.keys(dataArray[0]);
|
|
308
194
|
const placeholders = dataArray.map(() => `(${keys.map(() => '?').join(',')})`).join(',');
|
|
309
195
|
const values = dataArray.flatMap(obj => keys.map(k => this._serializeValue(obj[k])));
|
|
310
|
-
const sql = `INSERT INTO
|
|
196
|
+
const sql = `INSERT INTO
|
|
311
197
|
${table}
|
|
312
198
|
(${keys.map(k => `
|
|
313
199
|
${k}
|
|
@@ -316,69 +202,62 @@ ${k}
|
|
|
316
202
|
return result.affectedRows;
|
|
317
203
|
});
|
|
318
204
|
}
|
|
319
|
-
async
|
|
320
|
-
|
|
321
|
-
await this.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (Array.isArray(value) || (typeof value === 'object' && value !== null))
|
|
327
|
-
return JSON.stringify(value);
|
|
328
|
-
return value;
|
|
329
|
-
}
|
|
330
|
-
_deserializeValue(value) {
|
|
331
|
-
try {
|
|
332
|
-
if (typeof value === 'string' && (value.startsWith('[') || value.startsWith('{'))) {
|
|
333
|
-
const parsed = JSON.parse(value);
|
|
334
|
-
if (typeof parsed === 'object' && parsed !== null)
|
|
335
|
-
return parsed;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
catch (e) { }
|
|
339
|
-
return value;
|
|
340
|
-
}
|
|
341
|
-
async increment(table, increments, where = {}) {
|
|
342
|
-
return this._queueRequest(async () => {
|
|
343
|
-
const incrementClauses = Object.keys(increments).map(field => `
|
|
344
|
-
${field}
|
|
345
|
-
=
|
|
346
|
-
${field}
|
|
205
|
+
async increment(table, incs, where) {
|
|
206
|
+
return this._execute('increment', table, async () => {
|
|
207
|
+
await this.ensureTable(table, where);
|
|
208
|
+
const set = Object.keys(incs).map(f => `
|
|
209
|
+
${f}
|
|
210
|
+
=
|
|
211
|
+
${f}
|
|
347
212
|
+ ?`).join(', ');
|
|
348
|
-
const
|
|
349
|
-
const
|
|
350
|
-
const sql = `UPDATE
|
|
213
|
+
const wKeys = Object.keys(where);
|
|
214
|
+
const sql = `UPDATE
|
|
351
215
|
${table}
|
|
352
|
-
SET ${
|
|
353
|
-
|
|
216
|
+
SET ${set} WHERE ${wKeys.map(k => `
|
|
217
|
+
${k}
|
|
218
|
+
= ?`).join(" AND ")}`;
|
|
219
|
+
const result = await this.query(sql, [...Object.values(incs), ...Object.values(where).map(v => typeof v === 'object' ? JSON.stringify(v) : v)]);
|
|
354
220
|
return result.affectedRows;
|
|
355
221
|
});
|
|
356
222
|
}
|
|
357
|
-
async decrement(table,
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
223
|
+
async decrement(table, decs, where) {
|
|
224
|
+
const incs = {};
|
|
225
|
+
for (const k in decs)
|
|
226
|
+
incs[k] = -decs[k];
|
|
227
|
+
return this.increment(table, incs, where);
|
|
228
|
+
}
|
|
229
|
+
async close() { if (this.pool)
|
|
230
|
+
await this.pool.end(); }
|
|
231
|
+
_getColumnType(v) {
|
|
232
|
+
if (v === null || v === undefined)
|
|
233
|
+
return 'TEXT';
|
|
234
|
+
if (typeof v === 'boolean')
|
|
235
|
+
return 'BOOLEAN';
|
|
236
|
+
if (typeof v === 'number')
|
|
237
|
+
return Number.isInteger(v) ? 'INT' : 'DOUBLE';
|
|
238
|
+
if (v instanceof Date)
|
|
239
|
+
return 'DATETIME';
|
|
240
|
+
if (typeof v === 'object')
|
|
241
|
+
return 'JSON';
|
|
242
|
+
return 'TEXT';
|
|
372
243
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
244
|
+
_serializeValue(v) {
|
|
245
|
+
if (v instanceof Date)
|
|
246
|
+
return v.toISOString().slice(0, 19).replace('T', ' ');
|
|
247
|
+
return (typeof v === 'object' && v !== null) ? JSON.stringify(v) : v;
|
|
248
|
+
}
|
|
249
|
+
_buildWhereClause(where) {
|
|
250
|
+
if (!where)
|
|
376
251
|
return { whereClause: '', values: [] };
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
252
|
+
const keys = Object.keys(where);
|
|
253
|
+
if (!keys.length)
|
|
254
|
+
return { whereClause: '', values: [] };
|
|
255
|
+
return {
|
|
256
|
+
whereClause: 'WHERE ' + keys.map(k => `
|
|
257
|
+
${k}
|
|
258
|
+
= ?`).join(' AND '),
|
|
259
|
+
values: Object.values(where).map(v => this._serializeValue(v))
|
|
260
|
+
};
|
|
382
261
|
}
|
|
383
262
|
}
|
|
384
263
|
exports.MySQLDatabase = MySQLDatabase;
|
package/dist/database/pg.d.ts
CHANGED
|
@@ -7,24 +7,22 @@ export declare class PostgreSQLDatabase extends IDatabase {
|
|
|
7
7
|
private _connected;
|
|
8
8
|
private _connectionPromise;
|
|
9
9
|
constructor(config: PostgreSQLConfig);
|
|
10
|
-
private
|
|
11
|
-
private _getBestColumnType;
|
|
12
|
-
private _ensureMissingColumns;
|
|
13
|
-
private _queueRequest;
|
|
10
|
+
private _execute;
|
|
14
11
|
private _processQueue;
|
|
15
12
|
query(sql: string, params?: any[]): Promise<any>;
|
|
16
|
-
ensureTable(table: string, data?:
|
|
17
|
-
insert(table: string, data:
|
|
18
|
-
update(table: string, data:
|
|
19
|
-
delete(table: string, where:
|
|
20
|
-
select<T = any>(table: string, where?:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
bulkInsert(table: string, dataArray:
|
|
13
|
+
ensureTable(table: string, data?: any): Promise<void>;
|
|
14
|
+
insert(table: string, data: any): Promise<any>;
|
|
15
|
+
update(table: string, data: any, where: any): Promise<number>;
|
|
16
|
+
delete(table: string, where: any): Promise<number>;
|
|
17
|
+
select<T = any>(table: string, where?: any): Promise<T[]>;
|
|
18
|
+
selectOne<T = any>(table: string, where?: any): Promise<T | null>;
|
|
19
|
+
set(table: string, data: any, where: any): Promise<any>;
|
|
20
|
+
bulkInsert(table: string, dataArray: any[]): Promise<number>;
|
|
21
|
+
increment(table: string, incs: Record<string, number>, where: any): Promise<number>;
|
|
22
|
+
decrement(table: string, decs: Record<string, number>, where: any): Promise<number>;
|
|
24
23
|
close(): Promise<void>;
|
|
24
|
+
private _getColumnType;
|
|
25
25
|
private _serializeValue;
|
|
26
|
-
increment(table: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
|
|
27
|
-
decrement(table: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
|
|
28
26
|
private _buildWhereClause;
|
|
29
27
|
}
|
|
30
28
|
export default PostgreSQLDatabase;
|