@tursodatabase/serverless 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.
- package/README.md +78 -0
- package/dist/client.d.ts +31 -0
- package/dist/client.js +184 -0
- package/dist/compat/index.d.ts +1 -0
- package/dist/compat/index.js +1 -0
- package/dist/compat.d.ts +136 -0
- package/dist/compat.js +177 -0
- package/dist/connection.d.ts +82 -0
- package/dist/connection.js +86 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/protocol.d.ts +89 -0
- package/dist/protocol.js +128 -0
- package/dist/session.d.ts +63 -0
- package/dist/session.js +176 -0
- package/dist/statement.d.ts +64 -0
- package/dist/statement.js +94 -0
- package/package.json +35 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { decodeValue } from './protocol.js';
|
|
2
|
+
import { Session } from './session.js';
|
|
3
|
+
/**
|
|
4
|
+
* A prepared SQL statement that can be executed in multiple ways.
|
|
5
|
+
*
|
|
6
|
+
* Each statement has its own session to avoid conflicts during concurrent execution.
|
|
7
|
+
* Provides three execution modes:
|
|
8
|
+
* - `get(args?)`: Returns the first row or null
|
|
9
|
+
* - `all(args?)`: Returns all rows as an array
|
|
10
|
+
* - `iterate(args?)`: Returns an async iterator for streaming results
|
|
11
|
+
*/
|
|
12
|
+
export class Statement {
|
|
13
|
+
constructor(sessionConfig, sql) {
|
|
14
|
+
this.session = new Session(sessionConfig);
|
|
15
|
+
this.sql = sql;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Execute the statement and return the first row.
|
|
19
|
+
*
|
|
20
|
+
* @param args - Optional array of parameter values for the SQL statement
|
|
21
|
+
* @returns Promise resolving to the first row or null if no results
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const stmt = client.prepare("SELECT * FROM users WHERE id = ?");
|
|
26
|
+
* const user = await stmt.get([123]);
|
|
27
|
+
* if (user) {
|
|
28
|
+
* console.log(user.name);
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
async get(args = []) {
|
|
33
|
+
const result = await this.session.execute(this.sql, args);
|
|
34
|
+
return result.rows[0] || null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Execute the statement and return all rows.
|
|
38
|
+
*
|
|
39
|
+
* @param args - Optional array of parameter values for the SQL statement
|
|
40
|
+
* @returns Promise resolving to an array of all result rows
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const stmt = client.prepare("SELECT * FROM users WHERE active = ?");
|
|
45
|
+
* const activeUsers = await stmt.all([true]);
|
|
46
|
+
* console.log(`Found ${activeUsers.length} active users`);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
async all(args = []) {
|
|
50
|
+
const result = await this.session.execute(this.sql, args);
|
|
51
|
+
return result.rows;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Execute the statement and return an async iterator for streaming results.
|
|
55
|
+
*
|
|
56
|
+
* This method provides memory-efficient processing of large result sets
|
|
57
|
+
* by streaming rows one at a time instead of loading everything into memory.
|
|
58
|
+
*
|
|
59
|
+
* @param args - Optional array of parameter values for the SQL statement
|
|
60
|
+
* @returns AsyncGenerator that yields individual rows
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const stmt = client.prepare("SELECT * FROM large_table WHERE category = ?");
|
|
65
|
+
* for await (const row of stmt.iterate(['electronics'])) {
|
|
66
|
+
* // Process each row individually
|
|
67
|
+
* console.log(row.id, row.name);
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
async *iterate(args = []) {
|
|
72
|
+
const { response, entries } = await this.session.executeRaw(this.sql, args);
|
|
73
|
+
let columns = [];
|
|
74
|
+
for await (const entry of entries) {
|
|
75
|
+
switch (entry.type) {
|
|
76
|
+
case 'step_begin':
|
|
77
|
+
if (entry.cols) {
|
|
78
|
+
columns = entry.cols.map(col => col.name);
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case 'row':
|
|
82
|
+
if (entry.row) {
|
|
83
|
+
const decodedRow = entry.row.map(decodeValue);
|
|
84
|
+
const rowObject = this.session.createRowObject(decodedRow, columns);
|
|
85
|
+
yield rowObject;
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case 'step_error':
|
|
89
|
+
case 'error':
|
|
90
|
+
throw new Error(entry.error?.message || 'SQL execution failed');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tursodatabase/serverless",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./compat": {
|
|
17
|
+
"import": "./dist/compat/index.js",
|
|
18
|
+
"types": "./dist/compat/index.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"dev": "tsc --watch",
|
|
24
|
+
"test": "ava integration-tests/*.test.mjs"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"description": "",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^24.0.13",
|
|
32
|
+
"ava": "^6.4.1",
|
|
33
|
+
"typescript": "^5.8.3"
|
|
34
|
+
}
|
|
35
|
+
}
|