class Change::SQL::Repo

Overview

Generic interface for querying a repository.

For now this is implemented directly with Postgres as a backing adapter, but extracting an adapter system should be fairly simple.

Defined in:

change/sql/repo.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(conn : DB::Database) #

[View source]

Instance Method Detail

def all(queryable : T.class, query : Query = Query.new) : Array(T) forall T #

Return all existing records of queryable that match the provided query, or just all records if no query is given.


[View source]
def conn : DB::Database #

[View source]
def conn=(conn : DB::Database) #

[View source]
def delete(queryable : T.class, id) forall T #

Delete the record of queryable with the primary key that matches the given id value.


[View source]
def get(queryable : T.class, id) : T | Nil forall T #

Get the record of queryable whose primary key matches the given id value. If no record matches the query, nil is returned instead.


[View source]
def insert(changeset : Changeset(T, U)) : Changeset(T, U) forall T, U #

Attempt to save a record of T to the repository. The insertion is only attempted if the changeset is currently valid.


[View source]
def one(queryable : T.class, query : Query = Query.new) : T | Nil forall T #

Return a single record of queryable that matches the provided query, or the first result of the query if no query is given.

If multiple records are returned by the query, the first is selected. If no record matches, returns nil.


[View source]
def update(changeset : Changeset(T, U)) : Changeset(T, U) forall T, U #

Attempt to update a record of T in the repository. The update is only attempted if the changeset is currently valid, and the instance of the changeset has a primary key value set.


[View source]