search for standard sites pub-search.waow.tech
search zig blog atproto
at multi-platform-schema 90 lines 3.1 kB view raw
1const std = @import("std"); 2const db = @import("db/mod.zig"); 3 4pub fn insertDocument( 5 uri: []const u8, 6 did: []const u8, 7 rkey: []const u8, 8 title: []const u8, 9 content: []const u8, 10 created_at: ?[]const u8, 11 publication_uri: ?[]const u8, 12 tags: []const []const u8, 13) !void { 14 const c = db.getClient() orelse return error.NotInitialized; 15 16 try c.exec( 17 "INSERT OR REPLACE INTO documents (uri, did, rkey, title, content, created_at, publication_uri) VALUES (?, ?, ?, ?, ?, ?, ?)", 18 &.{ uri, did, rkey, title, content, created_at orelse "", publication_uri orelse "" }, 19 ); 20 21 // update FTS index 22 c.exec("DELETE FROM documents_fts WHERE uri = ?", &.{uri}) catch {}; 23 c.exec( 24 "INSERT INTO documents_fts (uri, title, content) VALUES (?, ?, ?)", 25 &.{ uri, title, content }, 26 ) catch {}; 27 28 // update tags 29 c.exec("DELETE FROM document_tags WHERE document_uri = ?", &.{uri}) catch {}; 30 for (tags) |tag| { 31 c.exec( 32 "INSERT OR IGNORE INTO document_tags (document_uri, tag) VALUES (?, ?)", 33 &.{ uri, tag }, 34 ) catch {}; 35 } 36} 37 38pub fn insertPublication( 39 uri: []const u8, 40 did: []const u8, 41 rkey: []const u8, 42 name: []const u8, 43 description: ?[]const u8, 44 base_path: ?[]const u8, 45) !void { 46 const c = db.getClient() orelse return error.NotInitialized; 47 48 try c.exec( 49 "INSERT OR REPLACE INTO publications (uri, did, rkey, name, description, base_path) VALUES (?, ?, ?, ?, ?, ?)", 50 &.{ uri, did, rkey, name, description orelse "", base_path orelse "" }, 51 ); 52 53 // update FTS index 54 c.exec("DELETE FROM publications_fts WHERE uri = ?", &.{uri}) catch {}; 55 c.exec( 56 "INSERT INTO publications_fts (uri, name, description) VALUES (?, ?, ?)", 57 &.{ uri, name, description orelse "" }, 58 ) catch {}; 59} 60 61pub fn deleteDocument(uri: []const u8) void { 62 const c = db.getClient() orelse return; 63 64 // record tombstone 65 var ts_buf: [20]u8 = undefined; 66 const ts = std.fmt.bufPrint(&ts_buf, "{d}", .{std.time.timestamp()}) catch "0"; 67 c.exec( 68 "INSERT OR REPLACE INTO tombstones (uri, record_type, deleted_at) VALUES (?, 'document', ?)", 69 &.{ uri, ts }, 70 ) catch {}; 71 // delete record 72 c.exec("DELETE FROM documents WHERE uri = ?", &.{uri}) catch {}; 73 c.exec("DELETE FROM documents_fts WHERE uri = ?", &.{uri}) catch {}; 74 c.exec("DELETE FROM document_tags WHERE document_uri = ?", &.{uri}) catch {}; 75} 76 77pub fn deletePublication(uri: []const u8) void { 78 const c = db.getClient() orelse return; 79 80 // record tombstone 81 var ts_buf: [20]u8 = undefined; 82 const ts = std.fmt.bufPrint(&ts_buf, "{d}", .{std.time.timestamp()}) catch "0"; 83 c.exec( 84 "INSERT OR REPLACE INTO tombstones (uri, record_type, deleted_at) VALUES (?, 'publication', ?)", 85 &.{ uri, ts }, 86 ) catch {}; 87 // delete record 88 c.exec("DELETE FROM publications WHERE uri = ?", &.{uri}) catch {}; 89 c.exec("DELETE FROM publications_fts WHERE uri = ?", &.{uri}) catch {}; 90}