Fix API performance #73
1 changed files with 5 additions and 24 deletions
|
@ -32,36 +32,17 @@ module Backend
|
||||||
end
|
end
|
||||||
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
|
# DB model field helpers
|
||||||
module DbObject
|
module DbObject
|
||||||
# Defines DB model field helper functions
|
# Defines DB model field helper functions
|
||||||
macro db_object(type)
|
macro db_object(type)
|
||||||
include ::Backend::Api::Schema::Helpers::ObjectDbInit
|
|
||||||
|
|
||||||
db_init {{ type }}
|
|
||||||
|
|
||||||
private property model
|
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 }})
|
def initialize(@model : {{ type }})
|
||||||
@id = model.id.not_nil!.to_i
|
end
|
||||||
|
|
||||||
|
def initialize(id : Int32)
|
||||||
|
@model = {{ type }}.find(id).not_nil!
|
||||||
end
|
end
|
||||||
|
|
||||||
{% space_name = type.names.last.underscore.gsub(/_/, " ").capitalize %}
|
{% space_name = type.names.last.underscore.gsub(/_/, " ").capitalize %}
|
||||||
|
@ -69,7 +50,7 @@ module Backend
|
||||||
@[GraphQL::Field]
|
@[GraphQL::Field]
|
||||||
# {{ space_name }}'s ID
|
# {{ space_name }}'s ID
|
||||||
def id : Int32
|
def id : Int32
|
||||||
@id
|
@model.id.not_nil!.to_i32
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue