libsql-stateless-easy
Advanced tools
Comparing version 1.4.8 to 1.5.0
@@ -90,2 +90,4 @@ "use strict"; | ||
* | ||
* NOTE: libsql-stateless-easy implements this function using `batch` under the hood instead of the `serial` endpoint. | ||
* | ||
* The statements are executed sequentially on a new logical database connection. If a statement fails, | ||
@@ -125,5 +127,6 @@ * further statements are not executed and this method throws an error. All results from the statements | ||
db_url: conf.url, | ||
authToken: conf.authToken | ||
authToken: conf.authToken, | ||
intMode: conf.intMode | ||
}); | ||
} | ||
exports.createClient = createClient; |
@@ -20,3 +20,3 @@ "use strict"; | ||
throw new errors_js_1.LibsqlError('This is a HTTP client and only supports "https:" and "http:" URLs, ' + | ||
`got ${JSON.stringify(_url.protocol + ":")}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
`got ${JSON.stringify(_url.protocol)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
@@ -27,3 +27,3 @@ async function libsqlExecute(conf, stmt) { | ||
if (res.isOk) | ||
return (0, parsers_js_1.libsqlStatementResParser)(res.val); | ||
return (0, parsers_js_1.libsqlStatementResParser)(res.val, conf.intMode); | ||
else { | ||
@@ -41,3 +41,3 @@ if (res.err.kind === "LIBSQL_SERVER_ERROR") | ||
if (res.isOk) | ||
return (0, parsers_js_1.libsqlBatchStreamResParser)(res.val); | ||
return (0, parsers_js_1.libsqlBatchStreamResParser)(res.val, conf.intMode); | ||
else { | ||
@@ -61,3 +61,3 @@ if (res.err.kind === "LIBSQL_SERVER_ERROR") | ||
if (res.isOk) | ||
return (0, parsers_js_1.libsqlTransactionBatchStreamResParser)(res.val); | ||
return (0, parsers_js_1.libsqlTransactionBatchStreamResParser)(res.val, conf.intMode); | ||
else { | ||
@@ -64,0 +64,0 @@ if (res.err.kind === "LIBSQL_SERVER_ERROR") |
@@ -7,15 +7,21 @@ "use strict"; | ||
//======================================================== | ||
function libsqlValueParser(value) { | ||
if (value.type === "null") | ||
return null; | ||
if (value.type === "integer") | ||
return BigInt(value.value); | ||
if (value.type === "float") | ||
return Number(value.value); | ||
if (value.type === "text") | ||
return value.value; | ||
if (value.type === "blob") | ||
return js_base64_1.Base64.toUint8Array(value.base64); | ||
throw new errors_js_1.ProtoError("Invalid data type from server. Cannot parse."); | ||
function parseLibsqlInt(number, intMode) { | ||
switch (intMode) { | ||
case ("number"): return +number; | ||
case ("string"): return number; | ||
case ("bigint"): return BigInt(number); | ||
default: throw new errors_js_1.MisuseError(`Invalid value for "intMode".`); | ||
} | ||
} | ||
//======================================================== | ||
function libsqlValueParser(value, intMode) { | ||
switch (value.type) { | ||
case ("null"): return null; | ||
case ("integer"): return parseLibsqlInt(value.value, intMode); | ||
case ("float"): return Number(value.value); | ||
case ("text"): return value.value; | ||
case ("blob"): return js_base64_1.Base64.toUint8Array(value.base64); | ||
default: throw new errors_js_1.ProtoError("Invalid data type from server. Cannot parse."); | ||
} | ||
} | ||
exports.libsqlValueParser = libsqlValueParser; | ||
@@ -42,3 +48,3 @@ //======================================================== | ||
//======================================================== | ||
function libsqlStatementResParser(res) { | ||
function libsqlStatementResParser(res, intMode) { | ||
let Rows = []; | ||
@@ -49,3 +55,3 @@ for (let i = 0; i < res.rows.length; i++) { | ||
for (let j = 0; j < res.rows[i].length; j++) { | ||
const value = libsqlValueParser(res.rows[i][j]); | ||
const value = libsqlValueParser(res.rows[i][j], intMode); | ||
Object.defineProperty(row, j, { value }); | ||
@@ -73,6 +79,6 @@ const colName = res.cols[j].name; | ||
//======================================================== | ||
function libsqlBatchStreamResParser(res) { | ||
function libsqlBatchStreamResParser(res, intMode) { | ||
return res.step_results.map((r, i) => { | ||
if (r) | ||
return libsqlStatementResParser(r); | ||
return libsqlStatementResParser(r, intMode); | ||
else if (res.step_errors[i]) | ||
@@ -86,6 +92,6 @@ throw new errors_js_1.ResponseError(res.step_errors[i]?.message || "", res.step_errors[i]); | ||
//======================================================== | ||
function libsqlTransactionBatchStreamResParser(res) { | ||
const resResArr = libsqlBatchStreamResParser(res); | ||
function libsqlTransactionBatchStreamResParser(res, intMode) { | ||
const resResArr = libsqlBatchStreamResParser(res, intMode); | ||
return resResArr.slice(1, resResArr.length - 2).filter(r => r !== null); | ||
} | ||
exports.libsqlTransactionBatchStreamResParser = libsqlTransactionBatchStreamResParser; |
@@ -1,3 +0,3 @@ | ||
import { libsqlBatchReqStepExecCond, libsqlConfig } from "libsql-stateless"; | ||
import { TransactionMode, rawSQLStatement } from "./types.js"; | ||
import { libsqlBatchReqStepExecCond } from "libsql-stateless"; | ||
import { TransactionMode, rawSQLStatement, libsqlConfig, intMode } from "./types.js"; | ||
import { ____Transaction } from "./extras.js"; | ||
@@ -75,2 +75,4 @@ declare class libsqlClient { | ||
* | ||
* NOTE: libsql-stateless-easy implements this function using `batch` under the hood instead of the `serial` endpoint. | ||
* | ||
* The statements are executed sequentially on a new logical database connection. If a statement fails, | ||
@@ -102,3 +104,4 @@ * further statements are not executed and this method throws an error. All results from the statements | ||
authToken?: string; | ||
intMode?: intMode; | ||
}): libsqlClient; | ||
export {}; |
@@ -87,2 +87,4 @@ import { libsqlBatch, libsqlBatchTransaction, libsqlExecute, libsqlExecuteMultiple, libsqlServerCompatCheck } from "./functions.js"; | ||
* | ||
* NOTE: libsql-stateless-easy implements this function using `batch` under the hood instead of the `serial` endpoint. | ||
* | ||
* The statements are executed sequentially on a new logical database connection. If a statement fails, | ||
@@ -122,4 +124,5 @@ * further statements are not executed and this method throws an error. All results from the statements | ||
db_url: conf.url, | ||
authToken: conf.authToken | ||
authToken: conf.authToken, | ||
intMode: conf.intMode | ||
}); | ||
} |
@@ -1,3 +0,3 @@ | ||
import { libsqlConfig, libsqlBatchReqStepExecCond } from "libsql-stateless"; | ||
import { ResultSet, TransactionMode, rawSQLStatement } from "./types.js"; | ||
import { libsqlBatchReqStepExecCond } from "libsql-stateless"; | ||
import { ResultSet, TransactionMode, rawSQLStatement, libsqlConfig } from "./types.js"; | ||
export declare function libsqlExecute(conf: libsqlConfig, stmt: rawSQLStatement): Promise<ResultSet>; | ||
@@ -4,0 +4,0 @@ export declare function libsqlBatch(conf: libsqlConfig, steps: Array<rawSQLStatement>, step_conditions: Array<libsqlBatchReqStepExecCond | null | undefined>): Promise<Array<ResultSet | null>>; |
@@ -17,3 +17,3 @@ import { libsqlExecute as LIBlibsqlExecute, libsqlBatch as LIBlibsqlBatch, libsqlServerCompatCheck as LIBlibsqlServerCompatCheck } from "libsql-stateless"; | ||
throw new LibsqlError('This is a HTTP client and only supports "https:" and "http:" URLs, ' + | ||
`got ${JSON.stringify(_url.protocol + ":")}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
`got ${JSON.stringify(_url.protocol)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
@@ -24,3 +24,3 @@ export async function libsqlExecute(conf, stmt) { | ||
if (res.isOk) | ||
return libsqlStatementResParser(res.val); | ||
return libsqlStatementResParser(res.val, conf.intMode); | ||
else { | ||
@@ -37,3 +37,3 @@ if (res.err.kind === "LIBSQL_SERVER_ERROR") | ||
if (res.isOk) | ||
return libsqlBatchStreamResParser(res.val); | ||
return libsqlBatchStreamResParser(res.val, conf.intMode); | ||
else { | ||
@@ -55,3 +55,3 @@ if (res.err.kind === "LIBSQL_SERVER_ERROR") | ||
if (res.isOk) | ||
return libsqlTransactionBatchStreamResParser(res.val); | ||
return libsqlTransactionBatchStreamResParser(res.val, conf.intMode); | ||
else { | ||
@@ -58,0 +58,0 @@ if (res.err.kind === "LIBSQL_SERVER_ERROR") |
import { libsqlBatchStreamResOkData, libsqlSQLValue, libsqlStatementResOkData } from "libsql-stateless"; | ||
import { ResultSet, rawValue } from "./types.js"; | ||
export declare function libsqlValueParser(value: libsqlSQLValue): rawValue; | ||
export declare function libsqlStatementResParser(res: libsqlStatementResOkData): ResultSet; | ||
export declare function libsqlBatchStreamResParser(res: libsqlBatchStreamResOkData): Array<ResultSet | null>; | ||
export declare function libsqlTransactionBatchStreamResParser(res: libsqlBatchStreamResOkData): Array<ResultSet>; | ||
import { ResultSet, rawValue, intMode } from "./types.js"; | ||
export declare function libsqlValueParser(value: libsqlSQLValue, intMode?: intMode): rawValue; | ||
export declare function libsqlStatementResParser(res: libsqlStatementResOkData, intMode?: intMode): ResultSet; | ||
export declare function libsqlBatchStreamResParser(res: libsqlBatchStreamResOkData, intMode?: intMode): Array<ResultSet | null>; | ||
export declare function libsqlTransactionBatchStreamResParser(res: libsqlBatchStreamResOkData, intMode?: intMode): Array<ResultSet>; |
import { Base64 } from "js-base64"; | ||
import { ProtoError, ResponseError } from "./errors.js"; | ||
import { MisuseError, ProtoError, ResponseError } from "./errors.js"; | ||
//======================================================== | ||
export function libsqlValueParser(value) { | ||
if (value.type === "null") | ||
return null; | ||
if (value.type === "integer") | ||
return BigInt(value.value); | ||
if (value.type === "float") | ||
return Number(value.value); | ||
if (value.type === "text") | ||
return value.value; | ||
if (value.type === "blob") | ||
return Base64.toUint8Array(value.base64); | ||
throw new ProtoError("Invalid data type from server. Cannot parse."); | ||
function parseLibsqlInt(number, intMode) { | ||
switch (intMode) { | ||
case ("number"): return +number; | ||
case ("string"): return number; | ||
case ("bigint"): return BigInt(number); | ||
default: throw new MisuseError(`Invalid value for "intMode".`); | ||
} | ||
} | ||
//======================================================== | ||
export function libsqlValueParser(value, intMode) { | ||
switch (value.type) { | ||
case ("null"): return null; | ||
case ("integer"): return parseLibsqlInt(value.value, intMode); | ||
case ("float"): return Number(value.value); | ||
case ("text"): return value.value; | ||
case ("blob"): return Base64.toUint8Array(value.base64); | ||
default: throw new ProtoError("Invalid data type from server. Cannot parse."); | ||
} | ||
} | ||
//======================================================== | ||
//from hrana-client-ts/src/result.ts | ||
@@ -37,3 +43,3 @@ // function rowFromRawValue( | ||
//======================================================== | ||
export function libsqlStatementResParser(res) { | ||
export function libsqlStatementResParser(res, intMode) { | ||
let Rows = []; | ||
@@ -44,3 +50,3 @@ for (let i = 0; i < res.rows.length; i++) { | ||
for (let j = 0; j < res.rows[i].length; j++) { | ||
const value = libsqlValueParser(res.rows[i][j]); | ||
const value = libsqlValueParser(res.rows[i][j], intMode); | ||
Object.defineProperty(row, j, { value }); | ||
@@ -67,6 +73,6 @@ const colName = res.cols[j].name; | ||
//======================================================== | ||
export function libsqlBatchStreamResParser(res) { | ||
export function libsqlBatchStreamResParser(res, intMode) { | ||
return res.step_results.map((r, i) => { | ||
if (r) | ||
return libsqlStatementResParser(r); | ||
return libsqlStatementResParser(r, intMode); | ||
else if (res.step_errors[i]) | ||
@@ -79,5 +85,5 @@ throw new ResponseError(res.step_errors[i]?.message || "", res.step_errors[i]); | ||
//======================================================== | ||
export function libsqlTransactionBatchStreamResParser(res) { | ||
const resResArr = libsqlBatchStreamResParser(res); | ||
export function libsqlTransactionBatchStreamResParser(res, intMode) { | ||
const resResArr = libsqlBatchStreamResParser(res, intMode); | ||
return resResArr.slice(1, resResArr.length - 2).filter(r => r !== null); | ||
} |
@@ -0,2 +1,4 @@ | ||
import { libsqlConfig as LIBlibsqlConfig } from "libsql-stateless"; | ||
export type rawValue = null | bigint | number | string | ArrayBuffer; | ||
export type intMode = "bigint" | "number" | "string"; | ||
export type rawSQLStatement = string | { | ||
@@ -7,2 +9,5 @@ sql: string; | ||
}; | ||
export interface libsqlConfig extends LIBlibsqlConfig { | ||
intMode?: intMode; | ||
} | ||
/** Row returned from an SQL statement. | ||
@@ -9,0 +14,0 @@ * |
{ | ||
"name": "libsql-stateless-easy", | ||
"version": "1.4.8", | ||
"version": "1.5.0", | ||
"homepage": "https://github.com/DaBigBlob/libsql-stateless-easy#readme", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -6,9 +6,9 @@ # libsql-stateless-easy | ||
- ✅ **Is built for:** Quick stateless query execution. (Mainly for serverless and edge functions.) | ||
- ⚠️ Supports everything in `@libsql/client` **except (explicit) `transactions` and local or in-memory DBs.** | ||
- ✅ **The API provided by `libsql-stateless-easy` is simple** and (almost) exactly the same as `@libsql/client`. | ||
- ⚠️ `libsql-stateless-easy` comes with the cost of (computational and memory) overheads potentially unneeded by you. But is still very very very slim compared to `@libsql/client`. | ||
- ✅ Supports everything in `@libsql/client/web` **except (explicit) `transactions`. | ||
- ✅ **The API provided by `libsql-stateless-easy` is simple** and exactly the same as `@libsql/client/web`. | ||
- ⚠️ `libsql-stateless-easy` comes with the cost of (computational and memory) overheads potentially unneeded by you. But is still very very very slim compared to `@libsql/client/web`. | ||
<br> | ||
**For much better performance, consider using [`libsql-stateless`](https://github.com/DaBigBlob/libsql-stateless) instead**: it, however, has a pretty raw and explicit API unappreciated by many developers. | ||
**For better performance lower resource use, consider using [`libsql-stateless`](https://github.com/DaBigBlob/libsql-stateless) instead**: it, however, has a pretty raw and explicit API unappreciated by many developers. | ||
@@ -15,0 +15,0 @@ # Installation |
61382
1348