services:
  powersync:
    restart: unless-stopped
    image: journeyapps/powersync-service:latest
    # The unified service runs an API server and replication worker in the same container.
    # These services can be executed in different containers by using individual entry commands e.g.
    # Start only the API server with
    #   command: ['start', '-r', 'api']
    # Start only the replication worker with
    #   command: ['start', '-r', 'sync']

    # Migations occur automatically by default. Default migrations can be disabled in `powersync.yaml`:
    #  migrations:
    #   disable_auto_migration: true
    #
    # Service migrations can be manually triggered by starting a container with the
    # following command:
    #     command: ['migrate', 'up']
    # Note that this container must finish executing before starting the sync or unified container.
    command: ["start", "-r", "unified"]
    volumes:
      # Mounts the specified config folder to the container
      # This folder should contain `powersync.yaml and sync_rules.yaml
      - ../config:/config
    environment:
      # This is the path (inside the container) to the YAML config file
      # Alternatively the config path can be specified in the command
      # e.g:
      #   command: ['start', '-r', 'unified', '-c', '/config/powersync.yaml']
      #
      # The config file can also be specified in Base 64 encoding
      # e.g.: Via an environment variable
      #   POWERSYNC_CONFIG_B64: [base64 encoded content]
      # or e.g.: Via a command line parameter
      #    command: ['start', '-r', 'unified', '-c64', '[base64 encoded content]']
      POWERSYNC_CONFIG_PATH: /config/powersync.yaml

      # Service memory limits should be adjusted to roughly 80 percent of the system memory capacity
      NODE_OPTIONS: --max-old-space-size=1000

      # Sync rules can be specified as base 64 encoded YAML
      # e.g: Via an environment variable
      # POWERSYNC_SYNC_RULES_B64: "[base64 encoded sync rules]"
      # or e.g.: Via a command line parameter
      #     command: ['start', '-r', 'unified', '-sync64', '[base64 encoded content]']

      # The following will be inserted into the powersync.yaml file via templating
      # Templates are used for filesystem and base64 configuration files
      PS_DATA_SOURCE_URI: ${PS_DATA_SOURCE_URI}
      PS_MONGO_URI: mongodb://mongo:27017/powersync_datafy

      # Note that powersync.yaml->client_auth->allow_local_jwks must be true for local URLs to work
      #PS_JWKS_URL: ${PS_JWKS_URL}

      # The port which the PowerSync API server should run on
      PS_PORT: ${PS_PORT}

      #  CA certificate for Postgres connection
      # PS_PG_CA_CERT:

      # Client certificate for Postgres connection
      # PS_PG_CLIENT_CERT:

      # Client private key for Postgres connection
      # PS_PG_CLIENT_PRIVATE_KEY:

      # Potential JWKs public key template
      # PS_JWK_N:
      # PS_JWK_E:
      # PS_JWK_KID:
    # This requires image: journeyapps/powersync-service:0.5.8 or later
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://localhost:${PS_PORT}/probes/liveness').then(r => r.ok ? process.exit(0) : process.exit(1)).catch(() => process.exit(1))"
        ]
      interval: 5s
      timeout: 1s
      retries: 15
    ports:
      - ${PS_PORT}:${PS_PORT}