Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
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
Stefane Fermigier
jio
Commits
207682c5
Commit
207682c5
authored
Feb 04, 2014
by
Marco Mariani
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'querystorage'
parents
4a1655ef
b284c4f5
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
946 additions
and
0 deletions
+946
-0
src/jio.storage/querystorage.js
src/jio.storage/querystorage.js
+180
-0
test/jio.storage/querystorage.tests.js
test/jio.storage/querystorage.tests.js
+763
-0
test/tests.html
test/tests.html
+3
-0
No files found.
src/jio.storage/querystorage.js
0 → 100644
View file @
207682c5
/*jslint indent: 2, maxlen: 80, nomen: true, regexp: true, unparam: true */
/*global define, window, RSVP, jIO, complex_queries */
(
function
(
dependencies
,
module
)
{
"
use strict
"
;
if
(
typeof
define
===
'
function
'
&&
define
.
amd
)
{
return
define
(
dependencies
,
module
);
}
window
.
query_storage
=
{};
module
(
window
.
query_storage
,
RSVP
,
jIO
);
}([
'
exports
'
,
'
rsvp
'
,
'
jio
'
],
function
(
exports
,
RSVP
,
jIO
)
{
"
use strict
"
;
/**
* The jIO QueryStorage extension
*
* @class QueryStorage
* @constructor
*/
function
QueryStorage
(
spec
)
{
this
.
_substorage
=
jIO
.
createJIO
(
spec
.
sub_storage
);
this
.
_key_schema
=
spec
.
key_schema
;
}
/**
* Get a document
* Parameters are passed through to the sub storage.
*
* @method get
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
get
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
get
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Create a document in the sub storage.
* Parameters are passed through to the sub storage.
*
* @method post
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
post
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
post
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Create or update a document in the sub storage.
* Parameters are passed through to the sub storage.
*
* @method put
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
put
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
put
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Remove a document.
* Parameters are passed through to the sub storage.
*
* @method remove
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
remove
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
remove
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Get an attachment.
* Parameters are passed through to the sub storage.
*
* @method getAttachment
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
getAttachment
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
getAttachment
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Add an attachment to a document.
* Parameters are passed through to the sub storage.
*
* @method putAttachment
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
putAttachment
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
putAttachment
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Remove an attachment.
* Parameters are passed through to the sub storage.
*
* @method removeAttachment
* @param {Object} command The JIO command
*/
QueryStorage
.
prototype
.
removeAttachment
=
function
(
command
)
{
var
args
=
[].
slice
.
call
(
arguments
,
1
);
this
.
_substorage
.
removeAttachment
.
apply
(
this
.
_substorage
,
args
).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
/**
* Retrieve documents.
* This method performs an .allDocs() call on the substorage,
* retrieving everything, then runs a complex query on the result.
*
* @method allDocs
* @param {Object} command The given parameters
* @param {Object} options The command options
*/
QueryStorage
.
prototype
.
allDocs
=
function
(
command
,
param
,
options
)
{
var
that
=
this
,
// we need the full documents in order to perform the query, will
// remove them later if they were not required.
include_docs
=
(
options
.
include_docs
||
options
.
query
)
?
true
:
false
;
this
.
_substorage
.
allDocs
({
include_docs
:
include_docs
}).
then
(
function
(
response
)
{
if
(
options
.
query
)
{
var
docs
=
response
.
data
.
rows
.
map
(
function
(
row
)
{
return
row
.
doc
;
}),
rows_map
=
{};
// build a mapping to avoid slowness
response
.
data
.
rows
.
forEach
(
function
(
row
)
{
rows_map
[
row
.
id
]
=
row
;
});
return
complex_queries
.
QueryFactory
.
create
(
options
.
query
,
that
.
_key_schema
).
exec
(
docs
,
options
).
then
(
function
(
filtered_docs
)
{
// reconstruct filtered rows, preserving the order from docs
var
rows
=
filtered_docs
.
map
(
function
(
doc
)
{
var
row
=
rows_map
[
doc
.
_id
];
// remove docs if not needed in the original call
if
(
!
options
.
include_docs
)
{
delete
row
.
doc
;
}
return
row
;
});
response
.
data
.
rows
=
rows
;
response
.
data
.
total_rows
=
rows
.
length
;
return
response
;
});
}
return
RSVP
.
resolve
(
response
);
}).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
jIO
.
addStorage
(
'
query
'
,
QueryStorage
);
}));
test/jio.storage/querystorage.tests.js
0 → 100644
View file @
207682c5
This diff is collapsed.
Click to expand it.
test/tests.html
View file @
207682c5
...
...
@@ -50,5 +50,8 @@
<script
src=
"../src/jio.storage/splitstorage.js"
></script>
<script
src=
"../test/jio.storage/splitstorage.tests.js"
></script>
<script
src=
"../src/jio.storage/querystorage.js"
></script>
<script
src=
"../test/jio.storage/querystorage.tests.js"
></script>
</body>
</html>
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