{
  "$schema": "./.schemas/page.schema.json",
  "title": "Database",
  "icon": "tablecells",
  "sidebar": false,
  "params": {
    "id": {
      "example": "7c1f2e3d-4b5a-4c69-8d7e-0a1b2c3d4e2a",
      "pattern": "[0-9a-fA-F-]{32,36}"
    }
  },
  "title_from": {
    "ref": "db",
    "value": {
      "if": {
        "all": [
          {
            "path": "name",
            "exists": true
          },
          {
            "path": "name",
            "not": ""
          }
        ]
      },
      "then": "name",
      "else": {
        "template": "Untitled"
      }
    }
  },
  "sources": {
    "db": {
      "url": "https://api.notion.com",
      "secret": "notion",
      "every": "5m",
      "headers": {
        "Notion-Version": "2026-03-11"
      },
      "requests": {
        "ds": "/v1/data_sources/{param.id}",
        "rows": {
          "path": "/v1/data_sources/{param.id}/query?filter_properties=title",
          "method": "POST",
          "body": {
            "sorts": [
              {
                "timestamp": "last_edited_time",
                "direction": "descending"
              }
            ],
            "page_size": 15
          }
        }
      },
      "derive": [
        {
          "name": "rows_cut",
          "value": {
            "if": {
              "any": [
                {
                  "path": "rows.has_more",
                  "equals": true
                },
                {
                  "path": "rows.request_status.type",
                  "equals": "incomplete"
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "name",
          "value": {
            "join": "plain_text",
            "of": "ds.title",
            "separator": ""
          }
        },
        {
          "name": "properties",
          "value": {
            "each": "ds.properties",
            "entries": true,
            "sort": "key",
            "fields": {
              "name": "key"
            }
          }
        },
        {
          "name": "property_names",
          "value": {
            "join": "name",
            "of": "properties",
            "separator": " · "
          }
        },
        {
          "name": "recent_raw",
          "value": {
            "each": "rows.results",
            "fields": {
              "id": "id",
              "url": "url",
              "created": "created_time",
              "edited": "last_edited_time",
              "pieces": {
                "first": "value.title",
                "of": "properties",
                "entries": true,
                "where": {
                  "path": "value.type",
                  "equals": "title"
                }
              }
            }
          }
        },
        {
          "name": "recent_titled",
          "value": {
            "each": "recent_raw",
            "fields": {
              "id": "id",
              "url": "url",
              "created": "created",
              "edited": "edited",
              "title": {
                "join": "plain_text",
                "of": "pieces",
                "separator": ""
              }
            }
          }
        },
        {
          "name": "recent_rows",
          "value": {
            "each": "recent_titled",
            "fields": {
              "id": "id",
              "url": "url",
              "created": "created",
              "edited": "edited",
              "title": {
                "if": {
                  "all": [
                    {
                      "path": "title",
                      "exists": true
                    },
                    {
                      "path": "title",
                      "not": ""
                    }
                  ]
                },
                "then": "title",
                "else": {
                  "template": "Untitled"
                }
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "rows",
      "type": "list",
      "label": "Recently edited",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "db",
        "empty": {
          "text": "No rows yet.",
          "when": {
            "value": {
              "count": "recent_rows",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "rows_cut",
              "equals": "yes"
            },
            "then": {
              "template": "15 edited last"
            },
            "else": {
              "template": "Recently edited"
            }
          },
          "items": {
            "each": "recent_rows",
            "fields": {
              "key": "id",
              "icon": {
                "template": "doc.text"
              },
              "text": "title",
              "detail": {
                "template": "{edited|ago}"
              },
              "url": "url"
            }
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Database",
      "items": [
        {
          "key": "Last edited",
          "value": "—"
        },
        {
          "key": "Properties",
          "value": "—"
        }
      ],
      "source": {
        "ref": "db",
        "map": {
          "items[0].value": {
            "template": "{ds.last_edited_time|ago}"
          },
          "items[1].value": {
            "path": "property_names",
            "default": "—"
          }
        }
      }
    },
    {
      "id": "source-id",
      "type": "list",
      "label": "Data source ID",
      "copyable": true,
      "items": [
        "{param.id}"
      ]
    }
  ]
}
