On December 3, 2018 at 09:23:12, Jeremy Evans (jeremyeva...@gmail.com) wrote:
On Monday, December 3, 2018 at 8:08:21 AM UTC-8, Jeremy Hinegardner wrote: > > Hey all, > > I'm working with a legacy database and looks like our best strategy for > managing things is going to be using Datasets and Database directly (lots > of non-trivial queries for the domain/business objects) and I'd like to do > something like `DB.fetch(big_sql_query, map_to: EntityClass) { > |entity_instance| ... }` or `DB[sql].all_mapped_to(EntityClass) # [ > entity_instance, entity_instance ... ]`. Something along those lines, open > to any and all suggestions. I did some searching and didn't find exactly > what I was looking for as an existing extension or in the core Sequel > documentation. > > The closest I've found in this realm (and used before) is from the > github-ds gem where you can map complex SQL to ActiveRecord instances - > https://github.com/github/github-ds/blob/312b43b3d96bb376b87bc5e05045c3 > 06f40f2623/lib/github/sql.rb#L208-L222 > > I'm thinking I've either missed the documentation or this is something > trivial to implement as a Sequel extension. In the end this will probably > end up being a light-weight Repository pattern build around Sequel. > > thoughts? > > -a different jeremy > Jeremy, You can easily do this mapping for any Sequel dataset: dataset = dataset.with_row_proc(lambda do |hash| EntityClass.new(hash) end) dataset.all # => [<#EntityClass ...>, ...] This (dataset row proc) is what Sequel::Model uses to make sure model datasets return model instances. Thanks Jeremy - that worked perfectly. -jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+unsubscr...@googlegroups.com. To post to this group, send email to sequel-talk@googlegroups.com. Visit this group at https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.