Reduce database statements in notifications

This commit is contained in:
roschaefer 2019-12-10 01:28:12 +01:00
parent b2ccc1b61e
commit 3beef5e3fb

View File

@ -1,3 +1,5 @@
import log from './helpers/databaseLogger'
const resourceTypes = ['Post', 'Comment'] const resourceTypes = ['Post', 'Comment']
const transformReturnType = record => { const transformReturnType = record => {
@ -45,13 +47,19 @@ export default {
const cypher = ` const cypher = `
MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id}) MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id})
${whereClause} ${whereClause}
RETURN resource, notification, user WITH user, notification, resource,
[(resource)<-[:WROTE]-(author:User) | author {.*}] as authors,
[(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post{.*, author: properties(author)} ] as posts
WITH resource, user, notification, authors, posts,
resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} as finalResource
RETURN notification {.*, from: finalResource, to: properties(user)}
${orderByClause} ${orderByClause}
${offset} ${limit} ${offset} ${limit}
` `
try { try {
const result = await session.run(cypher, { id: currentUser.id }) const result = await session.run(cypher, { id: currentUser.id })
return result.records.map(transformReturnType) log(result)
return result.records.map(r => r.get('notification'))
} finally { } finally {
session.close() session.close()
} }
@ -68,6 +76,7 @@ export default {
RETURN resource, notification, user RETURN resource, notification, user
` `
const result = await session.run(cypher, { resourceId: args.id, id: currentUser.id }) const result = await session.run(cypher, { resourceId: args.id, id: currentUser.id })
log(result)
const notifications = await result.records.map(transformReturnType) const notifications = await result.records.map(transformReturnType)
return notifications[0] return notifications[0]
} finally { } finally {