Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Vincent Bechu
jio
Commits
afe211a7
Commit
afe211a7
authored
Oct 26, 2016
by
Vincent Bechu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mappingstorage: upgrade code quality
parent
14304d63
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
66 deletions
+67
-66
src/jio.storage/mappingstorage.js
src/jio.storage/mappingstorage.js
+56
-57
test/jio.storage/mappingstorage.tests.js
test/jio.storage/mappingstorage.tests.js
+11
-9
No files found.
src/jio.storage/mappingstorage.js
View file @
afe211a7
/*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate */
/*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query*/
(
function
(
jIO
,
RSVP
)
{
"
use strict
"
;
...
...
@@ -10,6 +11,9 @@
this
.
_mapping_dict_attachment
=
spec
.
mapping_dict_attachment
||
{};
this
.
_query
=
spec
.
query
||
{};
if
(
this
.
_query
.
query
!==
undefined
)
{
this
.
_query
.
query
=
QueryFactory
.
create
(
this
.
_query
.
query
);
}
this
.
_id_is_mapped
=
(
this
.
_mapping_dict
.
id
!==
undefined
&&
this
.
_mapping_dict
.
id
.
equal
!==
"
id
"
);
var
property
,
query_list
=
[];
...
...
@@ -65,10 +69,19 @@
return
id
;
}
if
(
storage
.
_mapping_dict
.
id
.
equal
!==
undefined
)
{
query
=
storage
.
_mapping_dict
.
id
.
equal
+
'
: "
'
+
id
+
'
"
'
;
query
=
new
SimpleQuery
({
key
:
storage
.
_mapping_dict
.
id
.
equal
,
value
:
id
,
type
:
"
simple
"
});
if
(
storage
.
_query
.
query
!==
undefined
)
{
query
+=
'
AND
'
+
storage
.
_query
.
query
;
query
=
new
ComplexQuery
({
operator
:
"
AND
"
,
query_list
:
[
query
,
storage
.
_query
.
query
],
type
:
"
complex
"
});
}
query
=
Query
.
objectToSearchText
(
query
);
return
storage
.
_sub_storage
.
allDocs
({
"
query
"
:
query
,
"
sort_on
"
:
storage
.
_query
.
sort_on
,
...
...
@@ -129,7 +142,7 @@
return
false
;
}
function
mapDocument
(
storage
,
doc
,
delete_id
)
{
function
mapDocument
(
storage
,
doc
,
id_delete
)
{
var
mapped_doc
=
{},
property
,
property_list
=
[];
...
...
@@ -147,7 +160,7 @@
}
}
}
if
(
delete_id
)
{
if
(
id_delete
)
{
delete
mapped_doc
.
id
;
}
return
mapped_doc
;
...
...
@@ -174,13 +187,13 @@
}
MappingStorage
.
prototype
.
get
=
function
(
id
)
{
var
tha
t
=
this
;
var
contex
t
=
this
;
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
id_mappe
d
)
{
return
that
.
_sub_storage
.
get
(
id_mappe
d
);
.
push
(
function
(
sub_i
d
)
{
return
context
.
_sub_storage
.
get
(
sub_i
d
);
})
.
push
(
function
(
doc
)
{
return
mapDocument
(
tha
t
,
doc
,
true
);
return
mapDocument
(
contex
t
,
doc
,
true
);
})
.
push
(
undefined
,
function
(
error
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
+
id
...
...
@@ -198,20 +211,20 @@
};
MappingStorage
.
prototype
.
put
=
function
(
id
,
doc
)
{
var
tha
t
=
this
,
var
contex
t
=
this
,
mapped_doc
=
unmapDocument
(
this
,
doc
);
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
id_mappe
d
)
{
if
(
tha
t
.
_id_is_mapped
)
{
mapped_doc
[
tha
t
.
_mapping_dict
.
id
.
equal
]
=
id
;
.
push
(
function
(
sub_i
d
)
{
if
(
contex
t
.
_id_is_mapped
)
{
mapped_doc
[
contex
t
.
_mapping_dict
.
id
.
equal
]
=
id
;
}
if
(
id
===
undefined
)
{
throw
new
Error
();
}
return
that
.
_sub_storage
.
put
(
id_mappe
d
,
mapped_doc
);
return
context
.
_sub_storage
.
put
(
sub_i
d
,
mapped_doc
);
})
.
push
(
undefined
,
function
()
{
return
tha
t
.
_sub_storage
.
post
(
mapped_doc
);
return
contex
t
.
_sub_storage
.
post
(
mapped_doc
);
})
.
push
(
function
()
{
return
id
;
...
...
@@ -219,10 +232,10 @@
};
MappingStorage
.
prototype
.
remove
=
function
(
id
)
{
var
tha
t
=
this
;
var
contex
t
=
this
;
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
id_mappe
d
)
{
return
that
.
_sub_storage
.
remove
(
id_mappe
d
);
.
push
(
function
(
sub_i
d
)
{
return
context
.
_sub_storage
.
remove
(
sub_i
d
);
})
.
push
(
function
()
{
return
id
;
...
...
@@ -281,20 +294,11 @@
MappingStorage
.
prototype
.
bulk
=
function
(
id_list
)
{
var
i
,
tha
t
=
this
,
contex
t
=
this
,
mapped_result
=
[],
promise_list
=
id_list
.
map
(
function
(
parameter
)
{
return
getSubStorageId
(
tha
t
,
parameter
.
parameter_list
[
0
])
return
getSubStorageId
(
contex
t
,
parameter
.
parameter_list
[
0
])
.
push
(
function
(
id
)
{
if
(
parameter
.
method
===
"
put
"
)
{
return
{
"
method
"
:
parameter
.
method
,
"
parameter_list
"
:
[
id
,
unmapDocument
(
parameter
.
parameter_list
[
1
])
]
};
}
return
{
"
method
"
:
parameter
.
method
,
"
parameter_list
"
:
[
id
]};
});
});
...
...
@@ -304,18 +308,18 @@
return
RSVP
.
all
(
promise_list
);
})
.
push
(
function
(
id_list_mapped
)
{
return
tha
t
.
_sub_storage
.
bulk
(
id_list_mapped
);
return
contex
t
.
_sub_storage
.
bulk
(
id_list_mapped
);
})
.
push
(
function
(
result
)
{
for
(
i
=
0
;
i
<
result
.
length
;
i
+=
1
)
{
mapped_result
.
push
(
mapDocument
(
tha
t
,
result
[
i
],
false
));
mapped_result
.
push
(
mapDocument
(
contex
t
,
result
[
i
],
false
));
}
return
mapped_result
;
});
};
MappingStorage
.
prototype
.
buildQuery
=
function
(
option
)
{
var
tha
t
=
this
,
var
contex
t
=
this
,
i
,
query
,
property
,
...
...
@@ -323,27 +327,16 @@
sort_on
=
[];
function
mapQuery
(
one_query
)
{
var
i
,
result
=
"
(
"
,
key
;
var
i
,
query_list
=
[]
;
if
(
one_query
.
type
===
"
complex
"
)
{
for
(
i
=
0
;
i
<
one_query
.
query_list
.
length
;
i
+=
1
)
{
result
+=
"
(
"
+
mapQuery
(
one_query
.
query_list
[
i
])
+
"
)
"
;
if
(
i
<
one_query
.
query_list
.
length
-
1
)
{
result
+=
"
"
+
one_query
.
operator
+
"
"
;
}
}
result
+=
"
)
"
;
return
result
;
query_list
.
push
(
mapQuery
(
one_query
.
query_list
[
i
]));
}
if
(
that
.
_mapping_dict
.
hasOwnProperty
(
one_query
.
key
))
{
key
=
that
.
_mapping_dict
[
one_query
.
key
].
equal
;
}
else
{
if
(
that
.
_map_all_property
)
{
key
=
one_query
.
key
;
one_query
.
query_list
=
query_list
;
return
one_query
;
}
}
return
(
key
?
key
+
"
:
"
:
""
)
+
(
one_query
.
operator
?
"
"
+
one_query
.
operator
:
""
)
+
'
"
'
+
one_query
.
value
+
'
"
'
;
one_query
.
key
=
mapProperty
(
context
,
one_query
.
key
,
{},
{});
return
one_query
;
}
if
(
option
.
sort_on
!==
undefined
)
{
...
...
@@ -382,16 +375,22 @@
select_list
.
push
(
this
.
_mapping_dict
.
id
.
equal
);
}
if
(
option
.
query
!==
undefined
)
{
query
=
mapQuery
(
jIO
.
QueryFactory
.
create
(
option
.
query
));
query
=
mapQuery
(
QueryFactory
.
create
(
option
.
query
));
}
if
(
this
.
_query
.
query
!==
undefined
)
{
if
(
query
!==
undefined
)
{
query
+=
'
AND
'
;
}
else
{
query
=
""
;
if
(
query
===
undefined
)
{
query
=
this
.
_query
.
query
;
}
query
+=
this
.
_query
.
query
;
query
=
new
ComplexQuery
({
operator
:
"
AND
"
,
query_list
:
[
query
,
this
.
_query
.
query
],
type
:
"
complex
"
});
}
if
(
query
!==
undefined
)
{
query
=
Query
.
objectToSearchText
(
query
);
}
return
this
.
_sub_storage
.
allDocs
(
{
...
...
@@ -404,8 +403,8 @@
.
push
(
function
(
result
)
{
for
(
i
=
0
;
i
<
result
.
data
.
total_rows
;
i
+=
1
)
{
result
.
data
.
rows
[
i
].
value
=
mapDocument
(
tha
t
,
result
.
data
.
rows
[
i
].
value
,
false
);
if
(
result
.
data
.
rows
[
i
].
id
!==
undefined
&&
tha
t
.
_id_is_mapped
)
{
mapDocument
(
contex
t
,
result
.
data
.
rows
[
i
].
value
,
false
);
if
(
result
.
data
.
rows
[
i
].
id
!==
undefined
&&
contex
t
.
_id_is_mapped
)
{
result
.
data
.
rows
[
i
].
id
=
result
.
data
.
rows
[
i
].
value
.
id
;
delete
result
.
data
.
rows
[
i
].
value
.
id
;
...
...
test/jio.storage/mappingstorage.tests.js
View file @
afe211a7
...
...
@@ -48,12 +48,14 @@
},
mapping_dict
:
{
"
bar
"
:
{
"
equal
"
:
"
foo
"
}},
map_all_property
:
true
,
query
:
{
"
query
"
:
"
foo
"
},
query
:
{
"
query
"
:
'
foo: "bar"
'
},
mapping_dict_attachment
:
{
"
foo
"
:
{
"
get
"
:
"
bar
"
}}
});
deepEqual
(
jio
.
__storage
.
_mapping_dict
,
{
"
bar
"
:
{
"
equal
"
:
"
foo
"
}});
deepEqual
(
jio
.
__storage
.
_query
,
{
"
query
"
:
"
foo
"
});
equal
(
jio
.
__storage
.
_query
.
query
.
key
,
"
foo
"
);
equal
(
jio
.
__storage
.
_query
.
query
.
value
,
"
bar
"
);
equal
(
jio
.
__storage
.
_query
.
query
.
type
,
"
simple
"
);
deepEqual
(
jio
.
__storage
.
_mapping_dict_attachment
,
{
"
foo
"
:
{
"
get
"
:
"
bar
"
}});
equal
(
jio
.
__storage
.
_map_all_property
,
true
);
});
...
...
@@ -189,7 +191,7 @@
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
equal
(
options
.
query
,
'
otherId: "42" AND otherTitle: "foo"
'
,
'
( otherId: "42" AND otherTitle: "foo" )
'
,
"
allDoc 2713 called
"
);
return
[{
id
:
"
2713
"
}];
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment