@tursodatabase/database 0.1.4-pre.10
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 +129 -0
- package/browser.js +1 -0
- package/dist/bind.js +64 -0
- package/dist/compat.js +367 -0
- package/dist/promise.js +383 -0
- package/dist/sqlite-error.js +12 -0
- package/index.d.ts +130 -0
- package/index.js +398 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<h1 align="center">Turso Database for JavaScript</h1>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a title="JavaScript" target="_blank" href="https://www.npmjs.com/package/@tursodatabase/database"><img alt="npm" src="https://img.shields.io/npm/v/@tursodatabase/database"></a>
|
|
7
|
+
<a title="MIT" target="_blank" href="https://github.com/tursodatabase/turso/blob/main/LICENSE.md"><img src="http://img.shields.io/badge/license-MIT-orange.svg?style=flat-square"></a>
|
|
8
|
+
</p>
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a title="Users Discord" target="_blank" href="https://tur.so/discord"><img alt="Chat with other users of Turso on Discord" src="https://img.shields.io/discord/933071162680958986?label=Discord&logo=Discord&style=social"></a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## About
|
|
16
|
+
|
|
17
|
+
This package is the Turso in-memory database library for JavaScript.
|
|
18
|
+
|
|
19
|
+
> **⚠️ Warning:** This software is ALPHA, only use for development, testing, and experimentation. We are working to make it production ready, but do not use it for critical data right now.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- **SQLite compatible:** SQLite query language and file format support ([status](https://github.com/tursodatabase/turso/blob/main/COMPAT.md)).
|
|
24
|
+
- **In-process**: No network overhead, runs directly in your Node.js process
|
|
25
|
+
- **TypeScript support**: Full TypeScript definitions included
|
|
26
|
+
- **Cross-platform**: Supports Linux, macOS, Windows and browsers (through WebAssembly)
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @tursodatabase/database
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Getting Started
|
|
35
|
+
|
|
36
|
+
### In-Memory Database
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
import { connect } from '@tursodatabase/database';
|
|
40
|
+
|
|
41
|
+
// Create an in-memory database
|
|
42
|
+
const db = await connect(':memory:');
|
|
43
|
+
|
|
44
|
+
// Create a table
|
|
45
|
+
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
|
|
46
|
+
|
|
47
|
+
// Insert data
|
|
48
|
+
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
|
|
49
|
+
insert.run('Alice', 'alice@example.com');
|
|
50
|
+
insert.run('Bob', 'bob@example.com');
|
|
51
|
+
|
|
52
|
+
// Query data
|
|
53
|
+
const users = db.prepare('SELECT * FROM users').all();
|
|
54
|
+
console.log(users);
|
|
55
|
+
// Output: [
|
|
56
|
+
// { id: 1, name: 'Alice', email: 'alice@example.com' },
|
|
57
|
+
// { id: 2, name: 'Bob', email: 'bob@example.com' }
|
|
58
|
+
// ]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### File-Based Database
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
import { connect } from '@tursodatabase/database';
|
|
65
|
+
|
|
66
|
+
// Create or open a database file
|
|
67
|
+
const db = await connect('my-database.db');
|
|
68
|
+
|
|
69
|
+
// Create a table
|
|
70
|
+
db.exec(`
|
|
71
|
+
CREATE TABLE IF NOT EXISTS posts (
|
|
72
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
73
|
+
title TEXT NOT NULL,
|
|
74
|
+
content TEXT,
|
|
75
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
76
|
+
)
|
|
77
|
+
`);
|
|
78
|
+
|
|
79
|
+
// Insert a post
|
|
80
|
+
const insertPost = db.prepare('INSERT INTO posts (title, content) VALUES (?, ?)');
|
|
81
|
+
const result = insertPost.run('Hello World', 'This is my first blog post!');
|
|
82
|
+
|
|
83
|
+
console.log(`Inserted post with ID: ${result.lastInsertRowid}`);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Transactions
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
import { connect } from '@tursodatabase/database';
|
|
90
|
+
|
|
91
|
+
const db = await connect('transactions.db');
|
|
92
|
+
|
|
93
|
+
// Using transactions for atomic operations
|
|
94
|
+
const transaction = db.transaction((users) => {
|
|
95
|
+
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
|
|
96
|
+
for (const user of users) {
|
|
97
|
+
insert.run(user.name, user.email);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Execute transaction
|
|
102
|
+
transaction([
|
|
103
|
+
{ name: 'Alice', email: 'alice@example.com' },
|
|
104
|
+
{ name: 'Bob', email: 'bob@example.com' }
|
|
105
|
+
]);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### WebAssembly Support
|
|
109
|
+
|
|
110
|
+
Turso Database can run in browsers using WebAssembly. Check the `browser.js` and WASM artifacts for browser usage.
|
|
111
|
+
|
|
112
|
+
## API Reference
|
|
113
|
+
|
|
114
|
+
For complete API documentation, see [JavaScript API Reference](../../docs/javascript-api-reference.md).
|
|
115
|
+
|
|
116
|
+
## Related Packages
|
|
117
|
+
|
|
118
|
+
* The [@tursodatabase/serverless](https://www.npmjs.com/package/@tursodatabase/serverless) package provides a serverless driver with the same API.
|
|
119
|
+
* The [@tursodatabase/sync](https://www.npmjs.com/package/@tursodatabase/sync) package provides bidirectional sync between a local Turso database and Turso Cloud.
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
This project is licensed under the [MIT license](../../LICENSE.md).
|
|
124
|
+
|
|
125
|
+
## Support
|
|
126
|
+
|
|
127
|
+
- [GitHub Issues](https://github.com/tursodatabase/turso/issues)
|
|
128
|
+
- [Documentation](https://docs.turso.tech)
|
|
129
|
+
- [Discord Community](https://tur.so/discord)
|
package/browser.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tursodatabase/database-wasm32-wasi'
|
package/dist/bind.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Bind parameters to a statement.
|
|
2
|
+
//
|
|
3
|
+
// This function is used to bind parameters to a statement. It supports both
|
|
4
|
+
// named and positional parameters, and nested arrays.
|
|
5
|
+
//
|
|
6
|
+
// The `stmt` parameter is a statement object.
|
|
7
|
+
// The `params` parameter is an array of parameters.
|
|
8
|
+
//
|
|
9
|
+
// The function returns void.
|
|
10
|
+
export function bindParams(stmt, params) {
|
|
11
|
+
const len = params?.length;
|
|
12
|
+
if (len === 0) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (len === 1) {
|
|
16
|
+
const param = params[0];
|
|
17
|
+
if (isPlainObject(param)) {
|
|
18
|
+
bindNamedParams(stmt, param);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
bindValue(stmt, 1, param);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
bindPositionalParams(stmt, params);
|
|
25
|
+
}
|
|
26
|
+
// Check if object is plain (no prototype chain)
|
|
27
|
+
function isPlainObject(obj) {
|
|
28
|
+
if (!obj || typeof obj !== 'object')
|
|
29
|
+
return false;
|
|
30
|
+
const proto = Object.getPrototypeOf(obj);
|
|
31
|
+
return proto === Object.prototype || proto === null;
|
|
32
|
+
}
|
|
33
|
+
// Handle named parameters
|
|
34
|
+
function bindNamedParams(stmt, paramObj) {
|
|
35
|
+
const paramCount = stmt.parameterCount();
|
|
36
|
+
for (let i = 1; i <= paramCount; i++) {
|
|
37
|
+
const paramName = stmt.parameterName(i);
|
|
38
|
+
if (paramName) {
|
|
39
|
+
const key = paramName.substring(1); // Remove ':' or '$' prefix
|
|
40
|
+
const value = paramObj[key];
|
|
41
|
+
if (value !== undefined) {
|
|
42
|
+
bindValue(stmt, i, value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Handle positional parameters (including nested arrays)
|
|
48
|
+
function bindPositionalParams(stmt, params) {
|
|
49
|
+
let bindIndex = 1;
|
|
50
|
+
for (let i = 0; i < params.length; i++) {
|
|
51
|
+
const param = params[i];
|
|
52
|
+
if (Array.isArray(param)) {
|
|
53
|
+
for (let j = 0; j < param.length; j++) {
|
|
54
|
+
bindValue(stmt, bindIndex++, param[j]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
bindValue(stmt, bindIndex++, param);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function bindValue(stmt, index, value) {
|
|
63
|
+
stmt.bindAt(index, value);
|
|
64
|
+
}
|
package/dist/compat.js
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import { Database as NativeDB } from "#entry-point";
|
|
2
|
+
import { bindParams } from "./bind.js";
|
|
3
|
+
import { SqliteError } from "./sqlite-error.js";
|
|
4
|
+
// Step result constants
|
|
5
|
+
const STEP_ROW = 1;
|
|
6
|
+
const STEP_DONE = 2;
|
|
7
|
+
const STEP_IO = 3;
|
|
8
|
+
const convertibleErrorTypes = { TypeError };
|
|
9
|
+
const CONVERTIBLE_ERROR_PREFIX = "[TURSO_CONVERT_TYPE]";
|
|
10
|
+
function convertError(err) {
|
|
11
|
+
if ((err.code ?? "").startsWith(CONVERTIBLE_ERROR_PREFIX)) {
|
|
12
|
+
return createErrorByName(err.code.substring(CONVERTIBLE_ERROR_PREFIX.length), err.message);
|
|
13
|
+
}
|
|
14
|
+
return new SqliteError(err.message, err.code, err.rawCode);
|
|
15
|
+
}
|
|
16
|
+
function createErrorByName(name, message) {
|
|
17
|
+
const ErrorConstructor = convertibleErrorTypes[name];
|
|
18
|
+
if (!ErrorConstructor) {
|
|
19
|
+
throw new Error(`unknown error type ${name} from Turso`);
|
|
20
|
+
}
|
|
21
|
+
return new ErrorConstructor(message);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Database represents a connection that can prepare and execute SQL statements.
|
|
25
|
+
*/
|
|
26
|
+
class Database {
|
|
27
|
+
db;
|
|
28
|
+
memory;
|
|
29
|
+
open;
|
|
30
|
+
_inTransaction = false;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new database connection. If the database file pointed to by `path` does not exists, it will be created.
|
|
33
|
+
*
|
|
34
|
+
* @constructor
|
|
35
|
+
* @param {string} path - Path to the database file.
|
|
36
|
+
* @param {Object} opts - Options for database behavior.
|
|
37
|
+
* @param {boolean} [opts.readonly=false] - Open the database in read-only mode.
|
|
38
|
+
* @param {boolean} [opts.fileMustExist=false] - If true, throws if database file does not exist.
|
|
39
|
+
* @param {number} [opts.timeout=0] - Timeout duration in milliseconds for database operations. Defaults to 0 (no timeout).
|
|
40
|
+
*/
|
|
41
|
+
constructor(path, opts = {}) {
|
|
42
|
+
opts.readonly = opts.readonly === undefined ? false : opts.readonly;
|
|
43
|
+
opts.fileMustExist =
|
|
44
|
+
opts.fileMustExist === undefined ? false : opts.fileMustExist;
|
|
45
|
+
opts.timeout = opts.timeout === undefined ? 0 : opts.timeout;
|
|
46
|
+
this.db = new NativeDB(path);
|
|
47
|
+
this.memory = this.db.memory;
|
|
48
|
+
const db = this.db;
|
|
49
|
+
Object.defineProperties(this, {
|
|
50
|
+
inTransaction: {
|
|
51
|
+
get: () => this._inTransaction,
|
|
52
|
+
},
|
|
53
|
+
name: {
|
|
54
|
+
get() {
|
|
55
|
+
return path;
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
readonly: {
|
|
59
|
+
get() {
|
|
60
|
+
return opts.readonly;
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
open: {
|
|
64
|
+
get() {
|
|
65
|
+
return this.db.open;
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Prepares a SQL statement for execution.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} sql - The SQL statement string to prepare.
|
|
74
|
+
*/
|
|
75
|
+
prepare(sql) {
|
|
76
|
+
if (!this.open) {
|
|
77
|
+
throw new TypeError("The database connection is not open");
|
|
78
|
+
}
|
|
79
|
+
if (!sql) {
|
|
80
|
+
throw new RangeError("The supplied SQL string contains no statements");
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return new Statement(this.db.prepare(sql), this);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
throw convertError(err);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns a function that executes the given function in a transaction.
|
|
91
|
+
*
|
|
92
|
+
* @param {function} fn - The function to wrap in a transaction.
|
|
93
|
+
*/
|
|
94
|
+
transaction(fn) {
|
|
95
|
+
if (typeof fn !== "function")
|
|
96
|
+
throw new TypeError("Expected first argument to be a function");
|
|
97
|
+
const db = this;
|
|
98
|
+
const wrapTxn = (mode) => {
|
|
99
|
+
return (...bindParameters) => {
|
|
100
|
+
db.exec("BEGIN " + mode);
|
|
101
|
+
db._inTransaction = true;
|
|
102
|
+
try {
|
|
103
|
+
const result = fn(...bindParameters);
|
|
104
|
+
db.exec("COMMIT");
|
|
105
|
+
db._inTransaction = false;
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
db.exec("ROLLBACK");
|
|
110
|
+
db._inTransaction = false;
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const properties = {
|
|
116
|
+
default: { value: wrapTxn("") },
|
|
117
|
+
deferred: { value: wrapTxn("DEFERRED") },
|
|
118
|
+
immediate: { value: wrapTxn("IMMEDIATE") },
|
|
119
|
+
exclusive: { value: wrapTxn("EXCLUSIVE") },
|
|
120
|
+
database: { value: this, enumerable: true },
|
|
121
|
+
};
|
|
122
|
+
Object.defineProperties(properties.default.value, properties);
|
|
123
|
+
Object.defineProperties(properties.deferred.value, properties);
|
|
124
|
+
Object.defineProperties(properties.immediate.value, properties);
|
|
125
|
+
Object.defineProperties(properties.exclusive.value, properties);
|
|
126
|
+
return properties.default.value;
|
|
127
|
+
}
|
|
128
|
+
pragma(source, options) {
|
|
129
|
+
if (options == null)
|
|
130
|
+
options = {};
|
|
131
|
+
if (typeof source !== "string")
|
|
132
|
+
throw new TypeError("Expected first argument to be a string");
|
|
133
|
+
if (typeof options !== "object")
|
|
134
|
+
throw new TypeError("Expected second argument to be an options object");
|
|
135
|
+
const pragma = `PRAGMA ${source}`;
|
|
136
|
+
const stmt = this.prepare(pragma);
|
|
137
|
+
const results = stmt.all();
|
|
138
|
+
return results;
|
|
139
|
+
}
|
|
140
|
+
backup(filename, options) {
|
|
141
|
+
throw new Error("not implemented");
|
|
142
|
+
}
|
|
143
|
+
serialize(options) {
|
|
144
|
+
throw new Error("not implemented");
|
|
145
|
+
}
|
|
146
|
+
function(name, options, fn) {
|
|
147
|
+
throw new Error("not implemented");
|
|
148
|
+
}
|
|
149
|
+
aggregate(name, options) {
|
|
150
|
+
throw new Error("not implemented");
|
|
151
|
+
}
|
|
152
|
+
table(name, factory) {
|
|
153
|
+
throw new Error("not implemented");
|
|
154
|
+
}
|
|
155
|
+
loadExtension(path) {
|
|
156
|
+
throw new Error("not implemented");
|
|
157
|
+
}
|
|
158
|
+
maxWriteReplicationIndex() {
|
|
159
|
+
throw new Error("not implemented");
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Executes a SQL statement.
|
|
163
|
+
*
|
|
164
|
+
* @param {string} sql - The SQL statement string to execute.
|
|
165
|
+
*/
|
|
166
|
+
exec(sql) {
|
|
167
|
+
if (!this.open) {
|
|
168
|
+
throw new TypeError("The database connection is not open");
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
this.db.batch(sql);
|
|
172
|
+
}
|
|
173
|
+
catch (err) {
|
|
174
|
+
throw convertError(err);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Interrupts the database connection.
|
|
179
|
+
*/
|
|
180
|
+
interrupt() {
|
|
181
|
+
throw new Error("not implemented");
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Sets the default safe integers mode for all statements from this database.
|
|
185
|
+
*
|
|
186
|
+
* @param {boolean} [toggle] - Whether to use safe integers by default.
|
|
187
|
+
*/
|
|
188
|
+
defaultSafeIntegers(toggle) {
|
|
189
|
+
this.db.defaultSafeIntegers(toggle);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Closes the database connection.
|
|
193
|
+
*/
|
|
194
|
+
close() {
|
|
195
|
+
this.db.close();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Statement represents a prepared SQL statement that can be executed.
|
|
200
|
+
*/
|
|
201
|
+
class Statement {
|
|
202
|
+
stmt;
|
|
203
|
+
db;
|
|
204
|
+
constructor(stmt, database) {
|
|
205
|
+
this.stmt = stmt;
|
|
206
|
+
this.db = database;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Toggle raw mode.
|
|
210
|
+
*
|
|
211
|
+
* @param raw Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled.
|
|
212
|
+
*/
|
|
213
|
+
raw(raw) {
|
|
214
|
+
this.stmt.raw(raw);
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Toggle pluck mode.
|
|
219
|
+
*
|
|
220
|
+
* @param pluckMode Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled.
|
|
221
|
+
*/
|
|
222
|
+
pluck(pluckMode) {
|
|
223
|
+
this.stmt.pluck(pluckMode);
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Sets safe integers mode for this statement.
|
|
228
|
+
*
|
|
229
|
+
* @param {boolean} [toggle] - Whether to use safe integers.
|
|
230
|
+
*/
|
|
231
|
+
safeIntegers(toggle) {
|
|
232
|
+
this.stmt.safeIntegers(toggle);
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Get column information for the statement.
|
|
237
|
+
*
|
|
238
|
+
* @returns {Array} An array of column objects with name, column, table, database, and type properties.
|
|
239
|
+
*/
|
|
240
|
+
columns() {
|
|
241
|
+
return this.stmt.columns();
|
|
242
|
+
}
|
|
243
|
+
get source() {
|
|
244
|
+
throw new Error("not implemented");
|
|
245
|
+
}
|
|
246
|
+
get reader() {
|
|
247
|
+
throw new Error("not implemented");
|
|
248
|
+
}
|
|
249
|
+
get database() {
|
|
250
|
+
return this.db;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Executes the SQL statement and returns an info object.
|
|
254
|
+
*/
|
|
255
|
+
run(...bindParameters) {
|
|
256
|
+
const totalChangesBefore = this.db.db.totalChanges();
|
|
257
|
+
this.stmt.reset();
|
|
258
|
+
bindParams(this.stmt, bindParameters);
|
|
259
|
+
for (;;) {
|
|
260
|
+
const stepResult = this.stmt.step();
|
|
261
|
+
if (stepResult === STEP_IO) {
|
|
262
|
+
this.db.db.ioLoopSync();
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (stepResult === STEP_DONE) {
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
if (stepResult === STEP_ROW) {
|
|
269
|
+
// For run(), we don't need the row data, just continue
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const lastInsertRowid = this.db.db.lastInsertRowid();
|
|
274
|
+
const changes = this.db.db.totalChanges() === totalChangesBefore ? 0 : this.db.db.changes();
|
|
275
|
+
return { changes, lastInsertRowid };
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Executes the SQL statement and returns the first row.
|
|
279
|
+
*
|
|
280
|
+
* @param bindParameters - The bind parameters for executing the statement.
|
|
281
|
+
*/
|
|
282
|
+
get(...bindParameters) {
|
|
283
|
+
this.stmt.reset();
|
|
284
|
+
bindParams(this.stmt, bindParameters);
|
|
285
|
+
for (;;) {
|
|
286
|
+
const stepResult = this.stmt.step();
|
|
287
|
+
if (stepResult === STEP_IO) {
|
|
288
|
+
this.db.db.ioLoopSync();
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (stepResult === STEP_DONE) {
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
if (stepResult === STEP_ROW) {
|
|
295
|
+
return this.stmt.row();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Executes the SQL statement and returns an iterator to the resulting rows.
|
|
301
|
+
*
|
|
302
|
+
* @param bindParameters - The bind parameters for executing the statement.
|
|
303
|
+
*/
|
|
304
|
+
*iterate(...bindParameters) {
|
|
305
|
+
this.stmt.reset();
|
|
306
|
+
bindParams(this.stmt, bindParameters);
|
|
307
|
+
while (true) {
|
|
308
|
+
const stepResult = this.stmt.step();
|
|
309
|
+
if (stepResult === STEP_IO) {
|
|
310
|
+
this.db.db.ioLoopSync();
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (stepResult === STEP_DONE) {
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
if (stepResult === STEP_ROW) {
|
|
317
|
+
yield this.stmt.row();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Executes the SQL statement and returns an array of the resulting rows.
|
|
323
|
+
*
|
|
324
|
+
* @param bindParameters - The bind parameters for executing the statement.
|
|
325
|
+
*/
|
|
326
|
+
all(...bindParameters) {
|
|
327
|
+
this.stmt.reset();
|
|
328
|
+
bindParams(this.stmt, bindParameters);
|
|
329
|
+
const rows = [];
|
|
330
|
+
for (;;) {
|
|
331
|
+
const stepResult = this.stmt.step();
|
|
332
|
+
if (stepResult === STEP_IO) {
|
|
333
|
+
this.db.db.ioLoopSync();
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (stepResult === STEP_DONE) {
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
if (stepResult === STEP_ROW) {
|
|
340
|
+
rows.push(this.stmt.row());
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return rows;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Interrupts the statement.
|
|
347
|
+
*/
|
|
348
|
+
interrupt() {
|
|
349
|
+
throw new Error("not implemented");
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Binds the given parameters to the statement _permanently_
|
|
353
|
+
*
|
|
354
|
+
* @param bindParameters - The bind parameters for binding the statement.
|
|
355
|
+
* @returns this - Statement with binded parameters
|
|
356
|
+
*/
|
|
357
|
+
bind(...bindParameters) {
|
|
358
|
+
try {
|
|
359
|
+
bindParams(this.stmt, bindParameters);
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
catch (err) {
|
|
363
|
+
throw convertError(err);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
export { Database, SqliteError };
|