import { sqliteTable, text, unique } from "drizzle-orm/sqlite-core"; import { createInsertSchema, createSelectSchema } from "@alkdev/drizzlebox"; import { type Static, Type } from "@alkdev/typebox"; import { commonCols } from "./common.ts"; import { graphs } from "./graphs.ts"; const AttributesSchema = Type.Record(Type.String(), Type.Unknown()); export const nodes = sqliteTable("nodes", { ...commonCols, graphId: text("graph_id").notNull().references(() => graphs.id, { onDelete: "cascade", }), key: text("key").notNull(), attributes: text("attributes", { mode: "json" }).notNull().default({}), }, (table) => ({ graphKeyIdx: unique().on(table.graphId, table.key), })); export const SelectNodeSchema = createSelectSchema(nodes, { attributes: AttributesSchema, metadata: Type.Object({}, { additionalProperties: true }), createdAt: Type.Date(), updatedAt: Type.Date(), }); export type SelectNode = Static; export const InsertNodeSchema = createInsertSchema(nodes, { key: Type.String({ minLength: 1 }), attributes: AttributesSchema, }); export type InsertNode = Static;