{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://owlie.com/docs/schemas/connectors/definitions/sql.schema.json",
  "title": "Owlie SQL connector definition",
  "description": "The definition.json of a sql connector (engine major 1). Owlie derives the connector's manifest from it. A value the definition makes install-editable is a bare { \"$oxl\": \"config.<name>\" } naming a field declared under config.properties; each install keeps its own copy, seeded from the field's default.",
  "type": "object",
  "required": ["engine", "dialect", "entities"],
  "additionalProperties": false,
  "properties": {
    "engine": {
      "type": "integer",
      "const": 1
    },
    "dialect": {
      "enum": ["postgres", "mysql", "mssql", "oracle"]
    },
    "description": {
      "type": "string",
      "minLength": 1
    },
    "icon": {
      "type": "string",
      "minLength": 1
    },
    "validationQuery": {
      "type": "string",
      "minLength": 1
    },
    "config": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "properties": {
          "type": "object",
          "additionalProperties": {
            "type": "object"
          }
        }
      }
    },
    "pageSize": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 5000,
          "default": 500
        },
        {
          "type": "object",
          "required": ["$oxl"],
          "additionalProperties": false,
          "properties": {
            "$oxl": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      ]
    },
    "entities": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": ["identifier", "columns", "sync"],
        "additionalProperties": false,
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1
          },
          "is_account": {
            "type": "boolean",
            "default": false
          },
          "identifier": {
            "type": "string",
            "minLength": 1
          },
          "columns": {
            "type": "object",
            "additionalProperties": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "type": {
                  "enum": ["string", "number", "bigint", "int8", "boolean", "datetime"],
                  "default": "string"
                },
                "title": {
                  "type": "string"
                },
                "sensitive": {
                  "type": "boolean",
                  "default": false
                }
              }
            }
          },
          "statements": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "create": {
                "type": "string",
                "minLength": 1
              },
              "createdId": {
                "type": "string",
                "minLength": 1
              },
              "update": {
                "type": "string",
                "minLength": 1
              },
              "delete": {
                "type": "string",
                "minLength": 1
              },
              "enable": {
                "type": "string",
                "minLength": 1
              },
              "disable": {
                "type": "string",
                "minLength": 1
              }
            }
          },
          "sync": {
            "type": "object",
            "required": ["orderKey"],
            "additionalProperties": false,
            "properties": {
              "table": {
                "anyOf": [
                  {
                    "type": "string",
                    "minLength": 1
                  },
                  {
                    "type": "object",
                    "required": ["$oxl"],
                    "additionalProperties": false,
                    "properties": {
                      "$oxl": {
                        "type": "string",
                        "minLength": 1
                      }
                    }
                  }
                ]
              },
              "selectStatement": {
                "type": "string",
                "minLength": 1
              },
              "where": {
                "anyOf": [
                  {
                    "type": "string",
                    "minLength": 1
                  },
                  {
                    "type": "object",
                    "required": ["$oxl"],
                    "additionalProperties": false,
                    "properties": {
                      "$oxl": {
                        "type": "string",
                        "minLength": 1
                      }
                    }
                  }
                ]
              },
              "orderKey": {
                "type": "string",
                "minLength": 1
              },
              "updatedAt": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        }
      }
    },
    "grant_kinds": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": ["source", "target", "select", "sourceColumn", "targetColumn"],
        "additionalProperties": false,
        "properties": {
          "source": {
            "type": "string",
            "minLength": 1
          },
          "target": {
            "type": "string",
            "minLength": 1
          },
          "select": {
            "type": "string",
            "minLength": 1
          },
          "sourceColumn": {
            "type": "string",
            "minLength": 1
          },
          "targetColumn": {
            "type": "string",
            "minLength": 1
          },
          "membership": {
            "type": "boolean",
            "default": false
          },
          "add": {
            "type": "string",
            "minLength": 1
          },
          "remove": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    "custom_ops": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": ["script"],
        "additionalProperties": false,
        "properties": {
          "script": {
            "type": "string",
            "pattern": "^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$"
          },
          "timeoutMs": {
            "type": "integer",
            "minimum": 1,
            "maximum": 900000
          },
          "secrets": {
            "type": "object",
            "additionalProperties": {
              "type": "string",
              "minLength": 1
            }
          },
          "config": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1
            }
          },
          "entity": {
            "type": "string",
            "minLength": 1
          },
          "title": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "params_schema": {
            "type": "object"
          }
        }
      }
    },
    "handlers": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": ["script"],
        "additionalProperties": false,
        "properties": {
          "script": {
            "type": "string",
            "pattern": "^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$"
          },
          "timeoutMs": {
            "type": "integer",
            "minimum": 1,
            "maximum": 900000
          },
          "secrets": {
            "type": "object",
            "additionalProperties": {
              "type": "string",
              "minLength": 1
            }
          },
          "config": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    }
  }
}
