Skip to content

Whereable namespacing #193

@patmooney

Description

@patmooney

Sorry if this has been asked/answered but cannot find example in the docs...

Problem

If I have a function:

const getAThatBelongsToB = async (
   aWhere: a.Wherable,
   bWhere: b.Wherable
) => {
  // lateral won't cut it because b will likely just be empty
  const [result] = await db.sql<a.SQL | b.SQL, { result: a.JSONSelectable }[]>`
        SELECT to_jsonb(${"a"}.*) AS result
            FROM ${"a"}
            JOIN ${"b"} ON ${"b"}.${"id"} = ${"a"}.${"bId"} AND ${bWhere}
        WHERE ${aWhere}
        LIMIT 1
  `.run(cxn);
  return result?.result;
};

const result = await getAThatBelongsToB({ id: 123 }, { id: 456 });

// id is ambiguous

Here I'm interpolating the aWhere and bWhere where each contain id which precipitates an ambiguity errors from Postgres

Question:

Any useful conditions or otherwise which are able to safely namespace the where params automatically? Given they are of type "a.Whereable" or "b.Wherable", would it be practical for zapatos to ALWAYS namespace the params when used in this manner?

Example

 // where automatically becomes
  WHERE ${"a"}.${"id"} = 123

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions