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.
Files changed (37) hide show
  1. package/dist/databases/cassandra_db.js +233 -235
  2. package/dist/databases/couch_db.js +171 -173
  3. package/dist/databases/dirty_db.js +73 -76
  4. package/dist/databases/dirty_git_db.js +57 -75
  5. package/dist/databases/elasticsearch_db.js +241 -267
  6. package/dist/databases/memory_db.js +35 -37
  7. package/dist/databases/mock_db.js +36 -38
  8. package/dist/databases/mongodb_db.js +131 -133
  9. package/dist/databases/mssql_db.js +183 -185
  10. package/dist/databases/mysql_db.js +166 -168
  11. package/dist/databases/postgres_db.js +188 -190
  12. package/dist/databases/postgrespool_db.js +10 -10
  13. package/dist/databases/redis_db.js +118 -120
  14. package/dist/databases/rethink_db.js +119 -121
  15. package/dist/databases/sqlite_db.js +135 -137
  16. package/dist/index.js +195 -213
  17. package/lib/AbstractDatabase.ts +79 -0
  18. package/lib/CacheAndBufferLayer.ts +665 -0
  19. package/lib/logging.ts +32 -0
  20. package/package.json +16 -12
  21. package/dist/lib/AbstractDatabase.js +0 -38
  22. package/dist/lib/CacheAndBufferLayer.js +0 -657
  23. package/dist/lib/logging.js +0 -34
  24. package/dist/test/lib/databases.js +0 -74
  25. package/dist/test/test.js +0 -327
  26. package/dist/test/test_bulk.js +0 -74
  27. package/dist/test/test_elasticsearch.js +0 -157
  28. package/dist/test/test_findKeys.js +0 -69
  29. package/dist/test/test_flush.js +0 -83
  30. package/dist/test/test_getSub.js +0 -57
  31. package/dist/test/test_lru.js +0 -155
  32. package/dist/test/test_memory.js +0 -59
  33. package/dist/test/test_metrics.js +0 -772
  34. package/dist/test/test_mysql.js +0 -90
  35. package/dist/test/test_postgres.js +0 -40
  36. package/dist/test/test_setSub.js +0 -48
  37. package/dist/test/test_tojson.js +0 -62
@@ -1,187 +1,185 @@
1
- "use strict";
2
- /* eslint new-cap: ["error", {"capIsNewExceptions": ["mssql.NVarChar"]}] */
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.Database = void 0;
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 AbstractDatabase_1 = __importDefault(require("../lib/AbstractDatabase"));
28
- const async_1 = __importDefault(require("async"));
29
- const mssql_1 = __importDefault(require("mssql"));
30
- const Database = class MSSQL extends AbstractDatabase_1.default {
31
- db;
32
- constructor(settings) {
33
- super();
34
- settings = settings || {};
35
- if (settings.json != null) {
36
- settings.parseJSON = settings.json;
37
- }
38
- // set the request timeout to 5 minutes
39
- settings.requestTimeout = 300000;
40
- settings.server = settings.host;
41
- this.settings = settings;
42
- /*
43
- Turning off the cache and write buffer here. You
44
- can reenable it, but also take a look at maxInserts in
45
- the doBulk function to decide how you want to split it up.
46
- */
47
- this.settings.cache = 0;
48
- this.settings.writeInterval = 0;
49
- }
50
- init(callback) {
51
- const sqlCreate = "IF OBJECT_ID(N'dbo.store', N'U') IS NULL" +
52
- ' BEGIN' +
53
- ' CREATE TABLE [store] (' +
54
- ' [key] NVARCHAR(100) PRIMARY KEY,' +
55
- ' [value] NTEXT NOT NULL' +
56
- ' );' +
57
- ' END';
58
- // @ts-ignore
59
- new mssql_1.default.ConnectionPool(this.settings).connect().then((pool) => {
60
- this.db = pool;
61
- const request = new mssql_1.default.Request(this.db);
62
- request.query(sqlCreate, (err) => {
63
- callback(err);
64
- });
65
- this.db.on('error', (err) => {
66
- console.log(err);
67
- });
68
- });
69
- }
70
- get(key, callback) {
71
- const request = new mssql_1.default.Request(this.db);
72
- request.input('key', mssql_1.default.NVarChar(100), key);
73
- request.query('SELECT [value] FROM [store] WHERE [key] = @key', (err, results) => {
74
- let value = null;
75
- if (!err && results && results.rowsAffected[0] === 1) {
76
- // @ts-ignore
77
- value = results.recordset[0].value;
78
- }
79
- callback(err, value);
80
- });
81
- }
82
- findKeys(key, notKey, callback) {
83
- const request = new mssql_1.default.Request(this.db);
84
- let query = 'SELECT [key] FROM [store] WHERE [key] LIKE @key';
85
- // desired keys are key, e.g. pad:%
86
- key = key.replace(/\*/g, '%');
87
- request.input('key', mssql_1.default.NVarChar(100), key);
88
- if (notKey != null) {
89
- // not desired keys are notKey, e.g. %:%:%
90
- notKey = notKey.replace(/\*/g, '%');
91
- request.input('notkey', mssql_1.default.NVarChar(100), notKey);
92
- query += ' AND [key] NOT LIKE @notkey';
93
- }
94
- request.query(query, (err, results) => {
95
- const value = [];
96
- if (!err && results && results.rowsAffected[0] > 0) {
97
- for (let i = 0; i < results.recordset.length; i++) {
98
- value.push(results.recordset[i].key);
99
- }
100
- }
101
- callback(err, value);
102
- });
103
- }
104
- set(key, value, callback) {
105
- const request = new mssql_1.default.Request(this.db);
106
- if (key.length > 100) {
107
- callback('Your Key can only be 100 chars');
108
- }
109
- else {
110
- const query = 'MERGE [store] t USING (SELECT @key [key], @value [value]) s' +
111
- ' ON t.[key] = s.[key]' +
112
- ' WHEN MATCHED AND s.[value] IS NOT NULL THEN UPDATE SET t.[value] = s.[value]' +
113
- ' WHEN NOT MATCHED THEN INSERT ([key], [value]) VALUES (s.[key], s.[value]);';
114
- request.input('key', mssql_1.default.NVarChar(100), key);
115
- request.input('value', mssql_1.default.NText, value);
116
- request.query(query, (err, info) => {
117
- callback(err ? err.toString() : '');
118
- });
119
- }
120
- }
121
- remove(key, callback) {
122
- const request = new mssql_1.default.Request(this.db);
123
- request.input('key', mssql_1.default.NVarChar(100), key);
124
- request.query('DELETE FROM [store] WHERE [key] = @key', callback);
125
- }
126
- doBulk(bulk, callback) {
127
- const maxInserts = 100;
128
- const request = new mssql_1.default.Request(this.db);
129
- let firstReplace = true;
130
- let firstRemove = true;
131
- const replacements = [];
132
- let removeSQL = 'DELETE FROM [store] WHERE [key] IN (';
133
- for (const i in bulk) {
134
- if (bulk[i].type === 'set') {
135
- if (firstReplace) {
136
- replacements.push('BEGIN TRANSACTION;');
137
- firstReplace = false;
138
- }
139
- else if (Number(i) % maxInserts === 0) {
140
- replacements.push('\nCOMMIT TRANSACTION;\nBEGIN TRANSACTION;\n');
141
- }
142
- 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]);');
143
- }
144
- else if (bulk[i].type === 'remove') {
145
- if (!firstRemove) {
146
- removeSQL += ',';
147
- }
148
- firstRemove = false;
149
- removeSQL += `'${bulk[i].key}'`;
150
- }
151
- }
152
- removeSQL += ');';
153
- replacements.push('COMMIT TRANSACTION;');
154
- async_1.default.parallel([
155
- (callback) => {
156
- if (!firstReplace) {
157
- request.batch(replacements.join('\n'), (err, results) => {
158
- if (err) {
159
- callback(err);
160
- }
161
- callback(err, results);
162
- });
163
- }
164
- else {
165
- callback();
166
- }
167
- },
168
- (callback) => {
169
- if (!firstRemove) {
170
- request.query(removeSQL, callback);
171
- }
172
- else {
173
- callback();
174
- }
175
- },
176
- ], (err, results) => {
177
- if (err) {
178
- callback(err);
179
- }
180
- callback(err, results);
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;