Skip to content

added bulk insert - #11

Merged
Smef merged 5 commits into
mainfrom
add-bulk-create
Jul 16, 2026
Merged

added bulk insert#11
Smef merged 5 commits into
mainfrom
add-bulk-create

Conversation

@Smef

@Smef Smef commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Added bulk insert

Summary by CodeRabbit

  • New Features
    • Added bulk insertion support: create() now accepts arrays and returns inserted instances.
    • Introduced createMany() for inserting multiple records in a single query.
    • Added hasOne relationship support (e.g., fetching a “favorite” related record).
  • Documentation
    • Documented bulk inserting behavior, including defaults, mutators, lifecycle event order, and returned instances.
  • TypeScript / API Improvements
    • Improved query-builder typing for expressions and where conditions.
  • Chores
    • Updated package build/exports and bumped the version to 0.0.15.

Copilot AI review requested due to automatic review settings July 15, 2026 19:46
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a2280509-bde5-433c-9426-61ba4d38bdd9

📥 Commits

Reviewing files that changed from the base of the PR and between dbdfea2 and c9f763e.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (12)
  • package.json
  • src/index.ts
  • src/model/Builder.ts
  • src/model/Model.ts
  • src/model/StaticForwarder.ts
  • src/model/defineModel.ts
  • test/database/models/Person.ts
  • test/tests/Base.ts
  • test/tsconfig.json
  • tsconfig.cjs.json
  • tsconfig.esm.json
  • tsconfig.json

📝 Walkthrough

Walkthrough

Model creation now supports array inputs through create() and createMany(), with one multi-row insert, per-model lifecycle events, hydrated instances, typed model definitions, has-one relationships, query typing updates, and a revised package build.

Changes

Model API and bulk creation

Layer / File(s) Summary
Model factory and model behavior
src/model/defineModel.ts, src/model/Model.ts, src/index.ts
Adds the typed defineModel factory and reorganizes model proxying, mutators, dirty tracking, and exports.
Builder contracts and query execution
src/model/Builder.ts, src/model/StaticForwarder.ts
Adds shared expression and selection types and refactors constraint, aggregate, pagination, lookup, and forwarded query signatures.
Bulk creation API and persistence
src/model/StaticForwarder.ts, docs/content/2.models/3.inserting-and-updating.md
Array inputs delegate to createMany(), which applies lifecycle events, performs one insert, hydrates returned instances, and documents the behavior.
Relationship wiring and behavior coverage
src/model/Model.ts, test/database/models/Person.ts, test/tests/Base.ts
Adds hasOne() and tests bulk insertion, lifecycle ordering, defaults, mutators, empty input, null filtering, relationship results, and TypeScript validation.
Published build and compiler configuration
package.json, tsconfig.json, test/tsconfig.json
Updates package metadata, build output paths, exports, compiler settings, and development tool versions.

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
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and directly describes the main change: adding bulk insert support.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch add-bulk-create

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 15, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 new createMany() 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.

Comment on lines +205 to +207
if (Array.isArray(attributes)) {
return (this as any).createMany(attributes);
}
Comment on lines +228 to +234
const { db, table } = instances[0];
const rows = await (db as any)
.insertInto(table)
.values(instances.map((instance) => instance.getRawAttributes()))
.returningAll()
.execute();

Comment thread src/model/StaticForwarder.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/model/StaticForwarder.ts (2)

235-235: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Correct 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 value

Consider 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 createMany if 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

📥 Commits

Reviewing files that changed from the base of the PR and between 9802741 and dbdfea2.

📒 Files selected for processing (3)
  • docs/content/2.models/3.inserting-and-updating.md
  • src/model/StaticForwarder.ts
  • test/tests/Base.ts

@Smef
Smef merged commit 9f659d5 into main Jul 16, 2026
1 of 2 checks passed
@Smef
Smef deleted the add-bulk-create branch July 16, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants