ueberdb2 1.4.16
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/.github/workflows/npmpublish.yml +103 -0
- package/.travis.yml +46 -0
- package/CHANGELOG.md +167 -0
- package/CONTRIBUTING.md +103 -0
- package/LICENSE +202 -0
- package/README.md +356 -0
- package/SECURITY.md +5 -0
- package/databases/cassandra_db.js +250 -0
- package/databases/couch_db.js +201 -0
- package/databases/dirty_db.js +80 -0
- package/databases/dirty_git_db.js +78 -0
- package/databases/elasticsearch_db.js +288 -0
- package/databases/mock_db.js +42 -0
- package/databases/mongodb_db.js +136 -0
- package/databases/mssql_db.js +218 -0
- package/databases/mysql_db.js +178 -0
- package/databases/postgres_db.js +198 -0
- package/databases/postgrespool_db.js +11 -0
- package/databases/redis_db.js +128 -0
- package/databases/rethink_db.js +98 -0
- package/databases/sqlite_db.js +158 -0
- package/index.js +191 -0
- package/lib/AbstractDatabase.js +32 -0
- package/lib/CacheAndBufferLayer.js +610 -0
- package/package.json +122 -0
- package/test/lib/databases.js +62 -0
- package/test/lib/mysql.sql +84 -0
- package/test/test.js +312 -0
- package/test/test_bulk.js +71 -0
- package/test/test_lru.js +145 -0
- package/test/test_metrics.js +733 -0
- package/test/test_mysql.js +68 -0
- package/test/test_postgres.js +17 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 2020 Sylchauf
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
19
|
+
|
|
20
|
+
exports.Database = class extends AbstractDatabase {
|
|
21
|
+
constructor(settings) {
|
|
22
|
+
super();
|
|
23
|
+
this.settings = settings;
|
|
24
|
+
|
|
25
|
+
if (!this.settings.url) throw new Error('You must specify a mongodb url');
|
|
26
|
+
// For backwards compatibility:
|
|
27
|
+
if (this.settings.database == null) this.settings.database = this.settings.dbName;
|
|
28
|
+
|
|
29
|
+
if (!this.settings.collection) this.settings.collection = 'ueberdb';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
clearPing() {
|
|
33
|
+
if (this.interval) {
|
|
34
|
+
clearInterval(this.interval);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
schedulePing() {
|
|
39
|
+
this.clearPing();
|
|
40
|
+
this.interval = setInterval(() => {
|
|
41
|
+
this.database.command({
|
|
42
|
+
ping: 1,
|
|
43
|
+
});
|
|
44
|
+
}, 10000);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
init(callback) {
|
|
48
|
+
const MongoClient = require('mongodb').MongoClient;
|
|
49
|
+
|
|
50
|
+
MongoClient.connect(this.settings.url, (err, client) => {
|
|
51
|
+
if (!err) {
|
|
52
|
+
this.client = client;
|
|
53
|
+
this.database = client.db(this.settings.database);
|
|
54
|
+
this.collection = this.database.collection(this.settings.collection);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
callback(err);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
this.schedulePing();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get(key, callback) {
|
|
64
|
+
this.collection.findOne({_id: key}, (err, document) => {
|
|
65
|
+
if (err) callback(err);
|
|
66
|
+
else callback(null, document ? document.value : null);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.schedulePing();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
findKeys(key, notKey, callback) {
|
|
73
|
+
const selector = {
|
|
74
|
+
$and: [
|
|
75
|
+
{_id: {$regex: `${key.replace(/\*/g, '')}`}},
|
|
76
|
+
],
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
if (notKey) {
|
|
80
|
+
selector.$and.push({_id: {$not: {$regex: `${notKey.replace(/\*/g, '')}`}}});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.collection.find(selector, async (err, res) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
callback(err);
|
|
86
|
+
} else {
|
|
87
|
+
const data = await res.toArray();
|
|
88
|
+
|
|
89
|
+
callback(null, data.map((i) => i._id));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
this.schedulePing();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
set(key, value, callback) {
|
|
97
|
+
if (key.length > 100) {
|
|
98
|
+
callback('Your Key can only be 100 chars');
|
|
99
|
+
} else {
|
|
100
|
+
this.collection.update({_id: key}, {$set: {value}}, {upsert: true}, callback);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
this.schedulePing();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
remove(key, callback) {
|
|
107
|
+
this.collection.remove({_id: key}, callback);
|
|
108
|
+
|
|
109
|
+
this.schedulePing();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
doBulk(bulk, callback) {
|
|
113
|
+
const bulkMongo = this.collection.initializeOrderedBulkOp();
|
|
114
|
+
|
|
115
|
+
for (const i in bulk) {
|
|
116
|
+
if (bulk[i].type === 'set') {
|
|
117
|
+
bulkMongo.find({_id: bulk[i].key}).upsert().updateOne({$set: {value: bulk[i].value}});
|
|
118
|
+
} else if (bulk[i].type === 'remove') {
|
|
119
|
+
bulkMongo.find({_id: bulk[i].key}).deleteOne();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
bulkMongo.execute().then((res) => {
|
|
124
|
+
callback(null, res);
|
|
125
|
+
}).catch((error) => {
|
|
126
|
+
callback(error);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
this.schedulePing();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
close(callback) {
|
|
133
|
+
this.clearPing();
|
|
134
|
+
this.client.close(callback);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/* eslint new-cap: ["error", {"capIsNewExceptions": ["mssql.NVarChar"]}] */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 2019 - exspecto@gmail.com
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
* Note: This requires MS SQL Server >= 2008 due to the usage of the MERGE statement
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
25
|
+
const async = require('async');
|
|
26
|
+
const mssql = require('mssql');
|
|
27
|
+
|
|
28
|
+
exports.Database = class extends AbstractDatabase {
|
|
29
|
+
constructor(settings) {
|
|
30
|
+
super();
|
|
31
|
+
settings = settings || {};
|
|
32
|
+
|
|
33
|
+
if (settings.json != null) {
|
|
34
|
+
settings.parseJSON = settings.json;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// set the request timeout to 5 minutes
|
|
38
|
+
settings.requestTimeout = 300000;
|
|
39
|
+
|
|
40
|
+
settings.server = settings.host;
|
|
41
|
+
this.settings = settings;
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
Turning off the cache and write buffer here. You
|
|
45
|
+
can reenable it, but also take a look at maxInserts in
|
|
46
|
+
the doBulk function to decide how you want to split it up.
|
|
47
|
+
*/
|
|
48
|
+
this.settings.cache = 0;
|
|
49
|
+
this.settings.writeInterval = 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
init(callback) {
|
|
53
|
+
const sqlCreate =
|
|
54
|
+
"IF OBJECT_ID(N'dbo.store', N'U') IS NULL" +
|
|
55
|
+
' BEGIN' +
|
|
56
|
+
' CREATE TABLE [store] (' +
|
|
57
|
+
' [key] NVARCHAR(100) PRIMARY KEY,' +
|
|
58
|
+
' [value] NTEXT NOT NULL' +
|
|
59
|
+
' );' +
|
|
60
|
+
' END';
|
|
61
|
+
|
|
62
|
+
new mssql.ConnectionPool(this.settings).connect().then((pool) => {
|
|
63
|
+
this.db = pool;
|
|
64
|
+
|
|
65
|
+
const request = new mssql.Request(this.db);
|
|
66
|
+
|
|
67
|
+
request.query(sqlCreate, (err) => {
|
|
68
|
+
callback(err);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
this.db.on('error', (err) => {
|
|
72
|
+
console.log(err);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get(key, callback) {
|
|
78
|
+
const request = new mssql.Request(this.db);
|
|
79
|
+
|
|
80
|
+
request.input('key', mssql.NVarChar(100), key);
|
|
81
|
+
|
|
82
|
+
request.query('SELECT [value] FROM [store] WHERE [key] = @key', (err, results) => {
|
|
83
|
+
let value = null;
|
|
84
|
+
|
|
85
|
+
if (!err && results.rowsAffected[0] === 1) {
|
|
86
|
+
value = results.recordset[0].value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
callback(err, value);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
findKeys(key, notKey, callback) {
|
|
94
|
+
const request = new mssql.Request(this.db);
|
|
95
|
+
let query = 'SELECT [key] FROM [store] WHERE [key] LIKE @key';
|
|
96
|
+
|
|
97
|
+
// desired keys are key, e.g. pad:%
|
|
98
|
+
key = key.replace(/\*/g, '%');
|
|
99
|
+
|
|
100
|
+
request.input('key', mssql.NVarChar(100), key);
|
|
101
|
+
|
|
102
|
+
if (notKey != null) {
|
|
103
|
+
// not desired keys are notKey, e.g. %:%:%
|
|
104
|
+
notKey = notKey.replace(/\*/g, '%');
|
|
105
|
+
request.input('notkey', mssql.NVarChar(100), notKey);
|
|
106
|
+
query += ' AND [key] NOT LIKE @notkey';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
request.query(query, (err, results) => {
|
|
110
|
+
const value = [];
|
|
111
|
+
|
|
112
|
+
if (!err && results.rowsAffected[0] > 0) {
|
|
113
|
+
for (let i = 0; i < results.recordset.length; i++) {
|
|
114
|
+
value.push(results.recordset[i].key);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
callback(err, value);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
set(key, value, callback) {
|
|
123
|
+
const request = new mssql.Request(this.db);
|
|
124
|
+
|
|
125
|
+
if (key.length > 100) {
|
|
126
|
+
callback('Your Key can only be 100 chars');
|
|
127
|
+
} else {
|
|
128
|
+
const query =
|
|
129
|
+
'MERGE [store] t USING (SELECT @key [key], @value [value]) s' +
|
|
130
|
+
' ON t.[key] = s.[key]' +
|
|
131
|
+
' WHEN MATCHED AND s.[value] IS NOT NULL THEN UPDATE SET t.[value] = s.[value]' +
|
|
132
|
+
' WHEN NOT MATCHED THEN INSERT ([key], [value]) VALUES (s.[key], s.[value]);';
|
|
133
|
+
|
|
134
|
+
request.input('key', mssql.NVarChar(100), key);
|
|
135
|
+
request.input('value', mssql.NText, value);
|
|
136
|
+
|
|
137
|
+
request.query(query, (err, info) => {
|
|
138
|
+
callback(err);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
remove(key, callback) {
|
|
144
|
+
const request = new mssql.Request(this.db);
|
|
145
|
+
request.input('key', mssql.NVarChar(100), key);
|
|
146
|
+
request.query('DELETE FROM [store] WHERE [key] = @key', callback);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
doBulk(bulk, callback) {
|
|
150
|
+
const maxInserts = 100;
|
|
151
|
+
const request = new mssql.Request(this.db);
|
|
152
|
+
let firstReplace = true;
|
|
153
|
+
let firstRemove = true;
|
|
154
|
+
const replacements = [];
|
|
155
|
+
let removeSQL = 'DELETE FROM [store] WHERE [key] IN (';
|
|
156
|
+
|
|
157
|
+
for (const i in bulk) {
|
|
158
|
+
if (bulk[i].type === 'set') {
|
|
159
|
+
if (firstReplace) {
|
|
160
|
+
replacements.push('BEGIN TRANSACTION;');
|
|
161
|
+
firstReplace = false;
|
|
162
|
+
} else if (i % maxInserts === 0) {
|
|
163
|
+
replacements.push('\nCOMMIT TRANSACTION;\nBEGIN TRANSACTION;\n');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
replacements.push(
|
|
167
|
+
`MERGE [store] t USING (SELECT '${bulk[i].key}' [key], '${bulk[i].value}' [value]) s`,
|
|
168
|
+
'ON t.[key] = s.[key]',
|
|
169
|
+
'WHEN MATCHED AND s.[value] IS NOT NULL THEN UPDATE SET t.[value] = s.[value]',
|
|
170
|
+
'WHEN NOT MATCHED THEN INSERT ([key], [value]) VALUES (s.[key], s.[value]);');
|
|
171
|
+
} else if (bulk[i].type === 'remove') {
|
|
172
|
+
if (!firstRemove) {
|
|
173
|
+
removeSQL += ',';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
firstRemove = false;
|
|
177
|
+
removeSQL += `'${bulk[i].key}'`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
removeSQL += ');';
|
|
182
|
+
replacements.push('COMMIT TRANSACTION;');
|
|
183
|
+
|
|
184
|
+
async.parallel(
|
|
185
|
+
[
|
|
186
|
+
(callback) => {
|
|
187
|
+
if (!firstReplace) {
|
|
188
|
+
request.batch(replacements.join('\n'), (err, results) => {
|
|
189
|
+
if (err) {
|
|
190
|
+
callback(err);
|
|
191
|
+
}
|
|
192
|
+
callback(err, results);
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
callback();
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
(callback) => {
|
|
199
|
+
if (!firstRemove) {
|
|
200
|
+
request.query(removeSQL, callback);
|
|
201
|
+
} else {
|
|
202
|
+
callback();
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
(err, results) => {
|
|
207
|
+
if (err) {
|
|
208
|
+
callback(err);
|
|
209
|
+
}
|
|
210
|
+
callback(err, results);
|
|
211
|
+
}
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
close(callback) {
|
|
216
|
+
this.db.close(callback);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 2011 Peter 'Pita' Martischka
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
19
|
+
const mysql = require('mysql');
|
|
20
|
+
const util = require('util');
|
|
21
|
+
|
|
22
|
+
exports.Database = class extends AbstractDatabase {
|
|
23
|
+
constructor(settings) {
|
|
24
|
+
super();
|
|
25
|
+
this.logger = console;
|
|
26
|
+
this._mysqlSettings = {
|
|
27
|
+
charset: 'utf8mb4', // temp hack needs a proper fix..
|
|
28
|
+
...settings,
|
|
29
|
+
};
|
|
30
|
+
this.settings = {
|
|
31
|
+
engine: 'InnoDB',
|
|
32
|
+
// Limit the query size to avoid timeouts or other failures.
|
|
33
|
+
bulkLimit: 100,
|
|
34
|
+
json: true,
|
|
35
|
+
queryTimeout: 60000,
|
|
36
|
+
};
|
|
37
|
+
this._pool = null; // Initialized in init();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get isAsync() { return true; }
|
|
41
|
+
|
|
42
|
+
async _query(options) {
|
|
43
|
+
try {
|
|
44
|
+
return await new Promise((resolve, reject) => {
|
|
45
|
+
options = {timeout: this.settings.queryTimeout, ...options};
|
|
46
|
+
this._pool.query(options, (err, ...args) => err != null ? reject(err) : resolve(args));
|
|
47
|
+
});
|
|
48
|
+
} catch (err) {
|
|
49
|
+
this.logger.error(`${err.fatal ? 'Fatal ' : ''}MySQL error: ${err.stack || err}`);
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async init() {
|
|
55
|
+
this._pool = mysql.createPool(this._mysqlSettings);
|
|
56
|
+
const {database, charset} = this._mysqlSettings;
|
|
57
|
+
|
|
58
|
+
const sqlCreate = `${'CREATE TABLE IF NOT EXISTS `store` ( ' +
|
|
59
|
+
'`key` VARCHAR( 100 ) NOT NULL COLLATE utf8mb4_bin, ' +
|
|
60
|
+
'`value` LONGTEXT COLLATE utf8mb4_bin NOT NULL , ' +
|
|
61
|
+
'PRIMARY KEY ( `key` ) ' +
|
|
62
|
+
') ENGINE='}${this.settings.engine} CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`;
|
|
63
|
+
|
|
64
|
+
const sqlAlter = 'ALTER TABLE store MODIFY `key` VARCHAR(100) COLLATE utf8mb4_bin;';
|
|
65
|
+
|
|
66
|
+
await this._query({sql: sqlCreate});
|
|
67
|
+
|
|
68
|
+
// Checks for Database charset et al
|
|
69
|
+
const dbCharSet =
|
|
70
|
+
'SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME ' +
|
|
71
|
+
`FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${database}'`;
|
|
72
|
+
let [result] = await this._query({sql: dbCharSet});
|
|
73
|
+
|
|
74
|
+
result = JSON.parse(JSON.stringify(result));
|
|
75
|
+
if (result[0].DEFAULT_CHARACTER_SET_NAME !== charset) {
|
|
76
|
+
this.logger.error(`Database is not configured with charset ${charset} -- ` +
|
|
77
|
+
'This may lead to crashes when certain characters are pasted in pads');
|
|
78
|
+
this.logger.log(result[0], charset);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (result[0].DEFAULT_COLLATION_NAME.indexOf(charset) === -1) {
|
|
82
|
+
this.logger.error(
|
|
83
|
+
`Database is not configured with collation name that includes ${charset} -- ` +
|
|
84
|
+
'This may lead to crashes when certain characters are pasted in pads');
|
|
85
|
+
this.logger.log(result[0], charset, result[0].DEFAULT_COLLATION_NAME);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const tableCharSet =
|
|
89
|
+
'SELECT CCSA.character_set_name AS character_set_name ' +
|
|
90
|
+
'FROM information_schema.`TABLES` ' +
|
|
91
|
+
'T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA ' +
|
|
92
|
+
'WHERE CCSA.collation_name = T.table_collation ' +
|
|
93
|
+
`AND T.table_schema = '${database}' ` +
|
|
94
|
+
"AND T.table_name = 'store'";
|
|
95
|
+
[result] = await this._query({sql: tableCharSet});
|
|
96
|
+
if (!result[0]) {
|
|
97
|
+
this.logger.warn('Data has no character_set_name value -- ' +
|
|
98
|
+
'This may lead to crashes when certain characters are pasted in pads');
|
|
99
|
+
}
|
|
100
|
+
if (result[0] && (result[0].character_set_name !== charset)) {
|
|
101
|
+
this.logger.error(`table is not configured with charset ${charset} -- ` +
|
|
102
|
+
'This may lead to crashes when certain characters are pasted in pads');
|
|
103
|
+
this.logger.log(result[0], charset);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// check migration level, alter if not migrated
|
|
107
|
+
const level = await this.get('MYSQL_MIGRATION_LEVEL');
|
|
108
|
+
|
|
109
|
+
if (level !== '1') {
|
|
110
|
+
await this._query({sql: sqlAlter});
|
|
111
|
+
await this.set('MYSQL_MIGRATION_LEVEL', '1');
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async get(key) {
|
|
116
|
+
const [results] = await this._query({
|
|
117
|
+
sql: 'SELECT `value` FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
|
|
118
|
+
values: [key, key],
|
|
119
|
+
});
|
|
120
|
+
return results.length === 1 ? results[0].value : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async findKeys(key, notKey) {
|
|
124
|
+
let query = 'SELECT `key` FROM `store` WHERE `key` LIKE ?';
|
|
125
|
+
const params = [];
|
|
126
|
+
|
|
127
|
+
// desired keys are key, e.g. pad:%
|
|
128
|
+
key = key.replace(/\*/g, '%');
|
|
129
|
+
params.push(key);
|
|
130
|
+
|
|
131
|
+
if (notKey != null) {
|
|
132
|
+
// not desired keys are notKey, e.g. %:%:%
|
|
133
|
+
notKey = notKey.replace(/\*/g, '%');
|
|
134
|
+
query += ' AND `key` NOT LIKE ?';
|
|
135
|
+
params.push(notKey);
|
|
136
|
+
}
|
|
137
|
+
const [results] = await this._query({sql: query, values: params});
|
|
138
|
+
return results.map((val) => val.key);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async set(key, value) {
|
|
142
|
+
if (key.length > 100) throw new Error('Your Key can only be 100 chars');
|
|
143
|
+
await this._query({sql: 'REPLACE INTO `store` VALUES (?,?)', values: [key, value]});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async remove(key) {
|
|
147
|
+
await this._query({
|
|
148
|
+
sql: 'DELETE FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
|
|
149
|
+
values: [key, key],
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async doBulk(bulk) {
|
|
154
|
+
const replaces = [];
|
|
155
|
+
const deletes = [];
|
|
156
|
+
for (const op of bulk) {
|
|
157
|
+
switch (op.type) {
|
|
158
|
+
case 'set': replaces.push([op.key, op.value]); break;
|
|
159
|
+
case 'remove': deletes.push(op.key); break;
|
|
160
|
+
default: throw new Error(`unknown op type: ${op.type}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
await Promise.all([
|
|
164
|
+
replaces.length ? this._query({
|
|
165
|
+
sql: 'REPLACE INTO `store` VALUES ?;',
|
|
166
|
+
values: [replaces],
|
|
167
|
+
}) : null,
|
|
168
|
+
deletes.length ? this._query({
|
|
169
|
+
sql: 'DELETE FROM `store` WHERE `key` IN (?) AND BINARY `key` IN (?);',
|
|
170
|
+
values: [deletes, deletes],
|
|
171
|
+
}) : null,
|
|
172
|
+
]);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async close() {
|
|
176
|
+
await util.promisify(this._pool.end.bind(this._pool))();
|
|
177
|
+
}
|
|
178
|
+
};
|