A convenient CLI tool to quickly spin up DragonflyBSD virtual machines using QEMU with sensible defaults.
at main 88 lines 2.4 kB view raw
1import { Data, Effect } from "effect"; 2import type { DeleteResult, InsertResult } from "kysely"; 3import { ctx } from "./context.ts"; 4import type { Image } from "./db.ts"; 5 6export class DbError extends Data.TaggedError("DatabaseError")<{ 7 message?: string; 8}> {} 9 10export const listImages = (): Effect.Effect<Image[], DbError, never> => 11 Effect.tryPromise({ 12 try: () => ctx.db.selectFrom("images").selectAll().execute(), 13 catch: (error) => 14 new DbError({ 15 message: error instanceof Error ? error.message : String(error), 16 }), 17 }); 18 19export const getImage = ( 20 id: string, 21): Effect.Effect<Image | undefined, DbError, never> => 22 Effect.tryPromise({ 23 try: () => 24 ctx.db 25 .selectFrom("images") 26 .selectAll() 27 .where((eb) => 28 eb.or([ 29 eb.and([ 30 eb("repository", "=", id.split(":")[0]), 31 eb("tag", "=", id.split(":")[1] || "latest"), 32 ]), 33 eb("id", "=", id), 34 eb("digest", "=", id), 35 eb("path", "=", id), 36 ]) 37 ) 38 .executeTakeFirst(), 39 catch: (error) => 40 new DbError({ 41 message: error instanceof Error ? error.message : String(error), 42 }), 43 }); 44 45export const saveImage = ( 46 image: Image, 47): Effect.Effect<InsertResult[], DbError, never> => 48 Effect.tryPromise({ 49 try: () => 50 ctx.db.insertInto("images") 51 .values(image) 52 .onConflict((oc) => 53 oc 54 .column("repository") 55 .column("tag") 56 .doUpdateSet({ 57 size: image.size, 58 path: image.path, 59 format: image.format, 60 digest: image.digest, 61 }) 62 ) 63 .execute(), 64 catch: (error) => 65 new DbError({ 66 message: error instanceof Error ? error.message : String(error), 67 }), 68 }); 69 70export const deleteImage = ( 71 id: string, 72): Effect.Effect<DeleteResult[], DbError, never> => 73 Effect.tryPromise({ 74 try: () => 75 ctx.db.deleteFrom("images").where((eb) => 76 eb.or([ 77 eb.and([ 78 eb("repository", "=", id.split(":")[0]), 79 eb("tag", "=", id.split(":")[1] || "latest"), 80 ]), 81 eb("id", "=", id), 82 ]) 83 ).execute(), 84 catch: (error) => 85 new DbError({ 86 message: error instanceof Error ? error.message : String(error), 87 }), 88 });