Fix API performance #73

Merged
dergrimm merged 2 commits from fix-api-performance into main 2022-03-03 19:46:35 +00:00
Showing only changes of commit db9d8e6395 - Show all commits

View file

@ -32,36 +32,17 @@ module Backend
end
end
# DB model leverage helpers
module ObjectDbInit
# Defines a DB model specific initializer
macro db_init(type)
def initialize(obj : {{ type }})
initialize(obj.id.not_nil!)
end
end
end
# DB model field helpers
module DbObject
# Defines DB model field helper functions
macro db_object(type)
include ::Backend::Api::Schema::Helpers::ObjectDbInit
db_init {{ type }}
private property model
private property id
def initialize(@id : Int32, @model : {{ type }})
end
def initialize(@id : Int32)
@model = {{ type }}.find(@id).not_nil!
end
def initialize(@model : {{ type }})
@id = model.id.not_nil!.to_i
end
def initialize(id : Int32)
@model = {{ type }}.find(id).not_nil!
end
{% space_name = type.names.last.underscore.gsub(/_/, " ").capitalize %}
@ -69,7 +50,7 @@ module Backend
@[GraphQL::Field]
# {{ space_name }}'s ID
def id : Int32
@id
@model.id.not_nil!.to_i32
end
end
end