@hpcc-js/wasm-duckdb 1.13.1 → 1.15.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 CHANGED
@@ -133,6 +133,24 @@ const progress = connection.getQueryProgress();
133
133
  console.log(`Query is ${progress * 100}% complete`);
134
134
  ```
135
135
 
136
+ ##### `queryToJSON(sql: string): string`
137
+ Executes a SQL query and returns the result directly as a JSON string. This is a convenience method that combines `query()` and `toJSON()` into a single call.
138
+
139
+ ```typescript
140
+ const json = connection.queryToJSON("SELECT * FROM users WHERE age > 18");
141
+ // Returns: '[{"id":1,"name":"Alice","age":30},{"id":2,"name":"Bob","age":25}]'
142
+
143
+ const users = JSON.parse(json);
144
+ console.log(users[0].name); // "Alice"
145
+ ```
146
+
147
+ **Features:**
148
+ - No need to manually delete the result (automatic cleanup)
149
+ - Returns an empty array `[]` for queries with no results
150
+ - Handles all data types including NULL, numbers, strings, and booleans
151
+ - Proper JSON escaping for special characters
152
+ - NaN and Infinity are converted to `null`
153
+
136
154
  #### Transaction Management
137
155
 
138
156
  ##### `beginTransaction(): void`
@@ -549,6 +567,34 @@ users.forEach(user => {
549
567
  result.delete();
550
568
  ```
551
569
 
570
+ ### Direct JSON Query (queryToJSON)
571
+
572
+ ```typescript
573
+ const connection = duckdb.connect();
574
+
575
+ // Convenience method: execute query and get JSON in one call
576
+ const json = connection.queryToJSON("SELECT * FROM users ORDER BY age");
577
+
578
+ // No need to delete result - it's automatic!
579
+ const users = JSON.parse(json);
580
+ users.forEach(user => {
581
+ console.log(`${user.name} is ${user.age} years old`);
582
+ });
583
+
584
+ // Use with aggregations
585
+ const stats = connection.queryToJSON(`
586
+ SELECT
587
+ department,
588
+ COUNT(*) as count,
589
+ AVG(salary) as avg_salary
590
+ FROM employees
591
+ GROUP BY department
592
+ `);
593
+ console.log(stats);
594
+
595
+ connection.delete();
596
+ ```
597
+
552
598
  ### Error Handling
553
599
 
554
600
  ```typescript