@libsql/hrana-client
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -113,3 +113,3 @@ "use strict"; | ||
else if (stepResult !== null) { | ||
outputCallback(fromProto(stepResult)); | ||
outputCallback(fromProto(stepResult, this.#batch._stream.intMode)); | ||
} | ||
@@ -116,0 +116,0 @@ else { |
@@ -11,4 +11,12 @@ "use strict"; | ||
/** @private */ | ||
constructor() { } | ||
constructor() { | ||
this.intMode = "number"; | ||
} | ||
/** Representation of integers returned from the database. See {@link IntMode}. | ||
* | ||
* This value is inherited by {@link Stream} objects created with {@link openStream}, but you can | ||
* override the integer mode for every stream by setting {@link Stream.intMode} on the stream. | ||
*/ | ||
intMode; | ||
} | ||
exports.Client = Client; |
@@ -22,3 +22,3 @@ "use strict"; | ||
constructor(client, baseUrl, jwt) { | ||
super(); | ||
super(client.intMode); | ||
this.#client = client; | ||
@@ -25,0 +25,0 @@ this.#baseUrl = baseUrl.toString(); |
@@ -15,13 +15,13 @@ "use strict"; | ||
exports.stmtResultFromProto = stmtResultFromProto; | ||
function rowsResultFromProto(result) { | ||
function rowsResultFromProto(result, intMode) { | ||
const stmtResult = stmtResultFromProto(result); | ||
const rows = result["rows"].map(row => rowFromProto(stmtResult.columnNames, row)); | ||
const rows = result["rows"].map(row => rowFromProto(stmtResult.columnNames, row, intMode)); | ||
return { ...stmtResult, rows }; | ||
} | ||
exports.rowsResultFromProto = rowsResultFromProto; | ||
function rowResultFromProto(result) { | ||
function rowResultFromProto(result, intMode) { | ||
const stmtResult = stmtResultFromProto(result); | ||
let row; | ||
if (result.rows.length > 0) { | ||
row = rowFromProto(stmtResult.columnNames, result.rows[0]); | ||
row = rowFromProto(stmtResult.columnNames, result.rows[0], intMode); | ||
} | ||
@@ -31,7 +31,7 @@ return { ...stmtResult, row }; | ||
exports.rowResultFromProto = rowResultFromProto; | ||
function valueResultFromProto(result) { | ||
function valueResultFromProto(result, intMode) { | ||
const stmtResult = stmtResultFromProto(result); | ||
let value; | ||
if (result.rows.length > 0 && stmtResult.columnNames.length > 0) { | ||
value = (0, value_js_1.valueFromProto)(result.rows[0][0]); | ||
value = (0, value_js_1.valueFromProto)(result.rows[0][0], intMode); | ||
} | ||
@@ -41,3 +41,3 @@ return { ...stmtResult, value }; | ||
exports.valueResultFromProto = valueResultFromProto; | ||
function rowFromProto(colNames, values) { | ||
function rowFromProto(colNames, values, intMode) { | ||
const row = {}; | ||
@@ -47,3 +47,3 @@ // make sure that the "length" property is not enumerable | ||
for (let i = 0; i < values.length; ++i) { | ||
const value = (0, value_js_1.valueFromProto)(values[i]); | ||
const value = (0, value_js_1.valueFromProto)(values[i], intMode); | ||
Object.defineProperty(row, i, { value }); | ||
@@ -50,0 +50,0 @@ const colName = colNames[i]; |
@@ -12,3 +12,5 @@ "use strict"; | ||
/** @private */ | ||
constructor() { } | ||
constructor(intMode) { | ||
this.intMode = intMode; | ||
} | ||
/** Execute a statement and return rows. */ | ||
@@ -32,3 +34,3 @@ query(stmt) { | ||
const stmt = (0, stmt_js_1.stmtToProto)(this._sqlOwner(), inStmt, wantRows); | ||
return this._execute(stmt).then(fromProto); | ||
return this._execute(stmt).then((r) => fromProto(r, this.intMode)); | ||
} | ||
@@ -50,3 +52,8 @@ /** Return a builder for creating and executing a batch. */ | ||
} | ||
/** Representation of integers returned from the database. See {@link IntMode}. | ||
* | ||
* This value affects the results of all operations on this stream. | ||
*/ | ||
intMode; | ||
} | ||
exports.Stream = Stream; |
@@ -21,3 +21,3 @@ "use strict"; | ||
if (value < minInteger || value > maxInteger) { | ||
throw new RangeError("bigint is too large to be represented as a 64-bit integer and passed as argument"); | ||
throw new RangeError("This bigint value is too large to be represented as a 64-bit integer and passed as argument"); | ||
} | ||
@@ -49,3 +49,3 @@ return { "type": "integer", "value": "" + value }; | ||
exports.protoNull = { "type": "null" }; | ||
function valueFromProto(value) { | ||
function valueFromProto(value, intMode) { | ||
if (value["type"] === "null") { | ||
@@ -55,9 +55,18 @@ return null; | ||
else if (value["type"] === "integer") { | ||
// TODO: add an option to return integers as bigints | ||
const int = parseInt(value["value"], 10); | ||
if (!Number.isSafeInteger(int)) { | ||
throw new RangeError(`Received integer ${value["value"]} which cannot be ` + | ||
"safely represented as a JavaScript number"); | ||
if (intMode === "number") { | ||
const int = parseInt(value["value"], 10); | ||
if (!Number.isSafeInteger(int)) { | ||
throw new RangeError("Received integer which cannot be safely represented as a JavaScript number"); | ||
} | ||
return int; | ||
} | ||
return int; | ||
else if (intMode === "bigint") { | ||
return BigInt(value["value"]); | ||
} | ||
else if (intMode === "string") { | ||
return "" + value["value"]; | ||
} | ||
else { | ||
throw new Error("Invalid value for IntMode"); | ||
} | ||
} | ||
@@ -64,0 +73,0 @@ else if (value["type"] === "float") { |
@@ -11,3 +11,3 @@ "use strict"; | ||
constructor(client, state) { | ||
super(); | ||
super(client.intMode); | ||
this.#client = client; | ||
@@ -14,0 +14,0 @@ this.#state = state; |
@@ -109,3 +109,3 @@ import { ProtoError } from "./errors.js"; | ||
else if (stepResult !== null) { | ||
outputCallback(fromProto(stepResult)); | ||
outputCallback(fromProto(stepResult, this.#batch._stream.intMode)); | ||
} | ||
@@ -112,0 +112,0 @@ else { |
import type { Stream } from "./stream.js"; | ||
import type { IntMode } from "./value.js"; | ||
export type ProtocolVersion = 1 | 2; | ||
@@ -16,2 +17,8 @@ export declare const protocolVersions: Map<string, ProtocolVersion>; | ||
abstract get closed(): boolean; | ||
/** Representation of integers returned from the database. See {@link IntMode}. | ||
* | ||
* This value is inherited by {@link Stream} objects created with {@link openStream}, but you can | ||
* override the integer mode for every stream by setting {@link Stream.intMode} on the stream. | ||
*/ | ||
intMode: IntMode; | ||
} |
@@ -8,3 +8,11 @@ export const protocolVersions = new Map([ | ||
/** @private */ | ||
constructor() { } | ||
constructor() { | ||
this.intMode = "number"; | ||
} | ||
/** Representation of integers returned from the database. See {@link IntMode}. | ||
* | ||
* This value is inherited by {@link Stream} objects created with {@link openStream}, but you can | ||
* override the integer mode for every stream by setting {@link Stream.intMode} on the stream. | ||
*/ | ||
intMode; | ||
} |
@@ -19,3 +19,3 @@ import { fetch, Request, Headers } from "@libsql/isomorphic-fetch"; | ||
constructor(client, baseUrl, jwt) { | ||
super(); | ||
super(client.intMode); | ||
this.#client = client; | ||
@@ -22,0 +22,0 @@ this.#baseUrl = baseUrl.toString(); |
@@ -17,3 +17,3 @@ /// <reference types="node" /> | ||
export { Stream } from "./stream.js"; | ||
export type { Value, InValue } from "./value.js"; | ||
export type { Value, InValue, IntMode } from "./value.js"; | ||
export { HttpClient } from "./http/client.js"; | ||
@@ -20,0 +20,0 @@ export { HttpStream } from "./http/stream.js"; |
import { ResponseError } from "./errors.js"; | ||
import type { Value } from "./value.js"; | ||
import type { Value, IntMode } from "./value.js"; | ||
import type * as proto from "./proto.js"; | ||
@@ -40,5 +40,5 @@ /** Result of executing a database statement. */ | ||
export declare function stmtResultFromProto(result: proto.StmtResult): StmtResult; | ||
export declare function rowsResultFromProto(result: proto.StmtResult): RowsResult; | ||
export declare function rowResultFromProto(result: proto.StmtResult): RowResult; | ||
export declare function valueResultFromProto(result: proto.StmtResult): ValueResult; | ||
export declare function rowsResultFromProto(result: proto.StmtResult, intMode: IntMode): RowsResult; | ||
export declare function rowResultFromProto(result: proto.StmtResult, intMode: IntMode): RowResult; | ||
export declare function valueResultFromProto(result: proto.StmtResult, intMode: IntMode): ValueResult; | ||
export declare function errorFromProto(error: proto.Error): ResponseError; |
@@ -11,24 +11,24 @@ import { ResponseError } from "./errors.js"; | ||
} | ||
export function rowsResultFromProto(result) { | ||
export function rowsResultFromProto(result, intMode) { | ||
const stmtResult = stmtResultFromProto(result); | ||
const rows = result["rows"].map(row => rowFromProto(stmtResult.columnNames, row)); | ||
const rows = result["rows"].map(row => rowFromProto(stmtResult.columnNames, row, intMode)); | ||
return { ...stmtResult, rows }; | ||
} | ||
export function rowResultFromProto(result) { | ||
export function rowResultFromProto(result, intMode) { | ||
const stmtResult = stmtResultFromProto(result); | ||
let row; | ||
if (result.rows.length > 0) { | ||
row = rowFromProto(stmtResult.columnNames, result.rows[0]); | ||
row = rowFromProto(stmtResult.columnNames, result.rows[0], intMode); | ||
} | ||
return { ...stmtResult, row }; | ||
} | ||
export function valueResultFromProto(result) { | ||
export function valueResultFromProto(result, intMode) { | ||
const stmtResult = stmtResultFromProto(result); | ||
let value; | ||
if (result.rows.length > 0 && stmtResult.columnNames.length > 0) { | ||
value = valueFromProto(result.rows[0][0]); | ||
value = valueFromProto(result.rows[0][0], intMode); | ||
} | ||
return { ...stmtResult, value }; | ||
} | ||
function rowFromProto(colNames, values) { | ||
function rowFromProto(colNames, values, intMode) { | ||
const row = {}; | ||
@@ -38,3 +38,3 @@ // make sure that the "length" property is not enumerable | ||
for (let i = 0; i < values.length; ++i) { | ||
const value = valueFromProto(values[i]); | ||
const value = valueFromProto(values[i], intMode); | ||
Object.defineProperty(row, i, { value }); | ||
@@ -41,0 +41,0 @@ const colName = colNames[i]; |
@@ -7,2 +7,3 @@ import { Batch } from "./batch.js"; | ||
import type { InStmt } from "./stmt.js"; | ||
import type { IntMode } from "./value.js"; | ||
/** A stream for executing SQL statements (a "database connection"). */ | ||
@@ -12,3 +13,3 @@ export declare abstract class Stream { | ||
/** @private */ | ||
constructor(); | ||
constructor(intMode: IntMode); | ||
/** @private*/ | ||
@@ -43,2 +44,7 @@ abstract _sqlOwner(): SqlOwner; | ||
abstract get closed(): boolean; | ||
/** Representation of integers returned from the database. See {@link IntMode}. | ||
* | ||
* This value affects the results of all operations on this stream. | ||
*/ | ||
intMode: IntMode; | ||
} |
@@ -9,3 +9,5 @@ import { Batch } from "./batch.js"; | ||
/** @private */ | ||
constructor() { } | ||
constructor(intMode) { | ||
this.intMode = intMode; | ||
} | ||
/** Execute a statement and return rows. */ | ||
@@ -29,3 +31,3 @@ query(stmt) { | ||
const stmt = stmtToProto(this._sqlOwner(), inStmt, wantRows); | ||
return this._execute(stmt).then(fromProto); | ||
return this._execute(stmt).then((r) => fromProto(r, this.intMode)); | ||
} | ||
@@ -47,2 +49,7 @@ /** Return a builder for creating and executing a batch. */ | ||
} | ||
/** Representation of integers returned from the database. See {@link IntMode}. | ||
* | ||
* This value affects the results of all operations on this stream. | ||
*/ | ||
intMode; | ||
} |
@@ -6,4 +6,14 @@ import type * as proto from "./proto.js"; | ||
export type InValue = Value | boolean | Uint8Array | Date | RegExp | object; | ||
/** Possible representations of SQLite integers in JavaScript: | ||
* | ||
* - `"number"` (default): returns SQLite integers as JavaScript `number`-s (double precision floats). | ||
* `number` cannot precisely represent integers larger than 2^53-1 in absolute value, so attempting to read | ||
* larger integers will throw a `RangeError`. | ||
* - `"bigint"`: returns SQLite integers as JavaScript `bigint`-s (arbitrary precision integers). Bigints can | ||
* precisely represent all SQLite integers. | ||
* - `"string"`: returns SQLite integers as strings. | ||
*/ | ||
export type IntMode = "number" | "bigint" | "string"; | ||
export declare function valueToProto(value: InValue): proto.Value; | ||
export declare const protoNull: proto.Value; | ||
export declare function valueFromProto(value: proto.Value): Value; | ||
export declare function valueFromProto(value: proto.Value, intMode: IntMode): Value; |
@@ -18,3 +18,3 @@ import { Base64 } from "js-base64"; | ||
if (value < minInteger || value > maxInteger) { | ||
throw new RangeError("bigint is too large to be represented as a 64-bit integer and passed as argument"); | ||
throw new RangeError("This bigint value is too large to be represented as a 64-bit integer and passed as argument"); | ||
} | ||
@@ -45,3 +45,3 @@ return { "type": "integer", "value": "" + value }; | ||
export const protoNull = { "type": "null" }; | ||
export function valueFromProto(value) { | ||
export function valueFromProto(value, intMode) { | ||
if (value["type"] === "null") { | ||
@@ -51,9 +51,18 @@ return null; | ||
else if (value["type"] === "integer") { | ||
// TODO: add an option to return integers as bigints | ||
const int = parseInt(value["value"], 10); | ||
if (!Number.isSafeInteger(int)) { | ||
throw new RangeError(`Received integer ${value["value"]} which cannot be ` + | ||
"safely represented as a JavaScript number"); | ||
if (intMode === "number") { | ||
const int = parseInt(value["value"], 10); | ||
if (!Number.isSafeInteger(int)) { | ||
throw new RangeError("Received integer which cannot be safely represented as a JavaScript number"); | ||
} | ||
return int; | ||
} | ||
return int; | ||
else if (intMode === "bigint") { | ||
return BigInt(value["value"]); | ||
} | ||
else if (intMode === "string") { | ||
return "" + value["value"]; | ||
} | ||
else { | ||
throw new Error("Invalid value for IntMode"); | ||
} | ||
} | ||
@@ -60,0 +69,0 @@ else if (value["type"] === "float") { |
@@ -8,3 +8,3 @@ import { ClientError } from "../errors.js"; | ||
constructor(client, state) { | ||
super(); | ||
super(client.intMode); | ||
this.#client = client; | ||
@@ -11,0 +11,0 @@ this.#state = state; |
{ | ||
"name": "@libsql/hrana-client", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"keywords": [ | ||
@@ -10,3 +10,3 @@ "hrana", | ||
], | ||
"description": "Hrana client for connecting to sqld over a WebSocket", | ||
"description": "Hrana client for connecting to sqld over HTTP or WebSockets", | ||
"repository": { | ||
@@ -13,0 +13,0 @@ "type": "git", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136621
3640