Barazo AppView backend
barazo.forum
1import {
2 pgTable,
3 pgPolicy,
4 text,
5 integer,
6 timestamp,
7 jsonb,
8 boolean,
9 index,
10} from 'drizzle-orm/pg-core'
11import { sql } from 'drizzle-orm'
12import { appRole } from './roles.js'
13
14export const replies = pgTable(
15 'replies',
16 {
17 uri: text('uri').primaryKey(),
18 rkey: text('rkey').notNull(),
19 authorDid: text('author_did').notNull(),
20 content: text('content').notNull(),
21 rootUri: text('root_uri').notNull(),
22 rootCid: text('root_cid').notNull(),
23 parentUri: text('parent_uri').notNull(),
24 parentCid: text('parent_cid').notNull(),
25 communityDid: text('community_did').notNull(),
26 cid: text('cid').notNull(),
27 labels: jsonb('labels').$type<{ values: { val: string }[] }>(),
28 reactionCount: integer('reaction_count').notNull().default(0),
29 voteCount: integer('vote_count').notNull().default(0),
30 depth: integer('depth').notNull().default(1),
31 createdAt: timestamp('created_at', { withTimezone: true }).notNull(),
32 indexedAt: timestamp('indexed_at', { withTimezone: true }).notNull().defaultNow(),
33 isAuthorDeleted: boolean('is_author_deleted').notNull().default(false),
34 isModDeleted: boolean('is_mod_deleted').notNull().default(false),
35 moderationStatus: text('moderation_status', {
36 enum: ['approved', 'held', 'rejected'],
37 })
38 .notNull()
39 .default('approved'),
40 /** Trust status based on account age at indexing time. 'new' for accounts < 24h old. */
41 trustStatus: text('trust_status', {
42 enum: ['trusted', 'new'],
43 })
44 .notNull()
45 .default('trusted'),
46 // Note: search_vector (tsvector) and embedding (vector) columns exist in the
47 // database but are managed outside Drizzle schema (created by db:push custom SQL).
48 // search_vector is maintained by a database trigger.
49 // embedding is nullable vector(768) for optional semantic search.
50 },
51 (table) => [
52 index('replies_author_did_idx').on(table.authorDid),
53 index('replies_root_uri_idx').on(table.rootUri),
54 index('replies_parent_uri_idx').on(table.parentUri),
55 index('replies_created_at_idx').on(table.createdAt),
56 index('replies_community_did_idx').on(table.communityDid),
57 index('replies_moderation_status_idx').on(table.moderationStatus),
58 index('replies_trust_status_idx').on(table.trustStatus),
59 index('replies_root_uri_created_at_idx').on(table.rootUri, table.createdAt),
60 index('replies_root_uri_depth_idx').on(table.rootUri, table.depth),
61 index('replies_author_did_rkey_idx').on(table.authorDid, table.rkey),
62 pgPolicy('tenant_isolation', {
63 as: 'permissive',
64 to: appRole,
65 for: 'all',
66 using: sql`community_did = current_setting('app.current_community_did', true)`,
67 withCheck: sql`community_did = current_setting('app.current_community_did', true)`,
68 }),
69 ]
70).enableRLS()