データベースのクエリの時に、プロパティを用意していなくてもタイムスタンプで検索できるようになりました。概要説明とともに、検証もしてみます。

概要説明

データベースをクエリするとき、これまでは作成日時や最終更新日時のプロパティを事前に作っておく必要がありました。今回、作成日時や最終更新日時のタイムスタンプを使った新しいフィルタを追加しました。これは、検索のためだけに、作成日時や最終更新日時のプロパティを用意する必要がなくなったということを意味しています。

詳細については ここ を参照してください。ここでは、作成日時のフィルタを示します。

{
    "filter": {
        "timestamp": "created_time",
        "created_time": {
          "past_week": {}
        }
    }
}

こちらは最終更新日時のフィルタです。

{
    "filter": {
        "timestamp": "last_edited_time",
        "last_edited_time": {
          "after": "2021-05-10"
        }
    }
}

これらのフィルタタイプを使った合成フィルタも生成できます。

検証

これまで検証用のスクリプトを作成するのが面倒でしたが、自作の NotionRubyMapping で検証スクリプトが自動作成できるようになりました。 db.created_timedb.last_edited_time でフィルタ作成用の Property が取得できます。これらは特別な Filter object を生成します。

require "notion_ruby_mapping"
include NotionRubyMapping

NotionCache.instance.create_client ENV["NOTION_API_TOKEN"]
db = Database.new id: "c63c00ad201d4bed82b018dfca9b6ba0"
ct = db.created_time
let = db.last_edited_time
query = ct.filter_past_week.and(let.filter_after Date.new(2021, 5, 10))
print db.query_database query, dry_run: true

自動作成されたシェルスクリプトはこちら。

#!/bin/sh
curl -X POST '<https://api.notion.com/v1/databases/c63c00ad201d4bed82b018dfca9b6ba0/query>' \\
  -H 'Notion-Version: 2022-02-22' \\
  -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \\
  -H 'Content-Type: application/json' \\
  --data '{"filter":{"and":[{"timestamp":"created_time","created_time":{"past_week":{}}},{"timestamp":"last_edited_time","last_edited_time":{"after":"2021-05-10"}}]},"page_size":100}'

実行結果はこちら

{
  "object": "list",
  "results": [
    {
      "object": "page",
      "id": "c5782ea0-ade1-4222-a3d5-d7372e607f40",
      "created_time": "2022-03-25T21:35:00.000Z",
      "last_edited_time": "2022-03-25T22:18:00.000Z",
      "created_by": {
        "object": "user",
        "id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
      },
      "last_edited_by": {
        "object": "user",
        "id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
      },
      "cover": null,
      "icon": null,
      "parent": {
        "type": "database_id",
        "database_id": "c63c00ad-201d-4bed-82b0-18dfca9b6ba0"
      },
      "archived": false,
      "properties": {
        "開示?": {
          "id": "%3BfBx",
          "type": "checkbox",
          "checkbox": false
        },
        "日付": {
          "id": "%3CWbe",
          "type": "date",
          "date": {
            "start": "2022-03-19",
            "end": null,
            "time_zone": null
          }
        },
        "原題": {
          "id": "%3FMR%7D",
          "type": "rich_text",
          "rich_text": [
            {
              "type": "text",
              "text": {
                "content": "Query a database endpoint supports filtering by timestamp",
                "link": null
              },
              "annotations": {
                "bold": false,
                "italic": false,
                "strikethrough": false,
                "underline": false,
                "code": false,
                "color": "default"
              },
              "plain_text": "Query a database endpoint supports filtering by timestamp",
              "href": null
            }
          ]
        },
        "Number": {
          "id": "ANbi",
          "type": "number",
          "number": 46
        },
        "URL": {
          "id": "o%3A~F",
          "type": "url",
          "url": "<https://developers.notion.com/changelog/filter-databases-by-timestamp-even-if-they-dont-have-a-timestamp-property>"
        },
        "タグ": {
          "id": "tUJV",
          "type": "multi_select",
          "multi_select": [
            {
              "id": "05f3b6b1-b6f1-49a2-bbdb-955cfb4d36f4",
              "name": "IMPROVED",
              "color": "gray"
            }
          ]
        },
        "経過日数": {
          "id": "~Xof",
          "type": "formula",
          "formula": {
            "type": "number",
            "number": 7
          }
        },
        "名前": {
          "id": "title",
          "type": "title",
          "title": [
            {
              "type": "text",
              "text": {
                "content": "データベースのクエリがタイムスタンプ検索に対応しました",
                "link": null
              },
              "annotations": {
                "bold": false,
                "italic": false,
                "strikethrough": false,
                "underline": false,
                "code": false,
                "color": "default"
              },
              "plain_text": "データベースのクエリがタイムスタンプ検索に対応しました",
              "href": null
            }
          ]
        }
      },
      "url": "<https://www.notion.so/c5782ea0ade14222a3d5d7372e607f40>"
    }
  ],
  "next_cursor": null,
  "has_more": false,
  "type": "page",
  "page": {}
}

Notion API Changelog まとめ