ueberdb2 5.0.39 → 5.0.40

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,190 @@
1
+ require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let cassandra_driver = require("cassandra-driver");
4
+ //#region databases/cassandra_db.ts
5
+ /**
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS-IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ var Cassandra_db = class extends require_AbstractDatabase.default {
19
+ client;
20
+ pool;
21
+ /**
22
+ * @param {Object} settings The required settings object to initiate the Cassandra database
23
+ * @param {String[]} settings.clientOptions See
24
+ * http://www.datastax.com/drivers/nodejs/2.0/global.html#ClientOptions for a full set of
25
+ * options that can be used
26
+ * @param {String} settings.columnFamily The column family that should be used to store data. The
27
+ * column family will be created if it doesn't exist
28
+ * @param {Function} [settings.logger] Function that will be used to pass on log events emitted by
29
+ * the Cassandra driver. See https://github.com/datastax/nodejs-driver#logging for more
30
+ * information
31
+ */
32
+ constructor(settings) {
33
+ super(settings);
34
+ if (!settings.clientOptions) throw new Error("The Cassandra client options should be defined");
35
+ if (!settings.columnFamily) throw new Error("The Cassandra column family should be defined");
36
+ this.settings = { database: settings.database };
37
+ this.settings.clientOptions = settings.clientOptions;
38
+ this.settings.columnFamily = settings.columnFamily;
39
+ this.settings.logger = settings.logger;
40
+ }
41
+ /**
42
+ * Initializes the Cassandra client, connects to Cassandra and creates the CF if it didn't exist
43
+ * already
44
+ *
45
+ * @param {Function} callback Standard callback method.
46
+ * @param {Error} callback.err An error object (if any.)
47
+ */
48
+ init(callback) {
49
+ this.client = new cassandra_driver.Client(this.settings.clientOptions);
50
+ if (this.settings.logger) this.client.on("log", this.settings.logger);
51
+ this.client.execute("SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = ?", [this.settings.clientOptions.keyspace], (err, result) => {
52
+ if (err) return callback(err);
53
+ let isDefined = false;
54
+ const length = result.rows.length;
55
+ for (let i = 0; i < length; i++) if (result.rows[i].columnfamily_name === this.settings.columnFamily) {
56
+ isDefined = true;
57
+ break;
58
+ }
59
+ if (isDefined) return callback(null);
60
+ else {
61
+ const cql = `CREATE COLUMNFAMILY "${this.settings.columnFamily}" (key text PRIMARY KEY, data text)`;
62
+ this.client && this.client.execute(cql, callback);
63
+ }
64
+ });
65
+ }
66
+ /**
67
+ * Gets a value from Cassandra
68
+ *
69
+ * @param {String} key The key for which the value should be retrieved
70
+ * @param {Function} callback Standard callback method
71
+ * @param {Error} callback.err An error object, if any
72
+ * @param {String} callback.value The value for the given key (if any)
73
+ */
74
+ get(key, callback) {
75
+ const cql = `SELECT data FROM "${this.settings.columnFamily}" WHERE key = ?`;
76
+ this.client && this.client.execute(cql, [key], (err, result) => {
77
+ if (err) return callback(err);
78
+ if (!result.rows || result.rows.length === 0) return callback(null, null);
79
+ return callback(null, result.rows[0].data);
80
+ });
81
+ }
82
+ /**
83
+ * Cassandra has no native `findKeys` method. This function implements a naive filter by
84
+ * retrieving *all* the keys and filtering those. This should obviously be used with the utmost
85
+ * care and is probably not something you want to run in production.
86
+ *
87
+ * @param {String} key The filter for keys that should match
88
+ * @param {String} [notKey] The filter for keys that shouldn't match
89
+ * @param {Function} callback Standard callback method
90
+ * @param {Error} callback.err An error object, if any
91
+ * @param {String[]} callback.keys An array of keys that match the specified filters
92
+ */
93
+ findKeys(key, notKey, callback) {
94
+ let cql = null;
95
+ if (!notKey) {
96
+ cql = `SELECT key FROM "${this.settings.columnFamily}"`;
97
+ this.client && this.client.execute(cql, (err, result) => {
98
+ if (err) return callback(err);
99
+ const regex = new RegExp(`^${key.replace(/\*/g, ".*")}$`);
100
+ const keys = [];
101
+ result.rows.forEach((row) => {
102
+ if (regex.test(row.key)) keys.push(row.key);
103
+ });
104
+ return callback(null, keys);
105
+ });
106
+ } else if (notKey === "*:*:*") {
107
+ const matches = /^([^:]+):\*$/.exec(key);
108
+ if (matches) {
109
+ cql = `SELECT * from "${this.settings.columnFamily}" WHERE key = ?`;
110
+ this.client && this.client.execute(cql, [`ueberdb:keys:${matches[1]}`], (err, result) => {
111
+ if (err) return callback(err);
112
+ if (!result.rows || result.rows.length === 0) return callback(null, []);
113
+ return callback(null, result.rows.map((row) => row.data));
114
+ });
115
+ } else return callback(/* @__PURE__ */ new Error("Cassandra db only supports key patterns like pad:* when notKey is set to *:*:*"), null);
116
+ } else return callback(/* @__PURE__ */ new Error("Cassandra db currently only supports *:*:* as notKey"), null);
117
+ }
118
+ /**
119
+ * Sets a value for a key
120
+ *
121
+ * @param {String} key The key to set
122
+ * @param {String} value The value associated to this key
123
+ * @param {Function} callback Standard callback method
124
+ * @param {Error} callback.err An error object, if any
125
+ */
126
+ set(key, value, callback) {
127
+ this.doBulk([{
128
+ type: "set",
129
+ key,
130
+ value
131
+ }], callback);
132
+ }
133
+ /**
134
+ * Removes a key and it's value from the column family
135
+ *
136
+ * @param {String} key The key to remove
137
+ * @param {Function} callback Standard callback method
138
+ * @param {Error} callback.err An error object, if any
139
+ */
140
+ remove(key, callback) {
141
+ this.doBulk([{
142
+ type: "remove",
143
+ key
144
+ }], callback);
145
+ }
146
+ /**
147
+ * Performs multiple operations in one action
148
+ *
149
+ * @param {Object[]} bulk The set of operations that should be performed
150
+ * @param {Function} callback Standard callback method
151
+ * @param {Error} callback.err An error object, if any
152
+ */
153
+ doBulk(bulk, callback) {
154
+ const queries = [];
155
+ bulk.forEach((operation) => {
156
+ const matches = /^([^:]+):([^:]+)$/.exec(operation.key);
157
+ if (operation.type === "set") {
158
+ queries.push({
159
+ query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
160
+ params: [operation.value, operation.key]
161
+ });
162
+ if (matches) queries.push({
163
+ query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
164
+ params: ["1", `ueberdb:keys:${matches[1]}`]
165
+ });
166
+ } else if (operation.type === "remove") {
167
+ queries.push({
168
+ query: `DELETE FROM "${this.settings.columnFamily}" WHERE key=?`,
169
+ params: [operation.key]
170
+ });
171
+ if (matches) queries.push({
172
+ query: `DELETE FROM "${this.settings.columnFamily}" WHERE key = ?`,
173
+ params: [`ueberdb:keys:${matches[1]}`]
174
+ });
175
+ }
176
+ });
177
+ this.client && this.client.batch(queries, { prepare: true }, callback);
178
+ }
179
+ /**
180
+ * Closes the Cassandra connection
181
+ *
182
+ * @param {Function} callback Standard callback method
183
+ * @param {Error} callback.err Error object in case something goes wrong
184
+ */
185
+ close(callback) {
186
+ this.pool.shutdown(callback);
187
+ }
188
+ };
189
+ //#endregion
190
+ exports.default = Cassandra_db;
@@ -0,0 +1,133 @@
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let http = require("http");
4
+ http = require_runtime.__toESM(http);
5
+ let nano = require("nano");
6
+ nano = require_runtime.__toESM(nano);
7
+ //#region databases/couch_db.ts
8
+ /**
9
+ * 2012 Max 'Azul' Wiehle
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 Couch_db = class extends require_AbstractDatabase.default {
24
+ agent;
25
+ db;
26
+ constructor(settings) {
27
+ super(settings);
28
+ this.agent = null;
29
+ this.db = null;
30
+ this.settings = settings;
31
+ this.settings.cache = 1e3;
32
+ this.settings.writeInterval = 100;
33
+ this.settings.json = false;
34
+ }
35
+ get isAsync() {
36
+ return true;
37
+ }
38
+ async init() {
39
+ this.agent = new http.default.Agent({
40
+ keepAlive: true,
41
+ maxSockets: this.settings.maxListeners || 1
42
+ });
43
+ const client = (0, nano.default)({
44
+ url: `http://${this.settings.host}:${this.settings.port}`,
45
+ requestDefaults: { agent: this.agent }
46
+ });
47
+ if (this.settings.user && this.settings.password) await client.auth(this.settings.user, this.settings.password);
48
+ try {
49
+ await client.db.get(this.settings.database);
50
+ } catch (err) {
51
+ if (err.statusCode !== 404) throw err;
52
+ await client.db.create(this.settings.database);
53
+ }
54
+ this.db = client.use(this.settings.database);
55
+ }
56
+ async get(key) {
57
+ let doc;
58
+ try {
59
+ if (this.db) doc = await this.db.get(key);
60
+ } catch (err) {
61
+ if (err.statusCode === 404) return null;
62
+ throw err;
63
+ }
64
+ if (doc && "value" in doc) return doc.value;
65
+ return "";
66
+ }
67
+ async findKeys(key, notKey) {
68
+ const pfxLen = key.indexOf("*");
69
+ if (!this.db) return;
70
+ const pfx = pfxLen < 0 ? key : key.slice(0, pfxLen);
71
+ return (await this.db.find({
72
+ selector: { _id: pfxLen < 0 ? pfx : {
73
+ $gte: pfx,
74
+ $lte: `${pfx}\ufff0`,
75
+ $regex: this.createFindRegex(key, notKey).source
76
+ } },
77
+ fields: ["_id"]
78
+ })).docs.map((doc) => doc._id);
79
+ }
80
+ async set(key, value) {
81
+ let doc;
82
+ if (!this.db) return;
83
+ try {
84
+ doc = await this.db.get(key);
85
+ } catch (err) {
86
+ if (err.statusCode !== 404) throw err;
87
+ }
88
+ await this.db.insert({
89
+ _id: key,
90
+ value,
91
+ ...doc == null ? {} : { _rev: doc._rev }
92
+ });
93
+ }
94
+ async remove(key) {
95
+ let header;
96
+ if (!this.db) return;
97
+ try {
98
+ header = await this.db.head(key);
99
+ } catch (err) {
100
+ if (err.statusCode === 404) return;
101
+ throw err;
102
+ }
103
+ const etag = JSON.parse(header.etag);
104
+ await this.db.destroy(key, etag);
105
+ }
106
+ async doBulk(bulk) {
107
+ if (!this.db) return;
108
+ const keys = bulk.map((op) => op.key);
109
+ const revs = {};
110
+ for (const { key, value } of (await this.db.fetchRevs({ keys })).rows) if (value != null) revs[key] = value.rev;
111
+ const setters = [];
112
+ for (const item of bulk) {
113
+ const set = {
114
+ _id: item.key,
115
+ _rev: void 0,
116
+ _deleted: false,
117
+ value: ""
118
+ };
119
+ if (revs[item.key] != null) set._rev = revs[item.key];
120
+ if (item.type === "set") set.value = item.value;
121
+ if (item.type === "remove") set._deleted = true;
122
+ setters.push(set);
123
+ }
124
+ await this.db.bulk({ docs: setters });
125
+ }
126
+ async close() {
127
+ this.db = null;
128
+ if (this.agent) this.agent.destroy();
129
+ this.agent = null;
130
+ }
131
+ };
132
+ //#endregion
133
+ exports.default = Couch_db;
@@ -0,0 +1,62 @@
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let dirty_ts = require("dirty-ts");
4
+ dirty_ts = require_runtime.__toESM(dirty_ts);
5
+ //#region databases/dirty_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 dirty_db_default = class extends require_AbstractDatabase.default {
22
+ db;
23
+ constructor(settings) {
24
+ super(settings);
25
+ this.db = null;
26
+ if (!settings || !settings.filename) settings = { filename: null };
27
+ this.settings = settings;
28
+ this.settings.cache = 0;
29
+ this.settings.writeInterval = 0;
30
+ this.settings.json = false;
31
+ }
32
+ init(callback) {
33
+ this.db = new dirty_ts.default(this.settings.filename);
34
+ this.db.on("load", () => {
35
+ callback();
36
+ });
37
+ }
38
+ get(key, callback) {
39
+ callback(null, this.db.get(key));
40
+ }
41
+ findKeys(key, notKey, callback) {
42
+ const keys = [];
43
+ const regex = this.createFindRegex(key, notKey);
44
+ this.db.forEach((key) => {
45
+ if (key.search(regex) !== -1) keys.push(key);
46
+ });
47
+ callback(null, keys);
48
+ }
49
+ set(key, value, callback) {
50
+ this.db.set(key, value, callback);
51
+ }
52
+ remove(key, callback) {
53
+ this.db.rm(key, callback);
54
+ }
55
+ close(callback) {
56
+ this.db.close();
57
+ this.db = null;
58
+ if (callback) callback();
59
+ }
60
+ };
61
+ //#endregion
62
+ exports.default = dirty_db_default;
@@ -0,0 +1,68 @@
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let dirty_ts = require("dirty-ts");
4
+ dirty_ts = require_runtime.__toESM(dirty_ts);
5
+ let node_path = require("node:path");
6
+ let simple_git = require("simple-git");
7
+ //#region databases/dirty_git_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 dirty_git_db_default = class extends require_AbstractDatabase.default {
24
+ db;
25
+ constructor(settings) {
26
+ super(settings);
27
+ this.db = null;
28
+ if (!settings || !settings.filename) settings = {};
29
+ this.settings = settings;
30
+ this.settings.cache = 0;
31
+ this.settings.writeInterval = 0;
32
+ this.settings.json = false;
33
+ }
34
+ init(callback) {
35
+ this.db = new dirty_ts.default(this.settings.filename);
36
+ this.db.on("load", (err) => {
37
+ callback();
38
+ });
39
+ }
40
+ get(key, callback) {
41
+ callback(null, this.db.get(key));
42
+ }
43
+ findKeys(key, notKey, callback) {
44
+ const keys = [];
45
+ const regex = this.createFindRegex(key, notKey);
46
+ this.db.forEach((key, val) => {
47
+ if (key.search(regex) !== -1) keys.push(key);
48
+ });
49
+ callback(null, keys);
50
+ }
51
+ set(key, value, callback) {
52
+ this.db.set(key, value, callback);
53
+ (0, simple_git.simpleGit)((0, node_path.dirname)(this.settings.filename)).silent(true).add("./*.db").commit("Automated commit...").push([
54
+ "-u",
55
+ "origin",
56
+ "master"
57
+ ], () => console.debug("Stored git commit"));
58
+ }
59
+ remove(key, callback) {
60
+ this.db.rm(key, callback);
61
+ }
62
+ close(callback) {
63
+ this.db.close();
64
+ if (callback) callback();
65
+ }
66
+ };
67
+ //#endregion
68
+ exports.default = dirty_git_db_default;