Giter VIP home page Giter VIP logo

avram_scoped_id's Introduction

avram_scoped_id

Build Status

An small helper to set an scoped id for the model, based on the query you set as well.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      avram_scoped_id:
        github: microgit-com/avram_scoped_id
  2. Run shards install

  3. Require the shard after requiring Avram

If using Lucky, require the shard in your src/shards.cr file after requiring Avram:

# In src/shards.cr
# Put this after `require "avram"`
require "avram_scoped_id"

If not using Lucky, require the shard after Avram:

# In whichever file you require your shards
# Put this after `require "avram"`
require "avram_scoped_id"

Usage

Create column scoped_id in your model, as Int64 You can call it differently if you want, just change to your column in the set method inside the before_save below

class AddScopeIdToArticle::V20200510065019 < Avram::Migrator::Migration::V1
  def migrate
    alter table_for(Article) do
      add scoped_id : Int64, default: 0
    end
  end

  def rollback
    # drop table_for(Thing)
  end
end

Feel free to add index on the scoped_id for faster queries later on.

Then You add it to the operation where you want to save the new scoped id. Like SaveArticle.

class SaveArticle < Article::SaveOperation
  before_save do
    AvramScopedId.set column: scoped_id,
      query: ArticleQuery.new.team_id(team_id)
  end
end

Good thing to know is that variables in the query might fail due to they can be Nil - So use needs and set that in the query and if necessary with .not_nil!

Find scoped_id

To find by scoped id you can add method to query, like this:

class ArticleQuery < Article::BaseQuery
  def find_scoped(scoped_id : Int64) : Article
    scoped_id(scoped_id).first
  end
end

Don't forget to query based on the query you used when setting the scoped_id, like ArticleQuery.new.team_id(team_id) when finding the Article.

So example, the whole query would be:

ArticleQuery.new.team_id(team_id).find_scoped(1)

Development

TODO: Write development instructions here

Contributing

  1. Fork it (https://github.com/microgit-com/avram_scoped_id/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

avram_scoped_id's People

Contributors

confact avatar

Watchers

 avatar  avatar

Forkers

constxife

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.