@sqlrooms/duckdb 0.6.0 → 0.8.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.
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ A powerful wrapper around DuckDB-WASM that provides React hooks and utilities for working with DuckDB in browser environments.
2
+
3
+ ## Features
4
+
5
+ - 🔄 **React Integration**: Hooks for seamless integration with React applications
6
+ - 📊 **Type-Safe Queries**: Execute SQL queries with TypeScript type safety
7
+ - 🔍 **Data Validation**: Optional runtime validation using Zod schemas
8
+ - 📁 **File Operations**: Import data from various file formats (CSV, JSON, Parquet)
9
+ - 📤 **Data Export**: Export query results to CSV files
10
+ - 🏹 **Arrow Integration**: Work with Apache Arrow tables for efficient data processing
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @sqlrooms/duckdb
16
+ ```
17
+
18
+ ## Basic Usage
19
+
20
+ ### Using the SQL Hook
21
+
22
+ ```tsx
23
+ import {useSql} from '@sqlrooms/duckdb';
24
+
25
+ function UserList() {
26
+ // Basic usage with TypeScript types
27
+ const {data, isLoading, error} = useSql<{id: number; name: string}>({
28
+ query: 'SELECT id, name FROM users',
29
+ });
30
+
31
+ if (isLoading) return <div>Loading...</div>;
32
+ if (error) return <div>Error: {error.message}</div>;
33
+ if (!data) return null;
34
+
35
+ return (
36
+ <ul>
37
+ {Array.from(data.rows()).map((user) => (
38
+ <li key={user.id}>{user.name}</li>
39
+ ))}
40
+ </ul>
41
+ );
42
+ }
43
+ ```
44
+
45
+ For more information and examples on using the `useSql` hook, see the [useSql API documentation](/api/duckdb/functions/useSql).
46
+
47
+ ### Using Zod for Runtime Validation
48
+
49
+ ```tsx
50
+ import {useSql} from '@sqlrooms/duckdb';
51
+ import {z} from 'zod';
52
+
53
+ const userSchema = z.object({
54
+ id: z.number(),
55
+ name: z.string(),
56
+ email: z.string().email(),
57
+ created_at: z.string().transform((str) => new Date(str)),
58
+ });
59
+
60
+ function ValidatedUserList() {
61
+ const {data, isLoading, error} = useSql(userSchema, {
62
+ query: 'SELECT id, name, email, created_at FROM users',
63
+ });
64
+
65
+ if (isLoading) return <div>Loading...</div>;
66
+ if (error) {
67
+ if (error instanceof z.ZodError) {
68
+ return <div>Validation Error: {error.errors[0].message}</div>;
69
+ }
70
+ return <div>Error: {error.message}</div>;
71
+ }
72
+ if (!data) return null;
73
+
74
+ return (
75
+ <ul>
76
+ {data.toArray().map((user) => (
77
+ <li key={user.id}>
78
+ {user.name} ({user.email}) - Joined:{' '}
79
+ {user.created_at.toLocaleDateString()}
80
+ </li>
81
+ ))}
82
+ </ul>
83
+ );
84
+ }
85
+ ```
86
+
87
+ ### Creating Tables from Different Sources
88
+
89
+ ```tsx
90
+ import {
91
+ createTableFromQuery,
92
+ createTableFromObjects,
93
+ createViewFromFile,
94
+ } from '@sqlrooms/duckdb';
95
+
96
+ // Create a table from a SQL query
97
+ await createTableFromQuery(
98
+ 'filtered_users',
99
+ 'SELECT * FROM users WHERE active = true',
100
+ );
101
+
102
+ // Create a table from JavaScript objects
103
+ const users = [
104
+ {id: 1, name: 'Alice', email: 'alice@example.com'},
105
+ {id: 2, name: 'Bob', email: 'bob@example.com'},
106
+ ];
107
+ await createTableFromObjects('new_users', users);
108
+
109
+ // Create a view from a file upload
110
+ function FileUploader() {
111
+ const handleFileUpload = async (event) => {
112
+ const file = event.target.files[0];
113
+ if (file) {
114
+ await createViewFromFile(file.name, 'main', 'uploaded_data', file);
115
+ }
116
+ };
117
+
118
+ return (
119
+ <input
120
+ type="file"
121
+ accept=".csv,.json,.parquet"
122
+ onChange={handleFileUpload}
123
+ />
124
+ );
125
+ }
126
+ ```
127
+
128
+ ### Exporting Data to CSV
129
+
130
+ ```tsx
131
+ import {exportToCsv} from '@sqlrooms/duckdb';
132
+
133
+ function ExportButton() {
134
+ const handleExport = async () => {
135
+ await exportToCsv('SELECT * FROM users ORDER BY name', 'users_export.csv');
136
+ };
137
+
138
+ return <button onClick={handleExport}>Export to CSV</button>;
139
+ }
140
+ ```
141
+
142
+ ### Low-Level DuckDB Access
143
+
144
+ ```tsx
145
+ import {getDuckDb} from '@sqlrooms/duckdb';
146
+
147
+ async function executeCustomQuery() {
148
+ const {conn} = await getDuckDb();
149
+
150
+ // Execute a query directly
151
+ const result = await conn.query('SELECT COUNT(*) as count FROM users');
152
+
153
+ // Access the Arrow table directly
154
+ const count = result.getChildAt(0)?.get(0);
155
+ console.log(`Total users: ${count}`);
156
+
157
+ return result;
158
+ }
159
+ ```
160
+
161
+ ## Advanced Features
162
+
163
+ - **Batch Processing**: Handle large datasets with pagination
164
+ - **Arrow Integration**: Work directly with Apache Arrow tables for efficient data processing
165
+ - **Schema Management**: Create, inspect, and manage database schemas
166
+ - **File Management**: Register and manage files in the DuckDB instance
167
+
168
+ For more information, visit the SQLRooms documentation.
169
+
170
+ ```
171
+
172
+ ```
package/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * {@include ../README.md}
3
+ * @packageDocumentation
4
+ */
1
5
  export * from './duckdb';
2
6
  export * from './types';
3
7
  export * from './useDuckDb';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * {@include ../README.md}
3
+ * @packageDocumentation
4
+ */
1
5
  export * from './duckdb';
2
6
  export * from './types';
3
7
  export * from './useDuckDb';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC","sourcesContent":["export * from './duckdb';\nexport * from './types';\nexport * from './useDuckDb';\nexport * from './exportToCsv';\nexport * from './arrow-utils';\nexport * from './useSql';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport * from './duckdb';\nexport * from './types';\nexport * from './useDuckDb';\nexport * from './exportToCsv';\nexport * from './arrow-utils';\nexport * from './useSql';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"useDuckDb.d.ts","sourceRoot":"","sources":["../src/useDuckDb.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,qBAAqB,CAAC;AAC9C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAC,SAAS,EAAc,MAAM,SAAS,CAAC;AAG/C;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAE9B,MAAM,MAAM,MAAM,GAAG;IACnB,EAAE,EAAE,MAAM,CAAC,WAAW,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC,qBAAqB,CAAC;IACnC,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAeF,qBAAa,cAAe,SAAQ,KA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M,GAAE,MAAM,GAAG,MAAU,EAC3B,KAAK,SAAI,GACR,MAAM,CASR;AAED,eAAO,MAAM,SAAS,GAAI,KAAK,OAAO,WAErC,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,IAAI,MAAM,WAMlC,CAAC;AAEF,wBAAsB,aAAa,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAYtE;AAED,wBAAsB,kBAAkB,CACtC,SAAS,EAAE,MAAM,EACjB,MAAM,SAAS,GACd,OAAO,CAAC,SAAS,CAAC,CAmBpB;AAED,wBAAsB,mBAAmB,CACvC,MAAM,SAAS,GACd,OAAO,CAAC,SAAS,EAAE,CAAC,CAOtB;AAED,wBAAsB,gBAAgB,CACpC,SAAS,EAAE,MAAM,EACjB,MAAM,SAAS,GACd,OAAO,CAAC,OAAO,CAAC,CAMlB;AAED,wBAAsB,aAAa,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAiClE;AAED,wBAAsB,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGhE;AAED,wBAAsB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG3D;AAED,wBAAsB,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAGlD"}
package/dist/useSql.d.ts CHANGED
@@ -51,8 +51,10 @@ export type DuckDbQueryResult<T> = UseSqlQueryResult<T>;
51
51
  * userSchema,
52
52
  * {query: 'SELECT id, name, email, created_at as createdAt FROM users'}
53
53
  * );
54
+ * ```
54
55
  *
55
- * // Error handling is the same for both approaches
56
+ * ## Error Handling
57
+ * ```typescript
56
58
  * if (isLoading) return <div>Loading...</div>;
57
59
  * if (error) {
58
60
  * // With Zod, you can catch validation errors specifically
@@ -62,29 +64,9 @@ export type DuckDbQueryResult<T> = UseSqlQueryResult<T>;
62
64
  * return <div>Error: {error.message}</div>;
63
65
  * }
64
66
  * if (!data) return null;
65
- *
66
- * // Accessing data works the same way for both approaches
67
- * // Iterate through rows using the rows() iterator (recommended)
68
- * for (const user of data.rows()) {
69
- * console.log(user.name, user.email);
70
- * }
71
- *
72
- * // Traditional for loop with index access
73
- * for (let i = 0; i < data.length; i++) {
74
- * const user = data.getRow(i);
75
- * console.log(`User ${i}: ${user.name} (${user.email})`);
76
- * }
77
- *
78
- * // With Zod schema, transformed fields are available
79
- * // for (const user of validatedData.rows()) {
80
- * // console.log(`Created: ${user.createdAt.toISOString()}`); // createdAt is a Date object
81
- * // }
82
- *
83
- * // Get all rows as an array
84
- * const allUsers = data.toArray();
85
67
  * ```
86
68
  *
87
- * ## Performance and Advanced Operations
69
+ * ## Data Access Methods
88
70
  *
89
71
  * There are several ways to access data with different performance characteristics:
90
72
  *
@@ -92,22 +74,26 @@ export type DuckDbQueryResult<T> = UseSqlQueryResult<T>;
92
74
  * - Provides type safety and validation
93
75
  * - Converts data to JavaScript objects
94
76
  * - Slower for large datasets due to object creation and validation
95
- * - Zod validation adds additional overhead but ensures data correctness
96
77
  *
97
78
  * ```typescript
98
- * // Iterate through all rows with the iterator (recommended)
99
- * for (const row of data.rows()) {
100
- * console.log(row.name);
79
+ * // Iterate through rows using the rows() iterator (recommended)
80
+ * for (const user of data.rows()) {
81
+ * console.log(user.name, user.email);
101
82
  * }
102
83
  *
103
- * // Access rows with a traditional for loop
84
+ * // Traditional for loop with index access
104
85
  * for (let i = 0; i < data.length; i++) {
105
- * const row = data.getRow(i);
106
- * console.log(`Row ${i}: ${row.name}`);
86
+ * const user = data.getRow(i);
87
+ * console.log(`User ${i}: ${user.name} (${user.email})`);
107
88
  * }
108
89
  *
109
90
  * // Get all rows as an array
110
- * const allRows = data.toArray();
91
+ * const allUsers = data.toArray();
92
+ *
93
+ * // With Zod schema, transformed fields are available
94
+ * for (const user of validatedData.rows()) {
95
+ * console.log(`Created: ${user.createdAt.toISOString()}`); // createdAt is a Date object
96
+ * }
111
97
  * ```
112
98
  *
113
99
  * ### 2. Direct Arrow Table Access
@@ -126,9 +112,9 @@ export type DuckDbQueryResult<T> = UseSqlQueryResult<T>;
126
112
  * }
127
113
  *
128
114
  * // Note: For filtering data, it's most efficient to use SQL in your query
129
- * // const { data } = useSql<User>({
130
- * // query: "SELECT * FROM users WHERE age > 30"
131
- * // });
115
+ * const { data } = useSql<User>({
116
+ * query: "SELECT * FROM users WHERE age > 30"
117
+ * });
132
118
  * ```
133
119
  *
134
120
  * ### 3. Using Flechette for Advanced Operations
@@ -141,7 +127,6 @@ export type DuckDbQueryResult<T> = UseSqlQueryResult<T>;
141
127
  * import { tableFromIPC } from '@uwdata/flechette';
142
128
  *
143
129
  * // Convert Arrow table to Flechette table
144
- * // Note: This serialization step creates a copy of the data
145
130
  * const serializedData = data.arrowTable.serialize();
146
131
  * const flechetteTable = tableFromIPC(serializedData);
147
132
  *
@@ -158,7 +143,6 @@ export type DuckDbQueryResult<T> = UseSqlQueryResult<T>;
158
143
  * });
159
144
  *
160
145
  * // For large datasets, consider memory management
161
- * // Once you're done with the Arrow data, you can free the memory
162
146
  * serializedData = null; // Allow garbage collection of the serialized data
163
147
  * ```
164
148
  *
@@ -1 +1 @@
1
- {"version":3,"file":"useSql.d.ts","sourceRoot":"","sources":["../src/useSql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAGtC,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB;;;GAGG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAClC,iCAAiC;IACjC,UAAU,EAAE,KAAK,CAAC,KAAK,CAAC;IACxB,yEAAyE;IACzE,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;IACzB,kCAAkC;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,4DAA4D;IAC5D,IAAI,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAC5B,wDAAwD;IACxD,OAAO,IAAI,CAAC,EAAE,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EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS;iBACnE,CAAC,CAAC;gBAEH,IAAI,SAAS,EAAE,CAAC;oBACd,OAAO,CAAC,WAAW,CAAC,CAAC;gBACvB,CAAC;YACH,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAI,SAAS,EAAE,CAAC;oBACd,QAAQ,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBAChE,CAAC;YACH,CAAC;oBAAS,CAAC;gBACT,IAAI,SAAS,EAAE,CAAC;oBACd,YAAY,CAAC,KAAK,CAAC,CAAC;gBACtB,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QAEF,SAAS,EAAE,CAAC;QAEZ,OAAO,GAAG,EAAE;YACV,SAAS,GAAG,KAAK,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAErC,OAAO;QACL,IAAI;QACJ,KAAK;QACL,SAAS;KACV,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC","sourcesContent":["import * as arrow from 'apache-arrow';\nimport {useEffect, useState} from 'react';\nimport {getDuckDb} from './useDuckDb';\nimport {z} from 'zod';\n\n/**\n * A wrapper interface that exposes the underlying Arrow table,\n * a typed row accessor, and the number of rows.\n */\nexport interface UseSqlQueryResult<T> {\n /** The underlying Arrow table */\n arrowTable: arrow.Table;\n /** Returns a typed row at the specified index by converting on demand */\n getRow(index: number): T;\n /** Number of rows in the table */\n length: number;\n /** Returns an iterator that yields each row in the table */\n rows(): IterableIterator<T>;\n /** Returns an array containing all rows in the table */\n toArray(): T[];\n}\n\n/**\n * @deprecated Use UseSqlQueryResult instead\n */\nexport type DuckDbQueryResult<T> = UseSqlQueryResult<T>;\n\n/**\n * Creates a row accessor wrapper around an Arrow table that provides typed row access.\n */\nfunction createTypedRowAccessor<T>({\n arrowTable,\n validate,\n}: {\n arrowTable: arrow.Table;\n validate?: (row: unknown) => T;\n}): UseSqlQueryResult<T> {\n return {\n arrowTable,\n get length() {\n return arrowTable.numRows;\n },\n getRow(index: number): T {\n const row: Record<string, unknown> = {};\n arrowTable.schema.fields.forEach((field: arrow.Field) => {\n const column = arrowTable.getChild(field.name);\n if (column) {\n row[field.name] = column.get(index);\n }\n });\n\n // If a validator is provided, use it to validate/parse the row\n if (validate) {\n return validate(row);\n }\n return row as T;\n },\n *rows(): IterableIterator<T> {\n for (let i = 0; i < this.length; i++) {\n yield this.getRow(i);\n }\n },\n toArray(): T[] {\n const result: T[] = [];\n for (let i = 0; i < this.length; i++) {\n result.push(this.getRow(i));\n }\n return result;\n },\n };\n}\n\n/**\n * A React hook for executing SQL queries with automatic state management.\n * Provides two ways to ensure type safety:\n * 1. Using TypeScript types (compile-time safety only)\n * 2. Using Zod schemas (both compile-time and runtime validation)\n *\n * @example\n * ```typescript\n * // Option 1: Using TypeScript types (faster, no runtime validation)\n * interface User {\n * id: number;\n * name: string;\n * email: string;\n * }\n *\n * const {data, isLoading, error} = useSql<User>({\n * query: 'SELECT id, name, email FROM users'\n * });\n *\n * // Option 2: Using Zod schema (slower but with runtime validation)\n * const userSchema = z.object({\n * id: z.number(),\n * name: z.string(),\n * email: z.string().email(),\n * createdAt: z.string().transform(str => new Date(str)) // Transform string to Date\n * });\n *\n * const {data: validatedData, isLoading, error} = useSql(\n * userSchema,\n * {query: 'SELECT id, name, email, created_at as createdAt FROM users'}\n * );\n *\n * // Error handling is the same for both approaches\n * if (isLoading) return <div>Loading...</div>;\n * if (error) {\n * // With Zod, you can catch validation errors specifically\n * if (error instanceof z.ZodError) {\n * return <div>Validation Error: {error.errors[0].message}</div>;\n * }\n * return <div>Error: {error.message}</div>;\n * }\n * if (!data) return null;\n *\n * // Accessing data works the same way for both approaches\n * // Iterate through rows using the rows() iterator (recommended)\n * for (const user of data.rows()) {\n * console.log(user.name, user.email);\n * }\n *\n * // Traditional for loop with index access\n * for (let i = 0; i < data.length; i++) {\n * const user = data.getRow(i);\n * console.log(`User ${i}: ${user.name} (${user.email})`);\n * }\n *\n * // With Zod schema, transformed fields are available\n * // for (const user of validatedData.rows()) {\n * // console.log(`Created: ${user.createdAt.toISOString()}`); // createdAt is a Date object\n * // }\n *\n * // Get all rows as an array\n * const allUsers = data.toArray();\n * ```\n *\n * ## Performance and Advanced Operations\n *\n * There are several ways to access data with different performance characteristics:\n *\n * ### 1. Typed Row Access (getRow, rows(), toArray())\n * - Provides type safety and validation\n * - Converts data to JavaScript objects\n * - Slower for large datasets due to object creation and validation\n * - Zod validation adds additional overhead but ensures data correctness\n *\n * ```typescript\n * // Iterate through all rows with the iterator (recommended)\n * for (const row of data.rows()) {\n * console.log(row.name);\n * }\n *\n * // Access rows with a traditional for loop\n * for (let i = 0; i < data.length; i++) {\n * const row = data.getRow(i);\n * console.log(`Row ${i}: ${row.name}`);\n * }\n *\n * // Get all rows as an array\n * const allRows = data.toArray();\n * ```\n *\n * ### 2. Direct Arrow Table Access\n * - Much faster for large datasets\n * - Columnar access is more efficient for analytics\n * - No type safety or validation\n *\n * ```typescript\n * // For performance-critical operations with large datasets:\n * const nameColumn = data.arrowTable.getChild('name');\n * const emailColumn = data.arrowTable.getChild('email');\n *\n * // Fast columnar iteration (no object creation)\n * for (let i = 0; i < data.length; i++) {\n * console.log(nameColumn.get(i), emailColumn.get(i));\n * }\n *\n * // Note: For filtering data, it's most efficient to use SQL in your query\n * // const { data } = useSql<User>({\n * // query: \"SELECT * FROM users WHERE age > 30\"\n * // });\n * ```\n *\n * ### 3. Using Flechette for Advanced Operations\n *\n * For more advanced Arrow operations, consider using [Flechette](https://idl.uw.edu/flechette/),\n * a faster and lighter alternative to the standard Arrow JS implementation.\n *\n * ```typescript\n * // Example using Flechette with SQL query results\n * import { tableFromIPC } from '@uwdata/flechette';\n *\n * // Convert Arrow table to Flechette table\n * // Note: This serialization step creates a copy of the data\n * const serializedData = data.arrowTable.serialize();\n * const flechetteTable = tableFromIPC(serializedData);\n *\n * // Extract all columns into a { name: array, ... } object\n * const columns = flechetteTable.toColumns();\n *\n * // Create a new table with a selected subset of columns\n * const subtable = flechetteTable.select(['name', 'email']);\n *\n * // Convert to array of objects with customization options\n * const objects = flechetteTable.toArray({\n * useDate: true, // Convert timestamps to Date objects\n * useMap: true // Create Map objects for key-value pairs\n * });\n *\n * // For large datasets, consider memory management\n * // Once you're done with the Arrow data, you can free the memory\n * serializedData = null; // Allow garbage collection of the serialized data\n * ```\n *\n * Flechette provides several advantages:\n * - Better performance (1.3-1.6x faster value iteration, 7-11x faster row object extraction)\n * - Smaller footprint (~43k minified vs 163k for Arrow JS)\n * - Support for additional data types (including decimal-to-number conversion)\n * - More flexible data value conversion options\n *\n * @template Row The TypeScript type for each row in the result\n * @param options Configuration object containing the query and execution control\n * @returns Object containing the query result, loading state, and any error\n *\n * @template Schema The Zod schema type that defines the shape and validation of each row\n * @param schema A Zod schema that defines the expected shape and validation rules for each row\n * @param options Configuration object containing the query and execution control\n * @returns Object containing the validated query result, loading state, and any error\n */\nexport function useSql<Row>(options: {query: string; enabled?: boolean}): {\n data: UseSqlQueryResult<Row> | undefined;\n error: Error | null;\n isLoading: boolean;\n};\n\nexport function useSql<Schema extends z.ZodType>(\n schema: Schema,\n options: {\n query: string;\n enabled?: boolean;\n },\n): {\n data: UseSqlQueryResult<z.infer<Schema>> | undefined;\n error: Error | null;\n isLoading: boolean;\n};\n\n/**\n * Implementation of useSql that handles both overloads\n */\nexport function useSql<Row, Schema extends z.ZodType = z.ZodType>(\n schemaOrOptions: Schema | {query: string; enabled?: boolean},\n maybeOptions?: {query: string; enabled?: boolean},\n) {\n // Determine if we're using the schema overload\n const hasSchema = maybeOptions !== undefined;\n const options = hasSchema\n ? maybeOptions\n : (schemaOrOptions as {query: string; enabled?: boolean});\n const schema = hasSchema ? (schemaOrOptions as Schema) : undefined;\n\n const [data, setData] = useState<UseSqlQueryResult<Row> | undefined>(\n undefined,\n );\n const [error, setError] = useState<Error | null>(null);\n const [isLoading, setIsLoading] = useState(false);\n\n useEffect(() => {\n let isMounted = true;\n\n const fetchData = async () => {\n if (!options.enabled && options.enabled !== undefined) {\n return;\n }\n\n setIsLoading(true);\n setError(null);\n\n try {\n const duckDb = await getDuckDb();\n const result = await duckDb.conn.query(options.query);\n\n // Create a row accessor that optionally validates with the schema\n const rowAccessor = createTypedRowAccessor<Row>({\n arrowTable: result,\n validate: schema ? (row: unknown) => schema.parse(row) : undefined,\n });\n\n if (isMounted) {\n setData(rowAccessor);\n }\n } catch (err) {\n if (isMounted) {\n setError(err instanceof Error ? err : new Error(String(err)));\n }\n } finally {\n if (isMounted) {\n setIsLoading(false);\n }\n }\n };\n\n fetchData();\n\n return () => {\n isMounted = false;\n };\n }, [options.query, options.enabled]);\n\n return {\n data,\n error,\n isLoading,\n };\n}\n\n/**\n * @deprecated Use useSql instead\n */\nexport const useDuckDbQuery = useSql;\n"]}
1
+ {"version":3,"file":"useSql.js","sourceRoot":"","sources":["../src/useSql.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAyBtC;;GAEG;AACH,SAAS,sBAAsB,CAAI,EACjC,UAAU,EACV,QAAQ,GAIT;IACC,OAAO;QACL,UAAU;QACV,IAAI,MAAM;YACR,OAAO,UAAU,CAAC,OAAO,CAAC;QAC5B,CAAC;QACD,MAAM,CAAC,KAAa;YAClB,MAAM,GAAG,GAA4B,EAAE,CAAC;YACxC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAkB,EAAE,EAAE;gBACtD,MAAM,MAAM,GAAG,UAAU,CAAC,QAAQ,CAAC,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H,CAAC;QACH,CAAC,CAAC;QAEF,SAAS,EAAE,CAAC;QAEZ,OAAO,GAAG,EAAE;YACV,SAAS,GAAG,KAAK,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAErC,OAAO;QACL,IAAI;QACJ,KAAK;QACL,SAAS;KACV,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC","sourcesContent":["import * as arrow from 'apache-arrow';\nimport {useEffect, useState} from 'react';\nimport {getDuckDb} from './useDuckDb';\nimport {z} from 'zod';\n\n/**\n * A wrapper interface that exposes the underlying Arrow table,\n * a typed row accessor, and the number of rows.\n */\nexport interface UseSqlQueryResult<T> {\n /** The underlying Arrow table */\n arrowTable: arrow.Table;\n /** Returns a typed row at the specified index by converting on demand */\n getRow(index: number): T;\n /** Number of rows in the table */\n length: number;\n /** Returns an iterator that yields each row in the table */\n rows(): IterableIterator<T>;\n /** Returns an array containing all rows in the table */\n toArray(): T[];\n}\n\n/**\n * @deprecated Use UseSqlQueryResult instead\n */\nexport type DuckDbQueryResult<T> = UseSqlQueryResult<T>;\n\n/**\n * Creates a row accessor wrapper around an Arrow table that provides typed row access.\n */\nfunction createTypedRowAccessor<T>({\n arrowTable,\n validate,\n}: {\n arrowTable: arrow.Table;\n validate?: (row: unknown) => T;\n}): UseSqlQueryResult<T> {\n return {\n arrowTable,\n get length() {\n return arrowTable.numRows;\n },\n getRow(index: number): T {\n const row: Record<string, unknown> = {};\n arrowTable.schema.fields.forEach((field: arrow.Field) => {\n const column = arrowTable.getChild(field.name);\n if (column) {\n row[field.name] = column.get(index);\n }\n });\n\n // If a validator is provided, use it to validate/parse the row\n if (validate) {\n return validate(row);\n }\n return row as T;\n },\n *rows(): IterableIterator<T> {\n for (let i = 0; i < this.length; i++) {\n yield this.getRow(i);\n }\n },\n toArray(): T[] {\n const result: T[] = [];\n for (let i = 0; i < this.length; i++) {\n result.push(this.getRow(i));\n }\n return result;\n },\n };\n}\n\n/**\n * A React hook for executing SQL queries with automatic state management.\n * Provides two ways to ensure type safety:\n * 1. Using TypeScript types (compile-time safety only)\n * 2. Using Zod schemas (both compile-time and runtime validation)\n *\n * @example\n * ```typescript\n * // Option 1: Using TypeScript types (faster, no runtime validation)\n * interface User {\n * id: number;\n * name: string;\n * email: string;\n * }\n *\n * const {data, isLoading, error} = useSql<User>({\n * query: 'SELECT id, name, email FROM users'\n * });\n *\n * // Option 2: Using Zod schema (slower but with runtime validation)\n * const userSchema = z.object({\n * id: z.number(),\n * name: z.string(),\n * email: z.string().email(),\n * createdAt: z.string().transform(str => new Date(str)) // Transform string to Date\n * });\n *\n * const {data: validatedData, isLoading, error} = useSql(\n * userSchema,\n * {query: 'SELECT id, name, email, created_at as createdAt FROM users'}\n * );\n * ```\n *\n * ## Error Handling\n * ```typescript\n * if (isLoading) return <div>Loading...</div>;\n * if (error) {\n * // With Zod, you can catch validation errors specifically\n * if (error instanceof z.ZodError) {\n * return <div>Validation Error: {error.errors[0].message}</div>;\n * }\n * return <div>Error: {error.message}</div>;\n * }\n * if (!data) return null;\n * ```\n *\n * ## Data Access Methods\n *\n * There are several ways to access data with different performance characteristics:\n *\n * ### 1. Typed Row Access (getRow, rows(), toArray())\n * - Provides type safety and validation\n * - Converts data to JavaScript objects\n * - Slower for large datasets due to object creation and validation\n *\n * ```typescript\n * // Iterate through rows using the rows() iterator (recommended)\n * for (const user of data.rows()) {\n * console.log(user.name, user.email);\n * }\n *\n * // Traditional for loop with index access\n * for (let i = 0; i < data.length; i++) {\n * const user = data.getRow(i);\n * console.log(`User ${i}: ${user.name} (${user.email})`);\n * }\n *\n * // Get all rows as an array\n * const allUsers = data.toArray();\n *\n * // With Zod schema, transformed fields are available\n * for (const user of validatedData.rows()) {\n * console.log(`Created: ${user.createdAt.toISOString()}`); // createdAt is a Date object\n * }\n * ```\n *\n * ### 2. Direct Arrow Table Access\n * - Much faster for large datasets\n * - Columnar access is more efficient for analytics\n * - No type safety or validation\n *\n * ```typescript\n * // For performance-critical operations with large datasets:\n * const nameColumn = data.arrowTable.getChild('name');\n * const emailColumn = data.arrowTable.getChild('email');\n *\n * // Fast columnar iteration (no object creation)\n * for (let i = 0; i < data.length; i++) {\n * console.log(nameColumn.get(i), emailColumn.get(i));\n * }\n *\n * // Note: For filtering data, it's most efficient to use SQL in your query\n * const { data } = useSql<User>({\n * query: \"SELECT * FROM users WHERE age > 30\"\n * });\n * ```\n *\n * ### 3. Using Flechette for Advanced Operations\n *\n * For more advanced Arrow operations, consider using [Flechette](https://idl.uw.edu/flechette/),\n * a faster and lighter alternative to the standard Arrow JS implementation.\n *\n * ```typescript\n * // Example using Flechette with SQL query results\n * import { tableFromIPC } from '@uwdata/flechette';\n *\n * // Convert Arrow table to Flechette table\n * const serializedData = data.arrowTable.serialize();\n * const flechetteTable = tableFromIPC(serializedData);\n *\n * // Extract all columns into a { name: array, ... } object\n * const columns = flechetteTable.toColumns();\n *\n * // Create a new table with a selected subset of columns\n * const subtable = flechetteTable.select(['name', 'email']);\n *\n * // Convert to array of objects with customization options\n * const objects = flechetteTable.toArray({\n * useDate: true, // Convert timestamps to Date objects\n * useMap: true // Create Map objects for key-value pairs\n * });\n *\n * // For large datasets, consider memory management\n * serializedData = null; // Allow garbage collection of the serialized data\n * ```\n *\n * Flechette provides several advantages:\n * - Better performance (1.3-1.6x faster value iteration, 7-11x faster row object extraction)\n * - Smaller footprint (~43k minified vs 163k for Arrow JS)\n * - Support for additional data types (including decimal-to-number conversion)\n * - More flexible data value conversion options\n *\n * @template Row The TypeScript type for each row in the result\n * @param options Configuration object containing the query and execution control\n * @returns Object containing the query result, loading state, and any error\n *\n * @template Schema The Zod schema type that defines the shape and validation of each row\n * @param schema A Zod schema that defines the expected shape and validation rules for each row\n * @param options Configuration object containing the query and execution control\n * @returns Object containing the validated query result, loading state, and any error\n */\nexport function useSql<Row>(options: {query: string; enabled?: boolean}): {\n data: UseSqlQueryResult<Row> | undefined;\n error: Error | null;\n isLoading: boolean;\n};\n\nexport function useSql<Schema extends z.ZodType>(\n schema: Schema,\n options: {\n query: string;\n enabled?: boolean;\n },\n): {\n data: UseSqlQueryResult<z.infer<Schema>> | undefined;\n error: Error | null;\n isLoading: boolean;\n};\n\n/**\n * Implementation of useSql that handles both overloads\n */\nexport function useSql<Row, Schema extends z.ZodType = z.ZodType>(\n schemaOrOptions: Schema | {query: string; enabled?: boolean},\n maybeOptions?: {query: string; enabled?: boolean},\n) {\n // Determine if we're using the schema overload\n const hasSchema = maybeOptions !== undefined;\n const options = hasSchema\n ? maybeOptions\n : (schemaOrOptions as {query: string; enabled?: boolean});\n const schema = hasSchema ? (schemaOrOptions as Schema) : undefined;\n\n const [data, setData] = useState<UseSqlQueryResult<Row> | undefined>(\n undefined,\n );\n const [error, setError] = useState<Error | null>(null);\n const [isLoading, setIsLoading] = useState(false);\n\n useEffect(() => {\n let isMounted = true;\n\n const fetchData = async () => {\n if (!options.enabled && options.enabled !== undefined) {\n return;\n }\n\n setIsLoading(true);\n setError(null);\n\n try {\n const duckDb = await getDuckDb();\n const result = await duckDb.conn.query(options.query);\n\n // Create a row accessor that optionally validates with the schema\n const rowAccessor = createTypedRowAccessor<Row>({\n arrowTable: result,\n validate: schema ? (row: unknown) => schema.parse(row) : undefined,\n });\n\n if (isMounted) {\n setData(rowAccessor);\n }\n } catch (err) {\n if (isMounted) {\n setError(err instanceof Error ? err : new Error(String(err)));\n }\n } finally {\n if (isMounted) {\n setIsLoading(false);\n }\n }\n };\n\n fetchData();\n\n return () => {\n isMounted = false;\n };\n }, [options.query, options.enabled]);\n\n return {\n data,\n error,\n isLoading,\n };\n}\n\n/**\n * @deprecated Use useSql instead\n */\nexport const useDuckDbQuery = useSql;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlrooms/duckdb",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "zod": "^3.24.1"
25
25
  },
26
26
  "devDependencies": {
27
- "@sqlrooms/jest-config": "0.6.0",
27
+ "@sqlrooms/jest-config": "0.8.0",
28
28
  "@types/jest": "^29.5.12",
29
29
  "jest": "^29.7.0",
30
30
  "ts-jest": "^29.1.2"
@@ -38,5 +38,5 @@
38
38
  "test": "jest",
39
39
  "test:watch": "jest --watch"
40
40
  },
41
- "gitHead": "f46dfe6b5d135e1a039b49b3ba71cda7150eab0f"
41
+ "gitHead": "99b46a96ab900e6b005bcd30cfbfe7b3c9d51f8d"
42
42
  }