Get postId in a wordpress pattern file?

In block themes patterns won’t have access to context such as id it seems:
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns

One has to create a custom block to get post id and use useSelect hook to get post Id like so:
import { useSelect } from "@wordpress/data";


inside Edit block function:

const postId = useSelect(select => select('core/editor').getCurrentPostId());

then, postId can be used inside edit function and if it has to be used in save function, useEffect should be used inside edit to store id to attribute like so:

useEffect(() => { 
  if (postId) {
    setAttributes({postId})
  }, [postId]);

Provided you have defined postId attribute in block.json, you can get and use that attribute in save function or render_callback.