DAL Library
Clone
Repository.clone() copies an entity record by UUID. It fetches the source
record (including soft-deleted rows), builds a new row, and inserts it with a
fresh UUID.
How it works
The clone operation:
- Fetches the source record by UUID (including soft-deleted rows).
- Excludes the PK column (DB auto-generates) and all
@Uniquecolumns (includinguuid— a new UUID is generated). - Sets the
@CloneFieldcolumn to the source UUID (so the clone knows its origin). - Resets audit fields:
created_at= now,created_by= actor,updated_at= now,updated_by= actor,version= 1. - Resets deletable fields:
deleted_at= null,deleted_by= null. - Copies all other fields from the source.
- Inserts the new row with
RETURNING *.
No audit log is written for the clone itself (matches backend behavior — clone does not audit).
Use cases
- Template-based record creation — clone a "template" customer with pre-filled defaults, then edit the clone.
- Duplicate-and-edit — let a user copy an existing record instead of re-typing all fields.
- Soft-delete recovery — clone a soft-deleted record (the clone fetches soft-deleted rows too) to restore its data into a fresh, live row.
- Branching workflows — clone a record before applying a risky change so the original is preserved.
Usage
The entity must implement IAuditableEntity and IClonableEntity, and have at
least one @CloneField column:
Code
Code
Signature
Code
AuditableWriteOptions requires an actor (the UUID of the user performing the
clone). The audit and logger options are accepted but no audit log is
written for the clone operation itself.
Errors
NotFoundError— no record found with the givensourceUuid.Error— the entity has nouuidcolumn (no@Uniquecolumn found).Error— the INSERT returned no rows (clone failed).
Error handling with try/catch
Code
Next steps
- Audit trail — automatic field-level audit logging for write operations.
- Optimistic locking — the version guard on auditable writes.
- Repository — the full write API.
- Entities & decorators — the
@CloneFielddecorator andIClonableEntityinterface.
Last modified on