-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-database.js
192 lines (171 loc) · 5.01 KB
/
copy-database.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
require('dotenv').config();
const mongoose = require("mongoose");
async function transferData() {
try {
const productionDB = mongoose.createConnection(process.env.PROD_DB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
console.log('Connected to production database.');
// Create the schemas
const UserSchema = new mongoose.Schema({
name: String,
image: String,
view_count: Number,
isStreamer: {
type: Boolean,
},
followers: {
type: Number,
required: false,
},
hasUpdatedFollowersCount: {
type: Boolean,
default: false,
},
});
const HearthstoneCardSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
imageURL: {
type: String,
required: true
},
expansion: {
type: String,
required: true
},
dbf_id: {
type: Number,
required: false,
},
hsr_rating: {
type: Number,
required: false,
},
mana: {
type: Number,
required: true,
},
type: {
type: String,
enum: ['Minion', 'Spell', 'Weapon', 'Hero', 'Location', 'Quest'],
required: false,
},
hsClass: {
type: String,
enum: ['Death Knight', 'Demon Hunter', 'Druid', 'Hunter', 'Mage', 'Paladin', 'Priest', 'Rogue', 'Shaman', 'Warlock', 'Warrior', 'Neutral'],
required: true,
},
attack: {
type: Number,
required: false,
},
health: {
type: Number,
required: false,
},
rarity: {
type: String,
enum: ['Common', 'Rare', 'Epic', 'Legendary', 'Extra', 'Basic'],
required: false,
},
extraCards: [
{
name: {
type: String,
required: true,
},
imageURL: {
type: String,
required: true
},
expansion: {
type: String,
required: true
},
hsClass: {
type: String,
enum: ['Death Knight', 'Demon Hunter', 'Druid', 'Hunter', 'Mage', 'Paladin', 'Priest', 'Rogue', 'Shaman', 'Warlock', 'Warrior', 'Neutral'],
required: true,
},
rarity: {
type: String,
enum: ['Common', 'Rare', 'Epic', 'Legendary', 'Extra', 'Basic'],
required: true,
},
}
],
});
const RatingSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
streamer: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: false,
},
card: {
type: mongoose.Schema.Types.ObjectId,
ref: 'HearthstoneCard',
required: true,
},
rating: {
type: Number,
min: 0,
max: 4,
required: true,
},
chatRating: {
type: Number,
min: 0,
max: 4,
required: false,
},
review: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
},
});
const usersModel = productionDB.model('User', UserSchema);
const users = await usersModel.find({}).lean();
const cardsModel = productionDB.model('HearthstoneCard', HearthstoneCardSchema);
const cards = await cardsModel.find({}).lean();
const ratingsModel = productionDB.model('Rating', RatingSchema);
const ratings = await ratingsModel.find({}).lean();
console.log('Loaded production data.');
await productionDB.close();
console.log('Disconnected from production database.');
const developmentDB = await mongoose.createConnection(process.env.DB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
console.log('Connected to development database.');
// Register the models for the collections in the development database
const developmentUserModel = developmentDB.model('User', UserSchema);
const developmentCardsModel = developmentDB.model('HearthstoneCard', HearthstoneCardSchema);
const developmentRatingsModel = developmentDB.model('Rating', RatingSchema);
// Clear existing data in the development collections (if needed)
await developmentUserModel.deleteMany();
await developmentCardsModel.deleteMany();
await developmentRatingsModel.deleteMany();
// Insert the fetched data into the development database
await developmentUserModel.insertMany(users);
await developmentCardsModel.insertMany(cards);
await developmentRatingsModel.insertMany(ratings);
console.log('Data transferred to development database.');
// Disconnect from the development database
await developmentDB.close();
console.log('Disconnected from development database.');
} catch (error) {
console.error('An error occurred:', error);
}
}
transferData();