Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to create list of entities #114

Open
davydovanton opened this issue Jul 17, 2018 · 5 comments
Open

Question: how to create list of entities #114

davydovanton opened this issue Jul 17, 2018 · 5 comments

Comments

@davydovanton
Copy link

Hey,

I use surrealist for json schema in my presenters and I found interesting problem. I use hanami entioty for building data objects and I have no idea how to use Surrealist.surrealize_collection with it (and can I use it?).

Also, I want to use list of objects and I have no idea how to do it without Dry-Types. I thought that I can do something like this:

users: ArrayOf(Entities::User.defined_schema)

where Entities::User is surrealist schema for entity object. WDYT can we update documentation for this cases?

@nesaulov
Copy link
Owner

Hey, glad to hear that surrealist is actually used outside of rails!
Excellent question. As for collections of objects, without dry-types you can just specify Array type, but the schema of each item will not be validated.
ArrayOf sounds like a good solution to the subject, I would appreciate if you or someone else would implement this. Or I can do it myself but later, being very busy right now.

@kolasss
Copy link
Contributor

kolasss commented Sep 19, 2018

Why not use a redefined method?

    json_schema do
      {
        id: String,
        title: String,
        products: Array
      }
    end

    def products
      Products::Serializer.new(
        object.products
      ).build_schema
    end

@davydovanton
Copy link
Author

hey, good question. My answer: I want to use specific schema for each item in the list and have a ability easy change it. Something like this

    json_schema do
      {
        id: String,
        title: String,
        products: Array(title: String, body: String, id: Integer)
      }
    end

or

    json_schema do
      {
        id: String,
        title: String,
        products: Array(Product.json_schema)
      }
    end

@kolasss
Copy link
Contributor

kolasss commented Sep 20, 2018

you can try

def products
  Surrealist.surrealize_collection(object.products, raw: true)
end

I just don't like to use values in json_schema to invoke something, i think it is better to use keys to invoke methods

@nesaulov
Copy link
Owner

@kolasss I see your point, but I don't think it will hurt to have such a feature, it doesn't break anything or require any changes in the core of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants