abstract class
PgORM::Base
- PgORM::Base
- ActiveModel::Model
- Reference
- Object
Included Modules
- ActiveModel::Callbacks
- ActiveModel::Validation
- PgORM::Associations
- PgORM::Persistence
- PgORM::Table
- PgORM::Validators
Extended Modules
- PgORM::ChangeReceiver
- PgORM::FullTextSearch
- PgORM::Query
Defined in:
pg-orm/base.crConstant Summary
-
AM_PARENT_TYPE =
{:type => PgORM::Base} of Nil => Nil -
Log =
::Log.for(self) -
TABLES =
[] of String
Constructors
- .new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
- .new(pull : JSON::PullParser)
- .new(rs : DB::ResultSet)
Class Method Summary
-
.attributes : Array(Symbol)
Returns all attribute keys.
-
.clear
Removes all records from the table using DELETE.
-
.create(**attributes)
Creates and attempts to save a new record to the database.
-
.create!(**attributes)
Creates and saves a new record to the database.
-
.delete(ids : Enumerable(Value))
Deletes multiple records by an array of IDs.
-
.delete(ids : Enumerable(Enumerable(Value)))
Deletes multiple records with composite primary keys.
-
.delete(*ids) : Nil
Deletes one or more records by ID.
- .from_rs(rs : DB::ResultSet)
-
.truncate(cascade = true)
Quickly removes all records from the table using TRUNCATE.
-
.update(id : Value, args) : Nil
Updates one or more records by ID without loading them into memory.
-
.update(id : Enumerable(Value), args) : Nil
Updates multiple records by an array of IDs.
-
.update(id : Enumerable(Enumerable(Value)), args) : Nil
Updates multiple records with composite primary keys.
-
.update(id, **args) : Nil
Updates records using keyword arguments.
Macro Summary
- __customize_orm__
- __nilability_validation__
- default_primary_key(name, auto_generated = true, converter = nil, **tags)
Instance Method Summary
-
#==(other : self)
Returns
trueif this reference is the same as other. -
#apply_defaults
Generate code to apply default values
-
#assign_attributes(params : HTTP::Params | Hash(String, String) | Tuple(String, String))
Assign to mulitple 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
Hashof all attribute values -
#attributes_tuple
Returns a
NamedTupleof all attribute values. - #extra_attributes : Hash(String, PgORM::Value)
-
#persistent_attributes
Returns a
Hashof all attributes that can be persisted. - #to_json(json : JSON::Builder)
Macros inherited from module PgORM::Validators
ensure_unique(field, scope = [] of Nil, callback = nil, &transform)
ensure_unique
Macros inherited from module PgORM::Table
__process_table__
__process_table__,
table(name)
table
Instance methods inherited from module PgORM::Persistence
delete
delete,
destroy
destroy,
persisted?
persisted?,
reload!
reload!,
save(**options)
save,
save!(**options)
save!,
update(**attributes)
update,
update!(**attributes)
update!,
update_fields(**attributes)
update_fields
Macros inherited from module PgORM::Associations
__process_assoc_serialization__
__process_assoc_serialization__,
belongs_to(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
belongs_to,
has_many(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil, serialize = false)
has_many,
has_one(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
has_one
Constructor Detail
Class Method Detail
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.
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: "[email protected]")
if user.persisted?
puts "User created with ID: #{user.id}"
else
puts "Failed to create user: #{user.errors}"
end
See also: #save
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: "[email protected]")
# => #<User id: 1, name: "John", email: "[email protected]">
# Raises PgORM::Error::RecordInvalid if validation fails
User.create!(name: "") # => Error!
See also: #save!
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])
Deletes multiple records with composite primary keys.
Example
CompositeModel.delete([{key1: "a", key2: 1}, {key1: "b", key2: 2}])
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)
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.
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"})
Updates multiple records with composite primary keys.
Example
CompositeModel.update([{key1: "a", key2: 1}, {key1: "b", key2: 2}], {status: "active"})
Macro Detail
Instance Method Detail
Returns true if this reference is the same as other. Invokes same?.
Assign to mulitple attributes via HTTP::Params.