ueberdb2 5.0.39 → 5.0.41

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.
@@ -0,0 +1,149 @@
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let util = require("util");
4
+ util = require_runtime.__toESM(util);
5
+ let mysql2 = require("mysql2");
6
+ //#region databases/mysql_db.ts
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
+ var mysql_db_default = class extends require_AbstractDatabase.default {
23
+ _mysqlSettings;
24
+ _pool;
25
+ constructor(settings) {
26
+ super(settings);
27
+ this.logger = console;
28
+ this._mysqlSettings = {
29
+ charset: "utf8mb4",
30
+ ...settings
31
+ };
32
+ this.settings = {
33
+ engine: "InnoDB",
34
+ bulkLimit: 100,
35
+ json: true,
36
+ queryTimeout: 6e4
37
+ };
38
+ this._pool = null;
39
+ }
40
+ get isAsync() {
41
+ return true;
42
+ }
43
+ async _query(options) {
44
+ try {
45
+ return await new Promise((resolve, reject) => {
46
+ options = {
47
+ timeout: this.settings.queryTimeout,
48
+ ...options
49
+ };
50
+ this._pool && this._pool.query(options, (err, ...args) => err != null ? reject(err) : resolve(args));
51
+ });
52
+ } catch (err) {
53
+ this.logger.error(`${err.fatal ? "Fatal " : ""}MySQL error: ${err.stack || err}`);
54
+ throw err;
55
+ }
56
+ }
57
+ async init() {
58
+ if ("speeds" in this._mysqlSettings) delete this._mysqlSettings.speeds;
59
+ if ("filename" in this._mysqlSettings) delete this._mysqlSettings.filename;
60
+ this._pool = (0, mysql2.createPool)(this._mysqlSettings);
61
+ const { database, charset } = this._mysqlSettings;
62
+ const sqlCreate = `CREATE TABLE IF NOT EXISTS \`store\` ( \`key\` VARCHAR( 100 ) NOT NULL COLLATE utf8mb4_bin, \`value\` LONGTEXT COLLATE utf8mb4_bin NOT NULL , PRIMARY KEY ( \`key\` ) ) 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
+ const dbCharSet = `SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${database}'`;
66
+ let [result] = await this._query({ sql: dbCharSet });
67
+ result = JSON.parse(JSON.stringify(result));
68
+ if (result[0].DEFAULT_CHARACTER_SET_NAME !== charset) {
69
+ this.logger.error(`Database is not configured with charset ${charset} -- This may lead to crashes when certain characters are pasted in pads`);
70
+ this.logger.warn(result[0], charset);
71
+ }
72
+ if (result[0].DEFAULT_COLLATION_NAME.indexOf(charset) === -1) {
73
+ this.logger.error(`Database is not configured with collation name that includes ${charset} -- This may lead to crashes when certain characters are pasted in pads`);
74
+ this.logger.warn(result[0], charset, result[0].DEFAULT_COLLATION_NAME);
75
+ }
76
+ const tableCharSet = `SELECT CCSA.character_set_name AS character_set_name FROM information_schema.\`TABLES\` T,information_schema.\`COLLATION_CHARACTER_SET_APPLICABILITY\` CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = '${database}' AND T.table_name = 'store'`;
77
+ [result] = await this._query({ sql: tableCharSet });
78
+ if (!result[0]) this.logger.warn("Data has no character_set_name value -- This may lead to crashes when certain characters are pasted in pads");
79
+ if (result[0] && result[0].character_set_name !== charset) {
80
+ this.logger.error(`table is not configured with charset ${charset} -- This may lead to crashes when certain characters are pasted in pads`);
81
+ this.logger.warn(result[0], charset);
82
+ }
83
+ if (await this.get("MYSQL_MIGRATION_LEVEL") !== "1") {
84
+ await this._query({ sql: sqlAlter });
85
+ await this.set("MYSQL_MIGRATION_LEVEL", "1");
86
+ }
87
+ }
88
+ async get(key) {
89
+ const [results] = await this._query({
90
+ sql: "SELECT `value` FROM `store` WHERE `key` = ? AND BINARY `key` = ?",
91
+ values: [key, key]
92
+ });
93
+ return results.length === 1 ? results[0].value : null;
94
+ }
95
+ async findKeys(key, notKey) {
96
+ let query = "SELECT `key` FROM `store` WHERE `key` LIKE ?";
97
+ const params = [];
98
+ key = key.replace(/\*/g, "%");
99
+ params.push(key);
100
+ if (notKey != null) {
101
+ notKey = notKey.replace(/\*/g, "%");
102
+ query += " AND `key` NOT LIKE ?";
103
+ params.push(notKey);
104
+ }
105
+ const [results] = await this._query({
106
+ sql: query,
107
+ values: params
108
+ });
109
+ return results.map((val) => val.key);
110
+ }
111
+ async set(key, value) {
112
+ if (key.length > 100) throw new Error("Your Key can only be 100 chars");
113
+ await this._query({
114
+ sql: "REPLACE INTO `store` VALUES (?,?)",
115
+ values: [key, value]
116
+ });
117
+ }
118
+ async remove(key) {
119
+ await this._query({
120
+ sql: "DELETE FROM `store` WHERE `key` = ? AND BINARY `key` = ?",
121
+ values: [key, key]
122
+ });
123
+ }
124
+ async doBulk(bulk) {
125
+ const replaces = [];
126
+ const deletes = [];
127
+ for (const op of bulk) switch (op.type) {
128
+ case "set":
129
+ replaces.push([op.key, op.value]);
130
+ break;
131
+ case "remove":
132
+ deletes.push(op.key);
133
+ break;
134
+ default: throw new Error(`unknown op type: ${op.type}`);
135
+ }
136
+ await Promise.all([replaces.length ? this._query({
137
+ sql: "REPLACE INTO `store` VALUES ?;",
138
+ values: [replaces]
139
+ }) : null, deletes.length ? this._query({
140
+ sql: "DELETE FROM `store` WHERE `key` IN (?) AND BINARY `key` IN (?);",
141
+ values: [deletes, deletes]
142
+ }) : null]);
143
+ }
144
+ async close() {
145
+ await util.default.promisify(this._pool.end.bind(this._pool))();
146
+ }
147
+ };
148
+ //#endregion
149
+ exports.default = mysql_db_default;
@@ -0,0 +1,125 @@
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let async = require("async");
4
+ async = require_runtime.__toESM(async);
5
+ let pg = require("pg");
6
+ pg = require_runtime.__toESM(pg);
7
+ //#region databases/postgres_db.ts
8
+ /**
9
+ * 2011 Peter 'Pita' Martischka
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
+ var postgres_db_default = class extends require_AbstractDatabase.default {
24
+ db;
25
+ upsertStatement;
26
+ constructor(settings) {
27
+ super(settings);
28
+ if (typeof settings === "string") settings = { connectionString: settings };
29
+ this.settings = settings;
30
+ this.settings.cache = settings.cache || 1e3;
31
+ this.settings.writeInterval = 100;
32
+ this.settings.json = true;
33
+ this.settings.max = this.settings.max || 20;
34
+ this.settings.min = this.settings.min || 4;
35
+ this.settings.idleTimeoutMillis = this.settings.idleTimeoutMillis || 1e3;
36
+ this.db = new pg.Pool(this.settings);
37
+ }
38
+ init(callback) {
39
+ const testTableExists = "SELECT 1 as exists FROM pg_tables WHERE tablename = 'store'";
40
+ const createTable = "CREATE TABLE IF NOT EXISTS store (\"key\" character varying(100) NOT NULL, \"value\" text NOT NULL, CONSTRAINT store_pkey PRIMARY KEY (key))";
41
+ this.upsertStatement = null;
42
+ const detectUpsertMethod = (callback) => {
43
+ const upsertViaFunction = "SELECT ueberdb_insert_or_update($1,$2)";
44
+ const upsertNatively = "INSERT INTO store(key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = excluded.value";
45
+ const createFunc = "CREATE OR REPLACE FUNCTION ueberdb_insert_or_update(character varying, text) RETURNS void AS $$ BEGIN IF EXISTS( SELECT * FROM store WHERE key = $1 ) THEN UPDATE store SET value = $2 WHERE key = $1; ELSE INSERT INTO store(key,value) VALUES( $1, $2 ); END IF; RETURN; END; $$ LANGUAGE plpgsql;";
46
+ const testNativeUpsert = `EXPLAIN ${upsertNatively}`;
47
+ this.db.query(testNativeUpsert, ["test-key", "test-value"], (err) => {
48
+ if (err) {
49
+ this.upsertStatement = upsertViaFunction;
50
+ this.db.query(createFunc, [], callback);
51
+ return;
52
+ }
53
+ this.upsertStatement = upsertNatively;
54
+ callback();
55
+ });
56
+ };
57
+ this.db.query(testTableExists, (err, result) => {
58
+ if (err != null) return callback(err);
59
+ if (result.rows.length === 0) this.db.query(createTable, (err) => {
60
+ if (err != null) return callback(err);
61
+ detectUpsertMethod(callback);
62
+ });
63
+ else detectUpsertMethod(callback);
64
+ });
65
+ }
66
+ get(key, callback) {
67
+ this.db.query("SELECT value FROM store WHERE key=$1", [key], (err, results) => {
68
+ let value = null;
69
+ if (!err && results.rows.length === 1) value = results.rows[0].value;
70
+ callback(err, value);
71
+ });
72
+ }
73
+ findKeys(key, notKey, callback) {
74
+ let query = "SELECT key FROM store WHERE key LIKE $1";
75
+ const params = [];
76
+ key = key.replace(/\*/g, "%");
77
+ params.push(key);
78
+ if (notKey != null) {
79
+ notKey = notKey.replace(/\*/g, "%");
80
+ query += " AND key NOT LIKE $2";
81
+ params.push(notKey);
82
+ }
83
+ this.db.query(query, params, (err, results) => {
84
+ const value = [];
85
+ if (!err && results.rows.length > 0) results.rows.forEach((val) => {
86
+ value.push(val.key);
87
+ });
88
+ callback(err, value);
89
+ });
90
+ }
91
+ set(key, value, callback) {
92
+ if (key.length > 100) callback(Error("Your Key can only be 100 chars"), "");
93
+ else if (this.upsertStatement != null) this.db.query(this.upsertStatement, [key, value], callback);
94
+ }
95
+ remove(key, callback) {
96
+ this.db.query("DELETE FROM store WHERE key=$1", [key], callback);
97
+ }
98
+ doBulk(bulk, callback) {
99
+ const replaceVALs = [];
100
+ let removeSQL = "DELETE FROM store WHERE key IN (";
101
+ const removeVALs = [];
102
+ let removeCount = 0;
103
+ for (const i in bulk) if (bulk[i].type === "set") replaceVALs.push([bulk[i].key, bulk[i].value]);
104
+ else if (bulk[i].type === "remove") {
105
+ if (removeCount !== 0) removeSQL += ",";
106
+ removeCount += 1;
107
+ removeSQL += `$${removeCount}`;
108
+ removeVALs.push(bulk[i].key);
109
+ }
110
+ removeSQL += ");";
111
+ if (!this.upsertStatement) return;
112
+ const functions = replaceVALs.map((v) => (cb) => this.db.query(this.upsertStatement, v, cb));
113
+ const removeFunction = (callback) => {
114
+ if (!(removeVALs.length < 1)) this.db.query(removeSQL, removeVALs, callback);
115
+ else callback();
116
+ };
117
+ functions.push(removeFunction);
118
+ async.default.parallel(functions, callback);
119
+ }
120
+ close(callback) {
121
+ this.db.end(callback);
122
+ }
123
+ };
124
+ //#endregion
125
+ exports.default = postgres_db_default;
@@ -0,0 +1,10 @@
1
+ const require_postgres_db = require("./postgres_db.js");
2
+ //#region databases/postgrespool_db.ts
3
+ var PostgresDB = class extends require_postgres_db.default {
4
+ constructor(settings) {
5
+ console.warn("ueberdb: The postgrespool database driver is deprecated and will be removed in a future version. Use postgres instead.");
6
+ super(settings);
7
+ }
8
+ };
9
+ //#endregion
10
+ exports.default = PostgresDB;
@@ -0,0 +1,93 @@
1
+ require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let redis = require("redis");
4
+ //#region databases/redis_db.ts
5
+ /**
6
+ * 2011 Peter 'Pita' Martischka
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS-IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+ var RedisDB = class extends require_AbstractDatabase.default {
21
+ _client;
22
+ constructor(settings) {
23
+ super(settings);
24
+ this._client = null;
25
+ this.settings = settings || {};
26
+ }
27
+ get isAsync() {
28
+ return true;
29
+ }
30
+ async init() {
31
+ if (this.settings.url) this._client = (0, redis.createClient)({ url: this.settings.url });
32
+ else if (this.settings.host) {
33
+ const options = { socket: {
34
+ host: this.settings.host,
35
+ port: Number(this.settings.port)
36
+ } };
37
+ if (this.settings.password) options.password = this.settings.password;
38
+ if (this.settings.user) options.username = this.settings.user;
39
+ this._client = (0, redis.createClient)(options);
40
+ }
41
+ if (this._client) {
42
+ await this._client.connect();
43
+ await this._client.ping();
44
+ }
45
+ }
46
+ async get(key) {
47
+ if (this._client == null) return null;
48
+ return await this._client.get(key);
49
+ }
50
+ async findKeys(key, notKey) {
51
+ if (this._client == null) return null;
52
+ const [_, type] = /^([^:*]+):\*$/.exec(key) || [];
53
+ if (type != null && ["*:*:*", `${key}:*`].includes(notKey)) return await this._client.sMembers(`ueberDB:keys:${type}`);
54
+ let keys = await this._client.keys(key.replace(/[?[\]\\]/g, "\\$&"));
55
+ if (notKey != null) {
56
+ const regex = this.createFindRegex(key, notKey);
57
+ keys = keys.filter((k) => regex.test(k));
58
+ }
59
+ return keys;
60
+ }
61
+ async set(key, value) {
62
+ if (this._client == null) return null;
63
+ const matches = /^([^:]+):([^:]+)$/.exec(key);
64
+ await Promise.all([matches && this._client.sAdd(`ueberDB:keys:${matches[1]}`, matches[0]), this._client.set(key, value)]);
65
+ }
66
+ async remove(key) {
67
+ if (this._client == null) return null;
68
+ const matches = /^([^:]+):([^:]+)$/.exec(key);
69
+ await Promise.all([matches && this._client.sRem(`ueberDB:keys:${matches[1]}`, matches[0]), this._client.del(key)]);
70
+ }
71
+ async doBulk(bulk) {
72
+ if (this._client == null) return null;
73
+ const multi = this._client.multi();
74
+ for (const { key, type, value } of bulk) {
75
+ const matches = /^([^:]+):([^:]+)$/.exec(key);
76
+ if (type === "set") {
77
+ if (matches) multi.sAdd(`ueberDB:keys:${matches[1]}`, matches[0]);
78
+ multi.set(key, value);
79
+ } else if (type === "remove") {
80
+ if (matches) multi.sRem(`ueberDB:keys:${matches[1]}`, matches[0]);
81
+ multi.del(key);
82
+ }
83
+ }
84
+ await multi.exec();
85
+ }
86
+ async close() {
87
+ if (this._client == null) return null;
88
+ await this._client.quit();
89
+ this._client = null;
90
+ }
91
+ };
92
+ //#endregion
93
+ exports.default = RedisDB;
@@ -0,0 +1,92 @@
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let async = require("async");
4
+ async = require_runtime.__toESM(async);
5
+ let rethinkdb = require("rethinkdb");
6
+ rethinkdb = require_runtime.__toESM(rethinkdb);
7
+ //#region databases/rethink_db.ts
8
+ /**
9
+ * 2016 Remi Arnaud
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
+ var Rethink_db = class extends require_AbstractDatabase.default {
24
+ host;
25
+ db;
26
+ port;
27
+ table;
28
+ connection;
29
+ constructor(settings) {
30
+ super(settings);
31
+ if (!settings) settings = {};
32
+ if (!settings.host) settings.host = "localhost";
33
+ if (!settings.port) settings.port = 28015;
34
+ if (!settings.db) settings.db = "test";
35
+ if (!settings.table) settings.table = "test";
36
+ this.host = settings.host;
37
+ this.db = settings.db;
38
+ this.port = settings.port;
39
+ this.table = settings.table;
40
+ this.connection = null;
41
+ }
42
+ init(callback) {
43
+ rethinkdb.default.connect(this, (err, conn) => {
44
+ if (err) throw err;
45
+ this.connection = conn;
46
+ rethinkdb.default.table(this.table).run(this.connection, (err, cursor) => {
47
+ if (err) rethinkdb.default.tableCreate(this.table).run(this.connection, callback);
48
+ else if (callback) callback(null, cursor);
49
+ });
50
+ });
51
+ }
52
+ get(key, callback) {
53
+ rethinkdb.default.table(this.table).get(key).run(this.connection, (err, item) => {
54
+ callback(err, item ? item.content : item);
55
+ });
56
+ }
57
+ findKeys(key, notKey, callback) {
58
+ const keys = [];
59
+ const regex = this.createFindRegex(key, notKey);
60
+ rethinkdb.default.filter((item) => {
61
+ if (item.id.search(regex) !== -1) keys.push(item.id);
62
+ }).run(this.connection, callback);
63
+ }
64
+ set(key, value, callback) {
65
+ rethinkdb.default.table(this.table).insert({
66
+ id: key,
67
+ content: value
68
+ }, { conflict: "replace" }).run(this.connection, callback);
69
+ }
70
+ doBulk(bulk, callback) {
71
+ const _in = [];
72
+ const _out = [];
73
+ for (const i in bulk) if (bulk[i].type === "set") _in.push({
74
+ id: bulk[i].key,
75
+ content: bulk[i].value
76
+ });
77
+ else if (bulk[i].type === "remove") _out.push(bulk[i].key);
78
+ async.default.parallel([(cb) => {
79
+ rethinkdb.default.table(this.table).insert(_in, { conflict: "replace" }).run(this.connection, cb);
80
+ }, (cb) => {
81
+ rethinkdb.default.table(this.table).getAll(_out).delete().run(this.connection, cb);
82
+ }], callback);
83
+ }
84
+ remove(key, callback) {
85
+ rethinkdb.default.table(this.table).get(key).delete().run(this.connection, callback);
86
+ }
87
+ close(callback) {
88
+ if (this.connection) this.connection.close(callback);
89
+ }
90
+ };
91
+ //#endregion
92
+ exports.default = Rethink_db;
@@ -0,0 +1,49 @@
1
+ require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let rusty_store_kv = require("rusty-store-kv");
4
+ //#region databases/rusty_db.ts
5
+ var Rusty_db = class extends require_AbstractDatabase.default {
6
+ db;
7
+ constructor(settings) {
8
+ super(settings);
9
+ this.settings.cache = 0;
10
+ this.settings.writeInterval = 0;
11
+ this.settings.json = false;
12
+ }
13
+ get isAsync() {
14
+ return true;
15
+ }
16
+ findKeys(key, notKey) {
17
+ return this.db.findKeys(key, notKey);
18
+ }
19
+ get(key) {
20
+ const val = this.db.get(key);
21
+ if (!val) return val;
22
+ try {
23
+ return JSON.parse(val);
24
+ } catch (e) {
25
+ return val;
26
+ }
27
+ }
28
+ async init() {
29
+ this.db = new rusty_store_kv.KeyValueDB(this.settings.filename);
30
+ }
31
+ close() {
32
+ this.db?.close();
33
+ this.db = null;
34
+ }
35
+ remove(key) {
36
+ this.db.remove(key);
37
+ }
38
+ set(key, value) {
39
+ if (typeof value === "object") {
40
+ const valStr = JSON.stringify(value);
41
+ this.db.set(key, valStr);
42
+ } else this.db.set(key, value.toString());
43
+ }
44
+ destroy() {
45
+ this.db.destroy();
46
+ }
47
+ };
48
+ //#endregion
49
+ exports.default = Rusty_db;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ require("../_virtual/_rolldown/runtime.js");
3
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
4
+ let rusty_store_kv = require("rusty-store-kv");
5
+ //#region databases/sqlite_db.ts
6
+ /**
7
+ * 2011 Peter 'Pita' Martischka
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
+ var SQLiteDB = class extends require_AbstractDatabase.default {
22
+ db;
23
+ constructor(settings) {
24
+ super(settings);
25
+ this.db = null;
26
+ if (!settings || !settings.filename) settings = { filename: ":memory:" };
27
+ this.settings = settings;
28
+ if (settings.filename === ":memory:") {
29
+ this.settings.cache = 0;
30
+ this.settings.writeInterval = 0;
31
+ this.settings.json = true;
32
+ } else {
33
+ this.settings.cache = 1e3;
34
+ this.settings.writeInterval = 100;
35
+ this.settings.json = true;
36
+ }
37
+ }
38
+ init(callback) {
39
+ this.db = new rusty_store_kv.SQLite(this.settings.filename);
40
+ callback();
41
+ }
42
+ get(key, callback) {
43
+ const res = this.db.get(key);
44
+ callback(null, res ? res : null);
45
+ }
46
+ findKeys(key, notKey, callback) {
47
+ const res = this.db?.findKeys(key, notKey);
48
+ callback(null, res);
49
+ }
50
+ set(key, value, callback) {
51
+ const res = this.db.set(key, value);
52
+ res ? callback(null, null) : callback(null, res);
53
+ }
54
+ remove(key, callback) {
55
+ this.db.remove(key);
56
+ callback(null, null);
57
+ }
58
+ doBulk(bulk, callback) {
59
+ const convertedBulk = bulk.map((b) => {
60
+ if (b.value === null) return {
61
+ key: b.key,
62
+ type: b.type
63
+ };
64
+ else return b;
65
+ });
66
+ this.db.doBulk(convertedBulk);
67
+ callback();
68
+ }
69
+ close(callback) {
70
+ callback();
71
+ this.db.close();
72
+ }
73
+ };
74
+ //#endregion
75
+ exports.default = SQLiteDB;