Distribution rules determine who can read facts.
They are defined in the JinagaConfig
class.
Even though the rules are defined within the client, they are enforced by the replicator.
Add a static method to the JinagaConfig
class called Distribution
.
This method should return a string that describes the distribution rules.
Build distribution rules from your model.
public static class JinagaConfig
{
public static string Distribution() =>
DistributionRules.Describe(Distribute);
private static DistributionRules Distribute(DistributionRules r) => r
// Distribute sites with names and domains to the site creator.
.Share(Given<User>.Match((user, facts) =>
from site in facts.OfType<Site>()
where site.creator == user &&
!facts.Any<SiteDeleted>(deleted =>
deleted.site == site &&
!facts.Any<SiteRestored>(restored =>
restored.deleted == deleted))
select new {
site,
names =
from name in facts.OfType<SiteName>()
where name.site == site &&
!facts.Any<SiteName>(next =>
next.prior.Contains(name))
select name,
domains =
from domain in facts.OfType<SiteDomain>()
where domain.site == site &&
!facts.Any<SiteDomain>(next =>
next.prior.Contains(domain))
select domain
}
)).With(Given<User>.Match((user, facts) =>
from self in facts.OfType<User>()
where self == user
select self
));
}
The Share
method describes a specification that a user might want to execute.
It is common to copy specifications from your application.
The With
method identifies the users who can execute the specification.
The parameter to the With
method is a specification that starts from the same givens as the shared specification.
In some cases, the user who can execute the specification is a predecessor of a given fact. In those cases, you can use a lambda expression that follows the graph to the predecessor.
// Distribute site names to the site creator.
.Share(Given<Site>.Match((site, facts) =>
from name in facts.OfType<SiteName>()
where name.site == site &&
!facts.Any<SiteName>(next =>
next.prior.Contains(name))
select name
)).With(site => site.creator)
The WithEveryone
method allows any user to execute the specification, even if they are not authenticated.
This is useful for public queries.
// Distribute published posts to everyone.
.Share(Given<Site>.Match((site, facts) =>
from post in facts.OfType<Post>()
where post.site == site &&
!facts.Any<PostDeleted>(deleted =>
deleted.post == post &&
!facts.Any<PostRestored>(restored =>
restored.deleted == deleted)) &&
!facts.Any<Publish>(publish =>
publish.post == post)
select post
)).WithEveryone()
To deploy distribution rules to a hosted replicator, use the dotnet jinaga
command line tool.
Install the tool by running the following command.
dotnet tool install -g Jinaga.Tool
Then run the following command to deploy the distribution rules to a hosted replicator.
dotnet jinaga deploy distribution {assembly} {endpoint} {secret}
The {assembly}
parameter is the path to the assembly that contains the JinagaConfig
class.
You can find the endpoint and secret in the replicator's configuration.
If instead you are running your own replicator, run the following command to write the distribution rules to a file.
dotnet jinaga print distribution {assembly} > distribution.txt