Skip to content

Commit

Permalink
core api
Browse files Browse the repository at this point in the history
  • Loading branch information
DaBigBlob committed Sep 7, 2024
1 parent 995d02d commit fa59ea2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
5 changes: 5 additions & 0 deletions get_official_api
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

UNIXMILI=$(node -e "console.log(Date.now())")

curl -s "https://raw.githubusercontent.com/tursodatabase/libsql-client-ts/main/packages/libsql-core/src/api.ts" > "./src/api-${UNIXMILI}.ts"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
"edge"
],
"dependencies": {
"libsql-stateless": "^2.9.0"
"libsql-stateless": "^2.9.1"
}
}
21 changes: 5 additions & 16 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
libsqlExecute as LIBlibsqlExecute,
libsqlBatch as LIBlibsqlBatch,
libsqlServerCompatCheck as LIBlibsqlServerCompatCheck,
type libsqlConfig as LIBlibsqlConfig,
type libsqlBatchReqStepExecCond,
type libsqlBatchReqStep,
type libsqlError as LIBlibsqlError
Expand All @@ -12,14 +11,6 @@ import { libsqlBatchReqStepExecCondBuilder, libsqlBatchReqStepsBuilder, libsqlSt
import { libsqlBatchStreamResParser, libsqlStatementResParser, libsqlTransactionBatchStreamResParser } from "./parsers.js";
import { HttpServerError, ResponseError } from "./errors.js";

function confTranslate(conf: libsqlConfig): LIBlibsqlConfig {
return {
db_url: conf.url,
authToken: conf.authToken,
fetch: conf.fetch
}
}

function errorTranslate(err: LIBlibsqlError) {
if (err.kind==="LIBSQL_SERVER_ERROR")
return new HttpServerError(
Expand All @@ -34,7 +25,7 @@ function errorTranslate(err: LIBlibsqlError) {
}

export async function libsqlExecute(conf: libsqlConfig, stmt_or_sql: rawSQL|rawSQLStatement, or_args?: rawSQLArgs, or_want_rows?: boolean): Promise<ResultSet> {
const res = await LIBlibsqlExecute(confTranslate(conf), libsqlStatementBuilder(stmt_or_sql, or_args, or_want_rows));
const res = await LIBlibsqlExecute(conf, libsqlStatementBuilder(stmt_or_sql, or_args, or_want_rows));

if (res.isOk) return libsqlStatementResParser(res.val, conf.intMode);
else throw errorTranslate(res.err);
Expand All @@ -45,24 +36,22 @@ export async function libsqlBatch(
steps: Array<rawSQL|rawSQLStatement>,
step_conditions: Array<libsqlBatchReqStepExecCond|null|undefined>
): Promise<Array<ResultSet|null>> {
const res = await LIBlibsqlBatch(confTranslate(conf), libsqlBatchReqStepsBuilder(steps, step_conditions));
const res = await LIBlibsqlBatch(conf, libsqlBatchReqStepsBuilder(steps, step_conditions));

if (res.isOk) return libsqlBatchStreamResParser(res.val, conf.intMode);
else throw errorTranslate(res.err);
}

export async function libsqlServerCompatCheck(conf: libsqlConfig) {
return (await LIBlibsqlServerCompatCheck(
confTranslate(conf)
)).isOk;
return (await LIBlibsqlServerCompatCheck(conf)).isOk;
}

export async function libsqlBatchTransaction(
conf: libsqlConfig,
steps: Array<rawSQL|rawSQLStatement>,
mode: TransactionMode="deferred"
): Promise<Array<ResultSet>> {
const res = await LIBlibsqlBatch(confTranslate(conf), libsqlTransactionBatchReqStepsBuilder(steps, mode));
const res = await LIBlibsqlBatch(conf, libsqlTransactionBatchReqStepsBuilder(steps, mode));

if (res.isOk) return libsqlTransactionBatchStreamResParser(res.val, conf.intMode);
else throw errorTranslate(res.err);
Expand All @@ -75,6 +64,6 @@ export async function libsqlExecuteMultiple(conf: libsqlConfig, sql: string): Pr
}});
sqlArr[0].condition = undefined; //elm 0's ok index is set to -1; removing that

const res = await LIBlibsqlBatch(confTranslate(conf), sqlArr);
const res = await LIBlibsqlBatch(conf, sqlArr);
if (!res.isOk) throw errorTranslate(res.err);
}

0 comments on commit fa59ea2

Please sign in to comment.