@tursodatabase/serverless 0.1.0 → 0.1.2

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/dist/client.js DELETED
@@ -1,184 +0,0 @@
1
- import { executeCursor, encodeValue, decodeValue } from './protocol.js';
2
- function normalizeUrl(url) {
3
- return url.replace(/^libsql:\/\//, 'https://');
4
- }
5
- function isValidIdentifier(str) {
6
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(str);
7
- }
8
- export class Statement {
9
- constructor(connection, sql, args = []) {
10
- this.connection = connection;
11
- this.sql = sql;
12
- this.args = args;
13
- }
14
- async get() {
15
- const result = await this.execute();
16
- return result.rows[0] || null;
17
- }
18
- async all() {
19
- const result = await this.execute();
20
- return result.rows;
21
- }
22
- async *iterate() {
23
- const { response, entries } = await this.connection.executeRaw(this.sql, this.args);
24
- let columns = [];
25
- for await (const entry of entries) {
26
- switch (entry.type) {
27
- case 'step_begin':
28
- if (entry.cols) {
29
- columns = entry.cols.map(col => col.name);
30
- }
31
- break;
32
- case 'row':
33
- if (entry.row) {
34
- const decodedRow = entry.row.map(decodeValue);
35
- const rowObject = this.connection.createRowObject(decodedRow, columns);
36
- yield rowObject;
37
- }
38
- break;
39
- case 'step_error':
40
- case 'error':
41
- throw new Error(entry.error?.message || 'SQL execution failed');
42
- }
43
- }
44
- }
45
- async execute() {
46
- return this.connection.execute(this.sql, this.args);
47
- }
48
- }
49
- export class Connection {
50
- constructor(config) {
51
- this.baton = null;
52
- this.config = config;
53
- this.baseUrl = normalizeUrl(config.url);
54
- }
55
- prepare(sql, args = []) {
56
- return new Statement(this, sql, args);
57
- }
58
- async execute(sql, args = []) {
59
- const { response, entries } = await this.executeRaw(sql, args);
60
- const result = await this.processCursorEntries(entries);
61
- return result;
62
- }
63
- async executeRaw(sql, args = []) {
64
- const request = {
65
- baton: this.baton,
66
- batch: {
67
- steps: [{
68
- stmt: {
69
- sql,
70
- args: args.map(encodeValue),
71
- want_rows: true
72
- }
73
- }]
74
- }
75
- };
76
- const { response, entries } = await executeCursor(this.baseUrl, this.config.authToken, request);
77
- this.baton = response.baton;
78
- if (response.base_url) {
79
- this.baseUrl = response.base_url;
80
- }
81
- return { response, entries };
82
- }
83
- async processCursorEntries(entries) {
84
- let columns = [];
85
- let columnTypes = [];
86
- let rows = [];
87
- let rowsAffected = 0;
88
- let lastInsertRowid;
89
- for await (const entry of entries) {
90
- switch (entry.type) {
91
- case 'step_begin':
92
- if (entry.cols) {
93
- columns = entry.cols.map(col => col.name);
94
- columnTypes = entry.cols.map(col => col.decltype || '');
95
- }
96
- break;
97
- case 'row':
98
- if (entry.row) {
99
- const decodedRow = entry.row.map(decodeValue);
100
- const rowObject = this.createRowObject(decodedRow, columns);
101
- rows.push(rowObject);
102
- }
103
- break;
104
- case 'step_end':
105
- if (entry.affected_row_count !== undefined) {
106
- rowsAffected = entry.affected_row_count;
107
- }
108
- if (entry.last_insert_rowid) {
109
- lastInsertRowid = parseInt(entry.last_insert_rowid, 10);
110
- }
111
- break;
112
- case 'step_error':
113
- case 'error':
114
- throw new Error(entry.error?.message || 'SQL execution failed');
115
- }
116
- }
117
- return {
118
- columns,
119
- columnTypes,
120
- rows,
121
- rowsAffected,
122
- lastInsertRowid
123
- };
124
- }
125
- createRowObject(values, columns) {
126
- const row = [...values];
127
- // Add column name properties to the array as non-enumerable
128
- // Only add valid identifier names to avoid conflicts
129
- columns.forEach((column, index) => {
130
- if (column && isValidIdentifier(column)) {
131
- Object.defineProperty(row, column, {
132
- value: values[index],
133
- enumerable: false,
134
- writable: false,
135
- configurable: true
136
- });
137
- }
138
- });
139
- return row;
140
- }
141
- async batch(statements, mode) {
142
- const request = {
143
- baton: this.baton,
144
- batch: {
145
- steps: statements.map(sql => ({
146
- stmt: {
147
- sql,
148
- args: [],
149
- want_rows: false
150
- }
151
- }))
152
- }
153
- };
154
- const { response, entries } = await executeCursor(this.baseUrl, this.config.authToken, request);
155
- this.baton = response.baton;
156
- if (response.base_url) {
157
- this.baseUrl = response.base_url;
158
- }
159
- let totalRowsAffected = 0;
160
- let lastInsertRowid;
161
- for await (const entry of entries) {
162
- switch (entry.type) {
163
- case 'step_end':
164
- if (entry.affected_row_count !== undefined) {
165
- totalRowsAffected += entry.affected_row_count;
166
- }
167
- if (entry.last_insert_rowid) {
168
- lastInsertRowid = parseInt(entry.last_insert_rowid, 10);
169
- }
170
- break;
171
- case 'step_error':
172
- case 'error':
173
- throw new Error(entry.error?.message || 'Batch execution failed');
174
- }
175
- }
176
- return {
177
- rowsAffected: totalRowsAffected,
178
- lastInsertRowid
179
- };
180
- }
181
- }
182
- export function connect(config) {
183
- return new Connection(config);
184
- }