Giter Site home page Giter Site logo

Scoping resources about kan HOT 5 OPEN

davydovanton avatar davydovanton commented on August 15, 2024
Scoping resources

from kan.

Comments (5)

davydovanton avatar davydovanton commented on August 15, 2024 1

Also, I get an idea to introduce data_source. I see it like this (it's just a draft to explain my idea):

module Post
  class BaseDataSource
    include Kan::DataSource

    role(:base) { |_user, _| true }

   register(:index) { |_user, repo| repo }
  end

  class AdminDataSource
    include Kan::DataSource

    role(:admin) { |user, _| user.admin? }

    register(:index) { |_user, repo| repo.exclude(draft: true) }
  end
end
app = Kan::Application.new(
  data_sources: { post: [Post::BaseDataSource.new, Post::AdminDataSource.new] }
)

KanImport = Dry::AutoInject(app)
class Service
  include KanImport[data_source: 'data_source.post.index']
  include Import['post_repo']

  def call(user, params)
    source = data_source.call(user, post_repo) #=> relation with condition

    # ...
  end
end

In this case, you can get a specific repository or data source based on your role and use it for getting or setting data. WDYT?

from kan.

ianks avatar ianks commented on August 15, 2024 1

Also, I get an idea to introduce data_source. I see it like this (it's just a draft to explain my idea):

module Post
  class BaseDataSource
    include Kan::DataSource

    role(:base) { |_user, _| true }

   register(:index) { |_user, repo| repo }
  end

  class AdminDataSource
    include Kan::DataSource

    role(:admin) { |user, _| user.admin? }

    register(:index) { |_user, repo| repo.exclude(draft: true) }
  end
end
app = Kan::Application.new(
  data_sources: { post: [Post::BaseDataSource.new, Post::AdminDataSource.new] }
)

KanImport = Dry::AutoInject(app)
class Service
  include KanImport[data_source: 'data_source.post.index']
  include Import['post_repo']

  def call(user, params)
    source = data_source.call(user, post_repo) #=> relation with condition

    # ...
  end
end

In this case, you can get a specific repository or data source based on your role and use it for getting or setting data. WDYT?

I really dig this pattern.

from kan.

davydovanton avatar davydovanton commented on August 15, 2024

hey! I don't have any register_scope methods right now and I'm not sure that this logic should be in the core (but I can imagine a scope plugin for kan).

If you have a simple example with the repository and just one call (user.admin?) I can suggest to use the simplest logic:

class PostRepository
  def all_for_user(user)
    if user.admin?
      self
    else
      exclude(draft: true)
    end
  end
end

But if you want to use resource idea with kan I think you can do it in this way:

module Post
  class BaseResources
    include Kan::Abilities

    role(:base) { |_user, _| true }

    register(:index) { |_user, repo| repo }
  end

  class AdminResources
    include Kan::Abilities

    role(:admin) { |user, _| user.admin? }

    register(:index) { |_user, repo| repo.exclude(draft: true) }
  end
end
resources = Kan::Application.new(
  post: [Post::BaseResources.new, Post::AdminResources.new]
)

ResourcesImport = Dry::AutoInject(resources)
class Service
  include ResourcesImport[resource: 'post.index']
  include Import['post_repo']

  def call(user, params)
    resource.call(user, post_repo) #=> relation with condition
    # ...
  end
end

from kan.

DangerDawson avatar DangerDawson commented on August 15, 2024

@davydovanton we are looking at using the above approach, although we are unsure of who this would be used with something like rom-rb / hanami-model. As we currently only return data back from our repository methods and do not leak the relation, e.g. we only return a boolean, single entity, or a collection of entities.

from kan.

DangerDawson avatar DangerDawson commented on August 15, 2024

After giving this some thought, I see 3 separate solutions if you are using the repository pattern:

  • You return the repository method e.g.

register(:index) { |_user| :exclude_draft }

  • you return a set of conditions in a data structure that the repository method can understand e.g.

register(:index) { |_user| { draft: false } }

Additionally wrapping this up in a Dry::Monad::Result could give you the ability to check success or failure, and on success retrieve the conditions / method e.g.

register(:index) { |user| ( Success( { draft: false } ) }

from kan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.