Skip to content

Commit 1bb4fd3

Browse files
authored
Migrate address book on app start and delete old address book (#164)
1 parent 77d229e commit 1bb4fd3

File tree

4 files changed

+83
-65
lines changed

4 files changed

+83
-65
lines changed

main/api/contacts/index.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Store, { Schema } from "electron-store";
22
import { v4 as uuidv4 } from "uuid";
33
import { z } from "zod";
44

5-
import { getNodeAppBetaContacts } from "./utils/getNodeAppBetaContacts";
65
import { t } from "../trpc";
76

87
const contactDefinition = z.object({
@@ -35,7 +34,7 @@ const schema: Schema<{
3534
},
3635
};
3736

38-
const store = new Store({ schema, name: "contacts" });
37+
export const store = new Store({ schema, name: "contacts" });
3938

4039
export const contactsRouter = t.router({
4140
getContacts: t.procedure.query(async () => {
@@ -54,27 +53,6 @@ export const contactsRouter = t.router({
5453
null
5554
);
5655
}),
57-
migrateNodeAppBetaContacts: t.procedure.mutation(async () => {
58-
const betaContacts = await getNodeAppBetaContacts();
59-
const contacts = await store.get("contacts", []);
60-
const addressLookup = new Set(contacts.map((contact) => contact.address));
61-
62-
let order = 0;
63-
64-
for (const contact of betaContacts) {
65-
if (!addressLookup.has(contact.address)) {
66-
contacts.push({
67-
id: uuidv4(),
68-
name: contact.name,
69-
address: contact.address,
70-
order: order++,
71-
});
72-
}
73-
}
74-
75-
store.set("contacts", contacts);
76-
return contacts;
77-
}),
7856
addContact: t.procedure
7957
.input(
8058
z.object({

main/api/contacts/utils/getNodeAppBetaContacts.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import fs from "fs";
2+
import os from "os";
3+
import path from "path";
4+
5+
import NeDBStorage from "nedb";
6+
import { v4 as uuidv4 } from "uuid";
7+
8+
import { store } from "..";
9+
10+
type BetaContact = {
11+
name: string;
12+
address: string;
13+
};
14+
15+
let nedb: NeDBStorage;
16+
17+
function getNodeAppBetaContacts(): Promise<Array<BetaContact>> {
18+
const betaContactsDbPath = path.join(
19+
os.homedir(),
20+
"/.ironfish/node-app",
21+
"address_book.db",
22+
);
23+
24+
if (!fs.existsSync(betaContactsDbPath)) {
25+
return Promise.resolve([]);
26+
}
27+
28+
if (!nedb) {
29+
nedb = new NeDBStorage({
30+
filename: betaContactsDbPath,
31+
autoload: true,
32+
timestampData: true,
33+
});
34+
}
35+
36+
return new Promise((resolve, reject) => {
37+
nedb.find({}, (err: unknown, docs: Array<BetaContact>) => {
38+
if (err) {
39+
reject(err);
40+
} else {
41+
resolve(docs || []);
42+
}
43+
});
44+
});
45+
}
46+
47+
function deleteNodeAppBetaContacts(): void {
48+
const betaContactsDbPath = path.join(
49+
os.homedir(),
50+
"/.ironfish/node-app",
51+
"address_book.db",
52+
);
53+
54+
if (fs.existsSync(betaContactsDbPath)) {
55+
fs.rmSync(betaContactsDbPath);
56+
}
57+
}
58+
59+
export async function migrateNodeAppBetaContacts() {
60+
const betaContacts = await getNodeAppBetaContacts();
61+
const contacts = store.get("contacts", []);
62+
const addressLookup = new Set(contacts.map((contact) => contact.address));
63+
64+
let order = 0;
65+
66+
for (const contact of betaContacts) {
67+
if (!addressLookup.has(contact.address)) {
68+
contacts.push({
69+
id: uuidv4(),
70+
name: contact.name,
71+
address: contact.address,
72+
order: order++,
73+
});
74+
}
75+
}
76+
77+
store.set("contacts", contacts);
78+
deleteNodeAppBetaContacts();
79+
return contacts;
80+
}

main/background.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import serve from "electron-serve";
44
import { createIPCHandler } from "electron-trpc/main";
55

66
import { router } from "./api";
7+
import { migrateNodeAppBetaContacts } from "./api/contacts/utils/migrateNodeAppBetaContacts";
78
import { manager } from "./api/manager";
89
import { getUserSettings } from "./api/user-settings/userSettings";
910
import { mainWindow } from "./main-window";
@@ -79,6 +80,7 @@ app.whenReady().then(() => {
7980

8081
createThemeChangeHandler();
8182
setNativeThemeSource();
83+
migrateNodeAppBetaContacts();
8284

8385
const handler = createIPCHandler({ router });
8486

0 commit comments

Comments
 (0)