From 824ba78659740c1c9255b4f599ba9e5fc988498f Mon Sep 17 00:00:00 2001 From: mmouterde Date: Thu, 28 Apr 2022 09:50:12 +0200 Subject: [PATCH] fix https://github.com/pubkey/rxdb/pull/3778 --- src/plugins/dexie/dexie-helper.ts | 45 ++++++++--------- test/unit.test.ts | 1 + test/unit/dexie-helper.test.ts | 82 +++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 22 deletions(-) create mode 100644 test/unit/dexie-helper.test.ts diff --git a/src/plugins/dexie/dexie-helper.ts b/src/plugins/dexie/dexie-helper.ts index f50a24d0717..b89d28c2e15 100644 --- a/src/plugins/dexie/dexie-helper.ts +++ b/src/plugins/dexie/dexie-helper.ts @@ -157,36 +157,37 @@ export function dexieReplaceIfStartsWithPipeRevert(str: string): string { * @recursive */ export function fromStorageToDexie(documentData: RxDocumentData): any { - if (!documentData) { + if (!documentData || typeof documentData === 'string' || typeof documentData === 'number' || typeof documentData === 'boolean') { return documentData; - } - if (Array.isArray(documentData)) { + } else if (Array.isArray(documentData)) { return documentData.map(row => fromStorageToDexie(row)); + } else if (typeof documentData === 'object') { + const ret: any = {}; + Object.entries(documentData).forEach(([key, value]) => { + if (typeof value === 'object') { + value = fromStorageToDexie(value); + } + ret[dexieReplaceIfStartsWithPipe(key)] = value; + }); + return ret; } - - const ret: any = {}; - Object.entries(documentData).forEach(([key, value]) => { - if (typeof value === 'object') { - value = fromStorageToDexie(value); - } - ret[dexieReplaceIfStartsWithPipe(key)] = value; - }); - return ret; } export function fromDexieToStorage(documentData: any): RxDocumentData { - if (Array.isArray(documentData)) { + if (!documentData || typeof documentData === 'string' || typeof documentData === 'number' || typeof documentData === 'boolean') { + return documentData; + } else if (Array.isArray(documentData)) { return documentData.map(row => fromDexieToStorage(row)); + } else if (typeof documentData === 'object') { + const ret: any = {}; + Object.entries(documentData).forEach(([key, value]) => { + if (typeof value === 'object' || Array.isArray(documentData)) { + value = fromDexieToStorage(value); + } + ret[dexieReplaceIfStartsWithPipeRevert(key)] = value; + }); + return ret; } - - const ret: any = {}; - Object.entries(documentData).forEach(([key, value]) => { - if (typeof value === 'object') { - value = fromStorageToDexie(value); - } - ret[dexieReplaceIfStartsWithPipeRevert(key)] = value; - }); - return ret; } diff --git a/test/unit.test.ts b/test/unit.test.ts index e9232478229..6686e58ab96 100644 --- a/test/unit.test.ts +++ b/test/unit.test.ts @@ -51,3 +51,4 @@ import './unit/cross-instance.test.js'; import './unit/server.test.js'; import './unit/plugin.test.js'; import './unit/last.test.js'; +import './unit/dexie-helper.test.js' diff --git a/test/unit/dexie-helper.test.ts b/test/unit/dexie-helper.test.ts new file mode 100644 index 00000000000..bf3bd891eb4 --- /dev/null +++ b/test/unit/dexie-helper.test.ts @@ -0,0 +1,82 @@ +import assert from 'assert'; + +import config from './config'; +import { + addRxPlugin, + + +} from '../../'; + +import {RxDBKeyCompressionPlugin} from '../../plugins/key-compression'; + +addRxPlugin(RxDBKeyCompressionPlugin); +import {RxDBValidatePlugin} from '../../plugins/validate'; +import {fromStorageToDexie, fromDexieToStorage} from '../../plugins/dexie'; + +addRxPlugin(RxDBValidatePlugin); + + +/** + * Dexie Helper tests + */ +config.parallel('dexie-helper.test.js', () => { + if (config.storage.name !== 'dexie') { + return; + } + describe('.fromStorageToDexie()', () => { + it('should convert unsupported IndexedDB key', () => { + const result = fromStorageToDexie( + { + '|key': 'value', + '|objectArray': [{['|id']: '1'}], + '|nestedObject': { + key: 'value2', + '|objectArray': [{'|id': '2'}], + stringArray: ['415', '51'], + '|numberArray': [1, 2, 3], + '|falsyValue': null + } + } + ); + assert.deepStrictEqual(result, { + '__key': 'value', + '__objectArray': [{['__id']: '1'}], + '__nestedObject': { + key: 'value2', + '__objectArray': [{'__id': '2'}], + stringArray: ['415', '51'], + '__numberArray': [1, 2, 3], + '__falsyValue': null + } + }); + }); + }); + describe('.fromDexieToStorage()', () => { + it('should revert escaped unsupported IndexedDB key', () => { + const result = fromDexieToStorage({ + '__key': 'value', + '__objectArray': [{['__id']: '1'}], + '__nestedObject': { + key: 'value2', + '__objectArray': [{'__id': '2'}], + stringArray: ['415', '51'], + '__numberArray': [1, 2, 3], + '__falsyValue': null + } + } + ); + assert.deepStrictEqual(result, + { + '|key': 'value', + '|objectArray': [{['|id']: '1'}], + '|nestedObject': { + key: 'value2', + '|objectArray': [{'|id': '2'}], + stringArray: ['415', '51'], + '|numberArray': [1, 2, 3], + '|falsyValue': null + } + }); + }); + }); +});