avram_scoped_id v0.2.0
avram_scoped_id
An small helper to set an scoped id for the model, based on the query you set as well.
Installation
-
Add the dependency to your
shard.yml
:dependencies: avram_scoped_id: github: microgit-com/avram_scoped_id
-
Run
shards install
-
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 useneeds
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
- Fork it (https://github.com/microgit-com/avram_scoped_id/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Håkan Nylén - creator and maintainer
avram_scoped_id
- 0
- 1
- 0
- 1
- 1
- over 4 years ago
- May 10, 2020
MIT License
Sat, 23 Nov 2024 15:13:14 GMT