ueberdb2 4.1.23 → 4.1.26
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/dist/cassandra_db.d.ts +1 -0
- package/dist/couch_db.d.ts +1 -0
- package/dist/databases/cassandra_db.cjs +1 -0
- package/dist/databases/couch_db.cjs +1 -0
- package/dist/databases/dirty_db.cjs +1 -0
- package/dist/databases/dirty_git_db.cjs +1 -0
- package/dist/databases/elasticsearch_db.cjs +1 -0
- package/dist/databases/memory_db.cjs +1 -0
- package/dist/databases/mock_db.cjs +1 -0
- package/dist/databases/mongodb_db.cjs +1 -0
- package/dist/databases/mssql_db.cjs +5 -0
- package/dist/databases/mysql_db.cjs +1 -0
- package/dist/databases/postgres_db.cjs +1 -0
- package/dist/databases/postgrespool_db.cjs +1 -0
- package/dist/databases/redis_db.cjs +1 -0
- package/dist/databases/rethink_db.cjs +1 -0
- package/dist/databases/sqlite_db.cjs +4 -0
- package/dist/dirty_db.d.ts +1 -0
- package/dist/dirty_git_db.d.ts +1 -0
- package/dist/elasticsearch_db.d.ts +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/lib/AbstractDatabase.cjs +1 -0
- package/dist/lib/CacheAndBufferLayer.cjs +1 -0
- package/dist/lib/logging.cjs +1 -0
- package/dist/memory_db.d.ts +1 -0
- package/dist/mock_db.d.ts +1 -0
- package/dist/mongodb_db.d.ts +1 -0
- package/dist/mssql_db.d.ts +1 -0
- package/dist/mysql_db.d.ts +1 -0
- package/dist/postgres_db.d.ts +1 -0
- package/dist/postgrespool_db.d.ts +1 -0
- package/dist/redis_db.d.ts +1 -0
- package/dist/rethink_db.d.ts +1 -0
- package/dist/sqlite_db.d.ts +1 -0
- package/package.json +7 -6
- package/dist/databases/cassandra_db.js +0 -235
- package/dist/databases/couch_db.js +0 -173
- package/dist/databases/dirty_db.js +0 -75
- package/dist/databases/dirty_git_db.js +0 -59
- package/dist/databases/elasticsearch_db.js +0 -243
- package/dist/databases/memory_db.js +0 -37
- package/dist/databases/mock_db.js +0 -41
- package/dist/databases/mongodb_db.js +0 -134
- package/dist/databases/mssql_db.js +0 -185
- package/dist/databases/mysql_db.js +0 -167
- package/dist/databases/postgres_db.js +0 -190
- package/dist/databases/postgrespool_db.js +0 -12
- package/dist/databases/redis_db.js +0 -120
- package/dist/databases/rethink_db.js +0 -121
- package/dist/databases/sqlite_db.js +0 -138
- package/dist/index.js +0 -193
- package/dist/lib/AbstractDatabase.js +0 -40
- package/dist/lib/CacheAndBufferLayer.js +0 -658
- package/dist/lib/logging.js +0 -27
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var mongodb = require('mongodb');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 2020 Sylchauf
|
|
8
|
-
*
|
|
9
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
-
* you may not use this file except in compliance with the License.
|
|
11
|
-
* You may obtain a copy of the License at
|
|
12
|
-
*
|
|
13
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
17
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
-
* See the License for the specific language governing permissions and
|
|
19
|
-
* limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
const Database = class extends AbstractDatabase {
|
|
22
|
-
interval;
|
|
23
|
-
database;
|
|
24
|
-
client;
|
|
25
|
-
collection;
|
|
26
|
-
constructor(settings) {
|
|
27
|
-
super();
|
|
28
|
-
this.settings = settings;
|
|
29
|
-
if (!this.settings.url)
|
|
30
|
-
throw new Error('You must specify a mongodb url');
|
|
31
|
-
// For backwards compatibility:
|
|
32
|
-
if (this.settings.database == null)
|
|
33
|
-
this.settings.database = this.settings.dbName;
|
|
34
|
-
if (!this.settings.collection)
|
|
35
|
-
this.settings.collection = 'ueberdb';
|
|
36
|
-
}
|
|
37
|
-
clearPing() {
|
|
38
|
-
if (this.interval) {
|
|
39
|
-
clearInterval(this.interval[Symbol.toPrimitive]());
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
schedulePing() {
|
|
43
|
-
this.clearPing();
|
|
44
|
-
this.interval = setInterval(() => {
|
|
45
|
-
this.database.command({
|
|
46
|
-
ping: 1,
|
|
47
|
-
});
|
|
48
|
-
}, 10000);
|
|
49
|
-
}
|
|
50
|
-
init(callback) {
|
|
51
|
-
mongodb.MongoClient.connect(this.settings.url).then((v) => {
|
|
52
|
-
this.client = v;
|
|
53
|
-
this.database = v.db(this.settings.database);
|
|
54
|
-
this.collection = this.database.collection(this.settings.collection);
|
|
55
|
-
callback(null);
|
|
56
|
-
})
|
|
57
|
-
.catch((v) => {
|
|
58
|
-
callback(v);
|
|
59
|
-
});
|
|
60
|
-
this.schedulePing();
|
|
61
|
-
}
|
|
62
|
-
get(key, callback) {
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
this.collection.findOne({ _id: key })
|
|
65
|
-
.then((v) => {
|
|
66
|
-
callback(null, v && v.value);
|
|
67
|
-
}).catch(v => {
|
|
68
|
-
console.log(v);
|
|
69
|
-
callback(v);
|
|
70
|
-
});
|
|
71
|
-
this.schedulePing();
|
|
72
|
-
}
|
|
73
|
-
findKeys(key, notKey, callback) {
|
|
74
|
-
const selector = {
|
|
75
|
-
$and: [
|
|
76
|
-
{ _id: { $regex: `${key.replace(/\*/g, '')}` } },
|
|
77
|
-
],
|
|
78
|
-
};
|
|
79
|
-
if (notKey) {
|
|
80
|
-
// @ts-ignore
|
|
81
|
-
selector.$and.push({ _id: { $not: { $regex: `${notKey.replace(/\*/g, '')}` } } });
|
|
82
|
-
}
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
this.collection.find(selector).map((i) => i._id)
|
|
85
|
-
.toArray()
|
|
86
|
-
.then(r => {
|
|
87
|
-
callback(null, r);
|
|
88
|
-
})
|
|
89
|
-
.catch(v => callback(v));
|
|
90
|
-
this.schedulePing();
|
|
91
|
-
}
|
|
92
|
-
set(key, value, callback) {
|
|
93
|
-
if (key.length > 100) {
|
|
94
|
-
callback('Your Key can only be 100 chars');
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
// @ts-ignore
|
|
98
|
-
this.collection.updateMany({ _id: key }, { $set: { value } }, { upsert: true })
|
|
99
|
-
.then(() => callback(null))
|
|
100
|
-
.catch(v => callback(v));
|
|
101
|
-
}
|
|
102
|
-
this.schedulePing();
|
|
103
|
-
}
|
|
104
|
-
remove(key, callback) {
|
|
105
|
-
// @ts-ignore
|
|
106
|
-
this.collection.deleteOne({ _id: key })
|
|
107
|
-
.then(r => callback(null, r))
|
|
108
|
-
.catch(v => callback(v));
|
|
109
|
-
this.schedulePing();
|
|
110
|
-
}
|
|
111
|
-
doBulk(bulk, callback) {
|
|
112
|
-
const bulkMongo = this.collection.initializeOrderedBulkOp();
|
|
113
|
-
for (const i in bulk) {
|
|
114
|
-
if (bulk[i].type === 'set') {
|
|
115
|
-
bulkMongo.find({ _id: bulk[i].key }).upsert().updateOne({ $set: { value: bulk[i].value } });
|
|
116
|
-
}
|
|
117
|
-
else if (bulk[i].type === 'remove') {
|
|
118
|
-
bulkMongo.find({ _id: bulk[i].key }).deleteOne();
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
bulkMongo.execute().then((res) => {
|
|
122
|
-
callback(null, res);
|
|
123
|
-
}).catch((error) => {
|
|
124
|
-
callback(error);
|
|
125
|
-
});
|
|
126
|
-
this.schedulePing();
|
|
127
|
-
}
|
|
128
|
-
close(callback) {
|
|
129
|
-
this.clearPing();
|
|
130
|
-
this.client.close().then(r => callback(r));
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
exports.Database = Database;
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var async = require('async');
|
|
5
|
-
var mssql = require('mssql');
|
|
6
|
-
|
|
7
|
-
/* eslint new-cap: ["error", {"capIsNewExceptions": ["mssql.NVarChar"]}] */
|
|
8
|
-
/**
|
|
9
|
-
* 2019 - exspecto@gmail.com
|
|
10
|
-
*
|
|
11
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
-
* you may not use this file except in compliance with the License.
|
|
13
|
-
* You may obtain a copy of the License at
|
|
14
|
-
*
|
|
15
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
-
*
|
|
17
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
19
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
-
* See the License for the specific language governing permissions and
|
|
21
|
-
* limitations under the License.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* Note: This requires MS SQL Server >= 2008 due to the usage of the MERGE statement
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
const Database = class MSSQL extends AbstractDatabase {
|
|
28
|
-
db;
|
|
29
|
-
constructor(settings) {
|
|
30
|
-
super();
|
|
31
|
-
settings = settings || {};
|
|
32
|
-
if (settings.json != null) {
|
|
33
|
-
settings.parseJSON = settings.json;
|
|
34
|
-
}
|
|
35
|
-
// set the request timeout to 5 minutes
|
|
36
|
-
settings.requestTimeout = 300000;
|
|
37
|
-
settings.server = settings.host;
|
|
38
|
-
this.settings = settings;
|
|
39
|
-
/*
|
|
40
|
-
Turning off the cache and write buffer here. You
|
|
41
|
-
can reenable it, but also take a look at maxInserts in
|
|
42
|
-
the doBulk function to decide how you want to split it up.
|
|
43
|
-
*/
|
|
44
|
-
this.settings.cache = 0;
|
|
45
|
-
this.settings.writeInterval = 0;
|
|
46
|
-
}
|
|
47
|
-
init(callback) {
|
|
48
|
-
const sqlCreate = "IF OBJECT_ID(N'dbo.store', N'U') IS NULL" +
|
|
49
|
-
' BEGIN' +
|
|
50
|
-
' CREATE TABLE [store] (' +
|
|
51
|
-
' [key] NVARCHAR(100) PRIMARY KEY,' +
|
|
52
|
-
' [value] NTEXT NOT NULL' +
|
|
53
|
-
' );' +
|
|
54
|
-
' END';
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
new mssql.ConnectionPool(this.settings).connect().then((pool) => {
|
|
57
|
-
this.db = pool;
|
|
58
|
-
const request = new mssql.Request(this.db);
|
|
59
|
-
request.query(sqlCreate, (err) => {
|
|
60
|
-
callback(err);
|
|
61
|
-
});
|
|
62
|
-
this.db.on('error', (err) => {
|
|
63
|
-
console.log(err);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
get(key, callback) {
|
|
68
|
-
const request = new mssql.Request(this.db);
|
|
69
|
-
request.input('key', mssql.NVarChar(100), key);
|
|
70
|
-
request.query('SELECT [value] FROM [store] WHERE [key] = @key', (err, results) => {
|
|
71
|
-
let value = null;
|
|
72
|
-
if (!err && results && results.rowsAffected[0] === 1) {
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
value = results.recordset[0].value;
|
|
75
|
-
}
|
|
76
|
-
callback(err, value);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
findKeys(key, notKey, callback) {
|
|
80
|
-
const request = new mssql.Request(this.db);
|
|
81
|
-
let query = 'SELECT [key] FROM [store] WHERE [key] LIKE @key';
|
|
82
|
-
// desired keys are key, e.g. pad:%
|
|
83
|
-
key = key.replace(/\*/g, '%');
|
|
84
|
-
request.input('key', mssql.NVarChar(100), key);
|
|
85
|
-
if (notKey != null) {
|
|
86
|
-
// not desired keys are notKey, e.g. %:%:%
|
|
87
|
-
notKey = notKey.replace(/\*/g, '%');
|
|
88
|
-
request.input('notkey', mssql.NVarChar(100), notKey);
|
|
89
|
-
query += ' AND [key] NOT LIKE @notkey';
|
|
90
|
-
}
|
|
91
|
-
request.query(query, (err, results) => {
|
|
92
|
-
const value = [];
|
|
93
|
-
if (!err && results && results.rowsAffected[0] > 0) {
|
|
94
|
-
for (let i = 0; i < results.recordset.length; i++) {
|
|
95
|
-
value.push(results.recordset[i].key);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
callback(err, value);
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
set(key, value, callback) {
|
|
102
|
-
const request = new mssql.Request(this.db);
|
|
103
|
-
if (key.length > 100) {
|
|
104
|
-
callback('Your Key can only be 100 chars');
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
const query = 'MERGE [store] t USING (SELECT @key [key], @value [value]) s' +
|
|
108
|
-
' ON t.[key] = s.[key]' +
|
|
109
|
-
' WHEN MATCHED AND s.[value] IS NOT NULL THEN UPDATE SET t.[value] = s.[value]' +
|
|
110
|
-
' WHEN NOT MATCHED THEN INSERT ([key], [value]) VALUES (s.[key], s.[value]);';
|
|
111
|
-
request.input('key', mssql.NVarChar(100), key);
|
|
112
|
-
request.input('value', mssql.NText, value);
|
|
113
|
-
request.query(query, (err, info) => {
|
|
114
|
-
callback(err ? err.toString() : '');
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
remove(key, callback) {
|
|
119
|
-
const request = new mssql.Request(this.db);
|
|
120
|
-
request.input('key', mssql.NVarChar(100), key);
|
|
121
|
-
request.query('DELETE FROM [store] WHERE [key] = @key', callback);
|
|
122
|
-
}
|
|
123
|
-
doBulk(bulk, callback) {
|
|
124
|
-
const maxInserts = 100;
|
|
125
|
-
const request = new mssql.Request(this.db);
|
|
126
|
-
let firstReplace = true;
|
|
127
|
-
let firstRemove = true;
|
|
128
|
-
const replacements = [];
|
|
129
|
-
let removeSQL = 'DELETE FROM [store] WHERE [key] IN (';
|
|
130
|
-
for (const i in bulk) {
|
|
131
|
-
if (bulk[i].type === 'set') {
|
|
132
|
-
if (firstReplace) {
|
|
133
|
-
replacements.push('BEGIN TRANSACTION;');
|
|
134
|
-
firstReplace = false;
|
|
135
|
-
}
|
|
136
|
-
else if (Number(i) % maxInserts === 0) {
|
|
137
|
-
replacements.push('\nCOMMIT TRANSACTION;\nBEGIN TRANSACTION;\n');
|
|
138
|
-
}
|
|
139
|
-
replacements.push(`MERGE [store] t USING (SELECT '${bulk[i].key}' [key], '${bulk[i].value}' [value]) s`, 'ON t.[key] = s.[key]', 'WHEN MATCHED AND s.[value] IS NOT NULL THEN UPDATE SET t.[value] = s.[value]', 'WHEN NOT MATCHED THEN INSERT ([key], [value]) VALUES (s.[key], s.[value]);');
|
|
140
|
-
}
|
|
141
|
-
else if (bulk[i].type === 'remove') {
|
|
142
|
-
if (!firstRemove) {
|
|
143
|
-
removeSQL += ',';
|
|
144
|
-
}
|
|
145
|
-
firstRemove = false;
|
|
146
|
-
removeSQL += `'${bulk[i].key}'`;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
removeSQL += ');';
|
|
150
|
-
replacements.push('COMMIT TRANSACTION;');
|
|
151
|
-
async.parallel([
|
|
152
|
-
(callback) => {
|
|
153
|
-
if (!firstReplace) {
|
|
154
|
-
request.batch(replacements.join('\n'), (err, results) => {
|
|
155
|
-
if (err) {
|
|
156
|
-
callback(err);
|
|
157
|
-
}
|
|
158
|
-
callback(err, results);
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
callback();
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
(callback) => {
|
|
166
|
-
if (!firstRemove) {
|
|
167
|
-
request.query(removeSQL, callback);
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
callback();
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
], (err, results) => {
|
|
174
|
-
if (err) {
|
|
175
|
-
callback(err);
|
|
176
|
-
}
|
|
177
|
-
callback(err, results);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
close(callback) {
|
|
181
|
-
this.db && this.db.close(callback);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
exports.Database = Database;
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var mysql = require('mysql');
|
|
5
|
-
var util = require('util');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 2011 Peter 'Pita' Martischka
|
|
9
|
-
*
|
|
10
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
11
|
-
* you may not use this file except in compliance with the License.
|
|
12
|
-
* You may obtain a copy of the License at
|
|
13
|
-
*
|
|
14
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
-
*
|
|
16
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
18
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
-
* See the License for the specific language governing permissions and
|
|
20
|
-
* limitations under the License.
|
|
21
|
-
*/
|
|
22
|
-
const Database = class extends AbstractDatabase {
|
|
23
|
-
_mysqlSettings;
|
|
24
|
-
_pool;
|
|
25
|
-
constructor(settings) {
|
|
26
|
-
super();
|
|
27
|
-
this.logger = console;
|
|
28
|
-
this._mysqlSettings = {
|
|
29
|
-
charset: 'utf8mb4',
|
|
30
|
-
...settings,
|
|
31
|
-
};
|
|
32
|
-
this.settings = {
|
|
33
|
-
engine: 'InnoDB',
|
|
34
|
-
// Limit the query size to avoid timeouts or other failures.
|
|
35
|
-
bulkLimit: 100,
|
|
36
|
-
json: true,
|
|
37
|
-
queryTimeout: 60000,
|
|
38
|
-
};
|
|
39
|
-
this._pool = null; // Initialized in init();
|
|
40
|
-
}
|
|
41
|
-
get isAsync() { return true; }
|
|
42
|
-
async _query(options) {
|
|
43
|
-
try {
|
|
44
|
-
return await new Promise((resolve, reject) => {
|
|
45
|
-
options = { timeout: this.settings.queryTimeout, ...options };
|
|
46
|
-
this._pool && this._pool.query(options, (err, ...args) => err != null ? reject(err) : resolve(args));
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
catch (err) {
|
|
50
|
-
this.logger.error(`${err.fatal ? 'Fatal ' : ''}MySQL error: ${err.stack || err}`);
|
|
51
|
-
throw err;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async init() {
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
this._pool = mysql.createPool(this._mysqlSettings);
|
|
57
|
-
const { database, charset } = this._mysqlSettings;
|
|
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
|
-
const sqlAlter = 'ALTER TABLE store MODIFY `key` VARCHAR(100) COLLATE utf8mb4_bin;';
|
|
64
|
-
await this._query({ sql: sqlCreate });
|
|
65
|
-
// Checks for Database charset et al
|
|
66
|
-
const dbCharSet = 'SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME ' +
|
|
67
|
-
`FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${database}'`;
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
let [result] = await this._query({ sql: dbCharSet });
|
|
70
|
-
result = JSON.parse(JSON.stringify(result));
|
|
71
|
-
if (result[0].DEFAULT_CHARACTER_SET_NAME !== charset) {
|
|
72
|
-
this.logger.error(`Database is not configured with charset ${charset} -- ` +
|
|
73
|
-
'This may lead to crashes when certain characters are pasted in pads');
|
|
74
|
-
this.logger.log(result[0], charset);
|
|
75
|
-
}
|
|
76
|
-
if (result[0].DEFAULT_COLLATION_NAME.indexOf(charset) === -1) {
|
|
77
|
-
this.logger.error(`Database is not configured with collation name that includes ${charset} -- ` +
|
|
78
|
-
'This may lead to crashes when certain characters are pasted in pads');
|
|
79
|
-
this.logger.log(result[0], charset, result[0].DEFAULT_COLLATION_NAME);
|
|
80
|
-
}
|
|
81
|
-
const tableCharSet = 'SELECT CCSA.character_set_name AS character_set_name ' +
|
|
82
|
-
'FROM information_schema.`TABLES` ' +
|
|
83
|
-
'T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA ' +
|
|
84
|
-
'WHERE CCSA.collation_name = T.table_collation ' +
|
|
85
|
-
`AND T.table_schema = '${database}' ` +
|
|
86
|
-
"AND T.table_name = 'store'";
|
|
87
|
-
[result] = await this._query({ sql: tableCharSet });
|
|
88
|
-
if (!result[0]) {
|
|
89
|
-
this.logger.warn('Data has no character_set_name value -- ' +
|
|
90
|
-
'This may lead to crashes when certain characters are pasted in pads');
|
|
91
|
-
}
|
|
92
|
-
if (result[0] && (result[0].character_set_name !== charset)) {
|
|
93
|
-
this.logger.error(`table is not configured with charset ${charset} -- ` +
|
|
94
|
-
'This may lead to crashes when certain characters are pasted in pads');
|
|
95
|
-
this.logger.log(result[0], charset);
|
|
96
|
-
}
|
|
97
|
-
// check migration level, alter if not migrated
|
|
98
|
-
const level = await this.get('MYSQL_MIGRATION_LEVEL');
|
|
99
|
-
if (level !== '1') {
|
|
100
|
-
await this._query({ sql: sqlAlter });
|
|
101
|
-
await this.set('MYSQL_MIGRATION_LEVEL', '1');
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async get(key) {
|
|
105
|
-
const [results] = await this._query({
|
|
106
|
-
sql: 'SELECT `value` FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
|
|
107
|
-
values: [key, key],
|
|
108
|
-
});
|
|
109
|
-
return results.length === 1 ? results[0].value : null;
|
|
110
|
-
}
|
|
111
|
-
async findKeys(key, notKey) {
|
|
112
|
-
let query = 'SELECT `key` FROM `store` WHERE `key` LIKE ?';
|
|
113
|
-
const params = [];
|
|
114
|
-
// desired keys are key, e.g. pad:%
|
|
115
|
-
key = key.replace(/\*/g, '%');
|
|
116
|
-
params.push(key);
|
|
117
|
-
if (notKey != null) {
|
|
118
|
-
// not desired keys are notKey, e.g. %:%:%
|
|
119
|
-
notKey = notKey.replace(/\*/g, '%');
|
|
120
|
-
query += ' AND `key` NOT LIKE ?';
|
|
121
|
-
params.push(notKey);
|
|
122
|
-
}
|
|
123
|
-
const [results] = await this._query({ sql: query, values: params });
|
|
124
|
-
return results.map((val) => val.key);
|
|
125
|
-
}
|
|
126
|
-
async set(key, value) {
|
|
127
|
-
if (key.length > 100)
|
|
128
|
-
throw new Error('Your Key can only be 100 chars');
|
|
129
|
-
await this._query({ sql: 'REPLACE INTO `store` VALUES (?,?)', values: [key, value] });
|
|
130
|
-
}
|
|
131
|
-
async remove(key) {
|
|
132
|
-
await this._query({
|
|
133
|
-
sql: 'DELETE FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
|
|
134
|
-
values: [key, key],
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
async doBulk(bulk) {
|
|
138
|
-
const replaces = [];
|
|
139
|
-
const deletes = [];
|
|
140
|
-
for (const op of bulk) {
|
|
141
|
-
switch (op.type) {
|
|
142
|
-
case 'set':
|
|
143
|
-
replaces.push([op.key, op.value]);
|
|
144
|
-
break;
|
|
145
|
-
case 'remove':
|
|
146
|
-
deletes.push(op.key);
|
|
147
|
-
break;
|
|
148
|
-
default: throw new Error(`unknown op type: ${op.type}`);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
await Promise.all([
|
|
152
|
-
replaces.length ? this._query({
|
|
153
|
-
sql: 'REPLACE INTO `store` VALUES ?;',
|
|
154
|
-
values: [replaces],
|
|
155
|
-
}) : null,
|
|
156
|
-
deletes.length ? this._query({
|
|
157
|
-
sql: 'DELETE FROM `store` WHERE `key` IN (?) AND BINARY `key` IN (?);',
|
|
158
|
-
values: [deletes, deletes],
|
|
159
|
-
}) : null,
|
|
160
|
-
]);
|
|
161
|
-
}
|
|
162
|
-
async close() {
|
|
163
|
-
await util.promisify(this._pool.end.bind(this._pool))();
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
exports.Database = Database;
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
-
var async = require('async');
|
|
5
|
-
var pg = require('pg');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 2011 Peter 'Pita' Martischka
|
|
9
|
-
*
|
|
10
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
11
|
-
* you may not use this file except in compliance with the License.
|
|
12
|
-
* You may obtain a copy of the License at
|
|
13
|
-
*
|
|
14
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
-
*
|
|
16
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
-
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
18
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
-
* See the License for the specific language governing permissions and
|
|
20
|
-
* limitations under the License.
|
|
21
|
-
*/
|
|
22
|
-
const Database = class extends AbstractDatabase {
|
|
23
|
-
db;
|
|
24
|
-
upsertStatement;
|
|
25
|
-
constructor(settings) {
|
|
26
|
-
super();
|
|
27
|
-
if (typeof settings === 'string')
|
|
28
|
-
settings = { connectionString: settings };
|
|
29
|
-
this.settings = settings;
|
|
30
|
-
this.settings.cache = settings.cache || 1000;
|
|
31
|
-
this.settings.writeInterval = 100;
|
|
32
|
-
this.settings.json = true;
|
|
33
|
-
// Pool specific defaults
|
|
34
|
-
this.settings.max = this.settings.max || 20;
|
|
35
|
-
this.settings.min = this.settings.min || 4;
|
|
36
|
-
this.settings.idleTimeoutMillis = this.settings.idleTimeoutMillis || 1000;
|
|
37
|
-
// @ts-ignore
|
|
38
|
-
this.db = new pg.Pool(this.settings);
|
|
39
|
-
}
|
|
40
|
-
init(callback) {
|
|
41
|
-
const testTableExists = "SELECT 1 as exists FROM pg_tables WHERE tablename = 'store'";
|
|
42
|
-
const createTable = 'CREATE TABLE IF NOT EXISTS store (' +
|
|
43
|
-
'"key" character varying(100) NOT NULL, ' +
|
|
44
|
-
'"value" text NOT NULL, ' +
|
|
45
|
-
'CONSTRAINT store_pkey PRIMARY KEY (key))';
|
|
46
|
-
// this variable will be given a value depending on the result of the
|
|
47
|
-
// feature detection
|
|
48
|
-
this.upsertStatement = null;
|
|
49
|
-
/*
|
|
50
|
-
* - Detects if this Postgres version supports INSERT .. ON CONFLICT
|
|
51
|
-
* UPDATE (PostgreSQL >= 9.5 and CockroachDB)
|
|
52
|
-
* - If upsert is not supported natively, creates in the DB a pl/pgsql
|
|
53
|
-
* function that emulates it
|
|
54
|
-
* - Performs a side effect, setting this.upsertStatement to the sql
|
|
55
|
-
* statement that needs to be used, based on the detection result
|
|
56
|
-
* - calls the callback
|
|
57
|
-
*/
|
|
58
|
-
const detectUpsertMethod = (callback) => {
|
|
59
|
-
const upsertViaFunction = 'SELECT ueberdb_insert_or_update($1,$2)';
|
|
60
|
-
const upsertNatively = 'INSERT INTO store(key, value) VALUES ($1, $2) ' +
|
|
61
|
-
'ON CONFLICT (key) DO UPDATE SET value = excluded.value';
|
|
62
|
-
const createFunc = 'CREATE OR REPLACE FUNCTION ueberdb_insert_or_update(character varying, text) ' +
|
|
63
|
-
'RETURNS void AS $$ ' +
|
|
64
|
-
'BEGIN ' +
|
|
65
|
-
' IF EXISTS( SELECT * FROM store WHERE key = $1 ) THEN ' +
|
|
66
|
-
' UPDATE store SET value = $2 WHERE key = $1; ' +
|
|
67
|
-
' ELSE ' +
|
|
68
|
-
' INSERT INTO store(key,value) VALUES( $1, $2 ); ' +
|
|
69
|
-
' END IF; ' +
|
|
70
|
-
' RETURN; ' +
|
|
71
|
-
'END; ' +
|
|
72
|
-
'$$ LANGUAGE plpgsql;';
|
|
73
|
-
const testNativeUpsert = `EXPLAIN ${upsertNatively}`;
|
|
74
|
-
this.db.query(testNativeUpsert, ['test-key', 'test-value'], (err) => {
|
|
75
|
-
if (err) {
|
|
76
|
-
// the UPSERT statement failed: we will have to emulate it via
|
|
77
|
-
// an sql function
|
|
78
|
-
this.upsertStatement = upsertViaFunction;
|
|
79
|
-
// actually create the emulation function
|
|
80
|
-
this.db.query(createFunc, [], callback);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
// if we get here, the EXPLAIN UPSERT succeeded, and we can use a
|
|
84
|
-
// native UPSERT
|
|
85
|
-
this.upsertStatement = upsertNatively;
|
|
86
|
-
callback();
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
this.db.query(testTableExists, (err, result) => {
|
|
90
|
-
if (err != null)
|
|
91
|
-
return callback(err);
|
|
92
|
-
if (result.rows.length === 0) {
|
|
93
|
-
this.db.query(createTable, (err) => {
|
|
94
|
-
if (err != null)
|
|
95
|
-
return callback(err);
|
|
96
|
-
// @ts-ignore
|
|
97
|
-
detectUpsertMethod(callback);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
// @ts-ignore
|
|
102
|
-
detectUpsertMethod(callback);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
get(key, callback) {
|
|
107
|
-
this.db.query('SELECT value FROM store WHERE key=$1', [key], (err, results) => {
|
|
108
|
-
let value = null;
|
|
109
|
-
if (!err && results.rows.length === 1) {
|
|
110
|
-
value = results.rows[0].value;
|
|
111
|
-
}
|
|
112
|
-
callback(err, value);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
findKeys(key, notKey, callback) {
|
|
116
|
-
let query = 'SELECT key FROM store WHERE key LIKE $1';
|
|
117
|
-
const params = [];
|
|
118
|
-
// desired keys are %key:%, e.g. pad:%
|
|
119
|
-
key = key.replace(/\*/g, '%');
|
|
120
|
-
params.push(key);
|
|
121
|
-
if (notKey != null) {
|
|
122
|
-
// not desired keys are notKey:%, e.g. %:%:%
|
|
123
|
-
notKey = notKey.replace(/\*/g, '%');
|
|
124
|
-
query += ' AND key NOT LIKE $2';
|
|
125
|
-
params.push(notKey);
|
|
126
|
-
}
|
|
127
|
-
this.db.query(query, params, (err, results) => {
|
|
128
|
-
const value = [];
|
|
129
|
-
if (!err && results.rows.length > 0) {
|
|
130
|
-
results.rows.forEach((val) => {
|
|
131
|
-
value.push(val.key);
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
callback(err, value);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
set(key, value, callback) {
|
|
138
|
-
if (key.length > 100) {
|
|
139
|
-
const val = '';
|
|
140
|
-
callback(Error('Your Key can only be 100 chars'), val);
|
|
141
|
-
}
|
|
142
|
-
else if (this.upsertStatement != null) {
|
|
143
|
-
this.db.query(this.upsertStatement, [key, value], callback);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
remove(key, callback) {
|
|
147
|
-
this.db.query('DELETE FROM store WHERE key=$1', [key], callback);
|
|
148
|
-
}
|
|
149
|
-
doBulk(bulk, callback) {
|
|
150
|
-
const replaceVALs = [];
|
|
151
|
-
let removeSQL = 'DELETE FROM store WHERE key IN (';
|
|
152
|
-
const removeVALs = [];
|
|
153
|
-
let removeCount = 0;
|
|
154
|
-
for (const i in bulk) {
|
|
155
|
-
if (bulk[i].type === 'set') {
|
|
156
|
-
replaceVALs.push([bulk[i].key, bulk[i].value]);
|
|
157
|
-
}
|
|
158
|
-
else if (bulk[i].type === 'remove') {
|
|
159
|
-
if (removeCount !== 0)
|
|
160
|
-
removeSQL += ',';
|
|
161
|
-
removeCount += 1;
|
|
162
|
-
removeSQL += `$${removeCount}`;
|
|
163
|
-
removeVALs.push(bulk[i].key);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
removeSQL += ');';
|
|
167
|
-
if (!this.upsertStatement) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
// @ts-ignore
|
|
171
|
-
const functions = replaceVALs.map((v) => (cb) => this.db.query(this.upsertStatement, v, cb));
|
|
172
|
-
const removeFunction = (callback) => {
|
|
173
|
-
// @ts-ignore
|
|
174
|
-
if (!(removeVALs.length) > 1) {
|
|
175
|
-
this.db.query(removeSQL, removeVALs, callback);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
callback();
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
functions.push(removeFunction);
|
|
182
|
-
// @ts-ignore
|
|
183
|
-
async.parallel(functions, callback);
|
|
184
|
-
}
|
|
185
|
-
close(callback) {
|
|
186
|
-
this.db.end(callback);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
exports.Database = Database;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const postgres = require('./postgres_db');
|
|
4
|
-
const Database = class PostgresDB extends postgres.Database {
|
|
5
|
-
constructor(settings) {
|
|
6
|
-
console.warn('ueberdb: The postgrespool database driver is deprecated ' +
|
|
7
|
-
'and will be removed in a future version. Use postgres instead.');
|
|
8
|
-
super(settings);
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
exports.Database = Database;
|