provider.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { z } from "zod"
  2. import { fn } from "./util/fn"
  3. import { Actor } from "./actor"
  4. import { and, Database, eq, isNull } from "./drizzle"
  5. import { Identifier } from "./identifier"
  6. import { ProviderTable } from "./schema/provider.sql"
  7. export namespace Provider {
  8. export const list = fn(z.void(), () =>
  9. Database.use((tx) =>
  10. tx
  11. .select()
  12. .from(ProviderTable)
  13. .where(and(eq(ProviderTable.workspaceID, Actor.workspace()), isNull(ProviderTable.timeDeleted))),
  14. ),
  15. )
  16. export const create = fn(
  17. z.object({
  18. provider: z.string().min(1).max(64),
  19. credentials: z.string(),
  20. }),
  21. ({ provider, credentials }) =>
  22. Database.use((tx) =>
  23. tx
  24. .insert(ProviderTable)
  25. .values({
  26. id: Identifier.create("provider"),
  27. workspaceID: Actor.workspace(),
  28. provider,
  29. credentials,
  30. })
  31. .onDuplicateKeyUpdate({
  32. set: {
  33. credentials,
  34. timeDeleted: null,
  35. },
  36. }),
  37. ),
  38. )
  39. export const remove = fn(z.object({ provider: z.string() }), ({ provider }) =>
  40. Database.transaction((tx) =>
  41. tx
  42. .delete(ProviderTable)
  43. .where(and(eq(ProviderTable.provider, provider), eq(ProviderTable.workspaceID, Actor.workspace()))),
  44. ),
  45. )
  46. }