apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: mongodb-query
  labels:
    dagweave.io/catalog-item: mongodb-query
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.2"
    dagweave.io/catalog-category: "Databases"
spec:
  entrypoint: query
  templates:
    - name: query
      inputs:
        parameters:
          # Database name to run the query against.
          - name: database
          # Collection to query.
          - name: collection
          # MongoDB find() filter as a JSON document. Defaults to all documents.
          - name: filter-json
            default: "{}"
          # Name of the Kubernetes Secret holding the full connection URI (including
          # any credentials), e.g. mongodb+srv://user:pass@host/. Defaults let a user
          # leave them blank in the Inspector.
          - name: uri-secret-name
            default: mongodb-uri
          - name: uri-secret-key
            default: uri
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # mongo:7 (ships the mongosh shell)
        image: mongo@sha256:8bb4442761a63a8dda452744082cdac9644b429807eb977b42324a63cef59ca4
        command: [sh, -c]
        # The URI (with creds), database, collection and filter are all passed as env
        # vars (below), never interpolated into the shell or the JS eval, so values
        # containing quotes or shell metacharacters are safe. mongosh is Node-based so
        # the eval reads them from process.env; the filter is parsed with JSON.parse.
        args:
          - |
            set -eu
            mongosh "$MONGO_URI" --quiet --eval '
              db.getSiblingDB(process.env.DB_NAME)
                .getCollection(process.env.COLL)
                .find(JSON.parse(process.env.FILTER))
                .toArray()
            '
        env:
          - name: DB_NAME
            value: "{{inputs.parameters.database}}"
          - name: COLL
            value: "{{inputs.parameters.collection}}"
          - name: FILTER
            value: "{{inputs.parameters.filter-json}}"
          - name: MONGO_URI
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.uri-secret-name}}"
                key: "{{inputs.parameters.uri-secret-key}}"
