Skip to content

bug: .then.val() and .at() returns Actionable expression instead of first query result (docs mismatch) #21

@motopods

Description

@motopods

Hi, thanks for this great library.

I found a mismatch between documentation and runtime behavior for .then.val().

Summary
The README says .then.val() returns the first item (or undefined), but in practice it returns an Actionable object (query expression helpers), not the first element of the executed query result.

Documentation reference

Reproduction
Environment:

  • surqlize: 0.1.0
  • surrealdb: 2.0.3
  • runtime: bun

Minimal script:

const user = await db.select("user").then.val();
const users = await db.select("user");

console.log(user, users[0]);

Actual behavior

  • user is an Actionable-like object (contains methods like eq, contains, at, val, etc.)
  • users[0] is the actual first record object

Expected behavior

  • user should be the first record (or undefined), consistent with README

Likely root cause
In source, val delegates to at(0), and at returns a databaseFunction expression (Actionable), not a resolved JS array element:

  • val implementation:
    ): Actionable<C, OptionType<T>>;
    function val<C extends WorkableContext, T extends AbstractType>(
    this: Workable<C, ArrayType<T>>,
    ) {
    // Call at() with literal 0 to get the first element
    // Type assertion is safe as we're calling the at() function with correct parameters
    type AtFunction = (
    this: Workable<C, ArrayType<T>>,
    n: IntoWorkable<C, LiteralType<0>>,
    ) => unknown;
    return (at as AtFunction).call(
    this,
    intoWorkable(this[__ctx], t.literal(0), 0),
    );
    }
  • at implementation in same file returns array::at expression

Also, Query.then returns a function merged with actionable helpers, so .then.val() appears to bind to query-expression functions rather than resolved result-array helpers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions