Last active
November 27, 2021 08:59
-
-
Save akashrajkn/dddfc14e8c13bd5d1d9a7557ff004367 to your computer and use it in GitHub Desktop.
Notion (JS) script for adding an entry, updating an entry and retrieving a item based on the paperLink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on the blog post here: https://developers.notion.com/docs/getting-started | |
import { Client } from "@notionhq/client" | |
const notion = new Client({ auth: "<NOTION_KEY>" }) | |
const databaseId = "<NOTION_DATABASE_ID>" | |
async function addItem(paperLink, paperTitle, tags, comments, completedReading) { | |
// Add an item to the database | |
// Each item has a link (unique), title, tags (list of objects), comments and (boolean) reading status | |
try { | |
await notion.pages.create({ | |
parent : { database_id: databaseId }, | |
properties : { | |
'Paper Link': { | |
title : [{ | |
"text" : { "content" : paperLink } | |
} | |
] | |
}, | |
'Paper Title': { | |
"type" : 'rich_text', | |
"rich_text" : [{ | |
"text" : { "content" : paperTitle } | |
} | |
], | |
}, | |
// https://developers.notion.com/changelog/select-values-can-now-be-dynamically-created-via-create-and-update-page-endpoints-other-updates-since-public-beta-launch | |
'Tags' : { | |
"type" : "multi_select", | |
"multi_select" : tags | |
}, | |
'Added on': { | |
type : 'date', | |
date : { start: getTodayDate(), }, | |
}, | |
'Comments': { | |
"type" : 'rich_text', | |
"rich_text" : [{ | |
"text" : { "content" : comments } | |
} | |
], | |
}, | |
'Completed Reading' : { | |
"type" : "checkbox", | |
"checkbox" : completedReading | |
} | |
}, | |
}) | |
} catch (error) { | |
console.error(error.body) | |
} | |
} | |
async function updateItem(itemId, completedReading) { | |
// For the given itemId, update completedReading (boolean) status | |
try { | |
await notion.pages.update({ | |
page_id : itemId, | |
properties : { | |
'Completed Reading' : { 'checkbox': completedReading, }, | |
} | |
}) | |
} catch (error) { | |
console.error(error.body) | |
} | |
} | |
async function retrieveItems(paperLink) { | |
// Given the paperLink (unique), get the item in the notion database | |
const response = await notion.databases.query({ | |
database_id : databaseID, | |
filter : { | |
property : 'Paper Link', | |
text : { equals: paperLink } | |
} | |
}); | |
return response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment