Skip to content

Commit 49aced2

Browse files
committed
Store high level arrays as __dsList
1 parent 17682ea commit 49aced2

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

CHANGELOG.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
[1.0.1] 2016-07-13
1+
## [1.0.2] 2016-07-25
22

3-
Bug Fix
3+
### Bug Fix
44

5-
Storage connector no longer mutates original object state
5+
###### Storage connector stores high level arrays under __dsList ( records should only contain objects and not arrays on a highlevel )
66

7-
[1.0.0] 2016-07-01
7+
## [1.0.1] 2016-07-13
8+
9+
### Bug Fix
10+
11+
###### Storage connector no longer mutates original object state
12+
13+
## [1.0.0] 2016-07-01

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deepstream.io-storage-rethinkdb",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Connects deepstream.io to rethinkdb",
55
"main": "src/connector.js",
66
"scripts": {

src/transform-data.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use strict"
22

33
/**
4+
* This method is for the storage connector, to allow queries to happen more naturally
5+
* do not use in cache connectors
6+
*
47
* Inverts the data from the deepstream structure to reduce nesting.
58
*
69
* { _v: 1, _d: { name: 'elasticsearch' } } -> { name: 'elasticsearch', __ds = { _v: 1 } }
@@ -12,13 +15,26 @@
1215
*/
1316
module.exports.transformValueForStorage = function ( value ) {
1417
value = JSON.parse( JSON.stringify( value ) )
18+
1519
var data = value._d
1620
delete value._d
17-
data.__ds = value
21+
22+
if( data instanceof Array ) {
23+
data = {
24+
__dsList: data,
25+
__ds: value
26+
}
27+
} else {
28+
data.__ds = value
29+
}
30+
1831
return data
1932
}
2033

2134
/**
35+
* This method is for the storage connector, to allow queries to happen more naturally
36+
* do not use in cache connectors
37+
*
2238
* Inverts the data from the stored structure back to the deepstream structure
2339
*
2440
* { name: 'elasticsearch', __ds = { _v: 1 } } -> { _v: 1, _d: { name: 'elasticsearch' } }
@@ -29,8 +45,16 @@ module.exports.transformValueForStorage = function ( value ) {
2945
* @returns {Object} data
3046
*/
3147
module.exports.transformValueFromStorage = function( value ) {
48+
value = JSON.parse( JSON.stringify( value ) )
49+
3250
var data = value.__ds
3351
delete value.__ds
34-
data._d = value
52+
53+
if( value.__dsList instanceof Array ) {
54+
data._d = value.__dsList
55+
} else {
56+
data._d = value
57+
}
58+
3559
return data
3660
}

test/transform-dataSpec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* global describe, expect, it, jasmine */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const TransformData = require( '../src/transform-data' )
6+
7+
describe( 'Transforms outgoing data', () => {
8+
9+
it( 'Transforms objects', () => {
10+
const result = TransformData.transformValueForStorage( {
11+
_d: { firstname: "John", lastname: "Smith" },
12+
_v: 12
13+
} )
14+
expect( result ).to.deep.equal( {
15+
"__ds": {
16+
"_v": 12
17+
},
18+
"firstname": "John",
19+
"lastname": "Smith"
20+
} )
21+
} )
22+
23+
it( 'Transforms Lists', () => {
24+
const result = TransformData.transformValueForStorage( {
25+
_d: [ "John", "Smith" ],
26+
_v: 12
27+
} )
28+
expect( result ).to.deep.equal( {
29+
"__ds": {
30+
"_v": 12
31+
},
32+
"__dsList": [ "John", "Smith" ]
33+
} )
34+
} )
35+
} )
36+
37+
describe( 'Transforms incoming data', () => {
38+
39+
it( 'Transforms objects', () => {
40+
const result = TransformData.transformValueFromStorage( {
41+
"__ds": {
42+
"_v": 12
43+
},
44+
"firstname": "John",
45+
"lastname": "Smith"
46+
} )
47+
expect( result ).to.deep.equal( {
48+
_d: { firstname: "John", lastname: "Smith" },
49+
_v: 12
50+
} )
51+
} )
52+
53+
it( 'Transforms Lists', () => {
54+
const result = TransformData.transformValueFromStorage( {
55+
"__ds": {
56+
"_v": 12
57+
},
58+
"__dsList": [ "John", "Smith" ]
59+
} )
60+
expect( result ).to.deep.equal( {
61+
_d: [ "John", "Smith" ],
62+
_v: 12
63+
} )
64+
} )
65+
} )

0 commit comments

Comments
 (0)