Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F8929133
datastore.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
datastore.js
View Options
var
assert
=
require
(
"assert"
);
var
Sealious
=
require
(
"../main.js"
);
var
Chip
=
require
(
"./chip.js"
);
var
Datastore
=
function
(
parent_module_path
,
name
){
this
.
name
=
name
;
this
.
longid
=
"datastore."
+
name
;
this
.
default_configuration
=
{};
Sealious
.
ChipManager
.
add_chip
(
"datastore"
,
this
.
name
,
this
,
parent_module_path
);
}
Datastore
.
is_a_constructor
=
false
;
Datastore
.
prototype
=
new
Chip
();
Datastore
.
prototype
.
return_not_implemented
=
function
(
fn_name
){
return
function
(){
throw
new
Sealious
.
Errors
.
DeveloperError
(
"Function "
,
fn_name
,
"not implemented in "
,
this
.
longid
,
", aborting."
);
}
}
var
needs_to_implement
=
[
"find"
,
"insert"
,
"update"
,
"remove"
]
for
(
var
i
in
needs_to_implement
){
var
fn_name
=
needs_to_implement
[
i
];
Datastore
.
prototype
[
fn_name
]
=
Datastore
.
prototype
.
return_not_implemented
(
fn_name
)
}
Datastore
.
prototype
.
test_compatibility
=
function
(){
var
self
=
this
;
this
.
start
();
var
rand
=
Math
.
random
();
var
test_collection_name
=
"_test"
;
return
Promise
.
resolve
()
.
then
(
function
(){
//.insert method should respond with the created document
var
to_insert
=
{
value
:
1
,
random
:
rand
};
return
self
.
insert
(
test_collection_name
,
to_insert
)
.
then
(
function
(
response
){
assert
.
deepEqual
(
to_insert
,
response
,
".insert method should respond with the created document"
);
return
Promise
.
resolve
();
});
}).
then
(
function
(){
//check if find resolves with an array
return
self
.
find
(
test_collection_name
,
{},
{})
.
then
(
function
(
documents
){
assert
(
documents
instanceof
Array
,
"datastore."
+
self
.
name
+
".find should resolve with an array"
);
return
Promise
.
resolve
();
});
}).
then
(
function
(){
//check if amount of created documents checks out
var
creates
=
[
self
.
insert
(
test_collection_name
,
{
value
:
2
,
random
:
rand
}),
self
.
insert
(
test_collection_name
,
{
value
:
3
,
random
:
rand
}),
self
.
insert
(
test_collection_name
,
{
value
:
4
,
random
:
-
rand
}),
]
var
created_so_far
=
4
;
return
Promise
.
all
(
creates
)
.
then
(
function
(){
return
self
.
find
(
test_collection_name
,
{},
{});
}).
then
(
function
(
documents
){
assert
(
documents
.
length
==
created_so_far
,
"Inserted "
+
created_so_far
+
" documents so far, but "
+
documents
.
length
+
" were returned on .find()"
);
return
Promise
.
resolve
(
created_so_far
);
});
}).
then
(
function
(
created_so_far
){
//check if there is a proper amount of documents with random value set to rand
var
documents_with_rand
=
created_so_far
-
1
;
return
self
.
find
(
test_collection_name
,
{
random
:
rand
},
{})
.
then
(
function
(
documents
){
assert
(
documents
.
length
==
documents_with_rand
,
"Inserted "
+
documents_with_rand
+
" documents with `random` set to `"
+
rand
+
"` so far, but "
+
documents
.
length
+
" were returned on .find({random: "
+
rand
+
"})"
);
return
Promise
.
resolve
();
});
}).
then
(
function
(){
//Should store a complex object
var
complex_object
=
{
id
:
"aseoifaoeina"
,
body
:
{
name
:
"Anna"
,
surname
:
"Fontanna"
}};
return
self
.
insert
(
test_collection_name
,
complex_object
)
.
then
(
function
(
response
){
assert
.
deepEqual
(
complex_object
,
response
,
".insert with complex object should resolve with that complex object."
);
return
self
.
find
(
test_collection_name
,
{
id
:
complex_object
.
id
})
}).
then
(
function
(
response
){
assert
.
deepEqual
(
complex_object
,
response
[
0
],
".insert with complex object should store that complex object."
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
//Should handle dot-notation nested queries
return
self
.
find
(
test_collection_name
,
{
"body.name"
:
complex_object
.
body
.
name
})
.
then
(
function
(
response
){
assert
.
deepEqual
(
complex_object
,
response
[
0
],
".find method should handle dot notation queries"
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
return
self
.
find
(
test_collection_name
,
{
body
:
{
name
:
complex_object
.
body
.
name
}})
.
then
(
function
(
response
){
assert
.
deepEqual
(
complex_object
,
response
[
0
],
".find method should handle nested object queries"
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
//.update should modify document values with dot notation
complex_object
.
body
.
name
=
"Hanna"
;
return
self
.
update
(
test_collection_name
,
{
id
:
complex_object
.
id
},
{
"body.name"
:
complex_object
.
body
.
name
})
.
then
(
function
(){
return
self
.
find
(
test_collection_name
,
{
id
:
complex_object
.
id
});
}).
then
(
function
(
results
){
assert
.
deepEqual
(
complex_object
,
results
[
0
],
".update should modify document values with dot notation"
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
//.update should modify document values using nested object as a query
complex_object
.
body
.
name
=
"Marzanna"
;
return
self
.
update
(
test_collection_name
,
{
id
:
complex_object
.
id
},
{
body
:
{
name
:
complex_object
.
body
.
name
}})
.
then
(
function
(){
return
self
.
find
(
test_collection_name
,
{
id
:
complex_object
.
id
});
}).
then
(
function
(
results
){
assert
.
deepEqual
(
complex_object
,
results
[
0
],
".update should modify document values using nested object as a query "
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
//.update should insert new value to a field that previously had no value (undefined)
complex_object
.
body
.
other
=
"Focca"
;
return
self
.
update
(
test_collection_name
,
{
id
:
complex_object
.
id
},
{
body
:
{
other
:
complex_object
.
body
.
other
}})
.
then
(
function
(){
return
self
.
find
(
test_collection_name
,
{
id
:
complex_object
.
id
});
}).
then
(
function
(
results
){
assert
.
deepEqual
(
complex_object
,
results
[
0
],
".update should insert new value to a field that previously had no value (undefined)"
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
//.remove should remove only one document when "just_one" is set to true
return
Promise
.
all
([
self
.
insert
(
test_collection_name
,
complex_object
),
self
.
insert
(
test_collection_name
,
complex_object
),
self
.
insert
(
test_collection_name
,
complex_object
),
]).
then
(
function
(){
//all the "complex_object" documents have the same id
return
self
.
remove
(
test_collection_name
,
{
id
:
complex_object
.
id
},
true
);
}).
then
(
function
(){
return
self
.
find
(
test_collection_name
,
{
id
:
complex_object
.
id
});
}).
then
(
function
(
results
){
assert
(
results
.
length
==
3
,
".remove should remove only *one* document when `just_one` argument is set to true"
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(
complex_object
){
//.remove should remove all matching documents when "just_one" is falsy
return
Promise
.
all
([
self
.
insert
(
test_collection_name
,
complex_object
),
self
.
insert
(
test_collection_name
,
complex_object
),
self
.
insert
(
test_collection_name
,
complex_object
),
]).
then
(
function
(){
//all the "complex_object" documents have the same id
return
self
.
remove
(
test_collection_name
,
{
id
:
complex_object
.
id
},
false
);
}).
then
(
function
(){
return
self
.
find
(
test_collection_name
,
{
id
:
complex_object
.
id
});
}).
then
(
function
(
results
){
assert
(
results
.
length
==
0
,
".remove should remove all matching documents when 'just_one' is falsy"
);
return
Promise
.
resolve
(
complex_object
);
});
}).
then
(
function
(){
self
.
clear_collection
(
test_collection_name
);
}).
catch
(
function
(
err
){
self
.
clear_collection
(
test_collection_name
);
console
.
log
(
err
.
stack
);
return
Promise
.
reject
(
"Compatibility test unsuccesfull"
)
});
};
module
.
exports
=
Datastore
;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Sep 20, 21:49 (9 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
935252
Default Alt Text
datastore.js (7 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment