-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-backup.js
68 lines (62 loc) · 1.89 KB
/
import-backup.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
const fs = require('fs');
const { initializeApp } = require('firebase/app');
const { getFirestore, collection, doc, getDocs, writeBatch } = require('firebase/firestore');
// Initialiser Firebase
initializeApp();
const db = getFirestore();
// handle node first argument
const userID = process.argv[2];
// handle node second argument
const fileUrl = process.argv[3];
if (userID.length <= 0) {
throw new Error('Please provide a valid user ID');
}
if (fileUrl.length <= 0) {
throw new Error('Please provide a valid file URL');
}
const importBackup = async () => {
console.log(`1) Importing backup for user ${userID}`);
// Lire le fichier JSON et mettre à jour les walletId
const data = fs.readFileSync(fileUrl, 'utf8');
if (!data) {
console.error(err);
return;
}
const {
txs, userWallets, defiProtocols,
} = JSON.parse(data);
// update `uid` field to `userID`
console.log(`2) Updating data with user uid...`);
const updatedTxs = txs.map(tx => {
tx.uid = userID;
tx.createdAt = new Date(tx.createdAt);
return tx;
});
const updatedUserWallets = userWallets.map(wallet => {
wallet.uid = userID;
return wallet;
});
const updatedDefiProtocols = defiProtocols.map(protocol => {
protocol.uid = userID;
return protocol;
});
console.log(`3) Sending data to Firestore...`);
// batch save
const batch = writeBatch(db);
updatedTxs.forEach(({ id, ...tx }) => {
const docRef = doc(db, 'txs', id);
batch.set(docRef, tx);
});
updatedUserWallets.forEach(({ id, ...wallet }) => {
const docRef = doc(db, 'user-wallets', id);
batch.set(docRef, wallet);
});
updatedDefiProtocols.forEach(({ id, ...protocol }) => {
const docRef = doc(db, 'defi-protocols', id);
batch.set(docRef, protocol);
});
console.log(`4) Committing batch...`);
await batch.commit();
console.log(`5) Data has been sent to Firestore 🎉`);
}
importBackup();