Skip to content

Commit

Permalink
compileUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Aug 26, 2024
1 parent fa31d5b commit 95ea731
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ihp-ide/IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,27 @@ toBinding modelName Column { name } = "let " <> modelName <> "{" <> columnNameTo

onlyWritableColumns columns = columns |> filter (\Column { generator } -> isNothing generator)

compileUpdate :: CreateTable -> Text
compileUpdate table@(CreateTable { name, columns }) =
compileUpdate :: (?schema :: Schema) => CreateTable -> Text
compileUpdate table@(CreateTable { name, columns, inherits }) =
let
modelName = tableNameToModelName name
writableColumns = onlyWritableColumns columns

-- @todo: Find a better way.
colName = modelName |> pluralize |> Text.toLower

parentColumns = case inherits of
Nothing -> []
Just parentTableName ->
let parentTableDef = findTableByName parentTableName
in case parentTableDef of
Just parentTable ->
parentTable.unsafeGetCreateTable.columns |> filter (\column -> column.name /= "meta" && Text.toLower column.name /= colName && column.name /= "id")

Nothing -> error $ "Parent table " <> cs parentTableName <> " not found for table " <> cs name <> "."

allColumns = columns <> parentColumns

writableColumns = onlyWritableColumns allColumns

toUpdateBinding Column { name } = "fieldWithUpdate #" <> columnNameToFieldName name <> " model"
toPrimaryKeyBinding Column { name } = "model." <> columnNameToFieldName name
Expand Down

0 comments on commit 95ea731

Please sign in to comment.