Skip to content

Commit

Permalink
feat(sqlite): add to shared-server
Browse files Browse the repository at this point in the history
  • Loading branch information
KayBeSee committed Oct 14, 2022
1 parent 6ed74a1 commit f4a05db
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/shared-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"detect-rpi": "^1.4.0",
"lndconnect": "^0.2.10",
"socks-proxy-agent": "^6.1.0",
"sqlite": "^4.1.2",
"typescript": "^4.4.4",
"unchained-bitcoin": "^0.1.8"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/shared-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from './HWI/commands';
export * from './HWI/runCommand';
export * from './OnchainProviders';
export * from './LightningProviders';
export * from './OnchainProviders';
export * from './sqlite';
export * from './utils/utils';
export * from './utils/lightning';
export * from './utils/accountMap';
62 changes: 62 additions & 0 deletions packages/shared-server/src/sqlite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sqlite3 from 'sqlite3';
import { open, Database } from 'sqlite';

function createDbConnection(filename: string) {
return open({
filename,
driver: sqlite3.Database
});
}

export async function dbConnect(userDataPath: string) {
sqlite3.verbose();
const db = await createDbConnection(`${userDataPath}/lily.db`);
return db;
}

export async function createAddressTable(db: Database<sqlite3.Database, sqlite3.Statement>) {
const query = `CREATE TABLE IF NOT EXISTS address_labels (id INTEGER PRIMARY KEY, address TEXT NOT NULL, label TEX NOT NULL)`;
db.exec(query);
}

export async function addAddressLabel(
db: Database<sqlite3.Database, sqlite3.Statement>,
address: string,
label: string
) {
try {
const query = `INSERT INTO address_labels (id, address, label) VALUES (null, ?, ?)`;
const entry = await db.run(query, [address, label]);
return entry.lastID;
} catch (error) {
console.error(error);
throw error;
}
}

export async function deleteAddressLabel(
db: Database<sqlite3.Database, sqlite3.Statement>,
id: number
) {
try {
const query = `DELETE FROM address_labels WHERE id = ?`;
await db.run(query, [id]);
} catch (error) {
console.error(error);
throw error;
}
}

export async function getAllLabelsForAddress(
db: Database<sqlite3.Database, sqlite3.Statement>,
address: string
) {
try {
const query = `SELECT * FROM address_labels WHERE address = ?`;
const labels = await db.all(query, [address]);
return labels;
} catch (error) {
console.error(error);
throw error;
}
}
6 changes: 6 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,9 @@ export interface CoindeskCurrentPriceResponse {
};
};
}

export interface AddressLabel {
id: number;
address: string;
label: string;
}

0 comments on commit f4a05db

Please sign in to comment.