LoginSignup
1
0

More than 1 year has passed since last update.

Notion APIでデータベースが変更されたか監視してみる #notion

Last updated at Posted at 2022-12-07

データベースからアイテムを取得して、その中身の最終更新時間を取得してみます。

色々とNotion APIも種類がありますが。

  • Query a database
  • Retrieve a page

の二つを使ってDB -> Pageという順番で取得してみます。

APIキーやデータベースIDなど

前に書いた記事でAPIのざっくりした使い方がわかります。

Query DBでDBからアイテムを取得

学籍番号というプロパティの中でpo-07を含むデータを抽出してみます。

スクリーンショット 2022-12-07 15.42.21.png

公式サンプルを参考に取得してみました。

https://github.com/makenotion/notion-sdk-js/blob/90418939a90aa04f52e7bb9b15444cd77b29cc03/examples/database-email-update/index.js#L70-L73

const { Client } = require("@notionhq/client")

const studentDBId = '自身の利用するデータベースID'; //間にハイフンがなくて大丈夫
const notion = new Client({auth: `APIシークレット`};

// Initializing a client
const main = async () => {
    const response = await notion.databases.query({
        database_id: studentDBId,
        filter: {
          or: [
            {
              property: '学籍番号',
              rich_text: {
                contains: 'po-07'
              }
            }
          ],
        },
    });

    console.log(response);
    console.log(response.results.length);
}

main();

実行結果は以下のような形になります。

{
  object: 'list',
  results: [
    {
      object: 'page',
      id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      created_time: '2022-09-03T01:43:00.000Z',
      last_edited_time: '2022-12-07T06:35:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      cover: null,
      icon: null,
      parent: [Object],
      archived: false,
      properties: [Object],
      url: 'https://www.notion.so/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    },
    {省略},
    {省略},
    {省略}
  ],
  next_cursor: null,
  has_more: false,
  type: 'page',
  page: {}
}

ここでページIDが取得できました。

補足: フィルターの掛け方

こんな形でデータベースのプロパティの型によっても変わるみたいです。

{
    "filter": {
        "and": [
            {
                "property": "Seen",
                "checkbox": {
                    "equals": false
                }
            },
            {
                "property": "Yearly visitor count",
                "number": {
                    "greater_than": 1000000
                }
            }
        ]
    }
}

Retrieve a pageでページの情報を取得

先程の情報でも最終変更は取れますが、名前まで取得してみます。

const main = async () => {
    const response = await notion.databases.query({
        database_id: studentDBId,
        filter: {
          or: [
            {
              property: '学籍番号',
              rich_text: {
                contains: 'po-07'
              }
            }
          ],
        },
    });

    const items = response.results;

    for await (const item of items) {
        const pageId = item.id;
        const response = await notion.pages.retrieve({ page_id: pageId });
        console.log(response);
    }
}

main();

結果こんな感じでプロパティIDが取得できます。

{
  object: 'page',
  id: 'xxxxxxxxxxxxxxxxxxxxxx',
  created_time: '2022-09-03T01:38:00.000Z',
  last_edited_time: '2022-12-07T06:35:00.000Z',
  created_by: { object: 'user', id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' },
  last_edited_by: { object: 'user', id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' },
  cover: null,
  icon: null,
  parent: {
    type: 'database_id',
    database_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  },
  archived: false,
  properties: {
    'Discord名': { id: 'GpHg', type: 'rich_text', rich_text: [Array] },
  省略
  }
省略
}

タイトルはプロパティIDがtitleになるみたいなのでusers[1].properties.Name.title[0].text.content;といった形で取得できます。

const main = async () => {
    const response = await notion.databases.query({
        database_id: studentDBId,
        filter: {
          or: [
            {
              property: '学籍番号',
              rich_text: {
                contains: 'po-07'
              }
            }
          ],
        },
    });

    const items = response.results;

    const users = [];
    for await (const item of items) {
        const pageId = item.id;
        const response = await notion.pages.retrieve({ page_id: pageId });
        users.push(response)
    }
    
    //2番目のユーザーの情報だけ取ってみる。
    const userName = users[1].properties.Name.title[0].text.content;
    const lastedit = users[1].last_edited_time;
    const url = users[1].url;

    console.log(`${userName}さんのページが${lastedit}に更新されました。 ${url}`)
}

main();

結果

吉田さんのページが2022-12-07T07:20:00.000Zに更新されました。 https://www.notion.so/xxxxxxxxxxxxxxxxx

こんな感じで取得できました。

所感

  • Database
  • Page
  • Block

という順番で探っていく様子ですが、ページの中身はBlockになるみたいで、この時点だとまだ取得できてません。逆にDBを定義してDB内に全ての情報がある場合はBlockを使わなくても情報を取れる気がしました。

スクリーンショット 2022-12-07 16.35.14.png

まだページの中身を取得できてないので次はBlockのAPIを探っていきます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0