class

PgORM::Base

Inherits PgORM::Validators < PgORM::Table < PgORM::Persistence < PgORM::Associations < ActiveModel::Callbacks < ActiveModel::Validation < ActiveModel::Model < DB::Mappable < DB::Serializable < YAML::Serializable < JSON::Serializable < Reference < Object

Constants

AM_PARENT_TYPE = {:type => PgORM::Base} of Nil => Nil
CALLBACKS = { before_save: [] of Nil, after_save: [] of Nil, before_create: [] of Nil, after_create: [] of Nil, before_update: [] of Nil, after_update: [] of Nil, before_destroy: [] of Nil, after_destroy: [] of Nil, }
Log = ::Log.for(self)
TABLES = [] of String

Constructors

new(rs : DB::ResultSet)
Source

Class methods

attributes

Returns all attribute keys.

changefeed_ignored_update_columns
clear

Removes all records from the table using DELETE.

This is slower than truncate but respects foreign key constraints and triggers any database-level triggers.

Example

User.clear # DELETE FROM users

Warning: This deletes all data! Use with caution.

Source
create

Creates and attempts to save a new record to the database.

Returns the record regardless of whether it was saved successfully. Check #persisted? to confirm if the save succeeded.

Example

user = User.create(name: "John", email: "john@example.com")
if user.persisted?
  puts "User created with ID: #{user.id}"
else
  puts "Failed to create user: #{user.errors}"
end

See also: #save

Source
create!

Creates and saves a new record to the database.

Raises an exception if validation fails or the record cannot be saved.

Example

user = User.create!(name: "John", email: "john@example.com")
# => #<User id: 1, name: "John", email: "john@example.com">

# Raises PgORM::Error::RecordInvalid if validation fails
User.create!(name: "") # => Error!

See also: #save!

Source
delete(ids : Enumerable(Value))

Deletes multiple records by an array of IDs.

Does not load records into memory or run callbacks. Use #destroy if you need callbacks.

Example

User.delete([1, 2, 3, 4, 5])
Source
delete(ids : Enumerable(Enumerable(Value)))

Deletes multiple records with composite primary keys.

Example

CompositeModel.delete([{key1: "a", key2: 1}, {key1: "b", key2: 2}])
Source
delete(*ids) : Nil

Deletes one or more records by ID.

Does not load records into memory or run callbacks. More efficient than destroy but doesn't trigger callbacks or update associations.

Example

# Delete single record
User.delete(1)

# Delete multiple records
User.delete(1, 2, 3, 4, 5)
Source
from_rs(rs : DB::ResultSet)
Source
truncate(cascade = true)

Quickly removes all records from the table using TRUNCATE.

Much faster than clear for large tables, but:

  • Requires table-level locks
  • Resets auto-increment sequences
  • Can cascade to related tables if cascade: true

Example

# Truncate just this table
User.truncate(cascade: false)

# Truncate and cascade to related tables
User.truncate(cascade: true) # Also truncates related records

Warning: This deletes all data! Use with caution.

Source
update(id : Value, args) : Nil

Updates one or more records by ID without loading them into memory.

This is more efficient than loading, modifying, and saving records. Does not run validations or callbacks.

Example

# Update single record
User.update(1, {name: "John Updated"})
User.update(1, name: "John Updated")

# Update multiple records by ID
User.update([1, 2, 3], {active: false})

# Update with composite primary key
CompositeModel.update({key1: "a", key2: 1}, {status: "active"})
Source
update(id : Enumerable(Value), args) : Nil

Updates multiple records by an array of IDs.

Example

User.update([1, 2, 3], {active: false})
Source
update(id : Enumerable(Enumerable(Value)), args) : Nil

Updates multiple records with composite primary keys.

Example

CompositeModel.update([{key1: "a", key2: 1}, {key1: "b", key2: 2}], {status: "active"})
Source
update(id, **args) : Nil

Updates records using keyword arguments.

Example

User.update(1, name: "John", active: true)
Source

Instance methods

==(other : self)

Returns true if this reference is the same as other. Invokes same?.

Source
after_create
after_destroy
after_save
after_update
apply_defaults

Generate code to apply default values

assign_attributes(params : HTTP::Params | Hash(String, String) | Tuple(String, String))

Assign to multiple attributes via HTTP::Params.

assign_attributes(model : PgORM::Base)

Assign to multiple attributes from a model object

assign_attributes

Assign to multiple attributes.

attributes

Returns a Hash of all attribute values

attributes_tuple

Returns a NamedTuple of all attribute values.

before_create
before_destroy
before_save
before_update
extra_attributes
Source
persistent_attributes

Returns a Hash of all attributes that can be persisted.

run_create_callbacks

Wrap a block with callbacks for the appropriate crud operation.

Defined once here rather than once per subclass, and reached through the before_*/after_* methods rather than by expanding __before_* inline.

These methods yield, and Crystal cannot dispatch a yielding method through a pointer -- it inlines the body at each call site. Defining them per subclass therefore meant that a run_*_callbacks call on an abstract receiver emitted one inlined copy per concrete subclass, each containing a type-id dispatch chain for every self call in the block. Nesting two of them (run_update_callbacks { run_save_callbacks { ... } }) made that quadratic in the number of models.

With a single definition there is one inlined copy, and before_* / after_* are ordinary non-yielding overrides that share one dispatch wrapper.

Source
run_destroy_callbacks
Source
run_save_callbacks
Source
run_update_callbacks
Source
to_json(json : JSON::Builder)
Source

Macros

__customize_orm__
Source
__nilability_validation__
Source
changefeed_ignore_updates(*attributes, database_columns = [] of Symbol)

Suppress UPDATE notifications when only these persisted attributes change. An inherited declaration can be overridden, including with an empty list. Models without a declaration preserve the table's installed EventBus policy. Use database_columns for columns without model attributes (e.g. generated search vectors). EventBus validates these against the table at registration.

Source
default_primary_key(name, auto_generated = true, converter = nil, **tags)
Source