0

I have a workspace in Notion, which I use to take notes for an app I have on Github.

I want to add a database which will show some download stats from different sources (incuding Github) using the beta Notion API.

Right now I can add information at the end of a database just fine, but I don't understand how to remove the content which was posted before. Or even update it if I can.

This is what I have so far:

import { Client } from "@notionhq/client";
import dotenv from "dotenv";
import { Octokit } from "@octokit/rest";

dotenv.config();

const octokit = new Octokit();

const notion = new Client({ auth: process.env.NOTION_TOKEN });

const databaseId = process.env.NOTION_DATABASE_ID;

async function addEntry(release, name, download_count, tag) {
  try {
    await notion.request({
      path: "pages",
      method: "POST",
      body: {
        parent: { database_id: databaseId },
        properties: {
          Version: {
            title: [
              {
                text: {
                  content: release,
                },
              },
            ],
          },
          Name: {
            rich_text: [
              {
                text: {
                  content: name,
                },
              },
            ],
          },

          "Download Count": {
            type: "number",
            number: download_count,
          },
          Tags: {
            multi_select: [{ name: "Github" }, { name: tag }],
          },
        },
      },
    });
    console.log("Success! Entry added.");
  } catch (error) {
    console.error(error.body);
  }
}

(async () => {
  const latest_release = await octokit.repos.listReleases({
    owner: "ShadowMitia",
    repo: "steam_randomiser",
  });
  const releases = latest_release.data;

  let github_downloads = {};

  for (let release of releases) {
    for (let asset of release.assets) {
      console.log(release["tag_name"], asset["name"], asset["download_count"]);
      //   github_downloads[asset["label"]];
      addEntry(
        `${release["tag_name"]}`,
        `${asset["name"]}`,
        asset["download_count"],
        asset["name"].includes("linux") ? "Linux" : "Windows"
      );
    }
  }
})();
ShadowMitia
  • 2,411
  • 1
  • 19
  • 24

1 Answers1

0

To delete (archive) a page in a database. Set the archive parameter to true.

curl --location --request PATCH 'https://api.notion.com/v1/pages/YOUR_PAGE_ID' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_BOT_TOKEN' \
--data'{
    "parent":{

      "database_id":"YOUR_DATABASE_ID"

   },
   "archived": true,
   "properties":{

      "Name":{

         "title":[

            {

               "text":{

                  "content":"A Test Page"

               }

            }

         ]

      },
        "Email": {
        "email": "hello@test.com"
       },
       "multiselect_tags": { 
            "type": "multi_select",
            "multi_select":[{
                        "name": "Duc Loi Market"
                    },
                    {
                        "name": "Rainbow Grocery"
                    }]
        }
    }

}'

To clear data in a page you would set the data to empty or null depending on the property being updated. For example, if you have an array, you would set the property to an empty array.

        "multiselect_tags": { 
            "type": "multi_select",
            "multi_select":[ {
                        "name": "Duc Loi Market"
                    },
                    {
                        "name": "Rainbow Grocery"
                    }
                ]
            }
    }

//Empty a multi_select property

        "multiselect_tags": { 
            "type": "multi_select",
            "multi_select":[]
        }

If the property is a string, like the email property, set it to null

        "Email": {
        "email": "hello@test.com"
       }

//Empty the value of the email property on a page
        "Email": {
        "email": null
       }
adlopez15
  • 3,449
  • 2
  • 14
  • 19