Skip to content

Instantly share code, notes, and snippets.



Last active Mar 1, 2019
What would you like to do?
Sync a document from Bear Writer's DB to a file on disk

Sync a document from Bear Writer's DB to a file on disk

I really like Bear Writer's interface, but it stores all its notes in an SQLite DB. I spent ages rigging up a nice Markdown + Webpack pipeline for a React blog before I realised this, and rather than admit my mistake, potentially pushing forward to concentrate on the content rather than procrastinating on the pipeline, I built this.

Screenshot showing copying the doc id

Grab the Document ID from Bear, then run:

yarn add sqlite3 untildify
node sync-bear.js DOC-ID path/to/file.js

Screenshot of program running

👋 -glen.

const fs = require('fs')
const path = require('path')
const sqlite3 = require('sqlite3').verbose()
const untildify = require('untildify')
const [_,__, docId, filename,] = process.argv
const bail = msg => {
if (!docId || !filename || rest.length > 0) bail("Usage: node sync-bear.js DOC-ID-FROM-BEAR src/to/")
const BEAR_DB_PATH = untildify("~/Library/Containers/net.shinyfrog.bear/Data/Documents/Application Data/database.sqlite")
if (!fs.existsSync(BEAR_DB_PATH)) bail(`Bear DB not found, expecting it to be at: ${BEAR_DB_PATH}`)
const db = new sqlite3.Database(BEAR_DB_PATH)
let writing = false
const write_to_file = contents => {
writing = true
fs.writeFile(path.resolve(filename), contents, err => {
if (err) bail(`Error writing file: ${err}`)
process.stdout.write(" Done!\n")
writing = false
let last_contents = null
let since_last_update = 0
const exec_query = () => {
if (writing) return setTimeout(exec_query, 500)
db.get(`select ZTEXT from ZSFNOTE where ZUNIQUEIDENTIFIER='${docId}'`, (err, {ZTEXT}) => {
if (err) bail(`Error accessing DB: ${err}`)
if (last_contents !== ZTEXT) {
last_contents = ZTEXT
since_last_update = 0
setTimeout(exec_query, 100 + Math.log(++since_last_update) * 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment