Skip to content

Commit 372ec63

Browse files
committed
Use Kysely for hosted catalog snapshot store
1 parent 85e1636 commit 372ec63

1 file changed

Lines changed: 47 additions & 36 deletions

File tree

src/plugins/official-external-plugin-catalog-snapshot-store.ts

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/** Persists hosted official external plugin catalog snapshots in OpenClaw state. */
22
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";
39
import {
410
openOpenClawStateDatabase,
511
runOpenClawStateWriteTransaction,
@@ -28,6 +34,11 @@ type HostedCatalogSnapshotRow = {
2834
saved_at: string;
2935
};
3036

37+
type HostedCatalogSnapshotDatabase = Pick<
38+
OpenClawStateKyselyDatabase,
39+
"official_external_plugin_catalog_snapshots"
40+
>;
41+
3142
function resolveStoreEnv(
3243
options: HostedOfficialExternalPluginCatalogSnapshotStoreOptions,
3344
): NodeJS.ProcessEnv | undefined {
@@ -90,46 +101,46 @@ export function createSqliteHostedOfficialExternalPluginCatalogSnapshotStore(
90101
return null;
91102
}
92103
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;
102112
return rowToSnapshot(row);
103113
},
104114
async write(snapshot) {
105115
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+
);
133144
}, resolveStateDatabaseOptions(options));
134145
},
135146
};

0 commit comments

Comments
 (0)