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 categories = pgTable(
15 'categories',
16 {
17 id: text('id').primaryKey(),
18 slug: text('slug').notNull(),
19 name: text('name').notNull(),
20 description: text('description'),
21 parentId: text('parent_id'),
22 sortOrder: integer('sort_order').notNull().default(0),
23 communityDid: text('community_did').notNull(),
24 maturityRating: text('maturity_rating', {
25 enum: ['safe', 'mature', 'adult'],
26 })
27 .notNull()
28 .default('safe'),
29 createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
30 updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
31 },
32 (table) => [
33 uniqueIndex('categories_slug_community_did_idx').on(table.slug, table.communityDid),
34 index('categories_parent_id_idx').on(table.parentId),
35 index('categories_community_did_idx').on(table.communityDid),
36 index('categories_maturity_rating_idx').on(table.maturityRating),
37 foreignKey({
38 columns: [table.parentId],
39 foreignColumns: [table.id],
40 name: 'categories_parent_id_fk',
41 }).onDelete('set null'),
42 pgPolicy('tenant_isolation', {
43 as: 'permissive',
44 to: appRole,
45 for: 'all',
46 using: sql`community_did = current_setting('app.current_community_did', true)`,
47 withCheck: sql`community_did = current_setting('app.current_community_did', true)`,
48 }),
49 ]
50).enableRLS()