Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9583340
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/chip-types/resource-manager.js b/lib/chip-types/resource-manager.js
index a3bd63f3..6bb43ee3 100644
--- a/lib/chip-types/resource-manager.js
+++ b/lib/chip-types/resource-manager.js
@@ -1,223 +1,223 @@
var Promise = require("bluebird");
var ResourceRepresentation = require("./resource-representation.js");
var ChipManager = require("./chip-manager.js");
+var Uid = require("uid");
/**
* Manages resources in the database
* @class
*/
var ResourceManager = new function(){
this.create = function(dispatcher, type_name, body, owner, access_mode, access_mode_args){
console.log("resource-manager.js:create-resource");
if(arguments.length==3){
var owner=null;
var access_mode = "private";
var access_mode_args = [];
} else if(arguments.length==4){
var access_mode = "private";
} else if(arguments.length==5){
var access_mode = "private";
var access_mode_args = [];
} else if(arguments.length==6) {
} else {
throw new Sealious.Errors.ValidationError("Wrong amount of arguments: create_resource."); //~
}
if(!ChipManager.chip_is_registred("resource_type." + type_name)){
throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name); //~
}
var resource_type_object = ChipManager.get_chip("resource_type", type_name);
var encoded_body = null;
return new Promise(function(resolve, reject){
resource_type_object.validate_field_values(body)
.then(
function(){
return resource_type_object.encode_field_values(body);
}
).then(
function(response){
encoded_body = response;
- return dispatcher.metadata_increment_variable("first_free_id", dispatcher)
- }
- ).then(
- function(newID) {
+
+ var newID = Uid(10);
+
var resource_data = {
sealious_id: newID,
type: type_name,
body: encoded_body
};
resource_data.owner = owner;
resource_data.access_mode = access_mode;
resource_data.access_mode_args = access_mode_args;
dispatcher.datastore.insert("resources", resource_data, {}).then(function(data){
var database_entry = data[0];
var resource = new ResourceRepresentation(database_entry);
resolve(resource.getData());
});
}
).catch(function(e){
reject(e);
});
})
}
this.edit_resource_access_mode = function(dispatcher, resource_id, access_mode, access_mode_args){
if(arguments.length==3){
access_mode_args={};
}
return dispatcher.datastore.update("resources", {sealious_id: resource_id }, { $set: { access_mode:access_mode, access_mode_args: access_mode_args} });
}
this.get_resource_access_mode = function(dispatcher, resource_id){
resource_id = parseInt(resource_id);
return new Promise(function(resolve,reject){
dispatcher.datastore.find("resources", { sealious_id: resource_id }, {})
.then(function(documents){
var database_entry = documents[0];
var resource = new ResourceRepresentation(database_entry);
resolve(resource.get_access_mode());
});
});
}
this.delete = function(dispatcher, type_name, id){
if(!ChipManager.chip_is_registred("resource_type." + type_name)){
throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name);
}
return new Promise(function(resolve, reject){
dispatcher.datastore.delete("resources", {sealious_id: id, type: type_name}, {})
.then(function(data){
resolve();
}).catch(function(e){
reject(e);
});
})
}
/**
* Callback for idExists
* @callback ResourceManager~idExistsCallback
* @param {Boolean} exists - true if a resource with given id exists
*/
this.get_by_id = function(dispatcher, resource_id){
resource_id = parseInt(resource_id);
return new Promise(function(resolve,reject){
dispatcher.datastore.find("resources", { sealious_id: resource_id }, {})
.then(function(documents){
if (documents[0] === undefined) {
reject(new Sealious.Errors.NotFound("resource of id " + resource_id + " not found"));
} else {
var database_entry = documents[0];
var resource = new ResourceRepresentation(database_entry);
resolve(resource.getData());
}
});
});
}
this.list_by_type = function(dispatcher, type_name, params) {
if(arguments.length==2){
params = {};
}
return new Promise(function(resolve, reject){
if(!ChipManager.chip_is_registred("resource_type."+type_name)){
reject(new Sealious.Errors.ValidationError("resource type "+type_name+" does not exist"));
}else{
dispatcher.datastore.find("resources", { type: type_name }, {}, params).then(function(response) {
var ret = response.map(function(database_entry){
return new ResourceRepresentation(database_entry).getData();
})
resolve(ret);
})
}
})
}
this.find = function(dispatcher, field_values, type){
if(arguments.length==2){
type = null;
}
return new Promise(function(resolve, reject){
var query = {};
if(type){
query.type = type;
}
for(var field_name in field_values){
query["body." + field_name] = field_values[field_name];
}
dispatcher.datastore.find("resources", query)
.then(function(documents){
var parsed_documents = documents.map(function(document){return new ResourceRepresentation(document).getData()});
resolve(parsed_documents);
})
})
}
this.update_resource = function(dispatcher, resource_id, new_resource_data){
var query = {};
if(new_resource_data.hasOwnProperty("sealious_id")){
delete new_resource_data.sealious_id;
}
if(new_resource_data.hasOwnProperty("id")){
delete new_resource_data.id;
}
for(var field_name in new_resource_data){
query["body." + field_name] = new_resource_data[field_name];
}
return dispatcher.datastore.update("resources", {sealious_id: resource_id}, {$set: query});
}
this.search = function(dispatcher, type, field_name, query_string){
query = {body: {}};
query["body"][field_name] = new RegExp(query_string, "gi");
return new Promise(function(resolve, reject){
dispatcher.datastore.find("resources", query)
.then(function(documents){
var resource_representations = documents.map(function(document){return new ResourceRepresentation(document).getData()});
resolve(resource_representations);
})
})
}
this.search_by_mode = function(dispatcher, type, mode){
return new Promise(function(resolve, reject){
dispatcher.datastore.find("resources", {access_mode: mode, type: type}, {})
.then(function(documents){
var database_entry = documents;
var resource_representations = documents.map(function(document){
return new ResourceRepresentation(document).getData()
});
resolve(resource_representations);
});
});
}
this.get_resource_type_signature = function(dispatcher, type_name){
return new Promise(function(resolve, reject){
var resource_type_chip = ChipManager.get_chip("resource_type", type_name);
if(resource_type_chip){
resolve(resource_type_chip.get_signature());
}else{
reject(new Sealious.Errors.Error("ResourceManager tried to access chip type `" + type_name + "`, which does not exist."));
}
})
}
}
module.exports = ResourceManager;
diff --git a/package.json b/package.json
index 5c82be43..b824961d 100644
--- a/package.json
+++ b/package.json
@@ -1,44 +1,45 @@
{
"name": "sealious",
"version": "0.2.6",
"description": "",
"main": "./main.js",
"scripts": {
"test": "mocha ./tests/test.js && mocha ./tests/test.js -R travis-cov"
},
"repository": {
"type": "git",
"url": "https://github.com/Sealious/Sealious/"
},
"keywords": [
"sealious"
],
"author": "Kuba Orlik",
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/Sealious/Sealious/issues"
},
"dependencies": {
"Set": "~0.4.1",
"bluebird": "~2.9.9",
"ip": "~0.3.2",
"merge": "^1.2.0",
"open": "0.0.5",
"socket.io": "~1.2.0",
- "socket.io-client": "~1.2.0"
+ "socket.io-client": "~1.2.0",
+ "uid": "0.0.2"
},
"devDependencies": {
"blanket": "1.1.6",
"mocha": "*",
"should": "~4.4.0",
"travis-cov ": "*"
},
"config": {
"blanket": {
"pattern": "node_modules"
}
},
"peerDependencies": {
"sealious-base-chips": "^0.2.0"
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Oct 11, 09:27 (6 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
984117
Default Alt Text
(8 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment