Barazo AppView backend
barazo.forum
1import {
2 pgTable,
3 pgPolicy,
4 text,
5 integer,
6 timestamp,
7 index,
8 uniqueIndex,
9 foreignKey,
10} from 'drizzle-orm/pg-core'
11import { sql } from 'drizzle-orm'
12import { appRole } from './roles.js'
13
14export const pages = pgTable(
15 'pages',
16 {
17 id: text('id').primaryKey(),
18 slug: text('slug').notNull(),
19 title: text('title').notNull(),
20 content: text('content').notNull(),
21 status: text('status', {
22 enum: ['draft', 'published'],
23 })
24 .notNull()
25 .default('draft'),
26 metaDescription: text('meta_description'),
27 parentId: text('parent_id'),
28 sortOrder: integer('sort_order').notNull().default(0),
29 communityDid: text('community_did').notNull(),
30 createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
31 updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
32 },
33 (table) => [
34 uniqueIndex('pages_slug_community_did_idx').on(table.slug, table.communityDid),
35 index('pages_community_did_idx').on(table.communityDid),
36 index('pages_parent_id_idx').on(table.parentId),
37 index('pages_status_community_did_idx').on(table.status, table.communityDid),
38 foreignKey({
39 columns: [table.parentId],
40 foreignColumns: [table.id],
41 name: 'pages_parent_id_fk',
42 }).onDelete('set null'),
43 pgPolicy('tenant_isolation', {
44 as: 'permissive',
45 to: appRole,
46 for: 'all',
47 using: sql`community_did = current_setting('app.current_community_did', true)`,
48 withCheck: sql`community_did = current_setting('app.current_community_did', true)`,
49 }),
50 ]
51).enableRLS()