feat(orm): add Model.Copy and field copy metadata#187
Conversation
- Add Model.Copy / instance.copy that Browse, build copy values, then Create. - Introduce @field({ copy }) with BaseModel system fields marked copy: false. - Relink ManyToMany by id arrays and shallow-copy OneToMany via create commands. - Pipe copy through FieldsGet and Go IrField / parser / webapistore. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR adds a ChangesModel Copy Support
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant BaseModel
participant copyModel
participant Browse
participant buildCopyValues
participant createModel
BaseModel->>copyModel: provide source id and defaults
copyModel->>Browse: browse source with copy selection
Browse-->>copyModel: return source row
copyModel->>buildCopyValues: build create payload
buildCopyValues-->>copyModel: return transformed values
copyModel->>createModel: persist duplicate
createModel-->>BaseModel: return created model
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
modules/core/service/orm/model/model_copy.ts (1)
24-26: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUnused
isRelationTypehelper.Not called anywhere in this file, and it isn't exported.
buildCopyBrowseSelection/buildCopyValuesre-checkfield.typeliterals directly instead.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@modules/core/service/orm/model/model_copy.ts` around lines 24 - 26, Remove the unused, non-exported isRelationType helper from this file. Leave buildCopyBrowseSelection and buildCopyValues unchanged, including their existing direct field.type checks.modules/core/service/orm/model/model.ts (1)
457-467: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUnnecessary type cast for
this.Id.
Idis already declared aspublic readonly Id: stringon this class; the cast to{ Id?: string }is redundant.♻️ Simplify
- const id = String((this as { Id?: string }).Id || '').trim(); + const id = String(this.Id || '').trim();🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@modules/core/service/orm/model/model.ts` around lines 457 - 467, In Model.copy, remove the redundant cast around this.Id and read the declared readonly Id property directly while preserving the existing trim, validation, and copyModel behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@modules/core/service/orm/model/model_copy.ts`:
- Around line 24-26: Remove the unused, non-exported isRelationType helper from
this file. Leave buildCopyBrowseSelection and buildCopyValues unchanged,
including their existing direct field.type checks.
In `@modules/core/service/orm/model/model.ts`:
- Around line 457-467: In Model.copy, remove the redundant cast around this.Id
and read the declared readonly Id property directly while preserving the
existing trim, validation, and copyModel behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: da05af50-aefb-40cb-a437-26ce3649e890
📒 Files selected for processing (13)
internal/module/artifact/generate/webapistore.gointernal/module/artifact/generate/webapistore.ts.tplinternal/module/artifact/generate/webapistore_test.gointernal/parser/backendtsparser/field_resolved.gointernal/parser/backendtsparser/parser_migration_test.gomodules/core/service/orm/decorator/field.test.tsmodules/core/service/orm/decorator/field.tsmodules/core/service/orm/metadata/field.tsmodules/core/service/orm/model/model.tsmodules/core/service/orm/model/model_copy.test.tsmodules/core/service/orm/model/model_copy.tsmodules/core/service/orm/model/model_fields_get_facade.tspkg/meta/meta_field.go
- Remove unused isRelationType and dead undefined guard in copy helpers. - Simplify instance copy Id access and emit copy:false via non-nil template check. Co-authored-by: Cursor <cursoragent@cursor.com>
|
/improve |
- Assign copyScalarOrManyToOneValue directly after the caller already skips undefined. Co-authored-by: Cursor <cursoragent@cursor.com>
|
/improve |
PR Code Suggestions ✨Latest suggestions up to 652c7f3
Previous suggestionsSuggestions up to commit 1de33d8
Suggestions up to commit 08769e7
|
- Avoid passing raw relation objects without Id into Create FK columns. Co-authored-by: Cursor <cursoragent@cursor.com>
- Cover copy:true metadata, skip helpers, relation edge cases, and branch partials. Co-authored-by: Cursor <cursoragent@cursor.com>
- Guard with asObjectRecord and throw instead of silently copying defaults. Co-authored-by: Cursor <cursoragent@cursor.com>
|
/improve |
PR Code Suggestions ✨No code suggestions found for the PR. |
- Skip auth password, token/session identity and lifecycle, and user token/session collections on Copy. - Skip sequence watermark/idempotency keys and document mutation/upload request ids. Co-authored-by: Cursor <cursoragent@cursor.com>
User description
Summary
Model.Copy(id, defaults?)and instancecopythat Browse → buildCopyValues → Create (PR-P0-M2).@Field({ copy })metadata (default true); BaseModelId/CreatedAt/UpdatedAt/DeletedAtarecopy: false.{ create: [...] }; ManyToMany relink via id arrays (no target Copy); default soft-delete visibility.copythrough FieldsGet and Go IrField / backendtsparser / webapistore.Test plan
./choysum test typecheck core./choysum test unit core --be --pattern 'Copy|copy'./choysum test unit core --be(1830 ok)backendtsparserCopyFalse +webapistoreCopyContractMade with Cursor
PR Type
Enhancement
Description
Go Core: Add copy flag to field AST and webapistore generator.
TS ORM: Support @field({ copy }) decorator option and metadata.
TS ORM: Add Model.Copy and instance.copy methods for record duplication.
TS ORM: Support OneToMany shallow copying and ManyToMany relinking.
File Walkthrough
9 files
Include Copy metadata in web API store field metadataAdd copy metadata field to web API store templateParse copy option from field decorator into structural specAdd Copy property to IrFieldStructuralSpec ASTValidate and record copy flag in Field decoratorAdd copy option to field metadata type definitionsMark system fields copy false and expose Copy methodsImplement record copy logic, selection building, and relationsExpose copy metadata in FieldsGet response payload4 files
Add unit test for web API store copy metadata conversionAdd parser unit test for copy option on fieldsAdd unit tests for decorator copy option handlingAdd comprehensive unit tests for Model.Copy functionalitySummary by CodeRabbit
Model.Copyand instance.copy()to duplicate records by ID, honoring field-levelcopyrules (including nested relations) and merging defaults.copyoption on@Field(validated); systemIdand timestamp fields are marked non-copyable.copy: falsewhen explicitly configured.copy: falseis parsed, propagated, and emitted consistently across contracts, runtime ORM metadata, and generated store templates (omitted when not set / whencopy: true).copy: falseand omission behavior.