import { pgTable, foreignKey, unique, pgPolicy, uuid, text, timestamp } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"
export const users = pgTable("users", {
id: uuid().primaryKey().notNull(),
first_name: text(),
last_name: text(),
username: text(),
avatar_url: text(),
created_at: timestamp({ withTimezone: true, mode: 'string' }).defaultNow().notNull(),
email: text().notNull(),
}, (table) => [
foreignKey({
columns: [table.id],
foreignColumns: [table.id],
name: "users_id_fkey"
}).onDelete("cascade"),
unique("users_username_key").on(table.username),
pgPolicy("Users can update their own users table", { as: "permissive", for: "update", to: ["public"], using: sql`(( SELECT auth.uid() AS uid) = id)` }),
pgPolicy("Enable read access for all users", { as: "permissive", for: "select", to: ["public"] }),
pgPolicy("Enable insert for users based on id", { as: "permissive", for: "insert", to: ["public"] }),
]);
import { pgTable, foreignKey, unique, pgPolicy, uuid, text, timestamp } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"
export const users = pgTable("users", {
id: uuid().primaryKey().notNull(),
first_name: text(),
last_name: text(),
username: text(),
avatar_url: text(),
created_at: timestamp({ withTimezone: true, mode: 'string' }).defaultNow().notNull(),
email: text().notNull(),
}, (table) => [
foreignKey({
columns: [table.id],
foreignColumns: [table.id],
name: "users_id_fkey"
}).onDelete("cascade"),
unique("users_username_key").on(table.username),
pgPolicy("Users can update their own users table", { as: "permissive", for: "update", to: ["public"], using: sql`(( SELECT auth.uid() AS uid) = id)` }),
pgPolicy("Enable read access for all users", { as: "permissive", for: "select", to: ["public"] }),
pgPolicy("Enable insert for users based on id", { as: "permissive", for: "insert", to: ["public"] }),
]);