132 lines
3.1 KiB
TypeScript
132 lines
3.1 KiB
TypeScript
import { sql } from "drizzle-orm"
|
|
import {
|
|
doublePrecision,
|
|
foreignKey,
|
|
integer,
|
|
pgPolicy,
|
|
pgTable,
|
|
serial,
|
|
text,
|
|
uuid,
|
|
varchar
|
|
} from "drizzle-orm/pg-core"
|
|
import { authenticatedRole, authUsers } from "drizzle-orm/supabase"
|
|
|
|
export const users = pgTable("demo", {
|
|
id: serial("id").primaryKey(),
|
|
fullName: text("full_name"),
|
|
phone: varchar("phone", { length: 256 })
|
|
})
|
|
|
|
export const profiles = pgTable(
|
|
"profiles",
|
|
{
|
|
id: uuid("id").notNull().primaryKey(),
|
|
firstName: text("first_name"),
|
|
lastName: text("last_name")
|
|
},
|
|
(table) => [
|
|
foreignKey({
|
|
columns: [table.id],
|
|
foreignColumns: [authUsers.id],
|
|
name: "profiles_id_fkey"
|
|
}).onDelete("cascade"),
|
|
pgPolicy("select-own-profile", {
|
|
for: "select",
|
|
to: authenticatedRole,
|
|
using: sql`${table.id} = auth.uid()`
|
|
}),
|
|
pgPolicy("update-own-profile", {
|
|
for: "update",
|
|
to: authenticatedRole,
|
|
using: sql`${table.id} = auth.uid()`,
|
|
withCheck: sql`${table.id} = auth.uid()`
|
|
}),
|
|
pgPolicy("insert-profile", {
|
|
for: "insert",
|
|
to: authenticatedRole,
|
|
withCheck: sql`${table.id} = auth.uid()`
|
|
})
|
|
]
|
|
).enableRLS()
|
|
|
|
// === Catálogo de certificaciones ===
|
|
export const certifications = pgTable(
|
|
"certifications",
|
|
{
|
|
id: serial("id").primaryKey(),
|
|
name: text("name").notNull()
|
|
},
|
|
() => [
|
|
// Política: todos los usuarios autenticados pueden leer, nadie puede escribir
|
|
pgPolicy("select-certifications", {
|
|
for: "select",
|
|
to: authenticatedRole,
|
|
using: sql`true`
|
|
})
|
|
]
|
|
).enableRLS()
|
|
|
|
// === Catálogo de modelos de drones ===
|
|
export const droneModels = pgTable(
|
|
"drone_models",
|
|
{
|
|
id: serial("id").primaryKey(),
|
|
name: text("name").notNull()
|
|
},
|
|
() => [
|
|
pgPolicy("select-drone-models", {
|
|
for: "select",
|
|
to: authenticatedRole,
|
|
using: sql`true`
|
|
})
|
|
]
|
|
).enableRLS()
|
|
|
|
// === Tabla principal de pilotos ===
|
|
export const pilots = pgTable(
|
|
"pilots",
|
|
{
|
|
id: uuid("id").notNull().primaryKey(), // Igual que auth.uid()
|
|
name: text("name"),
|
|
location: text("location"),
|
|
latitude: doublePrecision("latitude"),
|
|
longitude: doublePrecision("longitude"),
|
|
company: text("company"),
|
|
position: text("position"),
|
|
description: text("description"),
|
|
differentiation: text("differentiation"),
|
|
coverageAreas: text("coverage_areas"),
|
|
specializationAreas: text("specialization_areas"),
|
|
certificationIds: integer("certification_ids").array(), // IDs de tabla certifications
|
|
droneModelIds: integer("drone_model_ids").array(), // IDs de tabla drone_models
|
|
email: text("email")
|
|
},
|
|
(table) => [
|
|
// Relación con la tabla auth.users
|
|
foreignKey({
|
|
columns: [table.id],
|
|
foreignColumns: [authUsers.id],
|
|
name: "pilots_id_fkey"
|
|
}).onDelete("cascade"),
|
|
|
|
// === RLS ===
|
|
pgPolicy("select-own-pilot", {
|
|
for: "select",
|
|
to: authenticatedRole,
|
|
using: sql`${table.id} = auth.uid()`
|
|
}),
|
|
pgPolicy("update-own-pilot", {
|
|
for: "update",
|
|
to: authenticatedRole,
|
|
using: sql`${table.id} = auth.uid()`,
|
|
withCheck: sql`${table.id} = auth.uid()`
|
|
}),
|
|
pgPolicy("insert-pilot", {
|
|
for: "insert",
|
|
to: authenticatedRole,
|
|
withCheck: sql`${table.id} = auth.uid()`
|
|
})
|
|
]
|
|
).enableRLS()
|