Last active
January 17, 2018 19:24
-
-
Save MichelDiz/c41d391f3ce5a19dbc37a0a0f79a817f 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
async registerDgraph({ username, email, password, role, isActive }) { | |
// const passwordHashed = await bcrypt.hash(password, 12); | |
console.log(username, '<====== username'); | |
const txn = dgraph.newTxn(); | |
try { | |
if (role === undefined) { | |
role = 'user'; | |
} | |
const now = new Date().toISOString(); | |
let mu = new dgraph.Mutation(); | |
mu.setSetNquads( | |
strToU8(` | |
_:user <username> "${username}" . | |
_:user <email> "${email}" . | |
_:user <role> "${role}" . | |
_:user <pass> "${password}" . | |
_:user <is_active> "${isActive}" . | |
_:user <CreationDate> "${now}". | |
_:user <LastAccessDate> "${now}" . | |
`) | |
); | |
const ag = await txn.mutate(mu); | |
const uid = ag.getUidsMap().get('user'); //isso é muito importante se vc precisa retornar o UID | |
// Commit transaction. | |
await txn.commit(); | |
console.log('start =========================>', uid, '<====== mutate uid'); | |
//mu.forEach(uidsMap => console.log(uidsMap.user)); | |
let query = ` | |
query { | |
getUserByUID(func: uid(${uid})) { | |
${queryForUser} | |
} | |
} | |
`; | |
// const vars = { $a: "Alice" }; | |
const res = await dgraph.newTxn().queryWithVars(query); | |
// Deserialize result. | |
const jsonStr = new Buffer(res.getJson_asU8()).toString(); | |
// OR if you want to use base64 | |
// const jsonStr = new Buffer(res.getJson_asB64(), "base64").toString(); | |
let resToJson = JSON.parse(jsonStr); | |
return resToJson.getUserByUID; | |
} catch (e) { | |
if (e === dgraph.ERR_ABORTED) { | |
// Retry or handle exception. | |
} else { | |
throw e; | |
} | |
} finally { | |
// Clean up. Calling this after txn.commit() is a no-op | |
// and hence safe. | |
await txn.discard(); | |
} | |
} | |
async checkpwdDgraph(email, password) { | |
console.log(email, password, '<====================== checkpwdDgraph INPUT'); | |
let query = ` query check{ | |
checkpwdDgraph(func: eq(email, "${email}")) { | |
checkpwd(pass, "${password}") | |
} | |
}`; | |
const res = await dgraph.newTxn().queryWithVars(query); | |
// Deserialize result. | |
const jsonStr = new Buffer(res.getJson_asU8()).toString(); | |
// OR if you want to use base64 | |
// const jsonStr = new Buffer(res.getJson_asB64(), "base64").toString(); | |
let resToJson = JSON.parse(jsonStr); | |
let bucketZero = null; | |
let bucket = null; | |
resToJson.checkpwdDgraph.forEach(pass => console.log(pass)); | |
if (Object.keys(resToJson.checkpwdDgraph).length > 0) { | |
let arrayToObjectToCall = array => | |
array.reduce((obj, item) => { | |
obj['user'] = item; | |
return item; | |
}, {}); | |
bucketZero = arrayToObjectToCall(resToJson.checkpwdDgraph); | |
} else { | |
bucketZero = undefined; | |
} | |
if (bucketZero) { | |
let arrayToObjectToCall = array => | |
array.reduce((obj, item) => { | |
obj['user'] = item; | |
return item; | |
}, {}); | |
bucket = arrayToObjectToCall(bucketZero.pass); | |
} else { | |
bucket = undefined; | |
} | |
console.log(bucket, '<====================== USER bucket checkpwdDgraph'); | |
return bucket; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment