forked from vertex-lab/nostr-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
103 lines (89 loc) · 2.76 KB
/
schema.sql
File metadata and controls
103 lines (89 loc) · 2.76 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
CREATE TABLE IF NOT EXISTS events (
id TEXT PRIMARY KEY,
pubkey TEXT NOT NULL,
created_at INTEGER NOT NULL,
kind INTEGER NOT NULL,
tags JSONB NOT NULL,
content TEXT NOT NULL,
sig TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS time_idx ON events(created_at DESC, id ASC);
CREATE INDEX IF NOT EXISTS kind_sorted_idx ON events(kind, created_at DESC, id ASC);
CREATE INDEX IF NOT EXISTS pubkey_kind_sorted_idx ON events(pubkey, kind, created_at DESC, id ASC);
CREATE TABLE IF NOT EXISTS users (
pubkey TEXT PRIMARY KEY,
name TEXT,
about TEXT,
picture TEXT,
website TEXT,
banner TEXT,
bot INTEGER,
lud16 TEXT,
event_id TEXT NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS users_name_idx ON users(name);
CREATE TRIGGER IF NOT EXISTS users_ai AFTER INSERT ON events
WHEN NEW.kind = 0 AND json_valid(NEW.content)
BEGIN
INSERT INTO users (pubkey, name, about, picture, website, banner, bot, lud16, event_id, created_at)
VALUES (
NEW.pubkey,
json_extract(NEW.content, '$.name'),
json_extract(NEW.content, '$.about'),
json_extract(NEW.content, '$.picture'),
json_extract(NEW.content, '$.website'),
json_extract(NEW.content, '$.banner'),
json_extract(NEW.content, '$.bot'),
json_extract(NEW.content, '$.lud16'),
NEW.id,
NEW.created_at
)
ON CONFLICT(pubkey) DO UPDATE SET
name = excluded.name,
about = excluded.about,
picture = excluded.picture,
website = excluded.website,
banner = excluded.banner,
bot = excluded.bot,
lud16 = excluded.lud16,
event_id = excluded.event_id,
created_at = excluded.created_at
WHERE excluded.created_at > users.created_at;
END;
CREATE TABLE IF NOT EXISTS relays (
url TEXT PRIMARY KEY,
fetched_at INTEGER NOT NULL,
name TEXT,
description TEXT,
banner TEXT,
icon TEXT,
pubkey TEXT,
self TEXT,
contact TEXT,
supported_nips JSON,
software TEXT,
version TEXT,
limitation JSON,
payments_url TEXT,
fees JSON
);
CREATE INDEX IF NOT EXISTS relays_name_idx ON relays(name);
CREATE TABLE IF NOT EXISTS tags (
event_id TEXT NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (key, value, event_id),
FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS tags_event_id_idx ON tags(event_id);
CREATE TRIGGER IF NOT EXISTS d_tags_ai AFTER INSERT ON events
WHEN NEW.kind BETWEEN 30000 AND 39999
BEGIN
INSERT INTO tags (event_id, key, value)
SELECT NEW.id, 'd', json_extract(value, '$[1]')
FROM json_each(NEW.tags)
WHERE json_type(value) = 'array' AND json_array_length(value) > 1 AND json_extract(value, '$[0]') = 'd'
LIMIT 1;
END;