Wire the app to the shared Supabase catalog (public catalog read integration)
Moves port/cable/category/device-template data from per-diagram embedded storage to the global public/private catalog backed by Supabase, per organized-ideas.md's "live reference, not snapshot" decision. - domain/types.ts, project.ts, compatibility.ts, bom.ts: lookup functions now take an explicit Catalog parameter instead of deriving data from Project — Project is reduced to just diagram-scoped fields. - New CatalogRepository/SupabaseCatalogRepository (mirrors the DiagramRepository pattern) and catalogStore.ts, replacing the catalog-related actions that used to live in projectStore. - device_templates gets a plain-text manufacturer column for now (the normalized manufacturer catalog from organized-ideas.md §3 is its own future pass, not blocking this one). - domain/library.ts is no longer imported by the app — it's now only the source scripts/generate-seed.mjs reads to produce supabase/seed.sql. - Swept every UI call site via tsc -b until clean; oxlint clean; 23/23 pgTAP RLS tests still passing after a `supabase db reset`.
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
import { v4 as uuid } from 'uuid'
|
||||
import type { Catalog, CableType, DeviceCategoryDef, DeviceTemplate, Port, PortType } from '../domain/types'
|
||||
import type { CatalogRepository } from './CatalogRepository'
|
||||
import { supabase } from './supabaseClient'
|
||||
|
||||
interface DeviceCategoryRow {
|
||||
id: string
|
||||
name: string
|
||||
is_public: boolean
|
||||
}
|
||||
|
||||
interface PortTypeRow {
|
||||
id: string
|
||||
name: string
|
||||
category: PortType['category']
|
||||
family: string
|
||||
compatible_family_ids: string[]
|
||||
max_connections: number | null
|
||||
is_public: boolean
|
||||
}
|
||||
|
||||
interface CableTypeRow {
|
||||
id: string
|
||||
name: string
|
||||
family: string
|
||||
family2: string | null
|
||||
unit: CableType['unit']
|
||||
cost_per_unit: number | null
|
||||
is_public: boolean
|
||||
}
|
||||
|
||||
interface DeviceTemplateRow {
|
||||
id: string
|
||||
name: string
|
||||
category_id: string
|
||||
manufacturer: string | null
|
||||
model: string | null
|
||||
cost: number | null
|
||||
is_public: boolean
|
||||
}
|
||||
|
||||
interface DeviceTemplatePortRow {
|
||||
id: string
|
||||
device_template_id: string
|
||||
name: string
|
||||
direction: Port['direction']
|
||||
port_type_id: string
|
||||
sort_order: number
|
||||
}
|
||||
|
||||
function toDeviceCategory(row: DeviceCategoryRow): DeviceCategoryDef {
|
||||
return { id: row.id, name: row.name, custom: !row.is_public }
|
||||
}
|
||||
|
||||
function toPortType(row: PortTypeRow): PortType {
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
category: row.category,
|
||||
family: row.family,
|
||||
compatibleFamilyIds: row.compatible_family_ids.length > 0 ? row.compatible_family_ids : undefined,
|
||||
maxConnections: row.max_connections ?? undefined,
|
||||
custom: !row.is_public,
|
||||
}
|
||||
}
|
||||
|
||||
function toCableType(row: CableTypeRow): CableType {
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
family: row.family,
|
||||
family2: row.family2 ?? undefined,
|
||||
unit: row.unit,
|
||||
costPerUnit: row.cost_per_unit ?? undefined,
|
||||
custom: !row.is_public,
|
||||
}
|
||||
}
|
||||
|
||||
function toDeviceTemplate(row: DeviceTemplateRow, portRows: DeviceTemplatePortRow[]): DeviceTemplate {
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
category: row.category_id,
|
||||
manufacturer: row.manufacturer ?? undefined,
|
||||
model: row.model ?? undefined,
|
||||
cost: row.cost ?? undefined,
|
||||
ports: portRows
|
||||
.filter((p) => p.device_template_id === row.id)
|
||||
.sort((a, b) => a.sort_order - b.sort_order)
|
||||
.map((p) => ({ id: p.id, name: p.name, direction: p.direction, portTypeId: p.port_type_id })),
|
||||
custom: !row.is_public,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Backs the app with the shared Supabase catalog tables instead of the
|
||||
* per-diagram embedded custom-type arrays the app used before this pass.
|
||||
* RLS scopes every select to what the current user can see (all public
|
||||
* entries, plus their own private ones) — see the `*_select` policies in
|
||||
* the init schema migration. Every write here creates or edits a private
|
||||
* (is_public = false) row owned by the current user; there is no public
|
||||
* write path yet (that's the submission workflow, organized-ideas.md §3).
|
||||
*/
|
||||
export class SupabaseCatalogRepository implements CatalogRepository {
|
||||
async load(): Promise<Catalog> {
|
||||
const [categories, portTypes, cableTypes, templates, templatePorts] = await Promise.all([
|
||||
supabase.from('device_categories').select('id, name, is_public').order('name'),
|
||||
supabase
|
||||
.from('port_types')
|
||||
.select('id, name, category, family, compatible_family_ids, max_connections, is_public')
|
||||
.order('name'),
|
||||
supabase
|
||||
.from('cable_types')
|
||||
.select('id, name, family, family2, unit, cost_per_unit, is_public')
|
||||
.order('name'),
|
||||
supabase.from('device_templates').select('id, name, category_id, manufacturer, model, cost, is_public').order('name'),
|
||||
supabase.from('device_template_ports').select('id, device_template_id, name, direction, port_type_id, sort_order'),
|
||||
])
|
||||
|
||||
for (const [label, result] of [
|
||||
['device categories', categories],
|
||||
['port types', portTypes],
|
||||
['cable types', cableTypes],
|
||||
['device templates', templates],
|
||||
['device template ports', templatePorts],
|
||||
] as const) {
|
||||
if (result.error) console.error(`Failed to load ${label} from Supabase`, result.error)
|
||||
}
|
||||
|
||||
const portRows = (templatePorts.data ?? []) as DeviceTemplatePortRow[]
|
||||
return {
|
||||
deviceCategories: ((categories.data ?? []) as DeviceCategoryRow[]).map(toDeviceCategory),
|
||||
portTypes: ((portTypes.data ?? []) as PortTypeRow[]).map(toPortType),
|
||||
cableTypes: ((cableTypes.data ?? []) as CableTypeRow[]).map(toCableType),
|
||||
deviceTemplates: ((templates.data ?? []) as DeviceTemplateRow[]).map((row) => toDeviceTemplate(row, portRows)),
|
||||
}
|
||||
}
|
||||
|
||||
private async currentUserId(): Promise<string | null> {
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser()
|
||||
return user?.id ?? null
|
||||
}
|
||||
|
||||
async addDeviceCategory(name: string): Promise<DeviceCategoryDef> {
|
||||
const ownerId = await this.currentUserId()
|
||||
const id = uuid()
|
||||
const { error } = await supabase
|
||||
.from('device_categories')
|
||||
.insert({ id, name, is_public: false, owner_id: ownerId })
|
||||
if (error) console.error('Failed to add device category to Supabase', error)
|
||||
return { id, name, custom: true }
|
||||
}
|
||||
|
||||
async addPortType(portType: Omit<PortType, 'id' | 'custom'>): Promise<PortType> {
|
||||
const ownerId = await this.currentUserId()
|
||||
const id = uuid()
|
||||
const { error } = await supabase.from('port_types').insert({
|
||||
id,
|
||||
name: portType.name,
|
||||
category: portType.category,
|
||||
family: portType.family,
|
||||
compatible_family_ids: portType.compatibleFamilyIds ?? [],
|
||||
max_connections: portType.maxConnections ?? null,
|
||||
is_public: false,
|
||||
owner_id: ownerId,
|
||||
})
|
||||
if (error) console.error('Failed to add port type to Supabase', error)
|
||||
return { ...portType, id, custom: true }
|
||||
}
|
||||
|
||||
async updatePortType(id: string, patch: Partial<Omit<PortType, 'id' | 'custom'>>): Promise<void> {
|
||||
const payload: Record<string, unknown> = {}
|
||||
if (patch.name !== undefined) payload.name = patch.name
|
||||
if (patch.category !== undefined) payload.category = patch.category
|
||||
if (patch.family !== undefined) payload.family = patch.family
|
||||
if (patch.compatibleFamilyIds !== undefined) payload.compatible_family_ids = patch.compatibleFamilyIds
|
||||
if (patch.maxConnections !== undefined) payload.max_connections = patch.maxConnections
|
||||
const { error } = await supabase.from('port_types').update(payload).eq('id', id)
|
||||
if (error) console.error('Failed to update port type in Supabase', error)
|
||||
}
|
||||
|
||||
async addCableType(cableType: Omit<CableType, 'id' | 'custom'>): Promise<CableType> {
|
||||
const ownerId = await this.currentUserId()
|
||||
const id = uuid()
|
||||
const { error } = await supabase.from('cable_types').insert({
|
||||
id,
|
||||
name: cableType.name,
|
||||
family: cableType.family,
|
||||
family2: cableType.family2 ?? null,
|
||||
unit: cableType.unit,
|
||||
cost_per_unit: cableType.costPerUnit ?? null,
|
||||
is_public: false,
|
||||
owner_id: ownerId,
|
||||
})
|
||||
if (error) console.error('Failed to add cable type to Supabase', error)
|
||||
return { ...cableType, id, custom: true }
|
||||
}
|
||||
|
||||
async updateCableType(id: string, patch: Partial<Omit<CableType, 'id' | 'custom'>>): Promise<void> {
|
||||
const payload: Record<string, unknown> = {}
|
||||
if (patch.name !== undefined) payload.name = patch.name
|
||||
if (patch.family !== undefined) payload.family = patch.family
|
||||
if (patch.family2 !== undefined) payload.family2 = patch.family2
|
||||
if (patch.unit !== undefined) payload.unit = patch.unit
|
||||
if (patch.costPerUnit !== undefined) payload.cost_per_unit = patch.costPerUnit
|
||||
const { error } = await supabase.from('cable_types').update(payload).eq('id', id)
|
||||
if (error) console.error('Failed to update cable type in Supabase', error)
|
||||
}
|
||||
|
||||
async addDeviceTemplate(template: Omit<DeviceTemplate, 'id' | 'custom'>): Promise<DeviceTemplate> {
|
||||
const ownerId = await this.currentUserId()
|
||||
const id = uuid()
|
||||
const { error } = await supabase.from('device_templates').insert({
|
||||
id,
|
||||
name: template.name,
|
||||
category_id: template.category,
|
||||
manufacturer: template.manufacturer ?? null,
|
||||
model: template.model ?? null,
|
||||
cost: template.cost ?? null,
|
||||
is_public: false,
|
||||
owner_id: ownerId,
|
||||
})
|
||||
if (error) {
|
||||
console.error('Failed to add device template to Supabase', error)
|
||||
return { ...template, id, custom: true }
|
||||
}
|
||||
const ports = template.ports.map((port, index) => ({ ...port, id: uuid(), sortOrder: index }))
|
||||
if (ports.length > 0) {
|
||||
const { error: portsError } = await supabase.from('device_template_ports').insert(
|
||||
ports.map((port) => ({
|
||||
id: port.id,
|
||||
device_template_id: id,
|
||||
name: port.name,
|
||||
direction: port.direction,
|
||||
port_type_id: port.portTypeId,
|
||||
sort_order: port.sortOrder,
|
||||
})),
|
||||
)
|
||||
if (portsError) console.error('Failed to add device template ports to Supabase', portsError)
|
||||
}
|
||||
return { ...template, id, ports, custom: true }
|
||||
}
|
||||
|
||||
async updateDeviceTemplate(id: string, patch: Partial<Omit<DeviceTemplate, 'id' | 'custom'>>): Promise<void> {
|
||||
const payload: Record<string, unknown> = {}
|
||||
if (patch.name !== undefined) payload.name = patch.name
|
||||
if (patch.category !== undefined) payload.category_id = patch.category
|
||||
if (patch.manufacturer !== undefined) payload.manufacturer = patch.manufacturer
|
||||
if (patch.model !== undefined) payload.model = patch.model
|
||||
if (patch.cost !== undefined) payload.cost = patch.cost
|
||||
if (Object.keys(payload).length > 0) {
|
||||
const { error } = await supabase.from('device_templates').update(payload).eq('id', id)
|
||||
if (error) console.error('Failed to update device template in Supabase', error)
|
||||
}
|
||||
|
||||
if (patch.ports !== undefined) {
|
||||
// Simplest correct approach: replace the whole port set rather than
|
||||
// diffing. Templates are edited as a whole in the UI (DeviceTemplateEditor),
|
||||
// so there's no risk of clobbering a concurrent partial edit.
|
||||
const { error: deleteError } = await supabase.from('device_template_ports').delete().eq('device_template_id', id)
|
||||
if (deleteError) console.error('Failed to clear device template ports in Supabase', deleteError)
|
||||
if (patch.ports.length > 0) {
|
||||
const { error: insertError } = await supabase.from('device_template_ports').insert(
|
||||
patch.ports.map((port, index) => ({
|
||||
id: uuid(),
|
||||
device_template_id: id,
|
||||
name: port.name,
|
||||
direction: port.direction,
|
||||
port_type_id: port.portTypeId,
|
||||
sort_order: index,
|
||||
})),
|
||||
)
|
||||
if (insertError) console.error('Failed to replace device template ports in Supabase', insertError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async removeDeviceTemplate(id: string): Promise<void> {
|
||||
const { error } = await supabase.from('device_templates').delete().eq('id', id)
|
||||
if (error) console.error('Failed to remove device template from Supabase', error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user