Spaces:
Sleeping
Sleeping
Add node_modules/@libsql/hrana-client/README.md
Browse files
node_modules/@libsql/hrana-client/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hrana client for TypeScript
|
| 2 |
+
|
| 3 |
+
**[API docs][docs] | [Github][github] | [npm][npm]**
|
| 4 |
+
|
| 5 |
+
[docs]: https://libsql.org/hrana-client-ts/
|
| 6 |
+
[github]: https://github.com/libsql/hrana-client-ts/
|
| 7 |
+
[npm]: https://www.npmjs.com/package/@libsql/hrana-client
|
| 8 |
+
|
| 9 |
+
This package implements a Hrana client for TypeScript. Hrana is the protocol for connecting to sqld using WebSocket or HTTP.
|
| 10 |
+
|
| 11 |
+
> This package is intended mostly for internal use. Consider using the [`@libsql/client`][libsql-client] package, which will use Hrana automatically.
|
| 12 |
+
|
| 13 |
+
[libsql-client]: https://www.npmjs.com/package/@libsql/client
|
| 14 |
+
|
| 15 |
+
## Usage
|
| 16 |
+
|
| 17 |
+
```typescript
|
| 18 |
+
import * as hrana from "@libsql/hrana-client";
|
| 19 |
+
|
| 20 |
+
// Open a `hrana.Client`, which works like a connection pool in standard SQL
|
| 21 |
+
// databases.
|
| 22 |
+
const url = process.env.URL ?? "ws://localhost:8080"; // Address of the sqld server
|
| 23 |
+
const jwt = process.env.JWT; // JWT token for authentication
|
| 24 |
+
// Here we are using Hrana over WebSocket:
|
| 25 |
+
const client = hrana.openWs(url, jwt);
|
| 26 |
+
// But we can also use Hrana over HTTP:
|
| 27 |
+
// const client = hrana.openHttp(url, jwt);
|
| 28 |
+
|
| 29 |
+
// Open a `hrana.Stream`, which is an interactive SQL stream. This corresponds
|
| 30 |
+
// to a "connection" from other SQL databases
|
| 31 |
+
const stream = client.openStream();
|
| 32 |
+
|
| 33 |
+
// Fetch all rows returned by a SQL statement
|
| 34 |
+
const books = await stream.query("SELECT title, year FROM book WHERE author = 'Jane Austen'");
|
| 35 |
+
// The rows are returned in an Array...
|
| 36 |
+
for (const book of books.rows) {
|
| 37 |
+
// every returned row works as an array (`book[1]`) and as an object (`book.year`)
|
| 38 |
+
console.log(`${book.title} from ${book.year}`);
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Fetch a single row
|
| 42 |
+
const book = await stream.queryRow("SELECT title, MIN(year) FROM book");
|
| 43 |
+
if (book.row !== undefined) {
|
| 44 |
+
console.log(`The oldest book is ${book.row.title} from year ${book.row[1]}`);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Fetch a single value, using a bound parameter
|
| 48 |
+
const year = await stream.queryValue(["SELECT MAX(year) FROM book WHERE author = ?", ["Jane Austen"]]);
|
| 49 |
+
if (year.value !== undefined) {
|
| 50 |
+
console.log(`Last book from Jane Austen was published in ${year.value}`);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// Execute a statement that does not return any rows
|
| 54 |
+
const res = await stream.run(["DELETE FROM book WHERE author = ?", ["J. K. Rowling"]])
|
| 55 |
+
console.log(`${res.affectedRowCount} books have been cancelled`);
|
| 56 |
+
|
| 57 |
+
// When you are done, remember to close the client
|
| 58 |
+
client.close();
|
| 59 |
+
```
|