2022-11-26 18:36:55 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
import PouchDB from 'pouchdb';
|
|
|
|
import uri from '../lib/ids';
|
2022-12-05 17:49:36 +00:00
|
|
|
import changeHandler from './change-handler';
|
2022-11-26 18:36:55 +00:00
|
|
|
|
|
|
|
const dbDefinitionId = uri('dbdef', {});
|
|
|
|
|
|
|
|
const currentDbDefinition = {
|
|
|
|
_id: dbDefinitionId,
|
|
|
|
type: dbDefinitionId,
|
|
|
|
def: { version: '0.000001' },
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
2022-12-24 15:39:55 +00:00
|
|
|
var localDb: any;
|
2022-11-26 18:36:55 +00:00
|
|
|
var db: any;
|
|
|
|
var dbReady: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const initDb = async (params: any) => {
|
2022-12-05 17:49:36 +00:00
|
|
|
console.log({ caller: 'initDb' });
|
|
|
|
|
2022-12-24 15:39:55 +00:00
|
|
|
if (globalThis.localDb === undefined) {
|
|
|
|
globalThis.localDb = new PouchDB('$local_dyomedea$', {
|
|
|
|
auto_compaction: true,
|
|
|
|
revs_limit: 10,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:36:55 +00:00
|
|
|
if (globalThis.db === undefined) {
|
|
|
|
globalThis.db = new PouchDB('dyomedea', {
|
2022-11-30 10:15:53 +00:00
|
|
|
auto_compaction: false,
|
2022-11-26 18:36:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const db = globalThis.db;
|
|
|
|
var previousDbDefinition = {
|
|
|
|
_id: dbDefinitionId,
|
|
|
|
type: dbDefinitionId,
|
|
|
|
def: { version: '0' },
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
previousDbDefinition = await db.get(dbDefinitionId);
|
|
|
|
} catch (error: any) {
|
|
|
|
if (error.status !== 404) {
|
|
|
|
console.log(
|
|
|
|
`Unexpected error fetching db definition: ${JSON.stringify(error)}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousDbDefinition.def.version < currentDbDefinition.def.version) {
|
|
|
|
previousDbDefinition.def = currentDbDefinition.def;
|
|
|
|
db.put(previousDbDefinition);
|
|
|
|
// TODO: support migrations
|
|
|
|
}
|
2022-11-30 10:15:53 +00:00
|
|
|
//await await db.compact();
|
2022-11-26 18:36:55 +00:00
|
|
|
|
|
|
|
globalThis.dbReady = true;
|
|
|
|
|
2022-12-23 14:41:03 +00:00
|
|
|
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 });
|
|
|
|
});
|
|
|
|
|
2022-12-05 17:49:36 +00:00
|
|
|
console.log({ caller: 'initDb / before db.changes' });
|
|
|
|
|
|
|
|
const changes = db
|
2022-12-06 13:36:50 +00:00
|
|
|
.changes({ since: 'now', live: true, include_docs: false })
|
2022-12-05 17:49:36 +00:00
|
|
|
.on('change', changeHandler)
|
|
|
|
.on('complete', (info: any) => {
|
|
|
|
console.log({ caller: 'changes / complete', info });
|
|
|
|
})
|
|
|
|
.on('error', (error: any) => {
|
|
|
|
console.log({ caller: 'changes / complete', error });
|
|
|
|
});
|
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
// console.log({ caller: 'initDb / back from db.changes', changes });
|
2022-12-05 17:49:36 +00:00
|
|
|
|
|
|
|
// changes.cancel();
|
2022-11-26 18:36:55 +00:00
|
|
|
};
|