Vasta Logo
Models

Inserting & Updating Models

Inserting and updating models with Vasta.

Setting Attributes Directly

You can set attributes directly on a model instance using property accessors and then save the model instance.

const pet = await Pet.findOrFail(1);

pet.name = "New Name";
pet.counter = pet.counter + 1;

await pet.save();

Alternatively, you set the attribute on the attributes property of the model instance.

const pet = await Pet.findOrFail(1);
pet.attributes.name = "New Name";
await pet.save();

This is functionally equivalent to setting the attribute directly on the model instance, but it allows you to work with the attributes as a plain object if needed.

This is particularly useful if you have a column which conflicts with a model's property name. For example, if you have a table column on your model it would conflict with the table property on the model class. In this case, you can set the table attribute through the attributes property to avoid the conflict.

Mass Assignment

Vasta also supports mass assignment of attributes through the assign method. This allows you to update multiple attributes of a model instance in a single call. This functions similarly to Object.assign(model.attributes, newAttributes).

const pet = await Pet.findOrFail(1);

pet.assign({
  name: "New Name",
  counter: pet.counter + 1,
});

await pet.save();

Dirty Tracking

Vasta tracks original values on each model instance and only updates changed fields when calling save() on existing records.

  • model.getDirty() returns the attributes currently considered dirty.
  • model.isDirty() returns true when there are dirty attributes.

Unsaved models are always considered dirty. For a new model, getDirty() returns the model's current attributes.

const pet = new Pet({ name: "Fluffy", type: "cat" });

pet.isDirty();
// true (new model, not persisted yet)

pet.getDirty();
// { name: "Fluffy", type: "cat", counter: 0 }
// Assuming `counter` has a default value of 0 from the model's default attributes

await pet.save();

pet.isDirty();
// false

pet.counter = pet.counter + 1;

pet.getDirty();
// { counter: 1 }

await pet.save();
// Only `counter` is sent in the update payload
Copyright © 2026