Add external model persistence foundation

This commit is contained in:
Julian Appel 2026-08-02 17:18:24 +02:00
parent d0fc1a7caf
commit 76493b573e
15 changed files with 3207 additions and 8 deletions

View file

@ -0,0 +1,43 @@
import { blob, index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import type {
ExternalCsvConfiguration,
ExternalCsvDocument,
} from "../../external-model/csv/external-csv-contracts.js";
import { externalImportKinds } from "../../external-model/domain/external-model-contracts.js";
import { externalModelSources } from "./external-model-sources.js";
import { projects } from "./projects.js";
export const externalImportBatches = sqliteTable(
"external_import_batches",
{
id: text("id").primaryKey(),
projectId: text("project_id")
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
sourceId: text("source_id")
.notNull()
.references(() => externalModelSources.id, { onDelete: "cascade" }),
importKind: text("import_kind", { enum: externalImportKinds }).notNull(),
importedAtIso: text("imported_at_iso").notNull(),
fileName: text("file_name").notNull(),
sha256: text("sha256").notNull(),
appliedProjectRevision: integer("applied_project_revision").notNull(),
configurationSnapshot: text("configuration_snapshot", { mode: "json" })
.$type<ExternalCsvConfiguration>()
.notNull(),
originalBytes: blob("original_bytes", { mode: "buffer" }).notNull(),
document: text("document", { mode: "json" })
.$type<ExternalCsvDocument>()
.notNull(),
},
(table) => [
index("external_import_batches_project_revision_idx").on(
table.projectId,
table.appliedProjectRevision
),
index("external_import_batches_source_imported_idx").on(
table.sourceId,
table.importedAtIso
),
]
);

View file

@ -0,0 +1,80 @@
import { index, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
import type {
ExternalModelObjectOverrideField,
ExternalModelObjectPlanningValues,
ExternalModelObjectSourceValues,
} from "../../external-model/domain/external-model-contracts.js";
import { externalObjectPresenceStatuses } from "../../external-model/domain/external-model-contracts.js";
import { circuitDeviceRows } from "./circuit-device-rows.js";
import { distributionBoards } from "./distribution-boards.js";
import { externalImportBatches } from "./external-import-batches.js";
import { externalModelSources } from "./external-model-sources.js";
import { externalRoomMappings } from "./external-room-mappings.js";
import { projectDevices } from "./project-devices.js";
import { projects } from "./projects.js";
export const externalModelObjects = sqliteTable(
"external_model_objects",
{
id: text("id").primaryKey(),
projectId: text("project_id")
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
sourceId: text("source_id")
.notNull()
.references(() => externalModelSources.id, { onDelete: "cascade" }),
ifcGuid: text("ifc_guid").notNull(),
lastSeenImportBatchId: text("last_seen_import_batch_id")
.notNull()
.references(() => externalImportBatches.id, { onDelete: "restrict" }),
lastAcceptedImportBatchId: text("last_accepted_import_batch_id")
.notNull()
.references(() => externalImportBatches.id, { onDelete: "restrict" }),
acceptedSourceValues: text("accepted_source_values", { mode: "json" })
.$type<ExternalModelObjectSourceValues>()
.notNull(),
planningValues: text("planning_values", { mode: "json" })
.$type<ExternalModelObjectPlanningValues>()
.notNull(),
overriddenFields: text("overridden_fields", { mode: "json" })
.$type<ExternalModelObjectOverrideField[]>()
.notNull(),
externalRoomMappingId: text("external_room_mapping_id").references(
() => externalRoomMappings.id,
{ onDelete: "set null" }
),
distributionBoardId: text("distribution_board_id").references(
() => distributionBoards.id,
{ onDelete: "set null" }
),
linkedProjectDeviceId: text("linked_project_device_id").references(
() => projectDevices.id,
{ onDelete: "set null" }
),
circuitDeviceRowId: text("circuit_device_row_id").references(
() => circuitDeviceRows.id,
{ onDelete: "set null" }
),
presenceStatus: text("presence_status", {
enum: externalObjectPresenceStatuses,
})
.notNull()
.default("present"),
},
(table) => [
uniqueIndex("external_model_objects_source_ifc_guid_unique").on(
table.sourceId,
table.ifcGuid
),
index("external_model_objects_project_presence_idx").on(
table.projectId,
table.presenceStatus
),
index("external_model_objects_distribution_board_idx").on(
table.distributionBoardId
),
index("external_model_objects_circuit_device_row_idx").on(
table.circuitDeviceRowId
),
]
);

View file

@ -0,0 +1,23 @@
import { sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
import { externalModelSourceTypes } from "../../external-model/domain/external-model-contracts.js";
import { projects } from "./projects.js";
export const externalModelSources = sqliteTable(
"external_model_sources",
{
id: text("id").primaryKey(),
projectId: text("project_id")
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
name: text("name").notNull(),
sourceType: text("source_type", { enum: externalModelSourceTypes })
.notNull()
.default("revit_csv"),
},
(table) => [
uniqueIndex("external_model_sources_project_type_unique").on(
table.projectId,
table.sourceType
),
]
);

View file

@ -0,0 +1,37 @@
import { index, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
import { distributionBoards } from "./distribution-boards.js";
import { externalModelSources } from "./external-model-sources.js";
import { projects } from "./projects.js";
import { rooms } from "./rooms.js";
export const externalRoomMappings = sqliteTable(
"external_room_mappings",
{
id: text("id").primaryKey(),
projectId: text("project_id")
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
sourceId: text("source_id")
.notNull()
.references(() => externalModelSources.id, { onDelete: "cascade" }),
normalizedSourceRoomKey: text("normalized_source_room_key").notNull(),
sourceFloorName: text("source_floor_name"),
sourceRoomNumber: text("source_room_number").notNull(),
sourceRoomName: text("source_room_name").notNull(),
roomId: text("room_id").references(() => rooms.id, { onDelete: "set null" }),
defaultDistributionBoardId: text("default_distribution_board_id").references(
() => distributionBoards.id,
{ onDelete: "set null" }
),
},
(table) => [
uniqueIndex("external_room_mappings_source_key_unique").on(
table.sourceId,
table.normalizedSourceRoomKey
),
index("external_room_mappings_project_room_idx").on(
table.projectId,
table.roomId
),
]
);