Class: QueryKit::Repository
- Inherits:
-
Object
- Object
- QueryKit::Repository
- Defined in:
- lib/querykit/repository.rb
Overview
Base repository class for implementing the repository pattern
Usage:
class UserRepository < QueryKit::Repository
table 'users'
model User
end
repo = UserRepository.new(db)
user = repo.find(1)
users = repo.all
users = repo.where('age', '>', 18)
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Class Method Summary collapse
-
.model(klass) ⇒ Object
Set the model class for this repository.
-
.model_class ⇒ Object
Get the model class.
-
.table(name) ⇒ Object
Set the table name for this repository.
-
.table_name ⇒ Object
Get the table name.
Instance Method Summary collapse
-
#all ⇒ Object
Get all records.
-
#count ⇒ Object
Count all records.
-
#delete(id) ⇒ Integer
(also: #destroy)
Delete a record by ID.
-
#delete_where(conditions) ⇒ Integer
Delete all records matching conditions.
-
#execute(custom_query) ⇒ Array
Execute a custom query with model mapping.
-
#execute_first(custom_query) ⇒ Object?
Execute a custom query and return first result.
-
#exists?(id = nil) ⇒ Boolean
Check if any records exist.
-
#find(id) ⇒ Object
Find a record by ID.
-
#find_by(column, value) ⇒ Object
Find a record by column value.
-
#first ⇒ Object
Get first record matching conditions.
-
#initialize(db = nil) ⇒ Repository
constructor
Initialize repository with optional database connection If no connection provided, uses global QueryKit.connection.
-
#insert(attributes) ⇒ Integer
(also: #create)
Insert a new record.
-
#transaction(&block) ⇒ Object
Begin a transaction.
-
#update(id, attributes) ⇒ Integer
Update a record by ID.
-
#where(column, operator_or_value, value = nil) ⇒ Object
Find multiple records by column value.
-
#where_in(column, values) ⇒ Object
Find records where column is IN array.
-
#where_not_in(column, values) ⇒ Object
Find records where column is NOT IN array.
Constructor Details
#initialize(db = nil) ⇒ Repository
Initialize repository with optional database connection If no connection provided, uses global QueryKit.connection
46 47 48 |
# File 'lib/querykit/repository.rb', line 46 def initialize(db = nil) @db = db || QueryKit.connection end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
17 18 19 |
# File 'lib/querykit/repository.rb', line 17 def db @db end |
Class Method Details
.model(klass) ⇒ Object
Set the model class for this repository
26 27 28 |
# File 'lib/querykit/repository.rb', line 26 def model(klass) @model_class = klass end |
.model_class ⇒ Object
Get the model class
36 37 38 |
# File 'lib/querykit/repository.rb', line 36 def model_class @model_class end |
.table(name) ⇒ Object
Set the table name for this repository
21 22 23 |
# File 'lib/querykit/repository.rb', line 21 def table(name) @table_name = name end |
.table_name ⇒ Object
Get the table name
31 32 33 |
# File 'lib/querykit/repository.rb', line 31 def table_name @table_name end |
Instance Method Details
#all ⇒ Object
Get all records
51 52 53 |
# File 'lib/querykit/repository.rb', line 51 def all @db.get(query, model_class) end |
#count ⇒ Object
Count all records
92 93 94 95 |
# File 'lib/querykit/repository.rb', line 92 def count result = @db.first(query.select('COUNT(*) as count')) result ? result['count'] : 0 end |
#delete(id) ⇒ Integer Also known as: destroy
Delete a record by ID
128 129 130 |
# File 'lib/querykit/repository.rb', line 128 def delete(id) @db.execute_delete(@db.delete(table_name).where('id', id)) end |
#delete_where(conditions) ⇒ Integer
Delete all records matching conditions
136 137 138 139 140 |
# File 'lib/querykit/repository.rb', line 136 def delete_where(conditions) delete_query = @db.delete(table_name) conditions.each { |column, value| delete_query.where(column, value) } @db.execute_delete(delete_query) end |
#execute(custom_query) ⇒ Array
Execute a custom query with model mapping
145 146 147 |
# File 'lib/querykit/repository.rb', line 145 def execute(custom_query) @db.get(custom_query, model_class) end |
#execute_first(custom_query) ⇒ Object?
Execute a custom query and return first result
152 153 154 |
# File 'lib/querykit/repository.rb', line 152 def execute_first(custom_query) @db.first(custom_query, model_class) end |
#exists?(id = nil) ⇒ Boolean
Check if any records exist
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/querykit/repository.rb', line 98 def exists?(id = nil) if id count_query = query.select('COUNT(*) as count').where('id', id) else count_query = query.select('COUNT(*) as count') end result = @db.first(count_query) result && result['count'] > 0 end |
#find(id) ⇒ Object
Find a record by ID
56 57 58 |
# File 'lib/querykit/repository.rb', line 56 def find(id) @db.first(query.where('id', id), model_class) end |
#find_by(column, value) ⇒ Object
Find a record by column value
61 62 63 |
# File 'lib/querykit/repository.rb', line 61 def find_by(column, value) @db.first(query.where(column, value), model_class) end |
#first ⇒ Object
Get first record matching conditions
87 88 89 |
# File 'lib/querykit/repository.rb', line 87 def first @db.first(query, model_class) end |
#insert(attributes) ⇒ Integer Also known as: create
Insert a new record
112 113 114 |
# File 'lib/querykit/repository.rb', line 112 def insert(attributes) @db.execute_insert(@db.insert(table_name).values(attributes)) end |
#transaction(&block) ⇒ Object
Begin a transaction
157 158 159 |
# File 'lib/querykit/repository.rb', line 157 def transaction(&block) @db.transaction(&block) end |
#update(id, attributes) ⇒ Integer
Update a record by ID
121 122 123 |
# File 'lib/querykit/repository.rb', line 121 def update(id, attributes) @db.execute_update(@db.update(table_name).set(attributes).where('id', id)) end |
#where(column, operator_or_value, value = nil) ⇒ Object
Find multiple records by column value
66 67 68 69 70 71 72 73 74 |
# File 'lib/querykit/repository.rb', line 66 def where(column, operator_or_value, value = nil) if value.nil? # Two arguments: column and value (assumes =) @db.get(query.where(column, operator_or_value), model_class) else # Three arguments: column, operator, value @db.get(query.where(column, operator_or_value, value), model_class) end end |
#where_in(column, values) ⇒ Object
Find records where column is IN array
77 78 79 |
# File 'lib/querykit/repository.rb', line 77 def where_in(column, values) @db.get(query.where_in(column, values), model_class) end |
#where_not_in(column, values) ⇒ Object
Find records where column is NOT IN array
82 83 84 |
# File 'lib/querykit/repository.rb', line 82 def where_not_in(column, values) @db.get(query.where_not_in(column, values), model_class) end |