added bulk insert - #11
Conversation
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (12)
📝 WalkthroughWalkthroughModel creation now supports array inputs through ChangesModel API and bulk creation
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant StaticForwarder
participant Model
participant Database
Caller->>StaticForwarder: create(array) or createMany(array)
StaticForwarder->>Model: construct and initialize each instance
StaticForwarder->>Model: dispatch saving and creating events
StaticForwarder->>Database: insert multiple rows with returningAll
Database-->>StaticForwarder: returned rows
StaticForwarder->>Model: hydrate attributes and dispatch created/saved
StaticForwarder-->>Caller: saved model instances
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
vasta-docs | c9f763e | Commit Preview URL Branch Preview URL |
Jul 16 2026, 06:33 PM |
There was a problem hiding this comment.
Pull request overview
Adds first-class bulk insert support to the model layer by allowing Model.create([...]) (and createMany([...])) to insert multiple records with a single query, returning hydrated model instances. This fits the codebase goal of providing an Eloquent-style DX on top of Kysely while keeping type-safety and lifecycle events consistent with single-record create()/save().
Changes:
- Extend
StaticForwarder.create()with overloads to accept an array payload and delegate to a newcreateMany()implementation. - Implement
createMany()bulk insert flow with per-model lifecycle event dispatch and result hydration. - Add documentation and tests covering query count, defaults/mutators, events, empty input, and type-level expectations.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/tests/Base.ts | Adds runtime + type-level tests verifying bulk insert behavior, event ordering, and single-query insertion. |
| src/model/StaticForwarder.ts | Introduces array overloads for create() and adds createMany() to perform bulk inserts and hydrate instances. |
| docs/content/2.models/3.inserting-and-updating.md | Documents bulk insert usage via create([...]) and createMany([...]), including event behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (Array.isArray(attributes)) { | ||
| return (this as any).createMany(attributes); | ||
| } |
| const { db, table } = instances[0]; | ||
| const rows = await (db as any) | ||
| .insertInto(table) | ||
| .values(instances.map((instance) => instance.getRawAttributes())) | ||
| .returningAll() | ||
| .execute(); | ||
|
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/model/StaticForwarder.ts (2)
235-235: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueCorrect typographical error.
📝 Proposed fix
- // The isntances have been saved to the database, so we should dispatch the "created" and "saved" events for each instance and set their attributes accordingly + // The instances have been saved to the database, so we should dispatch the "created" and "saved" events for each instance and set their attributes accordingly🤖 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 `@src/model/StaticForwarder.ts` at line 235, Correct the typographical error in the comment near the instance event-dispatch logic, changing “isntances” to “instances” without altering the surrounding wording or code behavior.
228-234: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueConsider chunking for large bulk inserts.
While this bulk insert implementation is efficient, passing a very large array of attributes (e.g., thousands of records) could exceed the database's maximum parameter limit (such as PostgreSQL's 65,535 parameter limit) and cause the query to fail. For robust operational scalability, consider either documenting a recommended maximum batch size or automatically chunking the inserts within
createManyif the array length exceeds a certain threshold.🤖 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 `@src/model/StaticForwarder.ts` around lines 228 - 234, Update createMany around the bulk insert using db.insertInto so large instances arrays are split into bounded chunks before execution, keeping each chunk within the database parameter limit and preserving the existing returned-row behavior by combining results from all chunk inserts.
🤖 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 `@src/model/StaticForwarder.ts`:
- Line 235: Correct the typographical error in the comment near the instance
event-dispatch logic, changing “isntances” to “instances” without altering the
surrounding wording or code behavior.
- Around line 228-234: Update createMany around the bulk insert using
db.insertInto so large instances arrays are split into bounded chunks before
execution, keeping each chunk within the database parameter limit and preserving
the existing returned-row behavior by combining results from all chunk inserts.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 1fdd2b83-71a7-42ec-807c-e58ccc289dae
📒 Files selected for processing (3)
docs/content/2.models/3.inserting-and-updating.mdsrc/model/StaticForwarder.tstest/tests/Base.ts
Added bulk insert
Summary by CodeRabbit
create()now accepts arrays and returns inserted instances.createMany()for inserting multiple records in a single query.hasOnerelationship support (e.g., fetching a “favorite” related record).whereconditions.0.0.15.