Skip to content

Commit 434023b

Browse files
committed
Added loading imported notes
1 parent fcb2ebd commit 434023b

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/db.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ const pool = new pg.Pool({
66
user: 'phpusr'
77
})
88

9-
//await addNoteInfo({ title: 'test', filePath: 'testpath', imported: true })
9+
export async function loadImportStatuses() {
10+
const res = await pool.query({
11+
text: 'SELECT * FROM imported_notes'
12+
})
13+
return res.rows
14+
}
1015

1116
export async function addImportStatus({ noteId, title, filePath, imported, error }) {
12-
const text = 'INSERT INTO imported_notes(note_id, title, file_path, imported, error) VALUES($1, $2, $3, $4, $5)'
13-
const values = [noteId, title, filePath, imported, error]
14-
const res = await pool.query(text, values)
15-
//console.log(res)
17+
await pool.query({
18+
text: 'INSERT INTO imported_notes("noteId", title, "filePath", imported, error) VALUES($1, $2, $3, $4, $5)',
19+
values: [noteId, title, filePath, imported, error]
20+
})
1621
}
1722

src/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { lstatSync, readdirSync, readFileSync } from 'node:fs'
22
import { markdownToBlocks } from '@tryfabric/martian'
33
import { addBlocks, createPage } from './notion_api.js'
4-
import { addImportStatus } from './db.js'
4+
import { addImportStatus, loadImportStatuses } from './db.js'
55

66

77
const notesDir = '/home/phpusr/tmp/notes/NOTES/car'
88
const importedNotes = {}
99
await importNotes()
1010

1111
async function importNotes() {
12+
const importedNoteList = await loadImportStatuses()
13+
importedNoteList.forEach(noteInfo => importedNotes[noteInfo.filePath] = noteInfo)
14+
console.info(`\nLoaded already imported ${importedNoteList.length} notes`)
15+
1216
// noinspection JSValidateTypes
1317
const files = readdirSync(notesDir, { recursive: true })
1418
.filter(it => lstatSync(`${notesDir}/${it}`).isFile())
@@ -17,7 +21,7 @@ async function importNotes() {
1721
for (const relFilePath of files) {
1822
const progress = Math.round(++fileIndex / files.length * 100)
1923
console.log(`\n[${progress}%][${fileIndex}/${files.length}] ${relFilePath}`)
20-
console.log('-'.repeat(30) + '\n')
24+
console.log('-'.repeat(100) + '\n')
2125

2226
const parentId = await createParentNotes(relFilePath)
2327

@@ -38,6 +42,8 @@ async function importNotes() {
3842
//console.log(noteInfo)
3943
await saveNoteToNotion(noteInfo)
4044
}
45+
46+
console.info('\nDONE')
4147
}
4248

4349
async function createParentNotes(relFilePath) {
@@ -61,7 +67,7 @@ async function createParentNotes(relFilePath) {
6167

6268
async function saveNoteToNotion(noteInfo) {
6369
if (importedNotes.hasOwnProperty(noteInfo.filePath)) {
64-
console.info(`Note "${noteInfo.filePath}" already exists`)
70+
console.info(` - Note "${noteInfo.filePath}" already exists`)
6571
return importedNotes[noteInfo.filePath]
6672
}
6773

@@ -76,7 +82,7 @@ async function saveNoteToNotion(noteInfo) {
7682
await addBlocks({parentId: page.id, blocks})
7783
} catch (e) {
7884
noteInfo.imported = false
79-
noteInfo.error = e
85+
noteInfo.error = e.body || e
8086
console.error('ERROR')
8187
}
8288
}

src/notion_api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function createPage({ title, pageId }) {
1313
if (!rootPageId) {
1414
// Creating root page with date
1515
rootPageId = (await createPage({
16-
title: new Date().toLocaleDateString(),
16+
title: new Date().toLocaleString(),
1717
pageId: ROOT_PAGE_ID
1818
})).id
1919
}

0 commit comments

Comments
 (0)