-
-
Save ProjectCheshire/48007909561a9146089d6c937281da4c to your computer and use it in GitHub Desktop.
This file contains 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
var session = require('./session') | |
class User { | |
/** | |
Creates a User Node in neo4j | |
* @param {string} uuid (required) - refference id | |
* @param {string} parentId (required only if reffered by another user) - uuid of parentId | |
* @param {int/float} sRate (optional) - special rate between user and | |
* returns Promise | |
*/ | |
static create(data,parentId=null,sRate = null){ | |
let sRateString = '' | |
let parent = '' | |
let relation = '' | |
if (parentId!=null){ | |
sRateString = sRate!=null ? `,sRate:${sRate}`:'' | |
parent = `Match(parent:User{uuid:"${parentId}"})` | |
relation = `-[:Parent]->(parent), | |
(parent)-[:Child]->(user)` | |
} | |
return session.run(`${parent} | |
Create(user:User{uuid:"${data.uuid}", firstName:"${data.firstName}", lastName:"${data.lastName}" ${sRateString}})${relation}`) | |
} | |
/** | |
* @param {string} uuid (required) - uuid of the node who's childern are being searched | |
* @param {int} limit (optional) - uuid of the node who's childern are being searched | |
* returns {Promise} | |
*/ | |
static getChildren(uuid,limit=0){ | |
let slimit = limit > 0 ? `..${limit}`:'' | |
return session.run( `MATCH(parent:User{uuid:"${uuid}"}) | |
MATCH path=(parent)-[:Child*${slimit}]->(child) | |
MATCH (child)-[:Parent]->(sParent) | |
RETURN parent,child,sParent, length(path) as generation | |
ORDER BY length(path) asc` ) | |
} | |
static getParent(uuid){ | |
return session.run(`MATCH(user:User{uuid:"${uuid}"}) | |
MATCH (user)-[:Parent]->(parent) | |
RETURN parent`) | |
} | |
} | |
module.exports = User |
This file contains 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
class Node{ | |
constructor(record,methods){ | |
Object.keys(record).map(key => { | |
this[key] = record[key] | |
}) | |
Object.keys(methods).map(methodName => { | |
this[methodName] = methods[methodName].bind(this); | |
}) | |
} | |
} |
This file contains 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
var getSession = require('../session').getSession | |
var Node = require('./Node') | |
// console.log(getSession); | |
class Model{ | |
constructor(name,schema){ | |
this.name = name | |
this.instanceMethods = schema.instanceMethods | |
this.rules = schema.rules | |
this.session = getSession() | |
Object.keys(schema.classMethods).map(methodName => { | |
this[methodName] = schema.classMethods[methodName].bind(this); | |
}) | |
} | |
create(data){ | |
return new Promise((resolve,reject) => { | |
// Validate and Filter Data before creating Node. | |
Object.keys(this.rules).map(fieldName => { | |
let result = validateField(this.rules,data[fieldName],fieldName) | |
if(typeof result === 'error'){ | |
reject(result) | |
} | |
if(typeof result !== 'undefined'){ | |
data[fieldName] = result | |
} | |
}) | |
//Create Node in neo4j | |
this.session.run(`Create(n:${this.name} ${toDataString(data)}) Return n`,data).then(result => { | |
let node = new Node(result.records[0]._fields[0].properties,this.instanceMethods) | |
// console.log(node); | |
resolve(node) | |
}).catch(err=>{ | |
reject(err) | |
}) | |
}) | |
} | |
} | |
function toDataString(data){ | |
let dataString = "" | |
Object.keys(data).map((key,index)=>{ | |
if(index === 0){ | |
dataString += '{' | |
} | |
dataString += `${key}:{${key}}` | |
if(index === (Object.keys(data).length - 1)){ | |
dataString += '}' | |
}else{ | |
dataString += ',' | |
} | |
}) | |
return dataString | |
} | |
//Private Methods | |
function validateField(rules,fieldData,fieldName){ | |
let rule = rules[fieldName]; | |
if(fieldData){ | |
if(typeof fieldData !== rule.type.toLowerCase()){ | |
return new Error(`${fieldName} is type ${typeof fieldName} expected type ${rule.type.toLowerCase()}`) | |
} | |
}else{ | |
} | |
}else{ | |
if(rule.required){ | |
if(rule.default){ | |
return rule.default | |
} | |
return new Error(`Required field ${fieldName} is missing.`) | |
} | |
} | |
} | |
module.exports = Model |
This file contains 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
class Schema { | |
constructor(config){ | |
this.rules = {} | |
this.classMethods = {} | |
this.instanceMethods = {} | |
Object.keys(config).map((fieldName,index) => { | |
this.rules[fieldName] = config[fieldName]; | |
}) | |
} | |
} | |
module.exports = Schema |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment