module Crecto::Repo

Overview

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

Defined in:

crecto/repo/config.cr
crecto/repo/query.cr
crecto/repo.cr

Instance Method Summary

Instance Method Detail

def 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)


[View source]
def aggregate(queryable, aggregate_function : Symbol, field : Symbol, query : Crecto::Repo::Query) #

[View source]
def all(queryable, query : Query? = Query.new, **opts) : Array #

Return a list of queryable instances using query

query = Query.where(name: "fred")
users = Repo.all(User, query)

[View source]
def all(queryable, query = Query.new) : Array #

Returns a list of queryable instances. Accepts an optional #query

users = Crecto::Repo.all(User)

[View source]
def config(&block) #

[View source]
def config #

[View source]
def delete(changeset : Crecto::Changeset::Changeset) #

Delete a changeset instance from the data store.

Repo.delete(changeset)

[View source]
def delete(queryable_instance, tx : DB::Transaction?) #

Delete a shema instance from the data store.

Repo.delete(user)

[View source]
def delete(queryable_instance) #

[View source]
def delete_all(queryable, query = Query.new) #

[View source]
def delete_all(queryable, query : Query?, tx : DB::Transaction?) #

Delete multipile records with a single query

query = Crecto::Repo::Query.where(name: "Fred")
Repo.delete_all(User, query)

[View source]
def 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)

[View source]
def get(queryable, id) #

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

user = Repo.get(User, 1)

[View source]
def 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)

[View source]
def 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)

[View source]
def get_association(queryable_instance, association_name : Symbol) #

Return the value of the given association on queryable_instance

user = Crecto::Repo.get(User, 1)
post = Repo.get_association(user, :post)

[View source]
def get_association!(queryable_instance, association_name : Symbol) #

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)

[View source]
def get_by(queryable, **opts) #

Return a single nilable instance of queryable using the query param

user = Repo.get_by(User, name: "fred", age: 21)

[View source]
def get_by!(queryable, **opts) #

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

user = Repo.get_by(User, name: "fred", age: 21)

[View source]
def insert(queryable_instance, tx : DB::Transaction?) #

Insert a schema instance into the data store.

user = User.new
Repo.insert(user)

[View source]
def insert(changeset : Crecto::Changeset::Changeset) #

Insert a changeset instance into the data store.

user = User.new
changeset = User.changeset(user)
Repo.insert(changeset)

[View source]
def insert(queryable_instance) #

[View source]
def 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.

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

[View source]
def 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])

[View source]
def transaction(multi : Crecto::Multi) #

[View source]
def update(changeset : Crecto::Changeset::Changeset) #

Update a changeset instance in the data store.

Repo.update(changeset)

[View source]
def update(queryable_instance) #

[View source]
def update(queryable_instance, tx : DB::Transaction?) #

Update a shema instance in the data store.

Repo.update(user)

[View source]
def update_all(queryable, query, update_hash : NamedTuple) #

[View source]
def update_all(queryable, query, update_hash : Hash) #

[View source]
def update_all(queryable, query, update_hash : NamedTuple, tx : DB::Transaction?) #

[View source]
def update_all(queryable, query, update_hash : Hash, tx : DB::Transaction?) #

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.now})

[View source]