ueberdb2 4.1.8 → 4.1.10
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/databases/cassandra_db.js +233 -235
- package/dist/databases/couch_db.js +171 -173
- package/dist/databases/dirty_db.js +73 -76
- package/dist/databases/dirty_git_db.js +57 -75
- package/dist/databases/elasticsearch_db.js +241 -267
- package/dist/databases/memory_db.js +35 -37
- package/dist/databases/mock_db.js +36 -38
- package/dist/databases/mongodb_db.js +131 -133
- package/dist/databases/mssql_db.js +183 -185
- package/dist/databases/mysql_db.js +166 -168
- package/dist/databases/postgres_db.js +188 -190
- package/dist/databases/postgrespool_db.js +10 -10
- package/dist/databases/redis_db.js +118 -120
- package/dist/databases/rethink_db.js +119 -121
- package/dist/databases/sqlite_db.js +135 -137
- package/dist/index.js +195 -213
- package/lib/AbstractDatabase.ts +79 -0
- package/lib/CacheAndBufferLayer.ts +665 -0
- package/lib/logging.ts +32 -0
- package/package.json +16 -12
- package/dist/lib/AbstractDatabase.js +0 -38
- package/dist/lib/CacheAndBufferLayer.js +0 -657
- package/dist/lib/logging.js +0 -34
- package/dist/test/lib/databases.js +0 -74
- package/dist/test/test.js +0 -327
- package/dist/test/test_bulk.js +0 -74
- package/dist/test/test_elasticsearch.js +0 -157
- package/dist/test/test_findKeys.js +0 -69
- package/dist/test/test_flush.js +0 -83
- package/dist/test/test_getSub.js +0 -57
- package/dist/test/test_lru.js +0 -155
- package/dist/test/test_memory.js +0 -59
- package/dist/test/test_metrics.js +0 -772
- package/dist/test/test_mysql.js +0 -90
- package/dist/test/test_postgres.js +0 -40
- package/dist/test/test_setSub.js +0 -48
- package/dist/test/test_tojson.js +0 -62
|
@@ -1,187 +1,185 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
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
|
-
const
|
|
129
|
-
let
|
|
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
|
-
close(callback) {
|
|
184
|
-
this.db && this.db.close(callback);
|
|
185
|
-
}
|
|
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
|
+
}
|
|
186
183
|
};
|
|
184
|
+
|
|
187
185
|
exports.Database = Database;
|