this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

make test utils for database

ansxor 2ac81532 ac835d3c

+88 -10
+2 -4
apps/api/index.ts
··· 2 2 import type { InferOutput } from "@atcute/lexicons/validations"; 3 3 import { json, XRPCRouter } from "@atcute/xrpc-server"; 4 4 import { cors } from "@atcute/xrpc-server/middlewares/cors"; 5 + import { createDb } from "db"; 5 6 import type { BlobRef } from "db/schema"; 6 7 import * as dbschema from "db/schema"; 7 8 import { inArray } from "drizzle-orm"; 8 - import { drizzle } from "drizzle-orm/postgres-js"; 9 9 import { 10 10 CaAnsxorCatnipGetTracks, 11 11 type CaAnsxorCatnipTrack, ··· 56 56 } satisfies TrackOutput; 57 57 } 58 58 59 - const db = drizzle("postgresql://postgres:postgres@0.0.0.0:5432/catnip", { 60 - schema: dbschema, 61 - }); 59 + const db = createDb(); 62 60 63 61 const router = new XRPCRouter({ middlewares: [cors()] }); 64 62
+2 -4
apps/api/scripts/create-track.ts
··· 1 + import { createDb } from "db"; 1 2 import type { BlobRef } from "db/schema"; 2 3 import * as dbschema from "db/schema"; 3 - import { drizzle } from "drizzle-orm/postgres-js"; 4 4 5 - const db = drizzle("postgresql://postgres:postgres@0.0.0.0:5432/catnip", { 6 - schema: dbschema, 7 - }); 5 + const db = createDb(); 8 6 9 7 const COLLECTION = "ca.ansxor.catnip.track"; 10 8
+12 -1
apps/firehose/index.ts
··· 1 1 import { JetstreamSubscription } from "@atcute/jetstream"; 2 2 import { is } from '@atcute/lexicons'; 3 + import { CaAnsxorCatnipTrack } from "lexicon/atcute-lexicon"; 3 4 4 5 const subscription = new JetstreamSubscription({ 5 6 url: "wss://jetstream2.us-east.bsky.network", 6 7 wantedCollections: ["ca.ansxor.catnip.track"] 7 8 }); 9 + 10 + async function loop(subscription: JetstreamSubscription) { 8 11 9 12 for await (const event of subscription) { 10 13 if (event.kind !== "commit") { ··· 17 20 } 18 21 const { collection, operation, rkey, rev } = event.commit; 19 22 20 - if (!is) 23 + if (!is(CaAnsxorCatnipTrack.mainSchema, commit.record)) { 24 + console.warn('invalid record', commit.record); 25 + continue; 26 + } 27 + 28 + 21 29 } 30 + } 31 + 32 + await loop(subscription);
+17
apps/firehose/tests/firehose.test.ts
··· 1 + import { test, expect, beforeAll, afterAll } from "bun:test"; 2 + import { setupTestDb, teardownTestDb } from "db/test-utils"; 3 + 4 + let db: Awaited<ReturnType<typeof setupTestDb>>; 5 + 6 + beforeAll(async () => { 7 + db = await setupTestDb(); 8 + }); 9 + 10 + afterAll(async () => { 11 + await teardownTestDb(); 12 + }); 13 + 14 + test("can query tracks", async () => { 15 + const tracks = await db.query.tracks.findMany(); 16 + expect(tracks).toEqual([]); 17 + });
+3 -1
packages/db/package.json
··· 2 2 "name": "db", 3 3 "version": "1.0.0", 4 4 "exports": { 5 - "./schema": "./src/schema.ts" 5 + ".": "./src/index.ts", 6 + "./schema": "./src/schema.ts", 7 + "./test-utils": "./src/test-utils.ts" 6 8 }, 7 9 "devDependencies": { 8 10 "@types/bun": "latest",
+10
packages/db/src/index.ts
··· 1 + import { drizzle } from "drizzle-orm/postgres-js"; 2 + import * as schema from "./schema"; 3 + 4 + export type Db = ReturnType<typeof createDb>; 5 + 6 + const DEFAULT_URL = "postgresql://postgres:postgres@0.0.0.0:5432/catnip"; 7 + 8 + export function createDb(url?: string) { 9 + return drizzle(url ?? process.env.DATABASE_URL ?? DEFAULT_URL, { schema }); 10 + }
+42
packages/db/src/test-utils.ts
··· 1 + import { migrate } from "drizzle-orm/postgres-js/migrator"; 2 + import { drizzle } from "drizzle-orm/postgres-js"; 3 + import postgres from "postgres"; 4 + import * as schema from "./schema"; 5 + 6 + const BASE_URL = 7 + process.env.TEST_DATABASE_BASE_URL ?? 8 + "postgresql://postgres:postgres@0.0.0.0:5432"; 9 + const TEST_DB_NAME = process.env.TEST_DATABASE_NAME ?? "catnip_test"; 10 + const MIGRATIONS_FOLDER = new URL("../drizzle", import.meta.url).pathname; 11 + 12 + /** 13 + * Creates a fresh test database, runs migrations, and returns a drizzle instance. 14 + * Call this in beforeAll/beforeEach. 15 + */ 16 + export async function setupTestDb() { 17 + const adminSql = postgres(`${BASE_URL}/postgres`); 18 + // Terminate existing connections to the test database 19 + await adminSql.unsafe( 20 + `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${TEST_DB_NAME}' AND pid <> pg_backend_pid()`, 21 + ); 22 + await adminSql.unsafe(`DROP DATABASE IF EXISTS ${TEST_DB_NAME}`); 23 + await adminSql.unsafe(`CREATE DATABASE ${TEST_DB_NAME}`); 24 + await adminSql.end(); 25 + 26 + const db = drizzle(`${BASE_URL}/${TEST_DB_NAME}`, { schema }); 27 + await migrate(db, { migrationsFolder: MIGRATIONS_FOLDER }); 28 + 29 + return db; 30 + } 31 + 32 + /** 33 + * Drops the test database. Call this in afterAll. 34 + */ 35 + export async function teardownTestDb() { 36 + const adminSql = postgres(`${BASE_URL}/postgres`); 37 + await adminSql.unsafe( 38 + `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${TEST_DB_NAME}' AND pid <> pg_backend_pid()`, 39 + ); 40 + await adminSql.unsafe(`DROP DATABASE IF EXISTS ${TEST_DB_NAME}`); 41 + await adminSql.end(); 42 + }