@powerdata/pd4 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,198 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+
3
+ /** PD4 column info from table details */
4
+ interface ColumnInfo {
5
+ name: string;
6
+ dtype: string;
7
+ }
8
+ /** PD4 table info returned by GET /v1/tables/:name */
9
+ interface TableInfo {
10
+ name: string;
11
+ columns: ColumnInfo[];
12
+ row_count: number;
13
+ }
14
+ /** A column-value pair in PD4's row format */
15
+ interface ColumnValue {
16
+ column: string;
17
+ value: string | number | boolean | null;
18
+ }
19
+ /** A row as returned by PD4 */
20
+ interface PD4Row {
21
+ key: number;
22
+ values: ColumnValue[];
23
+ }
24
+ /** Flat row for display: column name -> value, plus _pk */
25
+ type Row = Record<string, string | number | boolean | null>;
26
+ /** SSE connection status */
27
+ type ConnectionStatus = 'connecting' | 'connected' | 'error';
28
+ /** Callback for insert events: (keys, rows) */
29
+ type InsertHandler = (keys: number[], rows: Row[]) => void;
30
+ /** Callback for update events: (keys, newValues, oldValues) */
31
+ type UpdateHandler = (keys: number[], newValues: Row[], oldValues: Row[]) => void;
32
+ /** Callback for delete events: (keys) */
33
+ type DeleteHandler = (keys: number[]) => void;
34
+ /** Callback for status changes */
35
+ type StatusHandler = (status: ConnectionStatus) => void;
36
+ /** Options for fetch */
37
+ interface FetchOptions {
38
+ offset?: number;
39
+ limit?: number;
40
+ }
41
+ /** Result from fetch */
42
+ interface FetchResult {
43
+ rows: Row[];
44
+ total: number;
45
+ }
46
+ /** Result from insert */
47
+ interface InsertResult {
48
+ keys: number[];
49
+ count: number;
50
+ }
51
+ /** Constructor options for PD4Client */
52
+ interface PD4ClientOptions {
53
+ /**
54
+ * Custom EventSource constructor for environments without a global EventSource
55
+ * (e.g. Node.js). Pass the `EventSource` class from the `eventsource` npm package.
56
+ */
57
+ EventSource?: new (url: string) => EventSource;
58
+ }
59
+
60
+ /**
61
+ * A buffered view wrapping a PD4 table.
62
+ * Changes are accumulated locally and only become visible
63
+ * to other clients after commit().
64
+ */
65
+ declare class PD4Buffer {
66
+ private client;
67
+ readonly handle: string;
68
+ readonly table: string;
69
+ constructor(client: PD4Client, handle: string, table: string);
70
+ /** Update a row within the buffer (not visible to others until commit). */
71
+ update(key: number, values: Row): Promise<void>;
72
+ /** Fetch rows from the buffer (sees uncommitted changes). */
73
+ fetch(offset?: number, limit?: number): Promise<FetchResult>;
74
+ /** Commit all buffered writes atomically. Fires SSE events. */
75
+ commit(): Promise<void>;
76
+ /** Rollback all buffered writes (discard pending changes). */
77
+ rollback(): Promise<void>;
78
+ private _request;
79
+ }
80
+
81
+ declare class PD4Client {
82
+ readonly url: string;
83
+ readonly db: string;
84
+ private EventSourceCtor;
85
+ private eventSource;
86
+ private handlers;
87
+ private statusHandlers;
88
+ private _status;
89
+ constructor(url: string, db: string, options?: PD4ClientOptions);
90
+ private get v1();
91
+ private get api();
92
+ private request;
93
+ /** Check if PD4 is reachable */
94
+ ping(): Promise<boolean>;
95
+ /** Ensure the database is open */
96
+ ensureDatabase(): Promise<void>;
97
+ /** List all table names */
98
+ listTables(): Promise<string[]>;
99
+ /** Get table info */
100
+ getTable(name: string): Promise<TableInfo>;
101
+ /** Create a table (with auto_pk) */
102
+ createTable(name: string): Promise<void>;
103
+ /** Drop a table */
104
+ dropTable(name: string): Promise<void>;
105
+ /** Add a column to a table */
106
+ addColumn(table: string, name: string, dtype: string): Promise<void>;
107
+ /** Insert one or more rows (flat objects). Returns keys and count. */
108
+ insert(table: string, rows: Row | Row[]): Promise<InsertResult>;
109
+ /** Update a single row by key with a flat object of changed columns. */
110
+ update(table: string, key: number, values: Row): Promise<void>;
111
+ /** Delete rows by keys. */
112
+ delete(table: string, keys: number | number[]): Promise<void>;
113
+ /** Fetch rows from a table/view. Returns flat Row objects. */
114
+ fetch(table: string, opts?: FetchOptions): Promise<FetchResult>;
115
+ /** Insert rows in columnar format (low-level). */
116
+ insertColumnar(table: string, columns: string[], rows: (string | number | boolean | null)[][]): Promise<InsertResult>;
117
+ /** Update a row by key with column-value pairs (low-level). */
118
+ updateRaw(table: string, key: number, values: {
119
+ column: string;
120
+ value: string | number | boolean | null;
121
+ }[]): Promise<void>;
122
+ /** Fetch rows in PD4 wire format (low-level). */
123
+ fetchRaw(table: string, offset?: number, limit?: number): Promise<{
124
+ rows: PD4Row[];
125
+ total: number;
126
+ }>;
127
+ /** Create a buffered view wrapping a table. */
128
+ buffer(table: string): Promise<PD4Buffer>;
129
+ /** Get current SSE connection status. */
130
+ get status(): ConnectionStatus;
131
+ /** Connect to the SSE event stream. Called automatically on first on(). */
132
+ connect(): void;
133
+ /** Disconnect from the SSE event stream. */
134
+ disconnect(): void;
135
+ /**
136
+ * Subscribe to SSE events for a table.
137
+ * Auto-connects on first call. Returns an unsubscribe function.
138
+ */
139
+ on(table: string, event: 'insert', handler: InsertHandler): () => void;
140
+ on(table: string, event: 'update', handler: UpdateHandler): () => void;
141
+ on(table: string, event: 'delete', handler: DeleteHandler): () => void;
142
+ /** Subscribe to connection status changes. Returns an unsubscribe function. */
143
+ onStatus(handler: StatusHandler): () => void;
144
+ private _setStatus;
145
+ private _dispatch;
146
+ }
147
+
148
+ interface PD4ProviderProps {
149
+ url: string;
150
+ db: string;
151
+ children: React.ReactNode;
152
+ }
153
+ declare function PD4Provider({ url, db, children }: PD4ProviderProps): react_jsx_runtime.JSX.Element;
154
+ declare function usePD4(): PD4Client;
155
+
156
+ /**
157
+ * Subscribe to SSE events for a table. Auto-cleans up on unmount.
158
+ *
159
+ * Usage:
160
+ * usePD4Subscribe('metrics', 'insert', (keys, rows) => { ... })
161
+ * usePD4Subscribe('metrics', 'update', (keys, newVals, oldVals) => { ... })
162
+ * usePD4Subscribe('metrics', 'delete', (keys) => { ... })
163
+ */
164
+ declare function usePD4Subscribe(table: string, event: 'insert', handler: InsertHandler): void;
165
+ declare function usePD4Subscribe(table: string, event: 'update', handler: UpdateHandler): void;
166
+ declare function usePD4Subscribe(table: string, event: 'delete', handler: DeleteHandler): void;
167
+
168
+ interface UsePD4TableOptions {
169
+ limit?: number;
170
+ deleteDelay?: number;
171
+ }
172
+ interface UsePD4TableResult {
173
+ rows: Row[];
174
+ total: number;
175
+ columns: ColumnInfo[];
176
+ status: ConnectionStatus;
177
+ highlights: Record<number, string>;
178
+ }
179
+ /**
180
+ * Full table state management hook.
181
+ * Handles: initial fetch, SSE subscriptions for insert/update/delete,
182
+ * row state management, highlight tracking, and buffer-commit edge cases.
183
+ */
184
+ declare function usePD4Table(table: string, opts?: UsePD4TableOptions): UsePD4TableResult;
185
+
186
+ interface UsePD4QueryResult {
187
+ rows: Row[];
188
+ total: number;
189
+ loading: boolean;
190
+ error: string | null;
191
+ refetch: () => void;
192
+ }
193
+ /**
194
+ * One-shot query hook. Fetches rows from a table once (or on refetch).
195
+ */
196
+ declare function usePD4Query(table: string, offset?: number, limit?: number): UsePD4QueryResult;
197
+
198
+ export { PD4Provider, type PD4ProviderProps, type UsePD4QueryResult, type UsePD4TableOptions, type UsePD4TableResult, usePD4, usePD4Query, usePD4Subscribe, usePD4Table };
@@ -0,0 +1,198 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+
3
+ /** PD4 column info from table details */
4
+ interface ColumnInfo {
5
+ name: string;
6
+ dtype: string;
7
+ }
8
+ /** PD4 table info returned by GET /v1/tables/:name */
9
+ interface TableInfo {
10
+ name: string;
11
+ columns: ColumnInfo[];
12
+ row_count: number;
13
+ }
14
+ /** A column-value pair in PD4's row format */
15
+ interface ColumnValue {
16
+ column: string;
17
+ value: string | number | boolean | null;
18
+ }
19
+ /** A row as returned by PD4 */
20
+ interface PD4Row {
21
+ key: number;
22
+ values: ColumnValue[];
23
+ }
24
+ /** Flat row for display: column name -> value, plus _pk */
25
+ type Row = Record<string, string | number | boolean | null>;
26
+ /** SSE connection status */
27
+ type ConnectionStatus = 'connecting' | 'connected' | 'error';
28
+ /** Callback for insert events: (keys, rows) */
29
+ type InsertHandler = (keys: number[], rows: Row[]) => void;
30
+ /** Callback for update events: (keys, newValues, oldValues) */
31
+ type UpdateHandler = (keys: number[], newValues: Row[], oldValues: Row[]) => void;
32
+ /** Callback for delete events: (keys) */
33
+ type DeleteHandler = (keys: number[]) => void;
34
+ /** Callback for status changes */
35
+ type StatusHandler = (status: ConnectionStatus) => void;
36
+ /** Options for fetch */
37
+ interface FetchOptions {
38
+ offset?: number;
39
+ limit?: number;
40
+ }
41
+ /** Result from fetch */
42
+ interface FetchResult {
43
+ rows: Row[];
44
+ total: number;
45
+ }
46
+ /** Result from insert */
47
+ interface InsertResult {
48
+ keys: number[];
49
+ count: number;
50
+ }
51
+ /** Constructor options for PD4Client */
52
+ interface PD4ClientOptions {
53
+ /**
54
+ * Custom EventSource constructor for environments without a global EventSource
55
+ * (e.g. Node.js). Pass the `EventSource` class from the `eventsource` npm package.
56
+ */
57
+ EventSource?: new (url: string) => EventSource;
58
+ }
59
+
60
+ /**
61
+ * A buffered view wrapping a PD4 table.
62
+ * Changes are accumulated locally and only become visible
63
+ * to other clients after commit().
64
+ */
65
+ declare class PD4Buffer {
66
+ private client;
67
+ readonly handle: string;
68
+ readonly table: string;
69
+ constructor(client: PD4Client, handle: string, table: string);
70
+ /** Update a row within the buffer (not visible to others until commit). */
71
+ update(key: number, values: Row): Promise<void>;
72
+ /** Fetch rows from the buffer (sees uncommitted changes). */
73
+ fetch(offset?: number, limit?: number): Promise<FetchResult>;
74
+ /** Commit all buffered writes atomically. Fires SSE events. */
75
+ commit(): Promise<void>;
76
+ /** Rollback all buffered writes (discard pending changes). */
77
+ rollback(): Promise<void>;
78
+ private _request;
79
+ }
80
+
81
+ declare class PD4Client {
82
+ readonly url: string;
83
+ readonly db: string;
84
+ private EventSourceCtor;
85
+ private eventSource;
86
+ private handlers;
87
+ private statusHandlers;
88
+ private _status;
89
+ constructor(url: string, db: string, options?: PD4ClientOptions);
90
+ private get v1();
91
+ private get api();
92
+ private request;
93
+ /** Check if PD4 is reachable */
94
+ ping(): Promise<boolean>;
95
+ /** Ensure the database is open */
96
+ ensureDatabase(): Promise<void>;
97
+ /** List all table names */
98
+ listTables(): Promise<string[]>;
99
+ /** Get table info */
100
+ getTable(name: string): Promise<TableInfo>;
101
+ /** Create a table (with auto_pk) */
102
+ createTable(name: string): Promise<void>;
103
+ /** Drop a table */
104
+ dropTable(name: string): Promise<void>;
105
+ /** Add a column to a table */
106
+ addColumn(table: string, name: string, dtype: string): Promise<void>;
107
+ /** Insert one or more rows (flat objects). Returns keys and count. */
108
+ insert(table: string, rows: Row | Row[]): Promise<InsertResult>;
109
+ /** Update a single row by key with a flat object of changed columns. */
110
+ update(table: string, key: number, values: Row): Promise<void>;
111
+ /** Delete rows by keys. */
112
+ delete(table: string, keys: number | number[]): Promise<void>;
113
+ /** Fetch rows from a table/view. Returns flat Row objects. */
114
+ fetch(table: string, opts?: FetchOptions): Promise<FetchResult>;
115
+ /** Insert rows in columnar format (low-level). */
116
+ insertColumnar(table: string, columns: string[], rows: (string | number | boolean | null)[][]): Promise<InsertResult>;
117
+ /** Update a row by key with column-value pairs (low-level). */
118
+ updateRaw(table: string, key: number, values: {
119
+ column: string;
120
+ value: string | number | boolean | null;
121
+ }[]): Promise<void>;
122
+ /** Fetch rows in PD4 wire format (low-level). */
123
+ fetchRaw(table: string, offset?: number, limit?: number): Promise<{
124
+ rows: PD4Row[];
125
+ total: number;
126
+ }>;
127
+ /** Create a buffered view wrapping a table. */
128
+ buffer(table: string): Promise<PD4Buffer>;
129
+ /** Get current SSE connection status. */
130
+ get status(): ConnectionStatus;
131
+ /** Connect to the SSE event stream. Called automatically on first on(). */
132
+ connect(): void;
133
+ /** Disconnect from the SSE event stream. */
134
+ disconnect(): void;
135
+ /**
136
+ * Subscribe to SSE events for a table.
137
+ * Auto-connects on first call. Returns an unsubscribe function.
138
+ */
139
+ on(table: string, event: 'insert', handler: InsertHandler): () => void;
140
+ on(table: string, event: 'update', handler: UpdateHandler): () => void;
141
+ on(table: string, event: 'delete', handler: DeleteHandler): () => void;
142
+ /** Subscribe to connection status changes. Returns an unsubscribe function. */
143
+ onStatus(handler: StatusHandler): () => void;
144
+ private _setStatus;
145
+ private _dispatch;
146
+ }
147
+
148
+ interface PD4ProviderProps {
149
+ url: string;
150
+ db: string;
151
+ children: React.ReactNode;
152
+ }
153
+ declare function PD4Provider({ url, db, children }: PD4ProviderProps): react_jsx_runtime.JSX.Element;
154
+ declare function usePD4(): PD4Client;
155
+
156
+ /**
157
+ * Subscribe to SSE events for a table. Auto-cleans up on unmount.
158
+ *
159
+ * Usage:
160
+ * usePD4Subscribe('metrics', 'insert', (keys, rows) => { ... })
161
+ * usePD4Subscribe('metrics', 'update', (keys, newVals, oldVals) => { ... })
162
+ * usePD4Subscribe('metrics', 'delete', (keys) => { ... })
163
+ */
164
+ declare function usePD4Subscribe(table: string, event: 'insert', handler: InsertHandler): void;
165
+ declare function usePD4Subscribe(table: string, event: 'update', handler: UpdateHandler): void;
166
+ declare function usePD4Subscribe(table: string, event: 'delete', handler: DeleteHandler): void;
167
+
168
+ interface UsePD4TableOptions {
169
+ limit?: number;
170
+ deleteDelay?: number;
171
+ }
172
+ interface UsePD4TableResult {
173
+ rows: Row[];
174
+ total: number;
175
+ columns: ColumnInfo[];
176
+ status: ConnectionStatus;
177
+ highlights: Record<number, string>;
178
+ }
179
+ /**
180
+ * Full table state management hook.
181
+ * Handles: initial fetch, SSE subscriptions for insert/update/delete,
182
+ * row state management, highlight tracking, and buffer-commit edge cases.
183
+ */
184
+ declare function usePD4Table(table: string, opts?: UsePD4TableOptions): UsePD4TableResult;
185
+
186
+ interface UsePD4QueryResult {
187
+ rows: Row[];
188
+ total: number;
189
+ loading: boolean;
190
+ error: string | null;
191
+ refetch: () => void;
192
+ }
193
+ /**
194
+ * One-shot query hook. Fetches rows from a table once (or on refetch).
195
+ */
196
+ declare function usePD4Query(table: string, offset?: number, limit?: number): UsePD4QueryResult;
197
+
198
+ export { PD4Provider, type PD4ProviderProps, type UsePD4QueryResult, type UsePD4TableOptions, type UsePD4TableResult, usePD4, usePD4Query, usePD4Subscribe, usePD4Table };