feat(supabase): animals and relations access layer
animals.ts: POST search with species filter and a probe row, read by id with explicit columns, create with the optional first client in one call, update, archive. relations.ts: an animal's relations in one select (voided left out), create, end, dates, void. Kinds duplicate_relation and already_ended added.
This commit is contained in:
1 parent
3c2b503fc9
commit
e22357630f
7 files changed
+1612
-12
No files matched your search
@@ -3,6 +3,7 @@ import type { FaunosteoClient } from './client'
|
||||
import type { Database, Enums, Tables } from './database.types'
|
||||
import {
|
||||
failRecord,
|
||||
parseRecordAnswer,
|
||||
RecordFailure,
|
||||
UUID_PATTERN,
|
||||
type RecordPage,
|
||||
@@ -132,13 +133,8 @@ const CreatedSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
revision: z.number().int(),
|
||||
})
|
||||
const RevisionSchema = z.object({ revision: z.number().int() })
|
||||
|
||||
function parsed<T>(schema: z.ZodType<T>, data: unknown): T {
|
||||
const result = schema.safeParse(data)
|
||||
if (!result.success) throw new RecordFailure('unavailable', result.error)
|
||||
return result.data
|
||||
}
|
||||
/** The answer of every update, archive and relation write RPC. */
|
||||
export const RevisionSchema = z.object({ revision: z.number().int() })
|
||||
|
||||
export interface CreateClientInput {
|
||||
workspaceId: string
|
||||
@@ -166,7 +162,7 @@ export async function createClientRecord(
|
||||
country: f.country ?? undefined,
|
||||
})
|
||||
if (error) failRecord(error, status)
|
||||
return parsed(CreatedSchema, data)
|
||||
return parseRecordAnswer(CreatedSchema, data)
|
||||
}
|
||||
|
||||
type UpdateClientArgs = Database['public']['Functions']['update_client']['Args']
|
||||
@@ -204,7 +200,7 @@ export async function updateClientRecord(
|
||||
args as UpdateClientArgs,
|
||||
)
|
||||
if (error) failRecord(error, status)
|
||||
return parsed(RevisionSchema, data)
|
||||
return parseRecordAnswer(RevisionSchema, data)
|
||||
}
|
||||
|
||||
export async function setClientArchived(
|
||||
@@ -217,7 +213,7 @@ export async function setClientArchived(
|
||||
archived,
|
||||
})
|
||||
if (error) failRecord(error, status)
|
||||
return parsed(RevisionSchema, data)
|
||||
return parseRecordAnswer(RevisionSchema, data)
|
||||
}
|
||||
|
||||
const ArchivePreviewSchema = z.object({
|
||||
@@ -235,7 +231,7 @@ export async function fetchClientArchivePreview(
|
||||
client_id: clientId,
|
||||
})
|
||||
if (error) failRecord(error, status)
|
||||
return parsed(ArchivePreviewSchema, data)
|
||||
return parseRecordAnswer(ArchivePreviewSchema, data)
|
||||
}
|
||||
|
||||
const ClientAnimalSchema = z.object({
|
||||
@@ -274,7 +270,7 @@ export async function listClientAnimals(
|
||||
after_id: options.after?.relationId,
|
||||
})
|
||||
if (error) failRecord(error, status)
|
||||
const rows = parsed(z.array(ClientAnimalSchema), data)
|
||||
const rows = parseRecordAnswer(z.array(ClientAnimalSchema), data)
|
||||
const page = rows.slice(0, size)
|
||||
const last = page.at(-1)
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user