Created
July 12, 2015 23:03
-
-
Save devniel/57cb233695c9a8dc6686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| this.create = function(data, fn){ | |
| // Obtener clase | |
| var Class = require("./../models/Class"); | |
| Class.readByName(data.class, function(err, _class){ | |
| if(err) return fn(err); | |
| // Parse properties | |
| console.log("Class of instance : ", _class); | |
| var properties = []; | |
| for(var i in data.properties){ | |
| for(var j in _class.properties){ | |
| if(i == _class.properties[j].name){ | |
| console.log("Property : ", i , " ", data.properties[i]); | |
| var prop = _class.properties[j]; | |
| prop.value = data.properties[i]; | |
| prop.bid = data.bid; | |
| properties.push(prop); | |
| } | |
| } | |
| }; | |
| console.log("Parsed properties according to class ===> ", properties); | |
| // TODO : Comprobar que cumple con la estructura de la clase. | |
| // TODO : Insert properties | |
| if(data.labels == undefined) data.labels = []; | |
| var query = [ | |
| "START c=node(" + _class.id + ")", | |
| "CREATE ", | |
| "(n:Instance:" + data.labels.join(':') + ":B" + data.bid + ":C" + _class.id + " {`name`:'" + data.name + "'})", | |
| "WITH n,c", | |
| "CREATE (n)-[r:`class`]->(c)", | |
| "RETURN n" | |
| ].join("\n"); | |
| console.log("Create instance ..."); | |
| // Crear nodo instancia | |
| db.query(query, {}, function(err, new_instance){ | |
| if(err) return fn(err); | |
| new_instance = new_instance[0].n; | |
| console.log("Get properties ..."); | |
| // Crear propiedades | |
| async.map(properties, function(property, cb){ | |
| // Crear y asignar a clase | |
| Property.createForInstance(property, new_instance.id, cb); | |
| },function(err, results){ | |
| if(err) { | |
| // Rollback | |
| return self.delete(new_instance.id, function(err2){ | |
| if(err2) return fn(err2); | |
| // Return error | |
| return fn(err); | |
| }); | |
| }; | |
| console.log("Get instance ..."); | |
| // Obtener instancia | |
| self.readWithFormat(new_instance.id,function(err,new_instance){ | |
| if(err) return fn(err); | |
| console.log("New instance created : ", new_instance); | |
| fn(null,new_instance); | |
| }); | |
| }); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment