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
Aurélien Vermylen
jio
Commits
7e988905
Commit
7e988905
authored
Jan 23, 2015
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow to use a jio document as a full storage.
parent
902f54fe
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
154 additions
and
0 deletions
+154
-0
src/jio.storage/documentstorage.js
src/jio.storage/documentstorage.js
+154
-0
No files found.
src/jio.storage/documentstorage.js
0 → 100644
View file @
7e988905
/*jslint nomen: true*/
/*global console, Blob, atob, btoa*/
(
function
(
jIO
,
Blob
,
atob
,
btoa
)
{
"
use strict
"
;
/**
* The jIO DocumentStorage extension
*
* @class DocumentStorage
* @constructor
*/
function
DocumentStorage
(
spec
)
{
this
.
_sub_storage
=
jIO
.
createJIO
(
spec
.
sub_storage
);
this
.
_document_id
=
spec
.
document_id
;
}
var
DOCUMENT_EXTENSION
=
"
.json
"
,
DOCUMENT_REGEXP
=
new
RegExp
(
"
^jio_document/([
\\
w=]+)
"
+
DOCUMENT_EXTENSION
+
"
$
"
),
ATTACHMENT_REGEXP
=
new
RegExp
(
"
^jio_attachment/([
\\
w=]+)/([
\\
w=]+)$
"
);
function
getSubAttachmentIdFromParam
(
param
)
{
if
(
param
.
_attachment
===
undefined
)
{
return
'
jio_document/
'
+
btoa
(
param
.
_id
)
+
DOCUMENT_EXTENSION
;
}
return
'
jio_attachment/
'
+
btoa
(
param
.
_id
)
+
"
/
"
+
btoa
(
param
.
_attachment
);
}
DocumentStorage
.
prototype
.
get
=
function
(
param
)
{
var
result
,
context
=
this
;
return
this
.
_sub_storage
.
getAttachment
({
"
_id
"
:
this
.
_document_id
,
"
_attachment
"
:
getSubAttachmentIdFromParam
(
param
)
})
.
push
(
function
(
blob
)
{
return
jIO
.
util
.
readBlobAsText
(
blob
);
})
.
push
(
function
(
text
)
{
return
JSON
.
parse
(
text
.
target
.
result
);
})
.
push
(
function
(
json
)
{
result
=
json
;
return
context
.
_sub_storage
.
get
({
"
_id
"
:
context
.
_document_id
});
})
.
push
(
function
(
document
)
{
var
attachments
=
{},
exec
,
key
;
for
(
key
in
document
.
_attachments
)
{
if
(
document
.
_attachments
.
hasOwnProperty
(
key
))
{
if
(
ATTACHMENT_REGEXP
.
test
(
key
))
{
exec
=
ATTACHMENT_REGEXP
.
exec
(
key
);
if
(
atob
(
exec
[
1
])
===
param
.
_id
)
{
attachments
[
atob
(
exec
[
2
])]
=
{};
}
}
}
}
if
(
Object
.
getOwnPropertyNames
(
attachments
).
length
>
0
)
{
result
.
_attachments
=
attachments
;
}
return
result
;
});
};
DocumentStorage
.
prototype
.
post
=
function
(
param
)
{
var
doc_id
=
param
.
_id
;
if
(
doc_id
===
undefined
)
{
doc_id
=
jIO
.
util
.
generateUuid
();
}
param
.
_id
=
doc_id
;
return
this
.
put
(
param
);
};
DocumentStorage
.
prototype
.
put
=
function
(
param
)
{
var
doc_id
=
param
.
_id
;
return
this
.
_sub_storage
.
putAttachment
({
"
_id
"
:
this
.
_document_id
,
"
_attachment
"
:
getSubAttachmentIdFromParam
(
param
),
"
_blob
"
:
new
Blob
([
JSON
.
stringify
(
param
)],
{
type
:
"
application/json
"
})
})
.
push
(
function
()
{
return
doc_id
;
});
};
DocumentStorage
.
prototype
.
remove
=
function
(
param
)
{
return
this
.
_sub_storage
.
removeAttachment
({
"
_id
"
:
this
.
_document_id
,
"
_attachment
"
:
getSubAttachmentIdFromParam
(
param
)
})
.
push
(
function
()
{
return
param
.
_id
;
});
};
DocumentStorage
.
prototype
.
hasCapacity
=
function
(
capacity
)
{
return
(
capacity
===
"
list
"
);
};
DocumentStorage
.
prototype
.
buildQuery
=
function
()
{
return
this
.
_sub_storage
.
get
({
"
_id
"
:
this
.
_document_id
})
.
push
(
function
(
document
)
{
var
result
=
[],
key
;
for
(
key
in
document
.
_attachments
)
{
if
(
document
.
_attachments
.
hasOwnProperty
(
key
))
{
if
(
DOCUMENT_REGEXP
.
test
(
key
))
{
result
.
push
({
id
:
atob
(
DOCUMENT_REGEXP
.
exec
(
key
)[
1
]),
value
:
{}
});
}
}
}
return
result
;
});
};
DocumentStorage
.
prototype
.
getAttachment
=
function
(
param
)
{
return
this
.
_sub_storage
.
getAttachment
({
"
_id
"
:
this
.
_document_id
,
"
_attachment
"
:
getSubAttachmentIdFromParam
(
param
)
});
};
DocumentStorage
.
prototype
.
putAttachment
=
function
(
param
)
{
return
this
.
_sub_storage
.
putAttachment
({
"
_id
"
:
this
.
_document_id
,
"
_attachment
"
:
getSubAttachmentIdFromParam
(
param
),
"
_blob
"
:
param
.
_blob
});
};
DocumentStorage
.
prototype
.
removeAttachment
=
function
(
param
)
{
return
this
.
_sub_storage
.
removeAttachment
({
"
_id
"
:
this
.
_document_id
,
"
_attachment
"
:
getSubAttachmentIdFromParam
(
param
)
});
};
jIO
.
addStorage
(
'
document
'
,
DocumentStorage
);
}(
jIO
,
Blob
,
atob
,
btoa
));
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