-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.mjs
74 lines (59 loc) · 1.96 KB
/
migrate.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
import { env } from "node:process";
import assert from "node:assert";
import { config } from "dotenv";
import mongoose from "mongoose";
import { Surreal } from "surrealdb.js";
config();
const { MONGODB_URL, SURREAL_DB_URL, SURREAL_DB_NAMESPACE, SURREAL_DB_DATABASE, SURREAL_DB_USER, SURREAL_DB_PASS } = env;
assert(MONGODB_URL, "MONGODB_URL is not defined!");
assert(SURREAL_DB_URL && SURREAL_DB_NAMESPACE && SURREAL_DB_DATABASE && SURREAL_DB_USER && SURREAL_DB_PASS, "SURREAL_DB vars is not defined!");
await mongoose.connect(MONGODB_URL);
const mongo = mongoose.connection.db;
const surreal = new Surreal(SURREAL_DB_URL, {
ns: SURREAL_DB_NAMESPACE,
db: SURREAL_DB_DATABASE
});
await surreal.signin({
user: SURREAL_DB_USER,
pass: SURREAL_DB_PASS
});
const servers = await surreal.select("servers");
for (const server of servers) {
const [, serverId ] = server.id.split(":");
const { prefix, prefixEnabled, language, musicVolume, musicChannel, logChannel } = server;
if (await mongo.collection("servers").findOne({ serverId }))
continue;
await mongo.collection("servers").insertOne({
serverId,
prefix,
prefixEnabled,
language,
musicVolume,
musicChannel,
logChannel
});
}
console.log(`${servers.length} servers migrated!`);
const moderators = await surreal.select("moderators");
for (const moderator of moderators) {
const { serverId, userId } = moderator;
if (await mongo.collection("moderators").findOne({ serverId, userId }))
continue;
await mongo.collection("moderators").insertOne({
serverId,
userId
});
}
console.log(`${moderators.length} moderators migrated!`);
const restricted = await surreal.select("restricted");
for (const restrictedUser of restricted) {
const { userId } = restrictedUser;
if (await mongo.collection("restricted").findOne({ userId }))
continue;
await mongo.collection("restricted").insertOne({
userId
});
}
console.log(`${restricted.length} restricted users migrated!`);
await surreal.close();
await mongoose.disconnect();