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,170 +1,168 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
18
- return (mod && mod.__esModule) ? mod : { "default": mod };
19
- };
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.Database = void 0;
22
- const AbstractDatabase_1 = __importDefault(require("../lib/AbstractDatabase"));
23
- const mysql_1 = __importDefault(require("mysql"));
24
- const util_1 = __importDefault(require("util"));
25
- const Database = class extends AbstractDatabase_1.default {
26
- _mysqlSettings;
27
- _pool;
28
- constructor(settings) {
29
- super();
30
- this.logger = console;
31
- this._mysqlSettings = {
32
- charset: 'utf8mb4',
33
- ...settings,
34
- };
35
- this.settings = {
36
- engine: 'InnoDB',
37
- // Limit the query size to avoid timeouts or other failures.
38
- bulkLimit: 100,
39
- json: true,
40
- queryTimeout: 60000,
41
- };
42
- this._pool = null; // Initialized in init();
43
- }
44
- get isAsync() { return true; }
45
- async _query(options) {
46
- try {
47
- return await new Promise((resolve, reject) => {
48
- options = { timeout: this.settings.queryTimeout, ...options };
49
- // @ts-ignore
50
- this._pool && this._pool.query(options, (err, ...args) => err != null ? reject(err) : resolve(args));
51
- });
52
- }
53
- catch (err) {
54
- this.logger.error(`${err.fatal ? 'Fatal ' : ''}MySQL error: ${err.stack || err}`);
55
- throw err;
56
- }
57
- }
58
- async init() {
59
- // @ts-ignore
60
- this._pool = mysql_1.default.createPool(this._mysqlSettings);
61
- const { database, charset } = this._mysqlSettings;
62
- const sqlCreate = `${'CREATE TABLE IF NOT EXISTS `store` ( ' +
63
- '`key` VARCHAR( 100 ) NOT NULL COLLATE utf8mb4_bin, ' +
64
- '`value` LONGTEXT COLLATE utf8mb4_bin NOT NULL , ' +
65
- 'PRIMARY KEY ( `key` ) ' +
66
- ') ENGINE='}${this.settings.engine} CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`;
67
- const sqlAlter = 'ALTER TABLE store MODIFY `key` VARCHAR(100) COLLATE utf8mb4_bin;';
68
- await this._query({ sql: sqlCreate });
69
- // Checks for Database charset et al
70
- const dbCharSet = 'SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME ' +
71
- `FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${database}'`;
72
- // @ts-ignore
73
- let [result] = await this._query({ sql: dbCharSet });
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
- if (result[0].DEFAULT_COLLATION_NAME.indexOf(charset) === -1) {
81
- this.logger.error(`Database is not configured with collation name that includes ${charset} -- ` +
82
- 'This may lead to crashes when certain characters are pasted in pads');
83
- this.logger.log(result[0], charset, result[0].DEFAULT_COLLATION_NAME);
84
- }
85
- const tableCharSet = 'SELECT CCSA.character_set_name AS character_set_name ' +
86
- 'FROM information_schema.`TABLES` ' +
87
- 'T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA ' +
88
- 'WHERE CCSA.collation_name = T.table_collation ' +
89
- `AND T.table_schema = '${database}' ` +
90
- "AND T.table_name = 'store'";
91
- [result] = await this._query({ sql: tableCharSet });
92
- if (!result[0]) {
93
- this.logger.warn('Data has no character_set_name value -- ' +
94
- 'This may lead to crashes when certain characters are pasted in pads');
95
- }
96
- if (result[0] && (result[0].character_set_name !== charset)) {
97
- this.logger.error(`table is not configured with charset ${charset} -- ` +
98
- 'This may lead to crashes when certain characters are pasted in pads');
99
- this.logger.log(result[0], charset);
100
- }
101
- // check migration level, alter if not migrated
102
- const level = await this.get('MYSQL_MIGRATION_LEVEL');
103
- if (level !== '1') {
104
- await this._query({ sql: sqlAlter });
105
- await this.set('MYSQL_MIGRATION_LEVEL', '1');
106
- }
107
- }
108
- async get(key) {
109
- const [results] = await this._query({
110
- sql: 'SELECT `value` FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
111
- values: [key, key],
112
- });
113
- return results.length === 1 ? results[0].value : null;
114
- }
115
- async findKeys(key, notKey) {
116
- let query = 'SELECT `key` FROM `store` WHERE `key` LIKE ?';
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 ?';
125
- params.push(notKey);
126
- }
127
- const [results] = await this._query({ sql: query, values: params });
128
- return results.map((val) => val.key);
129
- }
130
- async set(key, value) {
131
- if (key.length > 100)
132
- throw new Error('Your Key can only be 100 chars');
133
- await this._query({ sql: 'REPLACE INTO `store` VALUES (?,?)', values: [key, value] });
134
- }
135
- async remove(key) {
136
- await this._query({
137
- sql: 'DELETE FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
138
- values: [key, key],
139
- });
140
- }
141
- async doBulk(bulk) {
142
- const replaces = [];
143
- const deletes = [];
144
- for (const op of bulk) {
145
- switch (op.type) {
146
- case 'set':
147
- replaces.push([op.key, op.value]);
148
- break;
149
- case 'remove':
150
- deletes.push(op.key);
151
- break;
152
- default: throw new Error(`unknown op type: ${op.type}`);
153
- }
154
- }
155
- await Promise.all([
156
- replaces.length ? this._query({
157
- sql: 'REPLACE INTO `store` VALUES ?;',
158
- values: [replaces],
159
- }) : null,
160
- deletes.length ? this._query({
161
- sql: 'DELETE FROM `store` WHERE `key` IN (?) AND BINARY `key` IN (?);',
162
- values: [deletes, deletes],
163
- }) : null,
164
- ]);
165
- }
166
- async close() {
167
- await util_1.default.promisify(this._pool.end.bind(this._pool))();
168
- }
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
+ // @ts-ignore
47
+ this._pool && this._pool.query(options, (err, ...args) => err != null ? reject(err) : resolve(args));
48
+ });
49
+ }
50
+ catch (err) {
51
+ this.logger.error(`${err.fatal ? 'Fatal ' : ''}MySQL error: ${err.stack || err}`);
52
+ throw err;
53
+ }
54
+ }
55
+ async init() {
56
+ // @ts-ignore
57
+ this._pool = mysql.createPool(this._mysqlSettings);
58
+ const { database, charset } = this._mysqlSettings;
59
+ const sqlCreate = `${'CREATE TABLE IF NOT EXISTS `store` ( ' +
60
+ '`key` VARCHAR( 100 ) NOT NULL COLLATE utf8mb4_bin, ' +
61
+ '`value` LONGTEXT COLLATE utf8mb4_bin NOT NULL , ' +
62
+ 'PRIMARY KEY ( `key` ) ' +
63
+ ') ENGINE='}${this.settings.engine} CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`;
64
+ const sqlAlter = 'ALTER TABLE store MODIFY `key` VARCHAR(100) COLLATE utf8mb4_bin;';
65
+ await this._query({ sql: sqlCreate });
66
+ // Checks for Database charset et al
67
+ const dbCharSet = 'SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME ' +
68
+ `FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${database}'`;
69
+ // @ts-ignore
70
+ let [result] = await this._query({ sql: dbCharSet });
71
+ result = JSON.parse(JSON.stringify(result));
72
+ if (result[0].DEFAULT_CHARACTER_SET_NAME !== charset) {
73
+ this.logger.error(`Database is not configured with charset ${charset} -- ` +
74
+ 'This may lead to crashes when certain characters are pasted in pads');
75
+ this.logger.log(result[0], charset);
76
+ }
77
+ if (result[0].DEFAULT_COLLATION_NAME.indexOf(charset) === -1) {
78
+ this.logger.error(`Database is not configured with collation name that includes ${charset} -- ` +
79
+ 'This may lead to crashes when certain characters are pasted in pads');
80
+ this.logger.log(result[0], charset, result[0].DEFAULT_COLLATION_NAME);
81
+ }
82
+ const tableCharSet = 'SELECT CCSA.character_set_name AS character_set_name ' +
83
+ 'FROM information_schema.`TABLES` ' +
84
+ 'T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA ' +
85
+ 'WHERE CCSA.collation_name = T.table_collation ' +
86
+ `AND T.table_schema = '${database}' ` +
87
+ "AND T.table_name = 'store'";
88
+ [result] = await this._query({ sql: tableCharSet });
89
+ if (!result[0]) {
90
+ this.logger.warn('Data has no character_set_name value -- ' +
91
+ 'This may lead to crashes when certain characters are pasted in pads');
92
+ }
93
+ if (result[0] && (result[0].character_set_name !== charset)) {
94
+ this.logger.error(`table is not configured with charset ${charset} -- ` +
95
+ 'This may lead to crashes when certain characters are pasted in pads');
96
+ this.logger.log(result[0], charset);
97
+ }
98
+ // check migration level, alter if not migrated
99
+ const level = await this.get('MYSQL_MIGRATION_LEVEL');
100
+ if (level !== '1') {
101
+ await this._query({ sql: sqlAlter });
102
+ await this.set('MYSQL_MIGRATION_LEVEL', '1');
103
+ }
104
+ }
105
+ async get(key) {
106
+ const [results] = await this._query({
107
+ sql: 'SELECT `value` FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
108
+ values: [key, key],
109
+ });
110
+ return results.length === 1 ? results[0].value : null;
111
+ }
112
+ async findKeys(key, notKey) {
113
+ let query = 'SELECT `key` FROM `store` WHERE `key` LIKE ?';
114
+ const params = [];
115
+ // desired keys are key, e.g. pad:%
116
+ key = key.replace(/\*/g, '%');
117
+ params.push(key);
118
+ if (notKey != null) {
119
+ // not desired keys are notKey, e.g. %:%:%
120
+ notKey = notKey.replace(/\*/g, '%');
121
+ query += ' AND `key` NOT LIKE ?';
122
+ params.push(notKey);
123
+ }
124
+ const [results] = await this._query({ sql: query, values: params });
125
+ return results.map((val) => val.key);
126
+ }
127
+ async set(key, value) {
128
+ if (key.length > 100)
129
+ throw new Error('Your Key can only be 100 chars');
130
+ await this._query({ sql: 'REPLACE INTO `store` VALUES (?,?)', values: [key, value] });
131
+ }
132
+ async remove(key) {
133
+ await this._query({
134
+ sql: 'DELETE FROM `store` WHERE `key` = ? AND BINARY `key` = ?',
135
+ values: [key, key],
136
+ });
137
+ }
138
+ async doBulk(bulk) {
139
+ const replaces = [];
140
+ const deletes = [];
141
+ for (const op of bulk) {
142
+ switch (op.type) {
143
+ case 'set':
144
+ replaces.push([op.key, op.value]);
145
+ break;
146
+ case 'remove':
147
+ deletes.push(op.key);
148
+ break;
149
+ default: throw new Error(`unknown op type: ${op.type}`);
150
+ }
151
+ }
152
+ await Promise.all([
153
+ replaces.length ? this._query({
154
+ sql: 'REPLACE INTO `store` VALUES ?;',
155
+ values: [replaces],
156
+ }) : null,
157
+ deletes.length ? this._query({
158
+ sql: 'DELETE FROM `store` WHERE `key` IN (?) AND BINARY `key` IN (?);',
159
+ values: [deletes, deletes],
160
+ }) : null,
161
+ ]);
162
+ }
163
+ async close() {
164
+ await util.promisify(this._pool.end.bind(this._pool))();
165
+ }
169
166
  };
167
+
170
168
  exports.Database = Database;