-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextautharangox.js
211 lines (149 loc) · 5.26 KB
/
nextautharangox.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const {Database,aql} = require("arangojs");
const db = new Database({
url: "http://localhost:8529",
databaseName: "mydb",
auth: {
username: "root",
password: ""
},
})
const getCollection = async (cName) => {
// get list of collections in database
const collections = await db.collections();
// check if collection exists, if so return collection, if not, create it
if (collections.find((c) => c._name === cName)) {
return await db.collection(cName);
} else {
return db.createCollection(cName);
}
};
export async function arangoDBAdapter() {
await getCollection("Users")
await getCollection("Accounts")
await getCollection("Sessions")
await getCollection("VerificationTokens")
return {
async createUser(user) {
try {
await db.query(aql `INSERT {${user}} IN Users`);
return user
} catch (error) {
console.log(error)
}
},
async getUser(id) {
try {
const user = await db.query(aql `FOR u in Users FILTER ${id} == u._id RETURN { u }`);
if (!user){return null;}
return (user[0]);
} catch (error) {
console.log(error)
}
},
async getUserByEmail(email) {
console.log(email)
try {
const user = await db.query(aql `FOR u in Users FILTER ${email} == u.email RETURN { u }`);
if (!user){return null;}
return (user[0]);
} catch (error) {
console.log(error)
}
},
async getUserByAccount({ provider, id }) {
try {
const account = await db.query(aql `FOR u in Accounts FILTER ${id} == ${"u."+provider} RETURN { u }`);
if (!account) {return null;}
const user = await this.getUser(account[0].userId);
if (!user){return null;}
return from(user);
} catch (error) {
console.log(error)
}
},
async updateUser(data) {
try {
const user = await db.query(aql `FOR u IN UserCollection FILTER u._id == ${data.id} UPDATE u WITH {${data}} IN UserCollection`);
return from(user);
} catch (error) {
console.log(error)
}
},
async deleteUser(userId) {
try {
} catch (error) {
console.log(error)
}
await db.query(aql `FOR u IN Users FILTER u._is == ${userId} REMOVE u IN Users`);
await db.query(aql `FOR u IN Accounts FILTER userId == ${userId} REMOVE u IN Accounts`);
await db.query(aql `FOR u IN Sessions FILTER userId == ${userId} REMOVE u IN Sessions`);
},
async linkAccount(account) {
try {
await db.query(aql `INSERT {${account}} IN Users`);
return account
} catch (error) {
console.log(error)
}
},
async unlinkAccount({ provider, id }) {
try {
const account = await db.query(aql `FOR u in Accounts FILTER ${id} == ${"u."+provider} REMOVE u IN Accounts RETURN { u }`);
return account;
} catch (error) {
console.log(error)
}
},
async createSession(data) {
try {
await db.query(aql`INSERT {${data}} IN Sessions`);
return data
} catch (error) {
console.log(error)
}
},
async getSessionAndUser(sessionToken) {
try {
const account = await db.query(aql `FOR u in Sessions FILTER ${sessionToken} == u.id RETURN { u }`);
if (!account) {return null;}
const user = await this.getUser(account[0].userId);
if (!user){return null;}
return user;
} catch (error) {
console.log(error)
}
},
async updateSession(data) {
try {
const Session = await db.query(aql `FOR u IN Sessions FILTER u.sessionToken == ${data.sessionToken} UPDATE u WITH {${data}} IN Sessions`);
return Session;
} catch (error) {
console.log(error)
}
},
async deleteSession(sessionToken) {
try {
const Session = await db.query(aql `FOR u in Sessions FILTER ${sessionToken} == u.sessionToken REMOVE u IN Sessions RETURN { u }`);
return Session;
} catch (error) {
console.log(error)
}
},
async createVerificationToken(data) {
try {
await db.query(aql `INSERT {${data}} IN VerificationTokens`);
return data
} catch (error) {
console.log(error)
}
},
async useVerificationToken({ identifier, token }) {
try {
const VerificationToken = await db.query(aql `FOR u in Sessions FILTER ${token} == u.token REMOVE u IN Sessions RETURN { u }`);
if (!VerificationToken){return null;}
} catch (error) {
console.log(error)
}
},
}
}