2022-11-26 18:36:55 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
import PouchDB from 'pouchdb';
|
|
|
|
import PouchDBFind from 'pouchdb-find';
|
|
|
|
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
|
|
|
|
|
|
|
PouchDB.plugin(PouchDBFind);
|
|
|
|
|
|
|
|
const dbDefinitionId = uri('dbdef', {});
|
|
|
|
|
|
|
|
const currentDbDefinition = {
|
|
|
|
_id: dbDefinitionId,
|
|
|
|
type: dbDefinitionId,
|
|
|
|
def: { version: '0.000001' },
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
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-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-12-10 20:25:15 +00:00
|
|
|
// revs_limit: 10,
|
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
|
|
|
await db.viewCleanup();
|
|
|
|
|
|
|
|
// WARNING: defs must use the canonical form and be identical to what will be returned by db.getIndexes
|
|
|
|
const requiredIndexes: any = [
|
|
|
|
{
|
|
|
|
name: 'type',
|
|
|
|
def: {
|
2022-12-10 15:52:52 +00:00
|
|
|
fields: [{ type: 'asc' }],
|
2022-11-26 18:36:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const existingIndexes = (await db.getIndexes()).indexes;
|
|
|
|
|
|
|
|
const pruneIndex = ({ name, def }: any) => ({ name, def });
|
|
|
|
|
|
|
|
const isSameIndex = (idx1: any, idx2: any) => {
|
|
|
|
return _.isEqual(pruneIndex(idx1), pruneIndex(idx2));
|
|
|
|
};
|
|
|
|
|
|
|
|
const findIndex = (targetIndexes: any, index: any) =>
|
|
|
|
targetIndexes.find((targetIndex: any) => {
|
|
|
|
return isSameIndex(targetIndex, index);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var index of existingIndexes) {
|
|
|
|
if (index.type === 'json') {
|
|
|
|
// Non system indexes
|
|
|
|
// console.log(`Checking existing index :${JSON.stringify(index)}`);
|
|
|
|
if (!findIndex(requiredIndexes, index)) {
|
|
|
|
// console.log(`db.deleteIndex(${JSON.stringify(index)})`);
|
|
|
|
await db.deleteIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index of requiredIndexes) {
|
|
|
|
if (!findIndex(existingIndexes, index)) {
|
|
|
|
// console.log(`db.createIndex(${JSON.stringify(index)})`);
|
|
|
|
await db.createIndex({ name: index.name, ...index.def });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.dbReady = true;
|
|
|
|
|
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
|
|
|
/* const indexes = await db.getIndexes();
|
|
|
|
console.log(`indexes: ${JSON.stringify(indexes)}`);
|
|
|
|
|
|
|
|
const explain1 = await db.explain({
|
|
|
|
selector: {
|
|
|
|
type: 'trkpt',
|
|
|
|
gpx: 'xxxx',
|
|
|
|
},
|
|
|
|
// sort: ['trkpt.time'],
|
|
|
|
// use_index: 'type-trkpt-gpx-time',
|
|
|
|
});
|
|
|
|
console.log(`explain1: ${JSON.stringify(explain1)}`);
|
|
|
|
const explain2 = await db.explain({
|
|
|
|
selector: {
|
|
|
|
type: 'gpx',
|
|
|
|
},
|
|
|
|
// sort: ['trkpt.time'],
|
|
|
|
// use_index: 'type-trkpt-gpx-time',
|
|
|
|
});
|
|
|
|
console.log(`explain2: ${JSON.stringify(explain2)}`);
|
|
|
|
*/
|
|
|
|
};
|