I’m working with a Notion workspace where I track app statistics from GitHub and other platforms. I’ve set up a database to store download metrics using Notion’s API.
Currently, my script successfully appends new records to the database, but I’m struggling with clearing old entries or updating existing ones before adding fresh data. I need to either wipe the database clean or modify existing records instead of just adding more.
Here’s my current implementation:
import { Client } from "@notionhq/client";
import dotenv from "dotenv";
import { Octokit } from "@octokit/rest";
dotenv.config();
const githubAPI = new Octokit();
const notionClient = new Client({ auth: process.env.NOTION_TOKEN });
const dbId = process.env.NOTION_DATABASE_ID;
async function createRecord(version, fileName, downloads, platform) {
try {
await notionClient.request({
path: "pages",
method: "POST",
body: {
parent: { database_id: dbId },
properties: {
Release: {
title: [{
text: { content: version }
}]
},
Filename: {
rich_text: [{
text: { content: fileName }
}]
},
Downloads: {
type: "number",
number: downloads
},
Categories: {
multi_select: [{ name: "GitHub" }, { name: platform }]
}
}
}
});
console.log("Record created successfully");
} catch (err) {
console.error(err.body);
}
}
(async () => {
const repoReleases = await githubAPI.repos.listReleases({
owner: "MyUsername",
repo: "my-project"
});
const releaseData = repoReleases.data;
for (let item of releaseData) {
for (let file of item.assets) {
const osType = file.name.includes("mac") ? "macOS" : "Windows";
await createRecord(
item.tag_name,
file.name,
file.download_count,
osType
);
}
}
})();
How can I modify this to clear existing database entries before populating it with updated information?