Try storing events in new database

This commit is contained in:
Alex Gleason 2023-08-06 15:03:29 -05:00
parent 91eac97d5c
commit 8ceb63fc89
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 47 additions and 12 deletions

View File

@ -1,4 +1,5 @@
import { Sqlite } from '@/deps.ts';
import { SignedEvent } from '@/event.ts';
interface User {
pubkey: string;
@ -13,7 +14,7 @@ class DittoDB {
this.#db = db;
this.#db.execute(`
CREATE TABLE events (
CREATE TABLE IF NOT EXISTS events (
id TEXT PRIMARY KEY,
kind INTEGER NOT NULL,
pubkey TEXT NOT NULL,
@ -23,10 +24,10 @@ class DittoDB {
sig TEXT NOT NULL
);
CREATE INDEX idx_events_kind ON events(kind);
CREATE INDEX idx_events_pubkey ON events(pubkey);
CREATE INDEX IF NOT EXISTS idx_events_kind ON events(kind);
CREATE INDEX IF NOT EXISTS idx_events_pubkey ON events(pubkey);
CREATE TABLE tags (
CREATE TABLE IF NOT EXISTS tags (
tag TEXT NOT NULL,
value_1 TEXT,
value_2 TEXT,
@ -35,17 +36,17 @@ class DittoDB {
FOREIGN KEY(event_id) REFERENCES events(id) ON DELETE CASCADE
);
CREATE INDEX idx_tags_tag ON tags(tag);
CREATE INDEX idx_tags_value_1 ON tags(value_1);
CREATE INDEX idx_tags_event_id ON tags(event_id);
CREATE INDEX IF NOT EXISTS idx_tags_tag ON tags(tag);
CREATE INDEX IF NOT EXISTS idx_tags_value_1 ON tags(value_1);
CREATE INDEX IF NOT EXISTS idx_tags_event_id ON tags(event_id);
CREATE TABLE users (
CREATE TABLE IF NOT EXISTS users (
pubkey TEXT PRIMARY KEY,
username TEXT NOT NULL,
inserted_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
);
CREATE UNIQUE INDEX idx_users_username ON users(username);
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_username ON users(username);
`);
}
@ -68,6 +69,38 @@ class DittoDB {
inserted_at: result[2],
};
}
insertEvent(event: SignedEvent): void {
this.#db.transaction(() => {
this.#db.query(
`
INSERT INTO events(id, kind, pubkey, content, created_at, tags, sig)
VALUES (?, ?, ?, ?, ?, ?, ?)
`,
[
event.id,
event.kind,
event.pubkey,
event.content,
event.created_at,
JSON.stringify(event.tags),
event.sig,
],
);
for (const [tag, value1, value2, value3] of event.tags) {
if (['p', 'e', 'q', 'd', 't', 'proxy'].includes(tag)) {
this.#db.query(
`
INSERT INTO tags(event_id, tag, value_1, value_2, value_3)
VALUES (?, ?, ?, ?, ?)
`,
[event.id, tag, value1 || null, value2 || null, value3 || null],
);
}
}
});
}
}
const db = new DittoDB(

View File

@ -1,9 +1,10 @@
import { Conf } from '@/config.ts';
import { db } from '@/db.ts';
import { RelayPool } from '@/deps.ts';
import { trends } from '@/trends.ts';
import { nostrDate, nostrNow } from '@/utils.ts';
import type { Event } from '@/event.ts';
import type { SignedEvent } from '@/event.ts';
const relay = new RelayPool([Conf.relay]);
@ -19,13 +20,14 @@ relay.subscribe(
);
/** Handle events through the loopback pipeline. */
function handleEvent(event: Event): void {
function handleEvent(event: SignedEvent): void {
console.info('loopback event:', event.id);
db.insertEvent(event);
trackHashtags(event);
}
/** Track whenever a hashtag is used, for processing trending tags. */
function trackHashtags(event: Event): void {
function trackHashtags(event: SignedEvent): void {
const date = nostrDate(event.created_at);
const tags = event.tags