Skip to content

Commit 122a3e9

Browse files
committed
ADD RxDocument.toJSON() is leaking _deleted
1 parent 00ffdfa commit 122a3e9

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
### X.X.X (comming soon)
44

5+
Bugfixes:
6+
- `RxDocument.toJSON()` is leaking meta field `_deleted`. [#3645](https://github.com/pubkey/rxdb/pull/3645) Thanks [@Bessonov](https://github.com/Bessonov)
7+
58
Features:
69
- Allow truthy values for the GraphQL replication `deletedFlag` field. [#3644](https://github.com/pubkey/rxdb/pull/3644) Thanks [@nirvdrum](https://github.com/nirvdrum)
710

docs-src/rx-document.md

+15
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ console.dir(json);
158158
*/
159159
```
160160

161+
You can also set `withMetaFields: true` to get additional meta fields like the revision, attachments or the deleted flag.
162+
163+
```js
164+
const json = myDocument.toJSON(true);
165+
console.dir(json);
166+
/* { passportId: 'h1rg9ugdd30o',
167+
firstName: 'Carolina',
168+
lastName: 'Gibson',
169+
_deleted: false,
170+
_attachments: { ... },
171+
_rev: '1-aklsdjfhaklsdjhf...'
172+
*/
173+
```
174+
175+
161176
### set()
162177
**Only temporary documents**
163178

src/rx-document.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,12 @@ export const basePrototype = {
238238
return valueObj;
239239
},
240240

241-
toJSON(this: RxDocument, withRevAndAttachments = false) {
242-
if (!withRevAndAttachments) {
241+
toJSON(this: RxDocument, withMetaFields = false) {
242+
if (!withMetaFields) {
243243
const data = flatClone(this._data);
244244
delete (data as any)._rev;
245245
delete (data as any)._attachments;
246+
delete (data as any)._deleted;
246247
return overwritable.deepFreezeWhenDevMode(data);
247248
} else {
248249
return overwritable.deepFreezeWhenDevMode(this._data);

test/unit/rx-document.test.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -687,21 +687,28 @@ config.parallel('rx-document.test.js', () => {
687687
});
688688
it('should get a fresh object each time', async () => {
689689
const c = await humansCollection.create(1);
690-
const doc: any = await c.findOne().exec();
690+
const doc = await c.findOne().exec(true);
691691
const json = doc.toJSON();
692692
const json2 = doc.toJSON();
693693
assert.ok(json !== json2);
694694
c.database.destroy();
695695
});
696-
it('should not return _rev if not wanted', async () => {
697-
const c = await humansCollection.create(1);
698-
const doc: any = await c.findOne().exec();
699-
const json = doc.toJSON(
700-
false // no ._rev
701-
);
702-
assert.ok(json.passportId);
703-
assert.ok(json.firstName);
704-
assert.strictEqual(typeof json._rev, 'undefined');
696+
it('should not return meta fields if not wanted', async () => {
697+
const c = await humansCollection.create(0);
698+
await c.insert({
699+
passportId: 'aatspywninca',
700+
firstName: 'Tester',
701+
lastName: 'Test',
702+
age: 10
703+
});
704+
const newHuman = await c.findOne('aatspywninca').exec(true);
705+
const jsonWithWithoutMetaFields = newHuman.toJSON();
706+
707+
const metaField = Object.keys(jsonWithWithoutMetaFields).find(key => key.startsWith('_'));
708+
if (metaField) {
709+
throw new Error('should not contain meta field ' + metaField);
710+
}
711+
705712
c.database.destroy();
706713
});
707714
it('should not return _attachments if not wanted', async () => {

0 commit comments

Comments
 (0)