Skip to main content
save is a method that saves or updates records. It automatically handles INSERT/UPDATE using UpsertBuilder and executes safely within a transaction.
save is not defined in BaseModelClass. It’s a standard pattern automatically generated by the Syncer in each Model class when you create an Entity.

Type Signature

Auto-Generated Code

Sonamu automatically generates the following code based on your Entity:
How it works:
  1. getPuri(“w”): Gets write Puri from BaseModelClass method
  2. ubRegister(): Registers records with UpsertBuilder
  3. transaction(): Starts transaction
  4. ubUpsert(): Automatically handles INSERT/UPDATE and returns IDs

Parameters

saveParams

Array of record data to save. Type: SaveParams[]

Role of id

  • If id exists: UPDATE the record with that ID
  • If id is absent: INSERT a new record

Return Value

Type: Promise<number[]> Returns an array of IDs for the saved records.

Basic Usage

Create New Record (INSERT)

Update Record (UPDATE)

Upsert (INSERT or UPDATE)

UpsertBuilder How It Works

1. Register Records (ubRegister)

2. Execute Transaction

3. Automatic INSERT/UPDATE Handling

UpsertBuilder automatically performs INSERT or UPDATE based on the presence of the id field:

Batch Save

You can save multiple records at once.

Saving Relationship Data

1:N Relationship

N:M Relationship

Practical Examples

Partial Update

You can update only specified fields.
Fields not specified are not changed. To set a field to NULL, explicitly pass null.

Transactions

save automatically executes within a transaction.

With @transactional

Validation

1. Automatic Zod Validation

SaveParams are automatically generated as Zod schemas from Entity definitions.

2. Custom Validation

API Usage

Auto-Generated save API

Client Code

React (TanStack Query)

Advanced Features

Unique Constraint Handling

UpsertBuilder automatically handles Unique constraints.

JSON Columns

Array Columns (PostgreSQL)

Performance Optimization

Batch Size Limit

Transaction Reuse

Cautions

1. Pass as Array

save must receive an array.

2. Return Value is ID Array

3. Caution with Partial Updates

4. Relationship Data Order

Save parent records before child records.

Next Steps

findById

Query saved records

del

Delete records

UpsertBuilder

UpsertBuilder detailed guide

@transactional

Using transactions