Skip to content

Commit

Permalink
Refactor Record to use class inheritance
Browse files Browse the repository at this point in the history
Refactored Record and relation structures to utilize class inheritance instead of 'include' for module inclusion. Replaced `define` with `db_context` to set schema and table context more explicitly. Simplified type references and improved readability by using `@type` in macros.

These changes make the codebase cleaner and more maintainable while enhancing the explicitness of class relationships and table definitions.
  • Loading branch information
eliasjpr committed Aug 23, 2024
1 parent 4019d20 commit 35e14a7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 99 deletions.
11 changes: 4 additions & 7 deletions spec/record_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ AcmeDB = Cql::Schema.define(
end
end

struct Post
include Cql::Record(Post, Int64)

define AcmeDB, :posts
struct Post < Cql::Record(Int64)
db_context AcmeDB, :posts

getter id : Int64?
getter title : String
Expand All @@ -32,9 +30,8 @@ struct Post
end
end

struct Comment
include Cql::Record(Comment, Int64)
define AcmeDB, :comments
struct Comment < Cql::Record(Int64)
db_context AcmeDB, :comments

getter id : Int64?
getter post_id : Int64
Expand Down
36 changes: 12 additions & 24 deletions spec/relations_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "./spec_helper"
AcmeDB2 = Cql::Schema.define(
:acme_db,
adapter: Cql::Adapter::Postgres,
uri: ENV["DATABASE_URL"]) do

AcmeDB2 = Cql::Schema.define(:acme_db, adapter: Cql::Adapter::Postgres, uri: ENV["DATABASE_URL"]) do
table :movies do
primary :id, Int64, auto_increment: true
text :title
Expand Down Expand Up @@ -32,10 +30,8 @@ AcmeDB2 = Cql::Schema.define(
end
end

struct Actor
include Cql::Record(Actor, Int64)

define AcmeDB2, :actors
struct Actor < Cql::Record(Int64)
db_context AcmeDB2, :actors

getter id : Int64?
getter name : String
Expand All @@ -44,10 +40,8 @@ struct Actor
end
end

struct Movie
include Cql::Record(Movie, Int64)

define AcmeDB2, :movies
struct Movie < Cql::Record(Int64)
db_context AcmeDB2, :movies

has_one :screenplay, Screenplay
many_to_many :actors, Actor, join_through: :movies_actors
Expand All @@ -60,10 +54,8 @@ struct Movie
end
end

struct Director
include Cql::Record(Director, Int64)

define AcmeDB2, :directors
struct Director < Cql::Record(Int64)
db_context AcmeDB2, :directors

getter id : Int64?
getter name : String
Expand All @@ -73,10 +65,8 @@ struct Director
end
end

struct Screenplay
include Cql::Record(Screenplay, Int64)

define AcmeDB2, :screenplays
struct Screenplay < Cql::Record(Int64)
db_context AcmeDB2, :screenplays

belongs_to :movie, foreign_key: :movie_id

Expand All @@ -87,10 +77,8 @@ struct Screenplay
end
end

struct MoviesActors
include Cql::Record(MoviesActors, Int64)

define AcmeDB2, :movies_actors
struct MoviesActors < Cql::Record(Int64)
db_context AcmeDB2, :movies_actors

getter id : Int64?
getter movie_id : Int64
Expand Down
Loading

0 comments on commit 35e14a7

Please sign in to comment.