Created
July 22, 2021 15:01
-
-
Save jwoertink/97db7318dc3b37cd14497c3993c22b5e to your computer and use it in GitHub Desktop.
Custom primary key ID for Avram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias CustomID = String | |
module Avram::Migrator::Columns::PrimaryKeys | |
class CustomIDPrimaryKey < Avram::Migrator::Columns::PrimaryKeys::Base | |
def initialize(@name) | |
end | |
def column_type : String | |
"character varying(18)" | |
end | |
end | |
end | |
module Avram::Migrator::Columns | |
class CustomIDColumn < Avram::Migrator::Columns::Base | |
@default : String? = nil | |
def initialize(@name, @nilable, @default) | |
end | |
def column_type : String | |
"character varying(18)" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SaveUser < User::SaveOperation | |
include CustomIDGenerator | |
before_save do | |
id.value ||= generate_new_custom_id | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < BaseModel | |
skip_default_columns | |
table do | |
primary_key id : CustomID | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment