Skip to content

Commit

Permalink
Add a function to directly load from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Jan 16, 2025
1 parent 72c9afb commit 89a5511
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
44 changes: 37 additions & 7 deletions ndk-mobile/src/cache-adapter/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as SQLite from 'expo-sqlite';
import { matchFilter } from 'nostr-tools';
import { migrations } from './migrations';

type EventRecord = {
export type NDKSqliteEventRecord = {
id: string;
created_at: number;
pubkey: string;
Expand Down Expand Up @@ -142,19 +142,19 @@ export class NDKCacheAdapterSqlite implements NDKCacheAdapter {
const events = this.db.getAllSync(
`SELECT * FROM events WHERE pubkey IN (${filter.authors.map(() => '?').join(',')}) AND kind IN (${filter.kinds.map(() => '?').join(',')})`,
[...filter.authors, ...filter.kinds]
) as EventRecord[];
) as NDKSqliteEventRecord[];
if (events.length > 0) foundEvents(subscription, events, filter);
} else if (filter.authors) {
const events = this.db.getAllSync(
`SELECT * FROM events WHERE pubkey IN (${filter.authors.map(() => '?').join(',')})`,
filter.authors
) as EventRecord[];
) as NDKSqliteEventRecord[];
if (events.length > 0) foundEvents(subscription, events, filter);
} else if (filter.kinds) {
const events = this.db.getAllSync(
`SELECT * FROM events WHERE kind IN (${filter.kinds.map(() => '?').join(',')})`,
filter.kinds
) as EventRecord[];
) as NDKSqliteEventRecord[];
if (events.length > 0) foundEvents(subscription, events, filter);
}

Expand All @@ -164,7 +164,7 @@ export class NDKCacheAdapterSqlite implements NDKCacheAdapter {
const events = this.db.getAllSync(
`SELECT * FROM events INNER JOIN event_tags ON events.id = event_tags.event_id WHERE event_tags.tag = ? AND event_tags.value IN (${filter[key].map(() => '?').join(',')})`,
[tag, ...filter[key]]
) as EventRecord[];
) as NDKSqliteEventRecord[];
if (events.length > 0) foundEvents(subscription, events, filter);
}
}
Expand Down Expand Up @@ -419,9 +419,39 @@ export class NDKCacheAdapterSqlite implements NDKCacheAdapter {
this.db.runSync(`DELETE FROM unpublished_events;`);
});
}

/**
* This function runs a query and returns parsed events.
* @param query The query to run.
* @param params The parameters to pass to the query.
* @param filterFn An optional filter function to filter events before deserializing them.
* @returns
*/
public getEvents(
query: string,
params: any[],
filterFn: (record: NDKSqliteEventRecord) => boolean
) {
let res = this.db.getAllSync(query, params) as NDKSqliteEventRecord[];

if (filterFn) {
res = res.filter(filterFn);
}

// deserialize the events
return res.map((record) => {
try {
const deserializedEvent = deserialize(record.event);
return new NDKEvent(undefined, deserializedEvent);
} catch (e) {
console.error('failed to deserialize event', e, record);
return null;
}
});
}
}

export function foundEvents(subscription: NDKSubscription, events: EventRecord[], filter?: NDKFilter) {
export function foundEvents(subscription: NDKSubscription, events: NDKSqliteEventRecord[], filter?: NDKFilter) {
// if we have a limit, sort and slice
if (filter?.limit && events.length > filter.limit) {
events = events.sort((a, b) => b.created_at - a.created_at).slice(0, filter.limit);
Expand All @@ -432,7 +462,7 @@ export function foundEvents(subscription: NDKSubscription, events: EventRecord[]
}
}

export function foundEvent(subscription: NDKSubscription, event: EventRecord, relayUrl: WebSocket['url'] | undefined, filter?: NDKFilter) {
export function foundEvent(subscription: NDKSubscription, event: NDKSqliteEventRecord, relayUrl: WebSocket['url'] | undefined, filter?: NDKFilter) {
try {
const deserializedEvent = deserialize(event.event);

Expand Down
1 change: 0 additions & 1 deletion ndk-mobile/src/hooks/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const useNDKWallet = () => {
}

if (wallet) {
wallet.on('ready', () => console.log('cashu wallet ready'));
wallet.on('ready', updateBalance);
wallet.on('balance_updated', updateBalance);
} else {
Expand Down

0 comments on commit 89a5511

Please sign in to comment.