/
dsl.ex
1361 lines (1230 loc) · 36.5 KB
/
dsl.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Ash.Resource.Dsl do
@attribute %Spark.Dsl.Entity{
name: :attribute,
describe: """
Declares an attribute on the resource.
""",
examples: [
"""
attribute :name, :string do
allow_nil? false
end
"""
],
transform: {Ash.Resource.Attribute, :transform, []},
target: Ash.Resource.Attribute,
args: [:name, :type],
modules: [:type],
schema: Ash.Resource.Attribute.attribute_schema()
}
@create_timestamp %Spark.Dsl.Entity{
name: :create_timestamp,
describe: """
Declares a non-writable attribute with a create default of `&DateTime.utc_now/0`
Accepts all the same options as `d:Ash.Resource.Dsl.attributes.attribute`, except it sets
the following different defaults:
```elixir
writable? false
private? true
default &DateTime.utc_now/0
match_other_defaults? true
type Ash.Type.UTCDatetimeUsec
allow_nil? false
```
""",
examples: [
"create_timestamp :inserted_at"
],
target: Ash.Resource.Attribute,
args: [:name],
schema: Ash.Resource.Attribute.create_timestamp_schema(),
transform: {Ash.Resource.Attribute, :transform, []}
}
@update_timestamp %Spark.Dsl.Entity{
name: :update_timestamp,
describe: """
Declares a non-writable attribute with a create and update default of `&DateTime.utc_now/0`
Accepts all the same options as `d:Ash.Resource.Dsl.attributes.attribute`, except it sets
the following different defaults:
```elixir
writable? false
private? true
default &DateTime.utc_now/0
match_other_defaults? true
update_default &DateTime.utc_now/0
type Ash.Type.UTCDatetimeUsec
allow_nil? false
```
""",
examples: [
"update_timestamp :inserted_at"
],
target: Ash.Resource.Attribute,
schema: Ash.Resource.Attribute.update_timestamp_schema(),
args: [:name],
transform: {Ash.Resource.Attribute, :transform, []}
}
@integer_primary_key %Spark.Dsl.Entity{
name: :integer_primary_key,
describe: """
Declares a generated, non writable, non-nil, primary key column of type integer.
Generated integer primary keys must be supported by the data layer.
Accepts all the same options as `d:Ash.Resource.Dsl.attributes.attribute`, except for `allow_nil?`, but it sets
the following different defaults:
```elixir
writable? false
primary_key? true
generated? true
type :integer
```
""",
examples: [
"integer_primary_key :id"
],
args: [:name],
target: Ash.Resource.Attribute,
schema: Ash.Resource.Attribute.integer_primary_key_schema(),
auto_set_fields: [allow_nil?: false],
transform: {Ash.Resource.Attribute, :transform, []}
}
@uuid_primary_key %Spark.Dsl.Entity{
name: :uuid_primary_key,
describe: """
Declares a non writable, non-nil, primary key column of type uuid, which defaults to `Ash.UUID.generate/0`.
Accepts all the same options as `d:Ash.Resource.Dsl.attributes.attribute`, except for `allow_nil?`, but it sets
the following different defaults:
```elixir
writable? false
default &Ash.UUID.generate/0
primary_key? true
generated? true
type :uuid
```
""",
examples: [
"uuid_primary_key :id"
],
args: [:name],
target: Ash.Resource.Attribute,
schema: Ash.Resource.Attribute.uuid_primary_key_schema(),
auto_set_fields: [allow_nil?: false],
transform: {Ash.Resource.Attribute, :transform, []}
}
@attributes %Spark.Dsl.Section{
name: :attributes,
describe: """
A section for declaring attributes on the resource.
""",
imports: [Ash.Resource.Attribute.Helpers],
examples: [
"""
attributes do
uuid_primary_key :id
attribute :first_name, :string do
allow_nil? false
end
attribute :last_name, :string do
allow_nil? false
end
attribute :email, :string do
allow_nil? false
constraints [
match: ~r/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/
]
end
attribute :type, :atom do
constraints [
one_of: [:admin, :teacher, :student]
]
end
create_timestamp :inserted_at
update_timestamp :updated_at
end
"""
],
entities: [
@attribute,
@create_timestamp,
@update_timestamp,
@integer_primary_key,
@uuid_primary_key
]
}
@has_one %Spark.Dsl.Entity{
name: :has_one,
describe: """
Declares a has_one relationship. In a relational database, the foreign key would be on the *other* table.
Generally speaking, a `has_one` also implies that the destination table is unique on that foreign key.
See the [relationships guide](/documentation/topics/relationships.md) for more.
""",
examples: [
"""
# In a resource called `Word`
has_one :dictionary_entry, DictionaryEntry do
source_attribute :text
destination_attribute :word_text
end
"""
],
no_depend_modules: [:destination],
target: Ash.Resource.Relationships.HasOne,
schema: Ash.Resource.Relationships.HasOne.opt_schema(),
args: [:name, :destination]
}
@has_many %Spark.Dsl.Entity{
name: :has_many,
describe: """
Declares a has_many relationship. There can be any number of related entities.
See the [relationships guide](/documentation/topics/relationships.md) for more.
""",
examples: [
"""
# In a resource called `Word`
has_many :definitions, DictionaryDefinition do
source_attribute :text
destination_attribute :word_text
end
"""
],
target: Ash.Resource.Relationships.HasMany,
no_depend_modules: [:destination],
schema: Ash.Resource.Relationships.HasMany.opt_schema(),
args: [:name, :destination]
}
@many_to_many %Spark.Dsl.Entity{
name: :many_to_many,
describe: """
Declares a many_to_many relationship. Many to many relationships require a join resource.
A join resource is a resource that consists of a relationship to the source and destination of the many to many.
See the [relationships guide](/documentation/topics/relationships.md) for more.
""",
examples: [
"""
# In a resource called `Word`
many_to_many :books, Book do
through BookWord
source_attribute :text
source_attribute_on_join_resource :word_text
destination_attribute :id
destination_attribute_on_join_resource :book_id
end
# And in `BookWord` (the join resource)
belongs_to :book, Book, primary_key?: true, allow_nil?: false
belongs_to :word, Word, primary_key?: true, allow_nil?: false
"""
],
no_depend_modules: [:destination, :through],
target: Ash.Resource.Relationships.ManyToMany,
schema: Ash.Resource.Relationships.ManyToMany.opt_schema(),
transform: {Ash.Resource.Relationships.ManyToMany, :transform, []},
args: [:name, :destination]
}
@belongs_to %Spark.Dsl.Entity{
name: :belongs_to,
describe: """
Declares a belongs_to relationship. In a relational database, the foreign key would be on the *source* table.
This creates a field on the resource with the corresponding name and type, unless `define_attribute?: false` is provided.
See the [relationships guide](/documentation/topics/relationships.md) for more.
""",
examples: [
"""
# In a resource called `Word`
belongs_to :dictionary_entry, DictionaryEntry do
source_attribute :text,
destination_attribute :word_text
end
"""
],
no_depend_modules: [:destination],
target: Ash.Resource.Relationships.BelongsTo,
schema: Ash.Resource.Relationships.BelongsTo.opt_schema(),
args: [:name, :destination]
}
@relationships %Spark.Dsl.Section{
name: :relationships,
describe: """
A section for declaring relationships on the resource.
Relationships are a core component of resource oriented design. Many components of Ash
will use these relationships. A simple use case is loading relationships (done via the `Ash.Query.load/2`).
See the [relationships guide](/documentation/topics/relationships.md) for more.
""",
examples: [
"""
relationships do
belongs_to :post, MyApp.Post do
primary_key? true
end
belongs_to :category, MyApp.Category do
primary_key? true
end
end
""",
"""
relationships do
belongs_to :author, MyApp.Author
many_to_many :categories, MyApp.Category do
through MyApp.PostCategory
destination_attribute_on_join_resource :category_id
source_attribute_on_join_resource :post_id
end
end
""",
"""
relationships do
has_many :posts, MyApp.Post do
destination_attribute :author_id
end
has_many :composite_key_posts, MyApp.CompositeKeyPost do
destination_attribute :author_id
end
end
"""
],
imports: [
Ash.Filter.TemplateHelpers
],
entities: [
@has_one,
@has_many,
@many_to_many,
@belongs_to
]
}
@action_change %Spark.Dsl.Entity{
name: :change,
describe: """
A change to be applied to the changeset.
See `Ash.Resource.Change` for more.
""",
examples: [
"change relate_actor(:reporter)",
"change {MyCustomChange, :foo}"
],
no_depend_modules: [:change],
target: Ash.Resource.Change,
schema: Ash.Resource.Change.action_schema(),
args: [:change]
}
@action_argument %Spark.Dsl.Entity{
name: :argument,
describe: """
Declares an argument on the action
""",
examples: [
"argument :password_confirmation, :string"
],
modules: [:type],
target: Ash.Resource.Actions.Argument,
args: [:name, :type],
transform: {Ash.Type, :set_type_transformation, []},
schema: Ash.Resource.Actions.Argument.schema()
}
@metadata %Spark.Dsl.Entity{
name: :metadata,
describe: """
A special kind of attribute that is only added to specific actions. Nothing sets this value, it must be set in a custom
change via `Ash.Resource.Info.put_metadata/3`.
""",
examples: [
"""
metadata :api_token, :string, allow_nil?: false
""",
"""
metadata :operation_id, :string, allow_nil?: false
"""
],
target: Ash.Resource.Actions.Metadata,
args: [:name, :type],
schema: Ash.Resource.Actions.Metadata.schema(),
transform: {Ash.Type, :set_type_transformation, []}
}
@change %Spark.Dsl.Entity{
name: :change,
describe: """
A change to be applied to the changeset.
See `Ash.Resource.Change` for more.
""",
examples: [
"change relate_actor(:reporter)",
"change {MyCustomChange, :foo}"
],
no_depend_modules: [:change],
target: Ash.Resource.Change,
schema: Ash.Resource.Change.schema(),
args: [:change]
}
@validate %Spark.Dsl.Entity{
name: :validate,
describe: """
Declares a validation for creates and updates.
See `Ash.Resource.Change` for more.
""",
examples: [
"validate {Mod, [foo: :bar]}",
"validate at_least_one_of_present([:first_name, :last_name])"
],
target: Ash.Resource.Validation,
schema: Ash.Resource.Validation.opt_schema(),
no_depend_modules: [:validation],
transform: {Ash.Resource.Validation, :transform, []},
args: [:validation]
}
@action_validate %Spark.Dsl.Entity{
name: :validate,
describe: """
Declares a validation to be applied to the changeset.
See `Ash.Resource.Validation` for more.
""",
examples: [
"validate changing(:email)"
],
target: Ash.Resource.Validation,
schema: Ash.Resource.Validation.action_schema(),
no_depend_modules: [:validation],
transform: {Ash.Resource.Validation, :transform, []},
args: [:validation]
}
@action %Spark.Dsl.Entity{
name: :action,
describe: """
Declares a generic action. A combination of arguments, a return type and a run function.
For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"""
action :top_user_emails, {:array, :string} do
argument :limit, :integer, default: 10, allow_nil?: false
run fn input, context ->
with {:ok, top_users} <- top_users(input.limit) do
{:ok, Enum.map(top_users, &(&1.email))}
end
end
end
"""
],
target: Ash.Resource.Actions.Action,
schema: Ash.Resource.Actions.Action.opt_schema(),
entities: [
arguments: [
@action_argument
]
],
args: [:name, :returns]
}
@create %Spark.Dsl.Entity{
name: :create,
describe: """
Declares a `create` action. For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"""
create :register do
primary? true
end
"""
],
imports: [
Ash.Resource.Change.Builtins,
Ash.Resource.Validation.Builtins,
Ash.Filter.TemplateHelpers
],
target: Ash.Resource.Actions.Create,
schema: Ash.Resource.Actions.Create.opt_schema(),
modules: [:manual],
no_depend_modules: [:touches_resources],
deprecations: [
manual?: "Use the `manual` option instead, and provide an implementation."
],
entities: [
changes: [
@action_change,
@action_validate
],
arguments: [
@action_argument
],
metadata: [
@metadata
]
],
args: [:name]
}
@prepare %Spark.Dsl.Entity{
name: :prepare,
describe: """
Declares a preparation, which can be used to prepare a query for a read action.
""",
examples: [
"""
prepare build(sort: [:foo, :bar])
"""
],
target: Ash.Resource.Preparation,
schema: Ash.Resource.Preparation.schema(),
no_depend_modules: [:preparation],
args: [:preparation]
}
@pagination %Spark.Dsl.Entity{
name: :pagination,
describe: """
Adds pagination options to a resource
""",
target: Ash.Resource.Actions.Read.Pagination,
schema: Ash.Resource.Actions.Read.pagination_schema(),
transform: {Ash.Resource.Actions.Read.Pagination, :transform, []}
}
@read %Spark.Dsl.Entity{
name: :read,
describe: """
Declares a `read` action. For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"""
read :read_all do
primary? true
end
"""
],
imports: [
Ash.Resource.Preparation.Builtins,
Ash.Filter.TemplateHelpers
],
target: Ash.Resource.Actions.Read,
schema: Ash.Resource.Actions.Read.opt_schema(),
transform: {Ash.Resource.Actions.Read, :transform, []},
no_depend_modules: [:touches_resources],
modules: [:manual],
entities: [
arguments: [
@action_argument
],
preparations: [
@prepare
],
pagination: [
@pagination
],
metadata: [
@metadata
]
],
args: [:name]
}
@update %Spark.Dsl.Entity{
name: :update,
describe: """
Declares a `update` action. For calling this action, see the `Ash.Api` documentation.
""",
imports: [
Ash.Resource.Change.Builtins,
Ash.Resource.Validation.Builtins,
Ash.Filter.TemplateHelpers
],
examples: [
"update :flag_for_review, primary?: true"
],
modules: [:manual],
entities: [
changes: [
@action_change,
@action_validate
],
metadata: [
@metadata
],
arguments: [
@action_argument
]
],
deprecations: [
manual?: "Use the `manual` option instead, and provide an implementation."
],
no_depend_modules: [:touches_resources],
target: Ash.Resource.Actions.Update,
schema: Ash.Resource.Actions.Update.opt_schema(),
args: [:name]
}
@destroy %Spark.Dsl.Entity{
name: :destroy,
describe: """
Declares a `destroy` action. For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"""
destroy :soft_delete do
primary? true
end
"""
],
modules: [:manual],
imports: [
Ash.Resource.Change.Builtins,
Ash.Resource.Validation.Builtins,
Ash.Filter.TemplateHelpers
],
deprecations: [
manual?: "Use the `manual` option instead, and provide an implementation."
],
no_depend_modules: [:touches_resources],
entities: [
changes: [
@action_change,
@action_validate
],
metadata: [
@metadata
],
arguments: [
@action_argument
]
],
target: Ash.Resource.Actions.Destroy,
schema: Ash.Resource.Actions.Destroy.opt_schema(),
args: [:name]
}
@actions %Spark.Dsl.Section{
name: :actions,
describe: """
A section for declaring resource actions.
All manipulation of data through the underlying data layer happens through actions.
There are four types of action: `create`, `read`, `update`, and `destroy`. You may
recognize these from the acronym `CRUD`. You can have multiple actions of the same
type, as long as they have different names. This is the primary mechanism for customizing
your resources to conform to your business logic. It is normal and expected to have
multiple actions of each type in a large application.
""",
schema: [
defaults: [
type: {:list, {:in, [:create, :read, :update, :destroy]}},
doc: """
Creates a simple action of each specified type, with the same name as the type.
These actions will be the primary actions, unless you've declared a different action
of the same type that is explicitly set as primary.
By default, resources have no default actions. Embedded resources, however, have a default
of all resource types.
"""
],
default_accept: [
type: {:list, :atom},
doc:
"A default value for the `accept` option for each action. Defaults to all public attributes."
]
],
examples: [
"""
actions do
create :signup do
argument :password, :string
argument :password_confirmation, :string
validate confirm(:password, :password_confirmation)
change {MyApp.HashPassword, []} # A custom implemented Change
end
read :me do
# An action that auto filters to only return the user for the current user
filter [id: actor(:id)]
end
update :update do
accept [:first_name, :last_name]
end
destroy do
change set_attribute(:deleted_at, &DateTime.utc_now/0)
# This tells it that even though this is a delete action, it
# should be treated like an update because `deleted_at` is set.
# This should be coupled with a `base_filter` on the resource
# or with the read actions having a `filter` for `is_nil: :deleted_at`
soft? true
end
end
"""
],
entities: [
@action,
@create,
@read,
@update,
@destroy
]
}
@identity %Spark.Dsl.Entity{
name: :identity,
describe: """
Represents a unique constraint on the resource.
See the [identities guide](/documentation/topics/identities.md) for more.
""",
examples: [
"identity :name, [:name]",
"identity :full_name, [:first_name, :last_name]"
],
target: Ash.Resource.Identity,
schema: Ash.Resource.Identity.schema(),
no_depend_modules: [:pre_check_with, :eager_check_with],
args: [:name, :keys]
}
@identities %Spark.Dsl.Section{
name: :identities,
describe: """
Unique identifiers for the resource
""",
examples: [
"""
identities do
identity :full_name, [:first_name, :last_name]
identity :email, [:email]
end
"""
],
entities: [
@identity
]
}
@resource %Spark.Dsl.Section{
name: :resource,
describe: """
General resource configuration
""",
examples: [
"""
resource do
description "A description of this resource"
base_filter [is_nil: :deleted_at]
end
"""
],
imports: [Ash.Filter.TemplateHelpers],
modules: [:simple_notifiers],
schema: [
description: [
type: :string,
doc: "A human readable description of the resource, to be used in generated documentation"
],
base_filter: [
type: :any,
doc: "A filter statement to be applied to any queries on the resource"
],
default_context: [
type: :any,
doc: "Default context to apply to any queries/changesets generated for this resource."
],
trace_name: [
type: :string,
doc: """
The name to use in traces. Defaults to the short_name stringified.
See the [monitoring guide](/documentation/topics/monitoring.md) for more.
"""
],
short_name: [
type: :atom,
doc: """
A short identifier for the resource, which should be unique.
See the [monitoring guide](/documentation/topics/monitoring.md) for more.
"""
],
simple_notifiers: [
type: {:list, :module},
doc:
"A list of notifiers that require no DSL. Can be used to avoid compile time dependencies on notifiers"
]
]
}
@define_calculation %Spark.Dsl.Entity{
name: :define_calculation,
describe: """
Defines a function with the corresponding name and arguments, that evaluates a calculation.
Use `:_record` to take an instance of a record.
See the [code interface guide](/documentation/topics/code-interface.md) for more.
""",
examples: [
"define_calculation :referral_link, args: [:id]",
"define_calculation :referral_link, args: [{:arg, :id}, {:ref, :id}]"
],
target: Ash.Resource.CalculationInterface,
schema: Ash.Resource.CalculationInterface.schema(),
transform: {Ash.Resource.CalculationInterface, :transform, []},
args: [:name]
}
@define %Spark.Dsl.Entity{
name: :define,
describe: """
Defines a function with the corresponding name and arguments.
See the [code interface guide](/documentation/topics/code-interface.md) for more.
""",
examples: [
"define :get_user_by_id, action: :get_by_id, args: [:id], get?: true"
],
target: Ash.Resource.Interface,
schema: Ash.Resource.Interface.schema(),
transform: {Ash.Resource.Interface, :transform, []},
args: [:name]
}
@code_interface %Spark.Dsl.Section{
name: :code_interface,
describe: """
Functions that will be defined on the Api module to interact with this resource.
See the [code interface guide](/documentation/topics/code-interface.md) for more.
""",
examples: [
"""
code_interface do
define_for MyApp.Api
define :create_user, action: :create
define :get_user_by_id, action: :get_by_id, args: [:id], get?: true
end
"""
],
modules: [:define_for],
schema: [
define_for: [
type: {:spark, Ash.Api},
doc:
"Defines the code interface on the resource module directly, using the provided Api.",
default: false
]
],
entities: [
@define,
@define_calculation
]
}
@validations %Spark.Dsl.Section{
name: :validations,
describe: """
Declare validations prior to performing actions against the resource
""",
imports: [Ash.Resource.Validation.Builtins],
examples: [
"""
validations do
validate {Mod, [foo: :bar]}
validate at_least_one_of_present([:first_name, :last_name])
end
"""
],
entities: [
@validate
]
}
@changes %Spark.Dsl.Section{
name: :changes,
describe: """
Declare changes that occur on create/update/destroy actions against the resource
See `Ash.Resource.Change` for more.
""",
imports: [
Ash.Resource.Validation.Builtins,
Ash.Resource.Change.Builtins,
Ash.Filter.TemplateHelpers
],
examples: [
"""
changes do
change {Mod, [foo: :bar]}
change set_context(%{some: :context})
end
"""
],
entities: [
@change
]
}
@preparations %Spark.Dsl.Section{
name: :preparations,
describe: """
Declare preparations that occur on all read actions for a given resource
""",
imports: [Ash.Resource.Preparation.Builtins, Ash.Resource.Validation.Builtins],
examples: [
"""
preparations do
prepare {Mod, [foo: :bar]}
prepare set_context(%{some: :context})
end
"""
],
entities: [
@prepare
]
}
@count %Spark.Dsl.Entity{
name: :count,
describe: """
Declares a named count aggregate on the resource
Supports `filter`, but not `sort` (because that wouldn't affect the count)
See the [aggregates guide](/documentation/topics/aggregates.md) for more.
""",
examples: [
"""
count :assigned_ticket_count, :assigned_tickets do
filter [active: true]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path],
schema:
Keyword.put(Keyword.delete(Ash.Resource.Aggregate.schema(), :sort), :uniq?,
type: :boolean,
doc: "Wether or not to count unique values only",
default: false
),
auto_set_fields: [kind: :count]
}
@first %Spark.Dsl.Entity{
name: :first,
describe: """
Declares a named `first` aggregate on the resource
First aggregates return the first value of the related record
that matches. Supports both `filter` and `sort`.
See the [aggregates guide](/documentation/topics/aggregates.md) for more.
""",
examples: [
"""
first :first_assigned_ticket_subject, :assigned_tickets, :subject do
filter [active: true]
sort [:subject]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path, :field],
schema: Ash.Resource.Aggregate.schema(),
auto_set_fields: [kind: :first]
}
@max %Spark.Dsl.Entity{
name: :max,
describe: """
Declares a named `max` aggregate on the resource
Supports `filter`, but not `sort` (because that wouldn't affect the max)
See the [aggregates guide](/documentation/topics/aggregates.md) for more.
""",
examples: [
"""
max :first_assigned_ticket_subject, :assigned_tickets, :severity do
filter [active: true]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path, :field],
schema: Ash.Resource.Aggregate.schema() |> Keyword.delete(:sort),
auto_set_fields: [kind: :max]
}
@min %Spark.Dsl.Entity{