---
description: >
  Sample SQL service commissioning file

metadata:
  name: Sample SQL service
  icon: https://www.cybus.io/wp-content/uploads/2017/10/for-whom1.svg
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  sqlURL:
    type: string
    default: 'mariadb://root:my-secret-pw@localhost:3306/mysql'
  initialReconnectDelay:
    type: integer
    default: 1000
  maxReconnectDelay:
    type: integer
    default: 30000
  factorReconnectDelay:
    type: integer
    default: 2

resources:
  sqlConnection:
    type: Cybus::Connection
    properties:
      protocol: Sql
      connection:
        url: !ref sqlURL
        connectionStrategy:
          initialDelay: !ref initialReconnectDelay
          maxDelay: !ref maxReconnectDelay
          incrementFactor: !ref factorReconnectDelay

  sqlQuery1:
    type: Cybus::Endpoint
    properties:
      protocol: Sql
      connection: !ref sqlConnection
      subscribe:
        query: 'SELECT User FROM user'
        # Be very careful with this setting. A low value might overload
        # the database (the unit is milliseconds!).
        interval: 2000

  sqlQuery2:
    type: Cybus::Endpoint
    properties:
      protocol: Sql
      connection: !ref sqlConnection
      write:
        # Here we use the placeholder $someValue. The protocol driver will insert
        # the value from the input JSON message under the key someValue
        query: 'INSERT INTO test (id, some_column) VALUES ($id, $someValue)'

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref sqlQuery1
          publish:
            topic: 'database-users'
        - subscribe:
            topic: 'database-insert-test'
          publish:
            endpoint: !ref sqlQuery2
