Skip to content

libsqlTypes

Person edited this page Nov 11, 2023 · 21 revisions

libsqlResult

Every function in this library returns a libsqlResult<T, R> type.

libsqlResult<T, R> is either:

{
    isOk: true,
    val: T
}

or:

{
    isOk: false,
    err: R
}

For example, the function awaitlibsqlExecute returns libsqlResult<libsqlStatementResOkData, libsqlStreamResErrData|libsqlPipelineResErr>, which is either:

{
    isOk: true,
    val: libsqlStatementResOkData
}

or:

{
    isOk: false,
    err: libsqlStreamResErrData|libsqlPipelineResErr
}

Benefits of Result Type

  • No need of ugly try { } catch { }.
  • Elegant error handling:
//you can do something like
const res = await libsqlExecute(conf, {sql: "SELECT * FROM mad_cats;"});

if (res.isOk) {
    //now res.val is guaranteed by typescript [js users, sorry]
    console.log(res.val.rows); //the rows returned
    console.log(res.val.affected_row_count) //affected rows
    //...
} else {
    //now res.err is guaranteed by typescript
    console.log(res.err.message); //error message
    doSomething(res.err.code) //do something with the error code
    //...
}

Reference: libsqlExecute, libsqlStatementResOkData, libsqlStreamResErrData, libsqlPipelineResErr




libsqlConfig

The connection config to your libsql-server (DB)

Structure

{
    db_url: string,
    authToken?: string
}



libsqlPipelineReq

Structure

{
    baton: string | null,
    requests: Array<libsqlCloseStreamReq|libsqlExecuteStreamReq|libsqlBatchStreamReq>
}

Reference: libsqlCloseStreamReq, libsqlExecuteStreamReq, libsqlBatchStreamReq




libsqlPipelineResOk

Structure

{
    baton: string | null,
    base_url: string | null,
    results: Array<libsqlStreamResOk|libsqlStreamResErr>
}

Reference: libsqlStreamResOk, libsqlStreamResErr




libsqlPipelineResErr

Returned when something went seriously wrong with the server.

Structure

string | 
{
    error: string
}



libsqlCloseStreamReq

Structure

{
    type: "close",
}



libsqlExecuteStreamReq

Structure

{
    type: "execute",
    stmt: libsqlSQLStatement
}

Reference: libsqlSQLStatement




libsqlBatchStreamReq

Structure

{
    type: "batch",
    batch: {
        steps: Array<libsqlBatchReqStep>,
    }
}

Reference: libsqlBatchReqStep




libsqlStreamResOk

Structure

{
    type: "ok",
    response:  libsqlCloseStreamResOk|libsqlExecuteStreamResOk|libsqlBatchStreamResOk
}

Reference: libsqlCloseStreamResOk, libsqlExecuteStreamResOk, libsqlBatchStreamResOk




libsqlStreamResErr

Structure

{
    type: "error",
    error: libsqlStreamResErrData
}

Reference: libsqlStreamResErrData




libsqlSQLStatement

An SQL statement, formatted for the server.

Structure

{
    sql: string,
    args?: Array<libsqlSQLValue>,
    named_args?: Array<{
        name: string,
        value: libsqlSQLValue,
    }>,
    want_rows?: boolean,
}

Reference: libsqlSQLValue




libsqlBatchReqStep

A step in a batch request.

Structure

{
    condition?: libsqlBatchReqStepExecCond | null,
    stmt: libsqlSQLStatement,
}

A batch step is executed only when condition is either null or evaluates to true. Reference: libsqlBatchReqStepExecCond, libsqlSQLStatement




libsqlCloseStreamResOk

Structure

{
    type: "close",
}



libsqlExecuteStreamResOk

Structure

{
    type: "execute",
    result: libsqlStatementResOkData
}

Reference: libsqlStatementResOkData




libsqlBatchStreamResOk

Structure

{
    type: "batch",
    result: libsqlBatchStreamResOkData,
}

Reference: libsqlBatchStreamResOkData




libsqlStreamResErrData

Returned when something went wrong with the server.

Structure

{
    message: string,
    code?: string | null
}



libsqlSQLValue

Types of values returned by the server. You are responsible for processing them appropriately. Or you could use libsql-stateless-easy instead of this library.

Structure

    { type: "null" } |
    { type: "integer", value: string } |
    { type: "float", value: number } |
    { type: "text", value: string } |
    { type: "blob", base64: string };



libsqlBatchReqStepExecCond

Each of these evaluates to either true or false.

Structure

    { type: "ok", step: number } | //uint32: 0-based index in the steps array
    { type: "error", step: number } | //uint32: 0-based index in the steps array
    { type: "not", cond: libsqlBatchReqStepExecCond } |
    { type: "and", conds: Array<libsqlBatchReqStepExecCond> } |
    { type: "or", conds: Array<libsqlBatchReqStepExecCond> } |
    { type: "is_autocommit" };
  • ok evaluates to true if the step (referenced by its 0-based index) was executed successfully. If the statement was skipped, this condition evaluates to false.
  • error evaluates to true if the step (referenced by its 0-based index) has produced an error. If the statement was skipped, this condition evaluates to false.
  • not evaluates cond and returns the logical negative.
  • and evaluates conds and returns the logical conjunction of them.
  • or evaluates conds and returns the logical disjunction of them.
  • is_autocommit evaluates to true if the stream is currently in the autocommit state (not inside an explicit transaction) Reference: libsqlBatchReqStepExecCond



libsqlStatementResOkData

The data returned by the server on execution of an SQL statement.

Structure

{
    cols: Array<libsqlSQLColumn>,
    rows: Array<Array<libsqlSQLValue>>,
    affected_row_count: number, //uint32
    last_insert_rowid: string | null
}

Reference: libsqlSQLColumn, libsqlSQLValue




libsqlBatchStreamResOkData

The data returned by the server on execution of a batch of SQL statement.

Structure

{
    step_results: Array<libsqlStatementResOkData | null>,
    step_errors: Array<libsqlStreamResErrData | null>
}

Reference: libsqlStatementResOkData, libsqlStreamResErrData




libsqlSQLColumn

Structure

{
    name: string | null,
    decltype: string | null
}



Clone this wiki locally