Hey, everyone,

I've been working on an open-source version of a library that I used at my 
previous job, and it's now ready for a version 1.0.0 release! I'd love for 
people to give it try and give feedback!

*Sequel::Packer* <https://github.com/PaulJuliusMartinez/sequel-packer> is a 
serialization library explicitly designed to work with Sequel, with the 
following qualities:

   - Declarative: Define the shape of your serialized data with a simple, 
   straightforward DSL.
   - Flexible: Certain contexts require different data. Packers provide an 
   easy way to opt-in to serializing certain data only when you need it. The 
   library also provides convenient escape hatches when you need to do 
   something not explicitly supported by the API.
   - Reusable: The Packer library naturally composes well with itself. 
   Nested data can be serialized in the same way no matter what endpoint it's 
   fetched from.
   - Efficient: When not using Sequel's TacticalEagerLoading 
   
<https://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/TacticalEagerLoading.html>
 plugin, 
   the Packer library will intelligently determine which associations and 
   nested associations it needs to eager load in order to avoid any N+1 
   query issues.


Installation instructions, a getting started guide, and extensive 
documentation are all available here: 
https://github.com/PaulJuliusMartinez/sequel-packer



*Sequel::Packer* uses your existing Sequel::Model declarations and 
leverages the use of associations to efficiently serialize data. Here's a 
quick example.

Suppose you have the following schema:

class User < Sequel::Model(:users)
  one_to_many :posts
end
class Post < Sequel::Model(:posts); end

Packer definitions use a simple domain-specific language (DSL) to declare 
which fields to serialize:

class PostPacker < Sequel::Packer
  model Post

  field :id
  field :title

  trait :truncated_content do
    field :truncated_content do |post|
      post.content[0..Post::PREVIEW_LENGTH]
    end
  end
end

class UserPacker < Sequel::Packer
  model User

  field :id
  field :name

  trait :posts do
    field :posts, PostPacker, :truncated_content
  end
end

Once defined, Packers are easy to use; just call .pack and pass in a Sequel 
dataset, an array of models, or a single model, and get back Ruby hashes. 
>From there you can simply call .to_json on the result!

UserPacker.pack(User.dataset)
=> [
  {id: 1, name: 'Paul'},
  {id: 2, name: 'Julius'},
  ...
]


UserPacker.pack(User[1], :posts)
=> {
  id: 1,
  name: 'Paul',
  posts: [
    {
      id: 15,
      title: 'Announcing Sequel::Packer!',
      truncated_content: 'Sequel::Packer is a new gem...',
    },
    {
      id: 21,
      title: 'Postgres Internals',
      truncated_content: 'I never quite understood autovacuum...',
    },
    ...
  ],
}

Packers make it easy to return data in a consistent format from multiple 
different endpoints, because you can just re-use the same Packer. And you 
rarely have to worry about performance issues; Sequel will ensure all 
nested associations are eager loaded to prevent N+1 query problems.

We had great success with a similar library at my last company, and 
engineers frequently commented that it was one of their favorite parts of 
our internal tooling. I'm excited to share it with other people!

- Paul

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/fb573f2c-6a84-4944-8222-2daffae071d2%40googlegroups.com.

Reply via email to