JavaScript
The JavaScript and TypeScript client library is used to communicate with an Endb instance from a JavaScript or TypeScript application. Type declarations are provided in the package for TypeScript.
Table of Contents
Install
npm install @endatabas/endb
npm install ws
Usage Examples
Import
import { Endb, EndbWebSocket } from '@endatabas/endb';
Endb
Use the Endb
class to communicate with Endb over HTTP.
It accepts an optional url
parameter.
Options can be supplied for accept
, username
, and password
.
Accept headers default to LD-JSON and can be set to any valid
content type listed in the HTTP API.
If you choose application/vnd.apache.arrow.file
or application/vnd.apache.arrow.stream
,
the raw response body will be be returned from sql()
.
var e = new Endb();
var e = new Endb('http://localhost:3803/sql');
var e = new Endb('http://localhost:3803/sql', {accept: 'text/csv'});
var e = new Endb('http://localhost:3803/sql', {accept: 'application/json', username: 'zig', password: 'zag'});
NOTE: Choosing accept headers other than LD-JSON will return
JavaScript data structures symmetrical with those returned from
the respective accept header provided to the HTTP API.
text/csv
returns comma-delimited strings, application/json
returns tuples as arrays, and so on.
EndbWebSocket
Use the EndbWebSocket
class to communicate with Endb over WebSockets.
It accepts an optional url
parameter.
Options can be supplied for ws
(any implementation of the
JavaScript WebSocket interface definition),
username
, and password
.
In a web browser, ws
will default to the web browser's WebSocket implementation.
EndbWebSocket
only communicates in LD-JSON, so the accept header cannot be set.
// in web browser:
var ews = new EndbWebSocket();
var ews = new EndbWebSocket('ws://localhost:3803/sql', {username: 'zig', password: 'zag'});
// in node.js:
import WebSocket from 'ws';
var ews = new EndbWebSocket({ws: WebSocket});
var ews = new EndbWebSocket('ws://localhost:3803/sql', {ws: WebSocket, username: 'zig', password: 'zag'});
sql()
The asynchronous sql
method is available to both Endb
and EndbWebSocket
.
It accepts q
, and optional p
, m
, and accept
parameters
and returns an array of strongly-typed documents.
(See JavaScript API Reference.)
To ignore the p
or m
parameters but still supply an accept header,
supply the default values or null
.
e.sql("SELECT * FROM users;");
e.sql("INSERT INTO USERS (date, name, email) VALUES (?, ?, ?);", [new Date(), 'Aaron', 'aaron@canadahealth.ca']);
e.sql("INSERT INTO USERS (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true);
e.sql("SELECT * FROM users;", [], false, 'text/csv');
e.sql("SELECT * FROM users;", null, null, 'application/json');
e.sql("INSERT INTO USERS (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true, 'text/csv');
Template Literals
It is possible to use Template Literals (string templating) to pass named SQL parameters. The parameter passed to the Template Literal is only valid when used in a position where a positional SQL parameter is also valid. The signature which accepts Template Literals does not accept any other parameters to the method.
var n = 'Michael';
e.sql`INSERT INTO users (name) VALUES (${n});`;
var u = {name: 'Radha', roles: ['artist', 'marketing']};
e.sql`INSERT INTO users ${u}`;
Data Types
When an LD-JSON (default) accept header is used, strongly typed data is returned according to this mapping:
null
-null
xsd:date
-Date
xsd:dateTime
-Date
xsd:base64Binary
-Uint8Array
xsd:integer
-BigInt
For more information on Endb data types, see the Data Types doc.
Complete Examples
import { Endb } from '@endatabas/endb';
var e = new Endb();
await e.sql("INSERT INTO users {name: 'Thupil'};");
var result = await e.sql("SELECT * FROM users;");
console.log(result);
var e2 = new Endb('http://localhost:3803/sql', {accept: 'application/json', username: 'zig', password: 'zag'});
await e.sql("INSERT INTO USERS (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true, 'text/csv');
result = await e.sql("SELECT * FROM users;", null, null, 'application/json');
console.log(result);
import WebSocket from 'ws';
import { EndbWebSocket } from '@endatabas/endb';
var ews = new EndbWebSocket({ws: WebSocket});
await ews.sql("insert into users {name: 'Lydia'};");
var ws_result = await ews.sql("select * from users;");
console.log(ws_result);
var ews2 = new EndbWebSocket({ws: WebSocket, username: 'zig', password: 'zag'});
await ews2.sql("INSERT INTO USERS (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true, 'text/csv');
ws_result = await ews2.sql("SELECT * FROM users;", null, null, 'application/json');
console.log(ws_result);
JavaScript API Reference
NOTE: The following API documentation is generated from source code docstrings in the
endb
repository.
Classes
- Endb
Endatabas client for the HTTP API
- EndbWebSocket
Endatabas client for the WebSocket API
Endb
Endatabas client for the HTTP API
Kind: global class
- Endb
new Endb([url], [opt])
.sql(q, [p], [m], [accept])
⇒Promise.<Array>
new Endb([url], [opt])
Create an Endb object (Endatabas client for the HTTP API)
Param | Type | Default | Description |
---|---|---|---|
[url] | string | "http://localhost:3803/sql" | HTTP URL to the Endatabas /sql API |
[opt] | Object | HTTP options |
|
[opt.accept] | string | "application/ld+json" | Accept Header content type |
[opt.username] | string | username for HTTP Basic Auth |
|
[opt.password] | string | password for HTTP Basic Auth |
endb.sql(q, [p], [m], [accept])
⇒ Promise.<Array>
Execute a SQL statement over HTTP
Kind: instance method of Endb
Returns: Promise.<Array>
- - Array of documents
Param | Type | Description |
---|---|---|
q | string | SQL query as string or Template Literal |
[p] | Array | Object | Positional parameters, named parameters, or an array of either |
[m] | boolean | many parameters flag |
[accept] | string | Accept Header content type |
Example
// Simple query
sql("SELECT * FROM users;");
// Positional parameters
sql("INSERT INTO users (date, name) VALUES (?, ?);", [new Date(), 'Aaron']);
// Named parameters
sql("INSERT INTO users {date: :d, name: :n};", {d: new Date(), n: 'Aaron'});
// Many positional parameters (batches)
sql("INSERT INTO users (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true);
// Many named parameters (batches)
sql("INSERT INTO users {name: :n};", [{n: 'Judy'}, {n: 'Addis'}], true);
// All parameters (many parameters and accept header)
sql("INSERT INTO users (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true, 'text/csv');
// Named parameters via Template Literals
sql(`INSERT INTO users (name) VALUES (${u});`, [{u: 'Michael'}]);
EndbWebSocket
Endatabas client for the WebSocket API
Kind: global class
- EndbWebSocket
new EndbWebSocket([url], [opt])
.close()
.sql(q, [p], [m])
⇒Promise.<Array>
new EndbWebSocket([url], [opt])
Create an EndbWebSocket object (Endatabas client for the WebSocket API)
Param | Type | Default | Description |
---|---|---|---|
[url] | string | "ws://localhost:3803/sql" | WebSocket URL to the Endatabas /sql API |
[opt] | Object | WebSocket options |
|
[opt.ws] | string | WebSocket implementation |
|
[opt.username] | string | username for Basic Auth |
|
[opt.password] | string | password for Basic Auth |
endbWebSocket.close()
Close the WebSocket connection
Kind: instance method of EndbWebSocket
endbWebSocket.sql(q, [p], [m])
⇒ Promise.<Array>
Execute a SQL statement over a WebSocket with LD-JSON
Kind: instance method of EndbWebSocket
Returns: Promise.<Array>
- - Array of documents
Param | Type | Description |
---|---|---|
q | string | SQL query as string or Template Literal |
[p] | Array | Object | Positional parameters, named parameters, or an array of either |
[m] | boolean | many parameters flag |
Example
// Simple query
sql("SELECT * FROM users;");
// Positional parameters
sql("INSERT INTO users (date, name) VALUES (?, ?);", [new Date(), 'Aaron']);
// Named parameters
sql("INSERT INTO users {date: :d, name: :n};", {d: new Date(), n: 'Aaron'});
// Many positional parameters (batches)
sql("INSERT INTO users (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true);
// Many named parameters (batches)
sql("INSERT INTO users {name: :n};", [{n: 'Judy'}, {n: 'Addis'}], true);
// All parameters (many parameters and accept header)
sql("INSERT INTO users (name) VALUES (?);", [['Aaron'], ['Kurt'], ['Cindy']], true, 'text/csv');
// Named parameters via Template Literals
sql(`INSERT INTO users (name) VALUES (${u});`, [{u: 'Michael'}]);