Skip to content

Commit

Permalink
Merge pull request #1976 from digitallyinduced/copyRecord
Browse files Browse the repository at this point in the history
Add copyRecord
  • Loading branch information
mpscholten authored Jun 22, 2024
2 parents 8116e87 + 06e5e30 commit afbcaca
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,4 +1125,23 @@ onlyWhereReferencesMaybe field referenced records = filter (\record -> get field
-- > |> isValid
--
isValid :: forall record. (HasField "meta" record MetaBag) => record -> Bool
isValid record = isEmpty record.meta.annotations
isValid record = isEmpty record.meta.annotations

-- | Copies all the fields (except the 'id' field) into a new record
--
-- Example: Duplicate a database record (except the primary key of course)
--
-- > project <- fetch projectId
-- > duplicatedProject <- createRecord (copyRecord project)
--
copyRecord :: forall record id. (Table record, SetField "id" record id, Default id, SetField "meta" record MetaBag) => record -> record
copyRecord existingRecord =
let
fieldsExceptId = (columnNames @record) |> filter (\field -> field /= "id")

meta :: MetaBag
meta = def { touchedFields = map (IHP.NameSupport.columnNameToFieldName . cs) fieldsExceptId }
in
existingRecord
|> set #id def
|> set #meta meta

0 comments on commit afbcaca

Please sign in to comment.