You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-6Lines changed: 21 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,26 +18,43 @@ An small helper to set an scoped id for the model, based on the query you set as
18
18
19
19
**If using Lucky**, require the shard in your src/shards.cr file after requiring Avram:
20
20
21
-
```
21
+
```crystal
22
22
# In src/shards.cr
23
23
# Put this after `require "avram"`
24
24
require "avram_scoped_id"
25
25
```
26
26
27
27
**If not using Lucky**, require the shard after Avram:
28
28
29
-
```
29
+
```crystal
30
30
# In whichever file you require your shards
31
31
# Put this after `require "avram"`
32
32
require "avram_scoped_id"
33
33
```
34
34
35
35
## Usage
36
-
Create column `scoped_id` in your model, as `Int64` - if not it will fail.
36
+
Create column `scoped_id` in your model, as `Int64`
37
+
You can call it differently if you want, just change to your column in the set method inside the `before_save` below
38
+
39
+
```crystal
40
+
class AddScopeIdToArticle::V20200510065019 < Avram::Migrator::Migration::V1
41
+
def migrate
42
+
alter table_for(Article) do
43
+
add scoped_id : Int64, default: 0
44
+
end
45
+
end
46
+
47
+
def rollback
48
+
# drop table_for(Thing)
49
+
end
50
+
end
51
+
```
52
+
53
+
> Feel free to add index on the scoped_id for faster queries later on.
37
54
38
55
Then You add it to the operation where you want to save the new scoped id. Like `SaveArticle`.
39
56
40
-
```
57
+
```crystal
41
58
class SaveArticle < Article::SaveOperation
42
59
before_save do
43
60
AvramScopedId.set column: scoped_id,
@@ -46,8 +63,6 @@ class SaveArticle < Article::SaveOperation
46
63
end
47
64
```
48
65
49
-
**scoped_id** is hardcoded for now due to how avram is built. so for now you need to have scoped_id on column.
50
-
51
66
> 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!`
0 commit comments