-
Notifications
You must be signed in to change notification settings - Fork 0
/
neo4j.mjs
122 lines (117 loc) · 3.37 KB
/
neo4j.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @typedef {Object} Context
* @property {import('discord.js').Message} message
* @property {[string]} content
* @property {import('pg').Pool} postgres
* @property {import('neo4j-driver').Session} neo4j
*/
/**
* Exports SQL data to Neo4j.
* @param {Context} context
*/
export async function sqlToNeo4j(context) {
await context.postgres.query(`
COPY (SELECT * FROM "Moshpit")
TO '${process.env['NEO4J_IMPORT']}/moshpit.csv'
WITH CSV header;
`);
await context.postgres.query(`
COPY (SELECT * FROM "MoshpitUser")
TO '${process.env['NEO4J_IMPORT']}/moshpit_user.csv'
WITH CSV header;
`);
await context.postgres.query(`
COPY (
SELECT *
FROM "Moshpit" m JOIN "MoshpitUser" mu
ON m.moshpit_id = mu.moshpit_id
)
TO '${process.env['NEO4J_IMPORT']}/in.csv'
WITH CSV header;
`);
await context.postgres.query(`
COPY (
SELECT *
FROM "Moshpit" m JOIN "MoshpitUser" mu
ON m.owner_discord_id = mu.discord_user_id
)
TO '${process.env['NEO4J_IMPORT']}/leader.csv'
WITH CSV header;
`);
await context.neo4j.run(`
LOAD CSV WITH HEADERS
FROM 'file:///moshpit.csv'
AS row
MERGE (moshpit:Moshpit {moshpit_id: row.moshpit_id})
`);
await context.neo4j.run(`
LOAD CSV WITH HEADERS
FROM 'file:///moshpit_user.csv'
AS row
MERGE (user:MoshpitUser {discord_user_id: row.discord_user_id});
`);
await context.neo4j.run(`
LOAD CSV WITH HEADERS
FROM 'file:///in.csv'
AS row
MATCH (moshpit:Moshpit {moshpit_id: row.moshpit_id})
MATCH (user:MoshpitUser {discord_user_id: row.discord_user_id})
MERGE (user)-[:IN]->(moshpit);
`);
await context.neo4j.run(`
LOAD CSV WITH HEADERS
FROM 'file:///leader.csv'
AS row
MATCH (moshpit:Moshpit {moshpit_id: row.moshpit_id})
MATCH (user:MoshpitUser {discord_user_id: row.discord_user_id})
MERGE (user)-[:LEADS]->(moshpit);
`);
}
/**
* Adds track to database.
* @param {Context} context
*/
export async function addTrack(context) {
// Define a shortcut function to reply in the channel
await context.neo4j.run(`
MERGE (t:TRACK {spotify_track_id: CURRENT_TRACK_ID})
MERGE (m:Moshpit {moshpit_id: MOSHPIT_ID})-[r:PLAYED]->(t)
ON CREATE SET r.score = 0;
`);
}
/**
* Likes current track.
* @param {Context} context
*/
export async function like(context) {
// Define a shortcut function to reply in the channel
await context.neo4j.run(`
MATCH (m:Moshpit)-[r:PLAYED]->(t:Track)
WHERE m.moshpit_id = MOSHPIT_ID AND t.spotify_track_id = CURRENT_TRACK_ID
SET r.score = r.score+1;
`);
}
/**
* Dislikes current track.
* @param {Context} context
*/
export async function dislike(context) {
// Define a shortcut function to reply in the channel
await context.neo4j.run(`
MATCH (m:Moshpit)-[r:PLAYED]->(t:Track)
WHERE m.moshpit_id = MOSHPIT_ID AND t.spotify_track_id = CURRENT_TRACK_ID
SET r.score = r.score-1;
`);
}
/**
* Gets scores of all listened to tracks.
* @param {Context} context
*/
export async function getTrackScores(context) {
// Define a shortcut function to reply in the channel
await context.neo4j.run(`
MATCH (m:MoshPit)-[r:PLAYED]->(t:Track)
WHERE m.moshpit_id = MOSHPIT_ID
RETURN t.spotify_track_id AS track, r.score AS score;
`);
}