Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mmouterde committed May 4, 2022
1 parent c8d7157 commit 824ba78
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/plugins/dexie/dexie-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,36 +157,37 @@ export function dexieReplaceIfStartsWithPipeRevert(str: string): string {
* @recursive
*/
export function fromStorageToDexie(documentData: RxDocumentData<any>): 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<any> {
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;
}


Expand Down
1 change: 1 addition & 0 deletions test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
82 changes: 82 additions & 0 deletions test/unit/dexie-helper.test.ts
Original file line number Diff line number Diff line change
@@ -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
}
});
});
});
});

0 comments on commit 824ba78

Please sign in to comment.