ueberdb2 5.0.38 → 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,135 @@
1
+ require("../_virtual/_rolldown/runtime.js");
2
+ const require_AbstractDatabase = require("../lib/AbstractDatabase.js");
3
+ let surrealdb = require("surrealdb");
4
+ //#region databases/surrealdb_db.ts
5
+ /**
6
+ * 2023 Samuel Schwanzer
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
+ const DATABASE = "ueberdb";
21
+ const WILDCARD = "*";
22
+ const replaceAt = function(index, replacement, original) {
23
+ return original.substring(0, index) + replacement + original.substring(index + replacement.length);
24
+ };
25
+ const escapeKey = (key) => {
26
+ const index = key.indexOf(":");
27
+ if (index > -1) return replaceAt(index, "_", key);
28
+ return key;
29
+ };
30
+ const unescapeKey = (key, originalKey) => {
31
+ const index = originalKey.indexOf(":");
32
+ if (index > -1) return replaceAt(index, ":", key);
33
+ return key;
34
+ };
35
+ var SurrealDB = class extends require_AbstractDatabase.default {
36
+ _client;
37
+ constructor(settings) {
38
+ super(settings);
39
+ this._client = null;
40
+ }
41
+ get isAsync() {
42
+ return true;
43
+ }
44
+ async init() {
45
+ if (this.settings.url) {
46
+ this._client = new surrealdb.Surreal();
47
+ await this._client.connect(this.settings.url);
48
+ } else if (this.settings.host) {
49
+ const port = this.settings.port || 8e3;
50
+ const protocol = this.settings.clientOptions?.protocol || "http://";
51
+ const path = this.settings.clientOptions?.path || "/rpc";
52
+ const host = this.settings.host;
53
+ this._client = new surrealdb.Surreal();
54
+ await this._client.connect(`${protocol}${host}:${port}${path}`);
55
+ }
56
+ if (this.settings.user && this.settings.password) await this._client.signin({
57
+ username: this.settings.user,
58
+ password: this.settings.password
59
+ });
60
+ await this._client.use({
61
+ namespace: DATABASE,
62
+ database: DATABASE
63
+ });
64
+ }
65
+ async get(key) {
66
+ if (this._client == null) return null;
67
+ key = escapeKey(key);
68
+ const rows = (await this._client.query("SELECT key, value FROM store WHERE key = $key", { key }))[0] || [];
69
+ if (rows.length === 0) return null;
70
+ const row = rows[0];
71
+ if (typeof row === "string") return row;
72
+ return unescapeKey(row.value, key);
73
+ }
74
+ async findKeys(key, notKey) {
75
+ if (this._client == null) return null;
76
+ const queryString = notKey != null ? `SELECT key FROM store WHERE ${this.transformWildcard(key, "key")} AND ${this.transformWildcardNegative(notKey, "notKey")}` : `SELECT key FROM store WHERE ${this.transformWildcard(key, "key")}`;
77
+ const cleanKey = key.replace(WILDCARD, "");
78
+ const cleanNotKey = (notKey || "").replace(WILDCARD, "");
79
+ const bindings = { key: cleanKey };
80
+ if (notKey != null) bindings.notKey = cleanNotKey;
81
+ const res = await this._client.query(queryString, bindings);
82
+ return this.transformResult(res[0] || [], cleanKey);
83
+ }
84
+ transformWildcard(key, keyExpr) {
85
+ if (key.startsWith(WILDCARD) && key.endsWith(WILDCARD)) return `${keyExpr} CONTAINS $${keyExpr}`;
86
+ else if (key.startsWith(WILDCARD)) return `string::ends_with(${keyExpr}, $${keyExpr})`;
87
+ else if (key.endsWith(WILDCARD)) return `string::starts_with(${keyExpr}, $${keyExpr})`;
88
+ else return `${keyExpr} = $${keyExpr}`;
89
+ }
90
+ transformWildcardNegative(key, keyExpr) {
91
+ if (key.startsWith(WILDCARD) && key.endsWith(WILDCARD)) return `key CONTAINSNOT $${keyExpr}`;
92
+ else if (key.startsWith(WILDCARD)) return `string::ends_with(key, $${keyExpr}) == false`;
93
+ else if (key.endsWith(WILDCARD)) return `string::starts_with(key, $${keyExpr}) == false`;
94
+ else return `key != $${keyExpr}`;
95
+ }
96
+ transformResult(rows, originalKey) {
97
+ const value = [];
98
+ if (typeof rows === "string") {
99
+ value.push(unescapeKey(rows, originalKey));
100
+ return value;
101
+ }
102
+ for (const row of rows) value.push(unescapeKey(row.key, originalKey));
103
+ return value;
104
+ }
105
+ async set(key, value) {
106
+ if (this._client == null) return null;
107
+ const exists = await this.get(key);
108
+ const escapedKey = escapeKey(key);
109
+ if (exists) await this._client.query("UPDATE store SET value = $value WHERE key = $key", {
110
+ key: escapedKey,
111
+ value
112
+ });
113
+ else await this._client.query("INSERT INTO store (key, value) VALUES ($key, $value)", {
114
+ key: escapedKey,
115
+ value
116
+ });
117
+ }
118
+ async remove(key) {
119
+ if (this._client == null) return null;
120
+ key = escapeKey(key);
121
+ return await this._client.query("DELETE FROM store WHERE key = $key", { key });
122
+ }
123
+ async doBulk(bulk) {
124
+ if (this._client == null) return null;
125
+ for (const b of bulk) if (b.type === "set") await this.set(b.key, b.value);
126
+ else if (b.type === "remove") await this.remove(b.key);
127
+ }
128
+ async close() {
129
+ if (this._client == null) return null;
130
+ await this._client.close();
131
+ this._client = null;
132
+ }
133
+ };
134
+ //#endregion
135
+ exports.default = SurrealDB;
package/dist/index.d.ts CHANGED
@@ -1,20 +1,4 @@
1
1
  import { Settings } from './lib/AbstractDatabase';
2
- import Cassandra_db from './databases/cassandra_db';
3
- import Couch_db from './databases/couch_db';
4
- import Dirty_db from './databases/dirty_db';
5
- import Dirty_git_db from './databases/dirty_git_db';
6
- import Elasticsearch_db from './databases/elasticsearch_db';
7
- import MemoryDB from './databases/memory_db';
8
- import Mock_db from './databases/mock_db';
9
- import Mongodb_db from './databases/mongodb_db';
10
- import MSSQL from './databases/mssql_db';
11
- import Mysql_db from './databases/mysql_db';
12
- import Postgres_db from './databases/postgres_db';
13
- import RedisDB from './databases/redis_db';
14
- import Rethink_db from './databases/rethink_db';
15
- import SQLiteDB from './databases/sqlite_db';
16
- import SurrealDB from './databases/surrealdb_db';
17
- import Rusty_db from "./databases/rusty_db";
18
2
  export type DatabaseType = 'mysql' | 'postgres' | 'sqlite' | 'rustydb' | 'mongodb' | 'redis' | 'cassandra' | 'dirty' | 'dirtygit' | 'elasticsearch' | 'memory' | 'mock' | 'mssql' | 'postgrespool' | 'rethink' | 'couch' | 'surrealdb';
19
3
  export declare class Database {
20
4
  readonly type: DatabaseType;
@@ -37,7 +21,7 @@ export declare class Database {
37
21
  * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
38
22
  */
39
23
  init(callback?: null): any;
40
- initDB(): Cassandra_db | Couch_db | Dirty_db | Dirty_git_db | Elasticsearch_db | MemoryDB | Mock_db | Mongodb_db | MSSQL | Mysql_db | Postgres_db | RedisDB | Rethink_db | SQLiteDB | SurrealDB | Rusty_db;
24
+ initDB(): Promise<import("./databases/cassandra_db").default | import("./databases/mysql_db").default | import("./databases/postgres_db").default | import("./databases/sqlite_db").default | import("./databases/rusty_db").default | import("./databases/mongodb_db").default | import("./databases/redis_db").default | import("./databases/dirty_db").default | import("./databases/dirty_git_db").default | import("./databases/elasticsearch_db").default | import("./databases/memory_db").default | import("./databases/mock_db").default | import("./databases/mssql_db").default | import("./databases/rethink_db").default | import("./databases/couch_db").default | import("./databases/surrealdb_db").default>;
41
25
  /**
42
26
  * Wrapper functions
43
27
  */