Crecto::Repo
A repository maps to an underlying data store, controlled by the adapter.
Instance methods
Calculate the given aggregate aggregate_function over the given field
Aggregate aggregate_function must be one of (:avg, :count, :max, :min:, :sum)
Returns a list of queryable instances. Accepts an optional query
users = Crecto::Repo.all(User)
Return a list of queryable instances using query
query = Query.where(name: "fred")
users = Repo.all(User, query)
Delete a shema instance from the data store.
Repo.delete(user)
Delete a changeset instance from the data store.
Repo.delete(changeset)
Delete a changeset instance from the data store or raise if the resulting changeset is invalid
Repo.delete!(changeset)
Delete a schema instance from the data store or raise if the resulting changeset is invalid
Repo.delete!(user)
Delete multipile records with a single query
query = Crecto::Repo::Query.where(name: "Fred")
Repo.delete_all(User, query)
Return a single nilable insance of queryable by primary key with id.
user = Repo.get(User, 1)
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)
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)
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)
Return the value of the given association on queryable_instance
user = Crecto::Repo.get(User, 1)
post = Repo.get_association(user, :post)
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)
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))
Return a single nilable instance of queryable using the opts param
user = Repo.get_by(User, name: "fred", age: 21)
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))
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)
Insert a schema instance into the data store.
user = User.new
Repo.insert(user)
Insert a changeset instance into the data store.
user = User.new
changeset = User.changeset(user)
Repo.insert(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)
Insert a schema instance into the data store or raise if the resulting changeset is invalid.
user = User.new
Repo.insert!(user)
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])
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])
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
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
Update a shema instance in the data store.
Repo.update(user)
Update a changeset instance in the data store.
Repo.update(changeset)
Update a changeset instance in the data store or raise if the resulting changeset is invalid
Repo.update(changeset)
Update a schema instance in the data store or raise if the resulting changeset is invalid
Repo.update!(user)
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})