diff --git a/src/db/index.ts b/src/db/index.ts index 93323d8..e713ea3 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,6 +1,7 @@ import _ from 'lodash'; import PouchDB from 'pouchdb'; import uri from '../lib/ids'; +import { toHex } from '../lib/to-hex'; import { getAccountById, getAccounts, @@ -81,38 +82,47 @@ export const initDb = async (params: any) => { } //await await db.compact(); - // const sync = PouchDB.sync( - // 'dyomedea', - // 'http://admin:password@localhost:5984/dyomedea', - // { - // live: true, - // retry: true, - // } - // ) - // .on('change', function (info) { - // // handle change - // console.log({ caller: 'Sync / change', info }); - // }) - // .on('paused', function (err) { - // // replication paused (e.g. replication up to date, user went offline) - // console.log({ caller: 'Sync / paused', err }); - // }) - // .on('active', function () { - // // replicate resumed (e.g. new changes replicating, user went back online) - // console.log({ caller: 'Sync / active' }); - // }) - // .on('denied', function (err) { - // // a document failed to replicate (e.g. due to permissions) - // console.error({ caller: 'Sync / denied', err }); - // }) - // .on('complete', function (info) { - // // handle complete - // console.log({ caller: 'Sync / complete', info }); - // }) - // .on('error', function (err) { - // // handle error - // console.error({ caller: 'Sync / error', err }); - // }); + if (currentAccount.remoteDbServer) { + const url = `${currentAccount.remoteDbServer}/userdb-${toHex( + currentAccount.remoteDbUser + )}`; + + const remoteDb = new PouchDB(url, { + auth: { + username: currentAccount.remoteDbUser, + password: currentAccount.remoteDbPassword, + }, + skip_setup: true, + }); + const sync = PouchDB.sync(db, remoteDb, { + live: true, + retry: true, + }) + .on('change', function (info) { + // handle change + console.log({ caller: 'Sync / change', info }); + }) + .on('paused', function (err) { + // replication paused (e.g. replication up to date, user went offline) + console.log({ caller: 'Sync / paused', err }); + }) + .on('active', function () { + // replicate resumed (e.g. new changes replicating, user went back online) + console.log({ caller: 'Sync / active' }); + }) + .on('denied', function (err) { + // a document failed to replicate (e.g. due to permissions) + console.error({ caller: 'Sync / denied', err }); + }) + .on('complete', function (info) { + // handle complete + console.log({ caller: 'Sync / complete', info }); + }) + .on('error', function (err) { + // handle error + console.error({ caller: 'Sync / error', err }); + }); + } // console.log({ caller: 'initDb / before db.changes' }); diff --git a/src/lib/to-hex.ts b/src/lib/to-hex.ts new file mode 100644 index 0000000..b560480 --- /dev/null +++ b/src/lib/to-hex.ts @@ -0,0 +1,17 @@ +// see https://9to5answer.com/javascript-unicode-string-to-hex + +export const toHex = (str: string) => { + let hex; + try { + hex = unescape(encodeURIComponent(str)) + .split('') + .map(function (v) { + return v.charCodeAt(0).toString(16); + }) + .join(''); + } catch (e) { + hex = str; + console.log('invalid text input: ' + str); + } + return hex; +};