module

Crecto::Repo

A repository maps to an underlying data store, controlled by the adapter.

Instance methods

aggregate(queryable, aggregate_function : Symbol, field : Symbol, query : Crecto::Repo::Query)
Source
aggregate(queryable, aggregate_function : Symbol, field : Symbol)

Calculate the given aggregate aggregate_function over the given field Aggregate aggregate_function must be one of (:avg, :count, :max, :min:, :sum)

Source
all(queryable, query = Query.new)

Returns a list of queryable instances. Accepts an optional query

users = Crecto::Repo.all(User)
Source
all(queryable, query : Query | Nil = Query.new, **opts)

Return a list of queryable instances using query

query = Query.where(name: "fred")
users = Repo.all(User, query)
Source
config
Source
config
Source
delete(queryable_instance, tx : DB::Transaction | Nil)

Delete a shema instance from the data store.

Repo.delete(user)
Source
delete(changeset : Crecto::Changeset::Changeset)

Delete a changeset instance from the data store.

Repo.delete(changeset)
Source
delete(queryable_instance)
Source
delete!(changeset : Crecto::Changeset::Changeset)

Delete a changeset instance from the data store or raise if the resulting changeset is invalid

Repo.delete!(changeset)
Source
delete!(queryable_instance, tx : DB::Transaction | Nil = nil)

Delete a schema instance from the data store or raise if the resulting changeset is invalid

Repo.delete!(user)
Source
delete_all(queryable, query : Query | Nil, tx : DB::Transaction | Nil)

Delete multipile records with a single query

query = Crecto::Repo::Query.where(name: "Fred")
Repo.delete_all(User, query)
Source
delete_all(queryable, query = Query.new)
Source
get(queryable, id, tx : DB::Transaction | Nil = nil)

Return a single nilable insance of queryable by primary key with id.

user = Repo.get(User, 1)
Source
get(queryable, id, query : Query)

Return a single nilable insance of queryable by primary key with id. Can pass a Query for the purpose of preloading associations

query = Query.preload(:posts)
user = Repo.get(User, 1, query)
Source
get!(queryable, id, query : Query)

Return a single insance of queryable by primary key with id. Can pass a Query for the purpose of preloading associations Raises NoResults error if the record does not exist

query = Query.preload(:posts)
user = Repo.get(User, 1, query)
Source
get!(queryable, id)

Return a single insance of queryable by primary key with id. Raises NoResults error if the record does not exist

user = Repo.get(User, 1)
Source
get_association(queryable_instance, association_name : Symbol, query : Query = Query.new)

Return the value of the given association on queryable_instance

user = Crecto::Repo.get(User, 1)
post = Repo.get_association(user, :post)
Source
get_association!(queryable_instance, association_name : Symbol, query : Query = Query.new)

Return the value of the given association on queryable_instance Raises NoResults error if association has no value. Will not raise for has_many associations.

user = Crecto::Repo.get(User, 1)
post = Repo.get_association!(user, :post)
Source
get_by(queryable, query)

Return a single nilable instance of queryable using the query param Allows for association preloading

user = Repo.get_by(User, Query.where(name: "fred", age: 21))
Source
get_by(queryable, **opts)

Return a single nilable instance of queryable using the opts param

user = Repo.get_by(User, name: "fred", age: 21)
Source
get_by!(queryable, query)

Return a single instance of queryable using the query param Raises NoResults error if the record does not exist

user = Repo.get_by(User, Query.where(name: "fred", age: 21))
Source
get_by!(queryable, **opts)

Return a single instance of queryable using the opts param Raises NoResults error if the record does not exist

user = Repo.get_by(User, name: "fred", age: 21)
Source
insert(queryable_instance, tx : DB::Transaction | Nil)

Insert a schema instance into the data store.

user = User.new
Repo.insert(user)
Source
insert(changeset : Crecto::Changeset::Changeset)

Insert a changeset instance into the data store.

user = User.new
changeset = User.changeset(user)
Repo.insert(changeset)
Source
insert(queryable_instance)
Source
insert!(changeset : Crecto::Changeset::Changeset)

Insert a changeset instance into the data store or raise if the resulting changest is invalid.

user = User.new
changeset = User.changeset(user)
Repo.insert!(changeset)
Source
insert!(queryable_instance, tx : DB::Transaction | Nil = nil)

Insert a schema instance into the data store or raise if the resulting changeset is invalid.

user = User.new
Repo.insert!(user)
Source
query(queryable, sql : String, params = [] of DbValue) : Array

Run aribtrary sql queries. query will cast the output as that object. In this example, query will try to cast the output as User. If query results happen to error nil is returned

Repo.query(User, "select * from users where id > ?", [30])
Source
query(sql : String, params = [] of DbValue) : DB::ResultSet

Run aribtrary sql. query will pass a PG::ResultSet as the return value once the query has been executed. Arguments are defined as ? and are interpolated to escape whats being passed. query can run without parameters as well.

Please note that you have to close the result set yourself, otherwise there will be staled connections!

query = Crecto::Repo.query("select * from users where id = ?", [30])
Source
raw_exec(args : Array)

Run a raw exec query directly on the adapter connection

Source
raw_exec(*args)

Run a raw exec query directly on the adapter connection

Source
raw_query(query, *args, &)

Run a raw query query directly on the adapter connection

Source
raw_query(query, args : Array)

Run a raw query query directly on the adapter connection

Source
raw_query(query, *args)

Run a raw query query directly on the adapter connection

Source
raw_scalar(*args)

Run a raw scalar query directly on the adapter connection

Source
transaction(multi : Crecto::Multi)
Source
transaction!

Run a live transaction. As opposed to transactions using Multi every operation on the yielded LiveTransaction object is executed immediately. This allows checking for results of previous operations. Any raise inside the block will roll back the transaction.

Crecto::Repo.transaction! do |tx|
  tx.insert!(user)
  post = Post.new.tap { |p| p.user = user }
  tx.insert!(post)
end
Source
transaction_with_savepoint!(savepoint_name : String | Nil = nil, &)

Run a nested transaction with savepoint support. If called within an existing transaction, creates a savepoint. If called outside a transaction, creates a new transaction.

Crecto::Repo.transaction_with_savepoint! do |tx|
  tx.insert!(user)
  Crecto::Repo.transaction_with_savepoint! do |inner_tx|
    # This creates a savepoint
    inner_tx.insert!(post)
    # Can rollback to this savepoint with inner_tx.rollback
  end
end
Source
update(queryable_instance, tx : DB::Transaction | Nil)

Update a shema instance in the data store.

Repo.update(user)
Source
update(changeset : Crecto::Changeset::Changeset)

Update a changeset instance in the data store.

Repo.update(changeset)
Source
update(queryable_instance)
Source
update!(changeset : Crecto::Changeset::Changeset)

Update a changeset instance in the data store or raise if the resulting changeset is invalid

Repo.update(changeset)
Source
update!(queryable_instance, tx : DB::Transaction | Nil = nil)

Update a schema instance in the data store or raise if the resulting changeset is invalid

Repo.update!(user)
Source
update_all(queryable, query, update_hash : Hash, tx : DB::Transaction | Nil)

Update multipile records with a single query

query = Crecto::Repo::Query.where(name: "Ted", count: 0)
Repo.update_all(User, query, {count: 1, date: Time.local})
Source
update_all(queryable, query, update_hash : NamedTuple, tx : DB::Transaction | Nil)
Source
update_all(queryable, query, update_hash : Hash)
Source
update_all(queryable, query, update_hash : NamedTuple)
Source

Nested types