canvas/dist/db/index.d.ts
2026-03-11 18:42:08 -07:00

343 lines
12 KiB
TypeScript

import * as jotai from 'jotai';
import { SupabaseClient } from '@supabase/supabase-js';
import * as react_jsx_runtime from 'react/jsx-runtime';
import React from 'react';
import * as _tanstack_react_query from '@tanstack/react-query';
/**
* Core types for @blinksgg/canvas
*/
/**
* JSON type compatible with Supabase's generated types.
* This allows the package to work with database JSONB fields.
*/
type Json = string | number | boolean | null | {
[key: string]: Json | undefined;
} | Json[];
/**
* Database graph record - container for nodes and edges.
*/
interface DBGraph {
id: string;
owner_id: string | null;
name: string;
description: string | null;
/** App-specific customization data (style guide, settings, etc.) */
data?: Json | null;
created_at: string;
updated_at: string;
}
/**
* Database node record - the shape of node data from your database.
* Uses Json type for JSONB fields to be compatible with Supabase's generated types.
*/
interface DBGraphNode {
id: string;
graph_id: string;
label: string | null;
node_type: string | null;
configuration: Json | null;
ui_properties: Json | null;
/** App-specific customization data */
data?: Json | null;
created_at: string;
updated_at: string;
}
/**
* Database edge record.
* Uses Json type for JSONB fields to be compatible with Supabase's generated types.
*/
interface DBGraphEdge {
id: string;
graph_id: string;
source_node_id: string;
target_node_id: string;
edge_type: string | null;
filter_condition: Json | null;
ui_properties: Json | null;
/** App-specific customization data */
data?: Json | null;
created_at: string;
updated_at: string;
}
/** Type aliases for backend-agnostic naming */
type CanvasNode = DBGraphNode;
type CanvasEdge = DBGraphEdge;
type CanvasGraph = DBGraph;
/** Change event for realtime subscriptions */
interface CanvasChangeEvent {
type: 'INSERT' | 'UPDATE' | 'DELETE';
table: 'nodes' | 'edges';
/** The new record (for INSERT/UPDATE) */
new?: CanvasNode | CanvasEdge;
/** The old record (for DELETE/UPDATE) */
old?: Partial<CanvasNode | CanvasEdge>;
}
/**
* Storage adapter for canvas persistence.
*
* Implement this interface to use any backend with `CanvasDbProvider`.
* All methods return Promises for async-first design.
*/
interface CanvasStorageAdapter {
/** Fetch all nodes for a graph */
fetchNodes(graphId: string): Promise<CanvasNode[]>;
/** Create a new node */
createNode(graphId: string, node: Partial<CanvasNode>): Promise<CanvasNode>;
/** Update an existing node */
updateNode(nodeId: string, updates: Partial<CanvasNode>): Promise<CanvasNode>;
/** Delete a node */
deleteNode(nodeId: string): Promise<void>;
/** Fetch all edges for a graph */
fetchEdges(graphId: string): Promise<CanvasEdge[]>;
/** Create a new edge */
createEdge(graphId: string, edge: Partial<CanvasEdge>): Promise<CanvasEdge>;
/** Update an existing edge */
updateEdge(edgeId: string, updates: Partial<CanvasEdge>): Promise<CanvasEdge>;
/** Delete an edge */
deleteEdge(edgeId: string): Promise<void>;
/** Fetch graph metadata */
fetchGraph?(graphId: string): Promise<CanvasGraph | null>;
/** Update graph metadata */
updateGraph?(graphId: string, updates: Partial<CanvasGraph>): Promise<CanvasGraph>;
/**
* Subscribe to realtime changes for a graph.
* Returns an unsubscribe function.
*
* If not implemented, the canvas will work in poll/callback mode only.
*/
subscribe?(graphId: string, onChange: (event: CanvasChangeEvent) => void): () => void;
/** Create multiple nodes in a single operation */
createNodes?(graphId: string, nodes: Partial<CanvasNode>[]): Promise<CanvasNode[]>;
/** Delete multiple nodes in a single operation */
deleteNodes?(nodeIds: string[]): Promise<void>;
/** Create multiple edges in a single operation */
createEdges?(graphId: string, edges: Partial<CanvasEdge>[]): Promise<CanvasEdge[]>;
/** Delete multiple edges in a single operation */
deleteEdges?(edgeIds: string[]): Promise<void>;
}
/**
* Simple in-memory adapter for testing and local-only canvases.
* Data is lost on page reload.
*/
declare class InMemoryStorageAdapter implements CanvasStorageAdapter {
private nodes;
private edges;
fetchNodes(graphId: string): Promise<CanvasNode[]>;
createNode(graphId: string, node: Partial<CanvasNode>): Promise<CanvasNode>;
updateNode(nodeId: string, updates: Partial<CanvasNode>): Promise<CanvasNode>;
deleteNode(nodeId: string): Promise<void>;
fetchEdges(graphId: string): Promise<CanvasEdge[]>;
createEdge(graphId: string, edge: Partial<CanvasEdge>): Promise<CanvasEdge>;
updateEdge(edgeId: string, updates: Partial<CanvasEdge>): Promise<CanvasEdge>;
deleteEdge(edgeId: string): Promise<void>;
createNodes(graphId: string, nodes: Partial<CanvasNode>[]): Promise<CanvasNode[]>;
deleteNodes(nodeIds: string[]): Promise<void>;
createEdges(graphId: string, edges: Partial<CanvasEdge>[]): Promise<CanvasEdge[]>;
deleteEdges(edgeIds: string[]): Promise<void>;
}
/**
* Global storage adapter atom.
* Set by `CanvasDbProvider` so that any store/hook can access the adapter.
*/
declare const storageAdapterAtom: jotai.PrimitiveAtom<CanvasStorageAdapter | null> & {
init: CanvasStorageAdapter | null;
};
/**
* Supabase Storage Adapter
*
* Implements CanvasStorageAdapter using Supabase as the backend.
* Wraps the existing query functions into the adapter interface.
*/
declare class SupabaseStorageAdapter implements CanvasStorageAdapter {
private client;
constructor(client: SupabaseClient);
/** Get the underlying Supabase client (for advanced use or backward compat) */
getClient(): SupabaseClient;
fetchNodes(graphId: string): Promise<CanvasNode[]>;
createNode(graphId: string, node: Partial<CanvasNode>): Promise<CanvasNode>;
updateNode(nodeId: string, updates: Partial<CanvasNode>): Promise<CanvasNode>;
deleteNode(nodeId: string): Promise<void>;
fetchEdges(graphId: string): Promise<CanvasEdge[]>;
createEdge(graphId: string, edge: Partial<CanvasEdge>): Promise<CanvasEdge>;
updateEdge(edgeId: string, updates: Partial<CanvasEdge>): Promise<CanvasEdge>;
deleteEdge(edgeId: string): Promise<void>;
subscribe(graphId: string, onChange: (event: CanvasChangeEvent) => void): () => void;
}
interface CanvasDbProviderProps {
/** Storage adapter */
adapter: CanvasStorageAdapter;
children: React.ReactNode;
}
/**
* Provider that makes a storage adapter available to canvas hooks and components.
*
* @example
* ```tsx
* <CanvasDbProvider adapter={new InMemoryStorageAdapter()}>
* <Canvas ... />
* </CanvasDbProvider>
* ```
*/
declare function CanvasDbProvider({ adapter, children }: CanvasDbProviderProps): react_jsx_runtime.JSX.Element;
/**
* Hook to access the current storage adapter
*/
declare function useStorageAdapter(): CanvasStorageAdapter;
/**
* Node query functions
*/
/**
* Fetch all nodes for a graph
*/
declare function fetchGraphNodes(supabase: SupabaseClient, graphId: string): Promise<DBGraphNode[]>;
/**
* Fetch a single node by ID
*/
declare function fetchGraphNode(supabase: SupabaseClient, nodeId: string): Promise<DBGraphNode | null>;
/**
* Create a new node
*/
declare function createGraphNode(supabase: SupabaseClient, node: Omit<DBGraphNode, 'id' | 'created_at' | 'updated_at'>): Promise<DBGraphNode>;
/**
* Update a node
*/
declare function updateGraphNode(supabase: SupabaseClient, nodeId: string, updates: Partial<Pick<DBGraphNode, 'label' | 'node_type' | 'configuration' | 'ui_properties'>>): Promise<DBGraphNode>;
/**
* Delete a node
*/
declare function deleteGraphNode(supabase: SupabaseClient, nodeId: string): Promise<void>;
/**
* Edge query functions
*/
/**
* Fetch all edges for a graph
*/
declare function fetchGraphEdges(supabase: SupabaseClient, graphId: string): Promise<DBGraphEdge[]>;
/**
* Create a new edge
*/
declare function createGraphEdge(supabase: SupabaseClient, edge: Omit<DBGraphEdge, 'id' | 'created_at' | 'updated_at'>): Promise<DBGraphEdge>;
/**
* Update an edge
*/
declare function updateGraphEdge(supabase: SupabaseClient, edgeId: string, updates: Partial<Pick<DBGraphEdge, 'edge_type' | 'filter_condition' | 'ui_properties'>>): Promise<DBGraphEdge>;
/**
* Delete an edge
*/
declare function deleteGraphEdge(supabase: SupabaseClient, edgeId: string): Promise<void>;
/**
* Query key factories for canvas queries
*/
declare const canvasKeys: {
all: readonly ["canvas"];
graphs: () => readonly ["canvas", "graphs"];
graph: (graphId: string) => readonly ["canvas", "graphs", string];
nodes: (graphId: string) => readonly ["canvas", "graphs", string, "nodes"];
node: (graphId: string, nodeId: string) => readonly ["canvas", "graphs", string, "nodes", string];
edges: (graphId: string) => readonly ["canvas", "graphs", string, "edges"];
edge: (graphId: string, edgeId: string) => readonly ["canvas", "graphs", string, "edges", string];
};
/**
* Hook for fetching graph nodes
*/
interface UseGraphNodesOptions {
enabled?: boolean;
}
/**
* Hook to fetch all nodes for a graph
*/
declare function useGraphNodes(graphId: string | null, options?: UseGraphNodesOptions): _tanstack_react_query.UseQueryResult<DBGraphNode[], Error>;
/**
* Hook for fetching graph edges
*/
interface UseGraphEdgesOptions {
enabled?: boolean;
}
/**
* Hook to fetch all edges for a graph
*/
declare function useGraphEdges(graphId: string | null, options?: UseGraphEdgesOptions): _tanstack_react_query.UseQueryResult<DBGraphEdge[], Error>;
interface UpdateNodeVariables {
nodeId: string;
graphId: string;
updates: Partial<Pick<DBGraphNode, 'label' | 'node_type' | 'configuration' | 'ui_properties'>>;
}
/**
* Hook to update a node with optimistic updates
*/
declare function useUpdateNode(): _tanstack_react_query.UseMutationResult<DBGraphNode, Error, UpdateNodeVariables, {
previousNodes: DBGraphNode[] | undefined;
graphId: string;
}>;
type CreateNodeVariables = Omit<DBGraphNode, 'id' | 'created_at' | 'updated_at'>;
/**
* Hook to create a new node
*/
declare function useCreateNode(): _tanstack_react_query.UseMutationResult<DBGraphNode, Error, CreateNodeVariables, {
previousNodes: DBGraphNode[] | undefined;
graphId: string;
optimisticId: string;
}>;
interface DeleteNodeVariables {
nodeId: string;
graphId: string;
}
/**
* Hook to delete a node with optimistic updates
*/
declare function useDeleteNode(): _tanstack_react_query.UseMutationResult<void, Error, DeleteNodeVariables, {
previousNodes: DBGraphNode[] | undefined;
graphId: string;
}>;
interface UpdateEdgeVariables {
edgeId: string;
graphId: string;
updates: Partial<Pick<DBGraphEdge, 'edge_type' | 'filter_condition' | 'ui_properties'>>;
}
/**
* Hook to update an edge with optimistic updates
*/
declare function useUpdateEdge(): _tanstack_react_query.UseMutationResult<DBGraphEdge, Error, UpdateEdgeVariables, {
previousEdges: DBGraphEdge[] | undefined;
graphId: string;
}>;
type CreateEdgeVariables = Omit<DBGraphEdge, 'id' | 'created_at' | 'updated_at'>;
/**
* Hook to create a new edge
*/
declare function useCreateEdge(): _tanstack_react_query.UseMutationResult<DBGraphEdge, Error, CreateEdgeVariables, {
previousEdges: DBGraphEdge[] | undefined;
graphId: string;
optimisticId: string;
}>;
interface DeleteEdgeVariables {
edgeId: string;
graphId: string;
}
/**
* Hook to delete an edge with optimistic updates
*/
declare function useDeleteEdge(): _tanstack_react_query.UseMutationResult<void, Error, DeleteEdgeVariables, {
previousEdges: DBGraphEdge[] | undefined;
graphId: string;
}>;
export { type CanvasChangeEvent, CanvasDbProvider, type CanvasDbProviderProps, type CanvasEdge, type CanvasGraph, type CanvasNode, type CanvasStorageAdapter, type CreateEdgeVariables, type CreateNodeVariables, type DeleteEdgeVariables, type DeleteNodeVariables, InMemoryStorageAdapter, SupabaseStorageAdapter, type UpdateEdgeVariables, type UpdateNodeVariables, type UseGraphEdgesOptions, type UseGraphNodesOptions, canvasKeys, createGraphEdge, createGraphNode, deleteGraphEdge, deleteGraphNode, fetchGraphEdges, fetchGraphNode, fetchGraphNodes, storageAdapterAtom, updateGraphEdge, updateGraphNode, useCreateEdge, useCreateNode, useDeleteEdge, useDeleteNode, useGraphEdges, useGraphNodes, useStorageAdapter, useUpdateEdge, useUpdateNode };