Objects API#

fastapi_restly.objects provides the session-level object primitives: building and updating SQLAlchemy objects from schema input, flushing and deleting them, and snapshotting loaded column values.

async fastapi_restly.objects.async_delete_object(session: AsyncSession, obj: object) → None#

Async equivalent of delete_object().

async fastapi_restly.objects.async_make_new_object(session: AsyncSession, model: type[_T], data: BaseModel) → _T#

Async equivalent of make_new_object().

async fastapi_restly.objects.async_save_object(session: AsyncSession, obj: _T) → _T#

Async equivalent of save_object().

async fastapi_restly.objects.async_update_object(session: AsyncSession, obj: _T, data: BaseModel) → _T#

Async equivalent of update_object().

fastapi_restly.objects.delete_object(session: Session, obj: object) → None#

Delete obj and flush the session.

fastapi_restly.objects.make_new_object(session: Session, model: type[_T], data: BaseModel) → _T#

Build an object of model from data and add it to session.

This is the schema-to-ORM mapping primitive. It resolves Restly reference fields, skips read-only inputs, applies schema defaults, and stages the object in the session. A field that the client did not send, and whose value is None, is not passed to the constructor, unless the constructor requires it. So the model’s default applies to it. It does not flush, so the object has no id yet. The default create of a view calls this with the view’s model.

The schema of data decides what is written: every field it declares, except those it marks ReadOnly. A time gets the form of its column; see Keep times in UTC.

fastapi_restly.objects.save_object(session: Session, obj: _T) → _T#

Flush the session and refresh obj from the database.

The refresh reloads the columns of obj and its eagerly loaded relationships, and nothing else: it does not reach related objects. Its lazy relationships are left unloaded, so a changed foreign key is not read from a stale relationship. Inside a view, the commit bracket saves the subject of the action and the other rows of the view’s model that it wrote. Call this in code outside a view, or for a row of another model that must be fresh before the commit. It does not load the relationships of a response.

fastapi_restly.objects.snapshot(obj: Any) → dict[str, Any]#

A dict of the column values that are loaded on obj, to compare old and new values in the commit hooks.

It is a shallow copy: a mutable value, such as a JSON dict, is shared with the object. So a change to such a value in place also shows in the snapshot. Assign a new value instead.

It reads only what is loaded, so it never starts a lazy load. A deferred or unloaded column is left out, where a load would raise MissingGreenlet on an async session. It needs no session.

fastapi_restly.objects.update_object(session: Session, obj: _T, data: BaseModel) → _T#

Apply writable fields from data to obj.

This is the schema-to-ORM update primitive. It resolves Restly reference fields and applies only writable inputs: the fields the client sent that the schema of data does not mark ReadOnly. A time gets the form of its column, as in make_new_object(). It does not flush. The default update of a view calls this.

See also

Run the same logic in a script or a worker shows these functions in a script or a worker, and CRUD Routes shows the business methods that call them in a view.