| 1 | import * as Evolu from "@evolu/common"; |
| 2 | import { createUseEvolu } from "@evolu/react"; |
| 3 | import { evoluReactWebDeps } from "@evolu/react-web"; |
| 4 | import { Schema, type RSSFeedId } from "./scheme.ts"; |
| 5 | |
| 6 | // Create Evolu instance for the React web platform |
| 7 | export const evolu = Evolu.createEvolu(evoluReactWebDeps)(Schema, { |
| 8 | name: Evolu.SimpleName.orThrow("alcove"), |
| 9 | reloadUrl: "/", |
| 10 | transports: [ |
| 11 | // { |
| 12 | // type: "WebSocket", |
| 13 | // url: "wss://relay.alcove.tools", |
| 14 | // }, |
| 15 | // { |
| 16 | // type: "WebSocket", |
| 17 | // url: "wss://relay2.alcove.tools", |
| 18 | // }, |
| 19 | { type: "WebSocket" as const, url: "ws://localhost:4000" }, |
| 20 | ], |
| 21 | }); |
| 22 | |
| 23 | export const useEvolu = createUseEvolu(evolu); |
| 24 | |
| 25 | /** |
| 26 | * Subscribe to unexpected Evolu errors (database, network, sync issues). |
| 27 | */ |
| 28 | evolu.subscribeError(() => { |
| 29 | const error = evolu.getError(); |
| 30 | if (!error) return; |
| 31 | |
| 32 | console.error("Evolu error:", error); |
| 33 | }); |
| 34 | |
| 35 | export const allFeedsQuery = evolu.createQuery((db) => |
| 36 | db |
| 37 | .selectFrom("rssFeed") |
| 38 | .selectAll() |
| 39 | .where("isDeleted", "is not", Evolu.sqliteTrue) |
| 40 | // Filter out null titles and feedUrls (required fields) |
| 41 | .where("title", "is not", null) |
| 42 | .where("feedUrl", "is not", null) |
| 43 | .$narrowType<{ |
| 44 | title: Evolu.kysely.NotNull; |
| 45 | feedUrl: Evolu.kysely.NotNull; |
| 46 | }>() |
| 47 | .orderBy("createdAt"), |
| 48 | ); |
| 49 | |
| 50 | export const postsByFeedQuery = (feedId: string) => |
| 51 | evolu.createQuery((db) => |
| 52 | db |
| 53 | .selectFrom("rssPost") |
| 54 | .selectAll() |
| 55 | .where("feedId", "=", feedId as RSSFeedId) |
| 56 | .where("isDeleted", "is not", Evolu.sqliteTrue) |
| 57 | // Filter out null required fields |
| 58 | .where("title", "is not", null) |
| 59 | .where("link", "is not", null) |
| 60 | .$narrowType<{ |
| 61 | title: Evolu.kysely.NotNull; |
| 62 | link: Evolu.kysely.NotNull; |
| 63 | }>() |
| 64 | .orderBy("id", "desc"), |
| 65 | ); |
| 66 | |
| 67 | export const allPostsQuery = evolu.createQuery((db) => |
| 68 | db |
| 69 | .selectFrom("rssPost") |
| 70 | .selectAll() |
| 71 | .where("isDeleted", "is not", Evolu.sqliteTrue) |
| 72 | // Filter out null required fields |
| 73 | .where("title", "is not", null) |
| 74 | .where("link", "is not", null) |
| 75 | .$narrowType<{ |
| 76 | title: Evolu.kysely.NotNull; |
| 77 | link: Evolu.kysely.NotNull; |
| 78 | }>() |
| 79 | .orderBy("id", "desc"), |
| 80 | ); |
| 81 | |
| 82 | export const feedsByCategoryQuery = evolu.createQuery((db) => |
| 83 | db |
| 84 | .selectFrom("rssFeed") |
| 85 | .selectAll() |
| 86 | .where("isDeleted", "is not", Evolu.sqliteTrue) |
| 87 | // Filter out null required fields |
| 88 | .where("title", "is not", null) |
| 89 | .where("feedUrl", "is not", null) |
| 90 | .$narrowType<{ |
| 91 | title: Evolu.kysely.NotNull; |
| 92 | feedUrl: Evolu.kysely.NotNull; |
| 93 | }>() |
| 94 | .orderBy("category", "asc"), |
| 95 | ); |
| 96 | |
| 97 | export const allReadStatusesQuery = evolu.createQuery((db) => |
| 98 | db.selectFrom("readStatus").selectAll().where("isRead", "=", 1), |
| 99 | ); |
| 100 | |
| 101 | export const allReadStatusesWithUnreadQuery = evolu.createQuery((db) => |
| 102 | db.selectFrom("readStatus").selectAll(), |
| 103 | ); |
| 104 | |
| 105 | export function reset() { |
| 106 | void evolu.resetAppOwner({ reload: true }); |
| 107 | } |