@nostr-dev-kit/cache-sqlite
Advanced tools
+69
-9
@@ -103,3 +103,8 @@ "use strict"; | ||
| last_connected_at INTEGER, | ||
| dont_connect_before INTEGER | ||
| dont_connect_before INTEGER, | ||
| consecutive_failures INTEGER, | ||
| last_failure_at INTEGER, | ||
| nip11_data TEXT, | ||
| nip11_fetched_at INTEGER, | ||
| metadata TEXT | ||
| ); | ||
@@ -274,3 +279,15 @@ `, | ||
| if (!this.db) throw new Error("Database not initialized"); | ||
| const stmt = "SELECT last_connected_at, dont_connect_before FROM relay_status WHERE url = ? LIMIT 1"; | ||
| const stmt = ` | ||
| SELECT | ||
| last_connected_at, | ||
| dont_connect_before, | ||
| consecutive_failures, | ||
| last_failure_at, | ||
| nip11_data, | ||
| nip11_fetched_at, | ||
| metadata | ||
| FROM relay_status | ||
| WHERE url = ? | ||
| LIMIT 1 | ||
| `; | ||
| try { | ||
@@ -280,6 +297,26 @@ const prepared = this.db.getDatabase().prepare(stmt); | ||
| if (result) { | ||
| return { | ||
| const info = { | ||
| lastConnectedAt: result.last_connected_at || void 0, | ||
| dontConnectBefore: result.dont_connect_before || void 0 | ||
| dontConnectBefore: result.dont_connect_before || void 0, | ||
| consecutiveFailures: result.consecutive_failures || void 0, | ||
| lastFailureAt: result.last_failure_at || void 0 | ||
| }; | ||
| if (result.nip11_data && result.nip11_fetched_at) { | ||
| try { | ||
| info.nip11 = { | ||
| data: JSON.parse(result.nip11_data), | ||
| fetchedAt: result.nip11_fetched_at | ||
| }; | ||
| } catch (e) { | ||
| console.error("Error parsing NIP-11 data:", e); | ||
| } | ||
| } | ||
| if (result.metadata) { | ||
| try { | ||
| info.metadata = JSON.parse(result.metadata); | ||
| } catch (e) { | ||
| console.error("Error parsing metadata:", e); | ||
| } | ||
| } | ||
| return info; | ||
| } | ||
@@ -532,9 +569,32 @@ return void 0; | ||
| if (!this.db) throw new Error("Database not initialized"); | ||
| const stmt = ` | ||
| INSERT OR REPLACE INTO relay_status (url, last_connected_at, dont_connect_before) | ||
| VALUES (?, ?, ?) | ||
| `; | ||
| try { | ||
| const existing = this.getRelayStatus(relayUrl); | ||
| const mergedMetadata = { | ||
| ...existing?.metadata, | ||
| ...info.metadata | ||
| }; | ||
| const stmt = ` | ||
| INSERT OR REPLACE INTO relay_status ( | ||
| url, | ||
| last_connected_at, | ||
| dont_connect_before, | ||
| consecutive_failures, | ||
| last_failure_at, | ||
| nip11_data, | ||
| nip11_fetched_at, | ||
| metadata | ||
| ) | ||
| VALUES (?, ?, ?, ?, ?, ?, ?, ?) | ||
| `; | ||
| const prepared = this.db.getDatabase().prepare(stmt); | ||
| prepared.run(relayUrl, info.lastConnectedAt || null, info.dontConnectBefore || null); | ||
| prepared.run( | ||
| relayUrl, | ||
| info.lastConnectedAt ?? existing?.lastConnectedAt ?? null, | ||
| info.dontConnectBefore ?? existing?.dontConnectBefore ?? null, | ||
| info.consecutiveFailures ?? existing?.consecutiveFailures ?? null, | ||
| info.lastFailureAt ?? existing?.lastFailureAt ?? null, | ||
| info.nip11 ? JSON.stringify(info.nip11.data) : existing?.nip11 ? JSON.stringify(existing.nip11.data) : null, | ||
| info.nip11?.fetchedAt ?? existing?.nip11?.fetchedAt ?? null, | ||
| Object.keys(mergedMetadata).length > 0 ? JSON.stringify(mergedMetadata) : null | ||
| ); | ||
| } catch (e) { | ||
@@ -541,0 +601,0 @@ console.error("Error updating relay status:", e); |
+69
-9
@@ -70,3 +70,8 @@ // src/index.ts | ||
| last_connected_at INTEGER, | ||
| dont_connect_before INTEGER | ||
| dont_connect_before INTEGER, | ||
| consecutive_failures INTEGER, | ||
| last_failure_at INTEGER, | ||
| nip11_data TEXT, | ||
| nip11_fetched_at INTEGER, | ||
| metadata TEXT | ||
| ); | ||
@@ -241,3 +246,15 @@ `, | ||
| if (!this.db) throw new Error("Database not initialized"); | ||
| const stmt = "SELECT last_connected_at, dont_connect_before FROM relay_status WHERE url = ? LIMIT 1"; | ||
| const stmt = ` | ||
| SELECT | ||
| last_connected_at, | ||
| dont_connect_before, | ||
| consecutive_failures, | ||
| last_failure_at, | ||
| nip11_data, | ||
| nip11_fetched_at, | ||
| metadata | ||
| FROM relay_status | ||
| WHERE url = ? | ||
| LIMIT 1 | ||
| `; | ||
| try { | ||
@@ -247,6 +264,26 @@ const prepared = this.db.getDatabase().prepare(stmt); | ||
| if (result) { | ||
| return { | ||
| const info = { | ||
| lastConnectedAt: result.last_connected_at || void 0, | ||
| dontConnectBefore: result.dont_connect_before || void 0 | ||
| dontConnectBefore: result.dont_connect_before || void 0, | ||
| consecutiveFailures: result.consecutive_failures || void 0, | ||
| lastFailureAt: result.last_failure_at || void 0 | ||
| }; | ||
| if (result.nip11_data && result.nip11_fetched_at) { | ||
| try { | ||
| info.nip11 = { | ||
| data: JSON.parse(result.nip11_data), | ||
| fetchedAt: result.nip11_fetched_at | ||
| }; | ||
| } catch (e) { | ||
| console.error("Error parsing NIP-11 data:", e); | ||
| } | ||
| } | ||
| if (result.metadata) { | ||
| try { | ||
| info.metadata = JSON.parse(result.metadata); | ||
| } catch (e) { | ||
| console.error("Error parsing metadata:", e); | ||
| } | ||
| } | ||
| return info; | ||
| } | ||
@@ -499,9 +536,32 @@ return void 0; | ||
| if (!this.db) throw new Error("Database not initialized"); | ||
| const stmt = ` | ||
| INSERT OR REPLACE INTO relay_status (url, last_connected_at, dont_connect_before) | ||
| VALUES (?, ?, ?) | ||
| `; | ||
| try { | ||
| const existing = this.getRelayStatus(relayUrl); | ||
| const mergedMetadata = { | ||
| ...existing?.metadata, | ||
| ...info.metadata | ||
| }; | ||
| const stmt = ` | ||
| INSERT OR REPLACE INTO relay_status ( | ||
| url, | ||
| last_connected_at, | ||
| dont_connect_before, | ||
| consecutive_failures, | ||
| last_failure_at, | ||
| nip11_data, | ||
| nip11_fetched_at, | ||
| metadata | ||
| ) | ||
| VALUES (?, ?, ?, ?, ?, ?, ?, ?) | ||
| `; | ||
| const prepared = this.db.getDatabase().prepare(stmt); | ||
| prepared.run(relayUrl, info.lastConnectedAt || null, info.dontConnectBefore || null); | ||
| prepared.run( | ||
| relayUrl, | ||
| info.lastConnectedAt ?? existing?.lastConnectedAt ?? null, | ||
| info.dontConnectBefore ?? existing?.dontConnectBefore ?? null, | ||
| info.consecutiveFailures ?? existing?.consecutiveFailures ?? null, | ||
| info.lastFailureAt ?? existing?.lastFailureAt ?? null, | ||
| info.nip11 ? JSON.stringify(info.nip11.data) : existing?.nip11 ? JSON.stringify(existing.nip11.data) : null, | ||
| info.nip11?.fetchedAt ?? existing?.nip11?.fetchedAt ?? null, | ||
| Object.keys(mergedMetadata).length > 0 ? JSON.stringify(mergedMetadata) : null | ||
| ); | ||
| } catch (e) { | ||
@@ -508,0 +568,0 @@ console.error("Error updating relay status:", e); |
+3
-3
| { | ||
| "name": "@nostr-dev-kit/cache-sqlite", | ||
| "version": "4.0.1", | ||
| "version": "5.0.0", | ||
| "description": "SQLite cache adapter for NDK using better-sqlite3, compatible with Node.js environments.", | ||
@@ -46,3 +46,3 @@ "keywords": [ | ||
| "devDependencies": { | ||
| "@nostr-dev-kit/ndk": "^2.16.0", | ||
| "@nostr-dev-kit/ndk": "^2.17.0", | ||
| "@types/better-sqlite3": "^7.6.8", | ||
@@ -58,4 +58,4 @@ "tsup": "^8.0.0", | ||
| "peerDependencies": { | ||
| "@nostr-dev-kit/ndk": "^2.16.0" | ||
| "@nostr-dev-kit/ndk": "^2.17.0" | ||
| } | ||
| } |
53739
7.6%1422
9.22%