|
| 1 | +import { Client } from "@notionhq/client" |
| 2 | + |
| 3 | +const notion = new Client({ auth: process.env.NOTION_KEY }) |
| 4 | + |
| 5 | +async function migrateSharedTasks(){ |
| 6 | + const dbByTitleFilter = (str) => ({ title: [{plain_text}] }) => plain_text === str |
| 7 | + |
| 8 | + const dbsSearchObj = { |
| 9 | + query: '', |
| 10 | + filter: { |
| 11 | + property: 'object', |
| 12 | + value: 'database' |
| 13 | + } |
| 14 | + } |
| 15 | + const dbsSearch = await notion.search(dbsSearchObj) |
| 16 | + const dbs = dbsSearch.results |
| 17 | + const dbsMap = { |
| 18 | + tasks: { |
| 19 | + main: dbs.find(dbByTitleFilter("Tasks")).id, |
| 20 | + shared: dbs.find(dbByTitleFilter("Tasks (shared)")).id |
| 21 | + }, |
| 22 | + projects: { |
| 23 | + main: dbs.find(dbByTitleFilter("Projects")).id |
| 24 | + }, |
| 25 | + rules: { |
| 26 | + main: dbs.find(dbByTitleFilter("Rules")).id |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + const tasksQuery = await notion.databases.query( |
| 31 | + { |
| 32 | + database_id: dbsMap.tasks.main, |
| 33 | + filter: { |
| 34 | + or: [ |
| 35 | + { |
| 36 | + property: "Is shared", |
| 37 | + formula: { |
| 38 | + checkbox: { |
| 39 | + equals: true |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + } |
| 46 | + ) |
| 47 | + const tasks = tasksQuery.results |
| 48 | + |
| 49 | + console.log(tasks[0].properties) |
| 50 | + |
| 51 | + tasks.forEach((task, i) => { |
| 52 | + const props = [ |
| 53 | + "Status", |
| 54 | + "Due", |
| 55 | + "Tags", |
| 56 | + "Project", |
| 57 | + "Theme", |
| 58 | + "Assignee", |
| 59 | + ].reduce((prev, key) => { |
| 60 | + const matchedProp = task.properties[key] |
| 61 | + const type = matchedProp?.type |
| 62 | + return matchedProp |
| 63 | + ? { ...prev, [key]: {[type]: matchedProp[type]} } |
| 64 | + : prev |
| 65 | + }, { |
| 66 | + title: { |
| 67 | + title: [ |
| 68 | + { |
| 69 | + "text": { |
| 70 | + "content": task.properties.Name.title[0].plain_text |
| 71 | + } |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + notion.pages.create({ |
| 78 | + parent: { database_id: dbsMap.tasks.shared }, |
| 79 | + properties: props |
| 80 | + }) |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +async function listDatabases() { |
| 85 | + search('', { value: 'database', property: 'object' }) |
| 86 | + .then((dbs) => { |
| 87 | + const info = dbs.map(({title, id}) => ({ |
| 88 | + id, |
| 89 | + title: title[0].plain_text |
| 90 | + })) |
| 91 | + console.log(info) |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +async function search(query, filter, sort) { |
| 96 | + let search = { query } |
| 97 | + if (filter) search = { ...search, filter } |
| 98 | + if (sort) search = { ...search, sort } |
| 99 | + |
| 100 | + const response = await notion.search(search) |
| 101 | + const results = await response.results |
| 102 | + return results |
| 103 | +} |
| 104 | + |
| 105 | +async function getItems(database_id, filter, sorts) { |
| 106 | + const response = await notion.databases.query({ |
| 107 | + database_id |
| 108 | + }); |
| 109 | + console.log(response) |
| 110 | +} |
| 111 | + |
| 112 | +async function addItem(databaseId, title) { |
| 113 | + try { |
| 114 | + const response = await notion.pages.create({ |
| 115 | + parent: { database_id: databaseId }, |
| 116 | + properties: { |
| 117 | + title: { |
| 118 | + title:[ |
| 119 | + { |
| 120 | + "text": { |
| 121 | + "content": title |
| 122 | + } |
| 123 | + } |
| 124 | + ] |
| 125 | + } |
| 126 | + }, |
| 127 | + }) |
| 128 | + console.log(response) |
| 129 | + console.log("Success! Entry added.") |
| 130 | + } catch (error) { |
| 131 | + console.error(error.body) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// addItem(XXX, "Yurts in Big Sur, California") |
| 136 | +// getItems(databaseId) |
| 137 | +// listDatabases() |
| 138 | +migrateSharedTasks() |
0 commit comments