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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Aurel
jio
Commits
a7d9f005
Commit
a7d9f005
authored
Oct 28, 2016
by
Vincent Bechu
Committed by
Aurel
Feb 23, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mappingstorage: fix post, add test, and change function names
parent
22daf6dc
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
253 additions
and
176 deletions
+253
-176
src/jio.storage/mappingstorage.js
src/jio.storage/mappingstorage.js
+62
-54
test/jio.storage/mappingstorage.tests.js
test/jio.storage/mappingstorage.tests.js
+191
-122
No files found.
src/jio.storage/mappingstorage.js
View file @
a7d9f005
...
...
@@ -7,21 +7,25 @@
function
MappingStorage
(
spec
)
{
this
.
_mapping_dict
=
spec
.
mapping_dict
||
{};
this
.
_sub_storage
=
jIO
.
createJIO
(
spec
.
sub_storage
);
this
.
_map_all_property
=
spec
.
map_all_property
||
false
;
this
.
_mapping_dict_attachment
=
spec
.
mapping_dict_attachment
||
{};
this
.
_map_all_property
=
spec
.
map_all_property
!==
undefined
?
spec
.
map_all_property
:
true
;
this
.
_attachment_mapping_dict
=
spec
.
attachment_mapping_dict
||
{};
this
.
_query
=
spec
.
query
||
{};
if
(
this
.
_query
.
query
!==
undefined
)
{
this
.
_query
.
query
=
QueryFactory
.
create
(
this
.
_query
.
query
);
}
this
.
_default_mapping
=
{};
this
.
_id_is_mapped
=
(
this
.
_mapping_dict
.
id
!==
undefined
&&
this
.
_mapping_dict
.
id
.
equal
!==
"
id
"
);
var
property
,
query_list
=
[];
// handle default_value.
// handle default_value.
for
(
property
in
this
.
_mapping_dict
)
{
if
(
this
.
_mapping_dict
.
hasOwnProperty
(
property
))
{
if
(
this
.
_mapping_dict
[
property
].
default_value
!==
undefined
)
{
this
.
_default_mapping
[
property
]
=
this
.
_mapping_dict
[
property
].
default_value
;
query_list
.
push
(
new
SimpleQuery
({
key
:
property
,
value
:
this
.
_mapping_dict
[
property
].
default_value
,
...
...
@@ -47,7 +51,7 @@
}
function
getAttachmentId
(
storage
,
sub_id
,
attachment_id
,
method
)
{
var
mapping_dict
=
storage
.
_
mapping_dict_attachmen
t
;
var
mapping_dict
=
storage
.
_
attachment_mapping_dic
t
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
if
(
mapping_dict
!==
undefined
...
...
@@ -106,13 +110,19 @@
});
}
function
unmapProperty
(
storage
,
property
,
doc
,
mapped_doc
)
{
if
(
storage
.
_mapping_dict
[
property
].
equal
!==
undefined
)
{
doc
[
storage
.
_mapping_dict
[
property
].
equal
]
=
mapped_doc
[
property
];
return
storage
.
_mapping_dict
[
property
].
equal
;
function
mapToSubProperty
(
storage
,
property
,
sub_doc
,
doc
)
{
if
(
storage
.
_mapping_dict
[
property
]
!==
undefined
)
{
if
(
storage
.
_mapping_dict
[
property
].
equal
!==
undefined
)
{
sub_doc
[
storage
.
_mapping_dict
[
property
].
equal
]
=
doc
[
property
];
return
storage
.
_mapping_dict
[
property
].
equal
;
}
if
(
storage
.
_mapping_dict
[
property
].
default_value
!==
undefined
)
{
sub_doc
[
property
]
=
storage
.
_mapping_dict
[
property
].
default_value
;
return
property
;
}
}
if
(
storage
.
_map
ping_dict
[
property
].
default_value
!==
undefined
)
{
doc
[
property
]
=
storage
.
_mapping_dict
[
property
].
default_value
;
if
(
storage
.
_map
_all_property
)
{
sub_doc
[
property
]
=
doc
[
property
]
;
return
property
;
}
throw
new
jIO
.
util
.
jIOError
(
...
...
@@ -121,11 +131,11 @@
);
}
function
map
Property
(
storage
,
property
,
doc
,
mapped_
doc
)
{
function
map
ToMainProperty
(
storage
,
property
,
sub_doc
,
doc
)
{
if
(
storage
.
_mapping_dict
[
property
]
!==
undefined
)
{
if
(
storage
.
_mapping_dict
[
property
].
equal
!==
undefined
)
{
if
(
doc
.
hasOwnProperty
(
storage
.
_mapping_dict
[
property
].
equal
))
{
mapped_doc
[
property
]
=
doc
[
storage
.
_mapping_dict
[
property
].
equal
];
if
(
sub_
doc
.
hasOwnProperty
(
storage
.
_mapping_dict
[
property
].
equal
))
{
doc
[
property
]
=
sub_
doc
[
storage
.
_mapping_dict
[
property
].
equal
];
}
return
storage
.
_mapping_dict
[
property
].
equal
;
}
...
...
@@ -134,56 +144,53 @@
}
}
if
(
storage
.
_map_all_property
)
{
if
(
doc
.
hasOwnProperty
(
property
))
{
mapped_doc
[
property
]
=
doc
[
property
];
if
(
sub_
doc
.
hasOwnProperty
(
property
))
{
doc
[
property
]
=
sub_
doc
[
property
];
}
return
property
;
}
return
false
;
}
function
map
Document
(
storage
,
doc
,
id_delete
)
{
var
mapped_
doc
=
{},
function
map
ToMainDocument
(
storage
,
sub_doc
,
delete_id_from_doc
)
{
var
doc
=
{},
property
,
property_list
=
[];
for
(
property
in
storage
.
_mapping_dict
)
{
if
(
storage
.
_mapping_dict
.
hasOwnProperty
(
property
))
{
property_list
.
push
(
map
Property
(
storage
,
property
,
doc
,
mapped_
doc
));
property_list
.
push
(
map
ToMainProperty
(
storage
,
property
,
sub_doc
,
doc
));
}
}
if
(
storage
.
_map_all_property
)
{
for
(
property
in
doc
)
{
if
(
doc
.
hasOwnProperty
(
property
))
{
for
(
property
in
sub_
doc
)
{
if
(
sub_
doc
.
hasOwnProperty
(
property
))
{
if
(
property_list
.
indexOf
(
property
)
<
0
)
{
mapped_doc
[
property
]
=
doc
[
property
];
doc
[
property
]
=
sub_
doc
[
property
];
}
}
}
}
if
(
id_delete
)
{
delete
mapped_
doc
.
id
;
if
(
delete_id_from_doc
)
{
delete
doc
.
id
;
}
return
mapped_
doc
;
return
doc
;
}
function
unmapDocument
(
storage
,
mapped_doc
)
{
var
doc
=
{},
property
,
property_list
=
[];
for
(
property
in
storage
.
_mapping_dict
)
{
if
(
storage
.
_mapping_dict
.
hasOwnProperty
(
property
))
{
property_list
.
push
(
unmapProperty
(
storage
,
property
,
doc
,
mapped_doc
));
function
mapToSubstorageDocument
(
storage
,
doc
)
{
var
sub_doc
=
{},
property
;
for
(
property
in
doc
)
{
if
(
doc
.
hasOwnProperty
(
property
))
{
mapToSubProperty
(
storage
,
property
,
sub_doc
,
doc
);
}
}
if
(
storage
.
_map_all_property
)
{
for
(
property
in
mapped_doc
)
{
if
(
mapped_doc
.
hasOwnProperty
(
property
))
{
if
(
property_list
.
indexOf
(
property
)
<
0
)
{
doc
[
property
]
=
mapped_doc
[
property
];
}
}
for
(
property
in
storage
.
_default_mapping
)
{
if
(
storage
.
_default_mapping
.
hasOwnProperty
(
property
))
{
sub_doc
[
property
]
=
storage
.
_default_mapping
[
property
];
}
}
delete
doc
.
id
;
return
doc
;
delete
sub_
doc
.
id
;
return
sub_
doc
;
}
MappingStorage
.
prototype
.
get
=
function
(
id
)
{
...
...
@@ -192,8 +199,8 @@
.
push
(
function
(
sub_id
)
{
return
context
.
_sub_storage
.
get
(
sub_id
);
})
.
push
(
function
(
doc
)
{
return
map
Document
(
context
,
doc
,
true
);
.
push
(
function
(
sub_
doc
)
{
return
map
ToMainDocument
(
context
,
sub_
doc
,
true
);
})
.
push
(
undefined
,
function
(
error
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
+
id
...
...
@@ -201,30 +208,31 @@
});
};
MappingStorage
.
prototype
.
post
=
function
(
doc
_mapped
)
{
MappingStorage
.
prototype
.
post
=
function
(
doc
)
{
if
(
!
this
.
_id_is_mapped
)
{
return
this
.
_sub_storage
.
post
.
apply
(
this
.
_sub_storage
,
unmapDocument
(
this
,
doc_mapped
)
);
return
this
.
_sub_storage
.
post
(
mapToSubstorageDocument
(
this
,
doc
));
}
throw
new
jIO
.
util
.
jIOError
(
"
post is not supported with id mapped
"
,
400
);
};
MappingStorage
.
prototype
.
put
=
function
(
id
,
doc
)
{
var
context
=
this
,
mapped_doc
=
unmap
Document
(
this
,
doc
);
sub_doc
=
mapToSubstorage
Document
(
this
,
doc
);
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
sub_id
)
{
if
(
context
.
_id_is_mapped
)
{
mapped
_doc
[
context
.
_mapping_dict
.
id
.
equal
]
=
id
;
sub
_doc
[
context
.
_mapping_dict
.
id
.
equal
]
=
id
;
}
if
(
id
===
undefined
)
{
throw
new
Error
();
}
return
context
.
_sub_storage
.
put
(
sub_id
,
mapped
_doc
);
return
context
.
_sub_storage
.
put
(
sub_id
,
sub
_doc
);
})
.
push
(
undefined
,
function
()
{
return
context
.
_sub_storage
.
post
(
mapped
_doc
);
return
context
.
_sub_storage
.
post
(
sub
_doc
);
})
.
push
(
function
()
{
return
id
;
...
...
@@ -318,7 +326,7 @@
})
.
push
(
function
(
result
)
{
for
(
i
=
0
;
i
<
result
.
length
;
i
+=
1
)
{
mapped_result
.
push
(
mapDocument
(
context
,
result
[
i
],
false
));
mapped_result
.
push
(
map
ToMain
Document
(
context
,
result
[
i
],
false
));
}
return
mapped_result
;
});
...
...
@@ -341,13 +349,13 @@
one_query
.
query_list
=
query_list
;
return
one_query
;
}
one_query
.
key
=
mapProperty
(
context
,
one_query
.
key
,
{},
{});
one_query
.
key
=
map
ToMain
Property
(
context
,
one_query
.
key
,
{},
{});
return
one_query
;
}
if
(
option
.
sort_on
!==
undefined
)
{
for
(
i
=
0
;
i
<
option
.
sort_on
.
length
;
i
+=
1
)
{
property
=
mapProperty
(
this
,
option
.
sort_on
[
i
][
0
],
{},
{});
property
=
map
ToMain
Property
(
this
,
option
.
sort_on
[
i
][
0
],
{},
{});
if
(
property
&&
sort_on
.
indexOf
(
property
)
<
0
)
{
select_list
.
push
([
property
,
option
.
sort_on
[
i
][
1
]]);
}
...
...
@@ -363,7 +371,7 @@
}
if
(
option
.
select_list
!==
undefined
)
{
for
(
i
=
0
;
i
<
option
.
select_list
.
length
;
i
+=
1
)
{
property
=
mapProperty
(
this
,
option
.
select_list
[
i
],
{},
{});
property
=
map
ToMain
Property
(
this
,
option
.
select_list
[
i
],
{},
{});
if
(
property
&&
select_list
.
indexOf
(
property
)
<
0
)
{
select_list
.
push
(
property
);
}
...
...
@@ -409,7 +417,7 @@
.
push
(
function
(
result
)
{
for
(
i
=
0
;
i
<
result
.
data
.
total_rows
;
i
+=
1
)
{
result
.
data
.
rows
[
i
].
value
=
mapDocument
(
context
,
result
.
data
.
rows
[
i
].
value
,
false
);
map
ToMain
Document
(
context
,
result
.
data
.
rows
[
i
].
value
,
false
);
if
(
result
.
data
.
rows
[
i
].
id
!==
undefined
&&
context
.
_id_is_mapped
)
{
result
.
data
.
rows
[
i
].
id
=
result
.
data
.
rows
[
i
].
value
.
id
;
...
...
test/jio.storage/mappingstorage.tests.js
View file @
a7d9f005
This diff is collapsed.
Click to expand it.
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