|
1 | 1 | /** Persists hosted official external plugin catalog snapshots in OpenClaw state. */ |
2 | 2 | import { existsSync } from "node:fs"; |
| 3 | +import { |
| 4 | + executeSqliteQuerySync, |
| 5 | + executeSqliteQueryTakeFirstSync, |
| 6 | + getNodeSqliteKysely, |
| 7 | +} from "../infra/kysely-sync.js"; |
| 8 | +import type { DB as OpenClawStateKyselyDatabase } from "../state/openclaw-state-db.generated.js"; |
3 | 9 | import { |
4 | 10 | openOpenClawStateDatabase, |
5 | 11 | runOpenClawStateWriteTransaction, |
@@ -28,6 +34,11 @@ type HostedCatalogSnapshotRow = { |
28 | 34 | saved_at: string; |
29 | 35 | }; |
30 | 36 |
|
| 37 | +type HostedCatalogSnapshotDatabase = Pick< |
| 38 | + OpenClawStateKyselyDatabase, |
| 39 | + "official_external_plugin_catalog_snapshots" |
| 40 | +>; |
| 41 | + |
31 | 42 | function resolveStoreEnv( |
32 | 43 | options: HostedOfficialExternalPluginCatalogSnapshotStoreOptions, |
33 | 44 | ): NodeJS.ProcessEnv | undefined { |
@@ -90,46 +101,46 @@ export function createSqliteHostedOfficialExternalPluginCatalogSnapshotStore( |
90 | 101 | return null; |
91 | 102 | } |
92 | 103 | const database = openOpenClawStateDatabase(resolveStateDatabaseOptions(options)); |
93 | | - const row = database.db |
94 | | - .prepare( |
95 | | - ` |
96 | | - SELECT feed_url, body, status, etag, last_modified, checksum, saved_at |
97 | | - FROM official_external_plugin_catalog_snapshots |
98 | | - WHERE feed_url = ? |
99 | | - `, |
100 | | - ) |
101 | | - .get(url) as HostedCatalogSnapshotRow | undefined; |
| 104 | + const stateDb = getNodeSqliteKysely<HostedCatalogSnapshotDatabase>(database.db); |
| 105 | + const row = executeSqliteQueryTakeFirstSync( |
| 106 | + database.db, |
| 107 | + stateDb |
| 108 | + .selectFrom("official_external_plugin_catalog_snapshots") |
| 109 | + .select(["feed_url", "body", "status", "etag", "last_modified", "checksum", "saved_at"]) |
| 110 | + .where("feed_url", "=", url), |
| 111 | + ) as HostedCatalogSnapshotRow | undefined; |
102 | 112 | return rowToSnapshot(row); |
103 | 113 | }, |
104 | 114 | async write(snapshot) { |
105 | 115 | const now = Date.now(); |
106 | | - runOpenClawStateWriteTransaction(({ db }) => { |
107 | | - db.prepare( |
108 | | - ` |
109 | | - INSERT INTO official_external_plugin_catalog_snapshots ( |
110 | | - feed_url, body, status, etag, last_modified, checksum, saved_at, updated_at_ms |
111 | | - ) VALUES ( |
112 | | - @feed_url, @body, @status, @etag, @last_modified, @checksum, @saved_at, @updated_at_ms |
113 | | - ) |
114 | | - ON CONFLICT(feed_url) DO UPDATE SET |
115 | | - body = excluded.body, |
116 | | - status = excluded.status, |
117 | | - etag = excluded.etag, |
118 | | - last_modified = excluded.last_modified, |
119 | | - checksum = excluded.checksum, |
120 | | - saved_at = excluded.saved_at, |
121 | | - updated_at_ms = excluded.updated_at_ms |
122 | | - `, |
123 | | - ).run({ |
124 | | - feed_url: snapshot.metadata.url, |
125 | | - body: snapshot.body, |
126 | | - status: snapshot.metadata.status, |
127 | | - etag: snapshot.metadata.etag ?? null, |
128 | | - last_modified: snapshot.metadata.lastModified ?? null, |
129 | | - checksum: snapshot.metadata.checksum, |
130 | | - saved_at: snapshot.savedAt, |
131 | | - updated_at_ms: now, |
132 | | - }); |
| 116 | + runOpenClawStateWriteTransaction((database) => { |
| 117 | + const stateDb = getNodeSqliteKysely<HostedCatalogSnapshotDatabase>(database.db); |
| 118 | + executeSqliteQuerySync( |
| 119 | + database.db, |
| 120 | + stateDb |
| 121 | + .insertInto("official_external_plugin_catalog_snapshots") |
| 122 | + .values({ |
| 123 | + feed_url: snapshot.metadata.url, |
| 124 | + body: snapshot.body, |
| 125 | + status: snapshot.metadata.status, |
| 126 | + etag: snapshot.metadata.etag ?? null, |
| 127 | + last_modified: snapshot.metadata.lastModified ?? null, |
| 128 | + checksum: snapshot.metadata.checksum, |
| 129 | + saved_at: snapshot.savedAt, |
| 130 | + updated_at_ms: now, |
| 131 | + }) |
| 132 | + .onConflict((conflict) => |
| 133 | + conflict.column("feed_url").doUpdateSet({ |
| 134 | + body: snapshot.body, |
| 135 | + status: snapshot.metadata.status, |
| 136 | + etag: snapshot.metadata.etag ?? null, |
| 137 | + last_modified: snapshot.metadata.lastModified ?? null, |
| 138 | + checksum: snapshot.metadata.checksum, |
| 139 | + saved_at: snapshot.savedAt, |
| 140 | + updated_at_ms: now, |
| 141 | + }), |
| 142 | + ), |
| 143 | + ); |
133 | 144 | }, resolveStateDatabaseOptions(options)); |
134 | 145 | }, |
135 | 146 | }; |
|
0 commit comments