From ad9b70173f2bd9e3ea84eea2fd924f889b26554f Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 26 Dec 2022 18:04:01 +0100 Subject: [PATCH] First working version with the account dialog. --- src/db/index.ts | 74 +++++++++++++++++++++++++++-------------------- src/lib/to-hex.ts | 17 +++++++++++ 2 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 src/lib/to-hex.ts 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; +};