Reimplementing GPX imports.
This commit is contained in:
1 parent
a2d0e8cf6a
commit
5fdd63103e
8 files changed
+148
-5
No files matched your search
+25
-1
@@ -1,4 +1,5 @@
|
||||
import { putNewGpx } from './gpx';
|
||||
import { initDb } from '.';
|
||||
import { existsGpx, putNewGpx } from './gpx';
|
||||
declare global {
|
||||
var db: any;
|
||||
var dbReady: boolean;
|
||||
@@ -60,3 +61,26 @@ describe('The gpx module', () => {
|
||||
expect(id).toEqual({ gpx: 4320000000000000 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('The gpx module with a real db', () => {
|
||||
beforeEach(async () => {
|
||||
await initDb({});
|
||||
globalThis.Date.now = () => 0;
|
||||
});
|
||||
afterEach(async () => {
|
||||
await db.destroy();
|
||||
db = undefined;
|
||||
globalThis.Date.now = originalDateNow;
|
||||
});
|
||||
|
||||
test("existsGpx returns false if the GPX doesn't exist", async () => {
|
||||
const exists = await existsGpx({ gpx: 1 });
|
||||
expect(exists).toBeFalsy();
|
||||
});
|
||||
test('existsGpx returns false if the GPX exists', async () => {
|
||||
const id = { gpx: 1 };
|
||||
await putNewGpx(id);
|
||||
const exists = await existsGpx(id);
|
||||
expect(exists).toBeTruthy();
|
||||
});
|
||||
});
|
||||
+53
-1
@@ -1,5 +1,6 @@
|
||||
import { PureComponent } from 'react';
|
||||
import getUri, { intToGpxId } from '../lib/ids';
|
||||
import { put } from './lib';
|
||||
import { get, put, putAll } from './lib';
|
||||
|
||||
const emptyGpx: Gpx = {
|
||||
$: {
|
||||
@@ -46,3 +47,54 @@ export const putNewGpx = async (
|
||||
);
|
||||
return id;
|
||||
};
|
||||
|
||||
export const existsGpx = async (id: IdGpx) => {
|
||||
const uri = getUri('gpx', id);
|
||||
try {
|
||||
await get(uri);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const prune = (id: any, object: any, docs: any[]) => {
|
||||
if (typeof object === 'object') {
|
||||
for (const key in object) {
|
||||
if (
|
||||
key === 'wpt' ||
|
||||
key === 'rte' ||
|
||||
key === 'rtept' ||
|
||||
key === 'trk' ||
|
||||
key === 'trkseg' ||
|
||||
key === 'trkpt'
|
||||
) {
|
||||
const subObjects = object[key];
|
||||
for (const index in subObjects) {
|
||||
const subId = { ...id };
|
||||
subId[key] = index;
|
||||
docs.push({
|
||||
_id: getUri(key, subId),
|
||||
type: key,
|
||||
doc: subObjects[index],
|
||||
});
|
||||
prune(subId, subObjects[index], docs);
|
||||
}
|
||||
object[key] = undefined;
|
||||
} else prune(id, object[key], docs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const pruneAndSaveImportedGpx = async (params: any) => {
|
||||
const { id, gpx } = params;
|
||||
let docs: any[] = [{ _id: getUri('gpx', id), type: 'gpx', doc: gpx }];
|
||||
prune(id, gpx, docs);
|
||||
console.log(JSON.stringify(docs));
|
||||
try {
|
||||
const result = await putAll(docs);
|
||||
console.log(JSON.stringify(result));
|
||||
} catch (err) {
|
||||
console.error(`error: ${err}`);
|
||||
}
|
||||
};
|
||||
@@ -38,3 +38,11 @@ export const getFamily = async (key: string, options: any = {}) => {
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
export const get = async (id: string) => {
|
||||
await db.get(id);
|
||||
};
|
||||
|
||||
export const putAll = async (docs: any[]) => {
|
||||
return await db.bulkDocs(docs);
|
||||
};
|
||||
Reference in new issue
Block a user