Skip to content

Conversation

@gregnr
Copy link
Collaborator

@gregnr gregnr commented Jun 4, 2025

Adds unwrapNode() utility function to simplify node parsing.

unwrapNode() unwraps a Node to get its type and nested value. Makes it easier to work with nodes by allowing you to narrow them based on their type.

const { type, node } = unwrapNode(wrappedNode);

switch (type) {
  case 'SelectStmt':
    // Now `node` is narrowed to `SelectStmt`
    break;
}

@jgoux
Copy link
Contributor

jgoux commented Jun 5, 2025

I would only expose unwrapNode to keep the library API surface minimal. It feels like assertDefined is super generic and could easily be implemented on user's side. Same for assertAndUnwrapNode.

Could you export all the types which are a member of Node? It's useful if we want to create specific Node handlers like this:

// this is way too complicated, I just want to `import type { CreateStmt } from "@supabase/pg-parser";`
type Node = Required<Required<ParseResult<17>>["stmts"][number]["stmt"]>;
type CreateStmt = Extract<Node, { CreateStmt: unknown }>["CreateStmt"];

export function handleCreateStmt(node: CreateStmt) {
  const schema = node.relation?.schemaname;
  const table = node.relation?.relname;
  const id = `${schema}.${table}`;
}

@gregnr
Copy link
Collaborator Author

gregnr commented Jun 5, 2025

That's fair. I ended up needing the others all the time for regular usage, but let's expose just unwrapNode for now and see how others use the library.

Could you export all the types which are a member of Node?

Do you mean like this? #6

@jgoux
Copy link
Contributor

jgoux commented Jun 5, 2025

Do you mean like this? #6

Yes, TIL! 😄

@gregnr gregnr merged commit 165ba3b into main Jun 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants