Skip to content

Commit

Permalink
Change ExampleBosk to use StateTreeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jul 18, 2023
1 parent cebd110 commit 9eeae2d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,21 @@ The library works particularly well with Java records.
You can define your state tree's root node as follows:

```
import io.vena.bosk.Entity;
import io.vena.bosk.StateTreeNode;
import io.vena.bosk.Identifier;
public record ExampleState (
Identifier id,
// Add fields here as you need them
String name
) implements Entity {}
) implements StateTreeNode {}
```

You can also use classes, especially if you're using Lombok:

```
@Value
@Accessors(fluent = true)
public class ExampleState implements Entity {
Identifier id;
public class ExampleState implements StateTreeNode {
// Add fields here as you need them
String name;
}
Expand All @@ -95,9 +93,9 @@ Now declare your singleton `Bosk` class to house and manage your application sta

```
import io.vena.bosk.Bosk;
import io.vena.bosk.Identifier;
import io.vena.bosk.Path;
import io.vena.bosk.DriverFactory;
import io.vena.bosk.Reference;
import io.vena.bosk.annotations.ReferencePath;
import io.vena.bosk.exceptions.InvalidTypeException;
@Singleton // You can use your framework's dependency injection for this
Expand All @@ -106,7 +104,7 @@ public class ExampleBosk extends Bosk<ExampleState> {
super(
"ExampleBosk",
ExampleState.class,
new ExampleState(Identifier.from("example"), "world"),
defaultRoot(),
driverFactory());
}
Expand All @@ -115,7 +113,11 @@ public class ExampleBosk extends Bosk<ExampleState> {
@ReferencePath("/name") Reference<String> name();
}
public final Refs refs = buildReferences(Refs.class);
public final Refs refs = rootReference().buildReferences(Refs.class);
private static ExampleState defaultRoot() {
return new ExampleState("world");
}
// Start off simple
private static DriverFactory<ExampleState> driverFactory() {
Expand Down Expand Up @@ -173,25 +175,23 @@ import io.vena.bosk.drivers.mongo.MongoDriver;
import io.vena.bosk.drivers.mongo.MongoDriverSettings;
...
private static DriverFactory<ExampleState> driverFactory() {
MongoClientSettings clientSettings = MongoClientSettings.builder()
.build();
private static DriverFactory<ExampleState> driverFactory() {
// Bosk requires certain client settings to provide the required consistency guarantees
MongoClientSettings clientSettings = MongoClientSettings.builder()
.build();
MongoDriverSettings driverSettings = MongoDriverSettings.builder()
.database("ExampleBoskDB") // Bosks using the same name here will share state
.build();
MongoDriverSettings driverSettings = MongoDriverSettings.builder()
.database("exampleDB")
.build();
// For advanced usage, you'll want to inject this object,
// but for getting started, we can just create one here.
BsonPlugin bsonPlugin = new BsonPlugin();
// For advanced usage, you'll want to inject this object,
// but for getting started, we can just create one here.
BsonPlugin bsonPlugin = new BsonPlugin();
return MongoDriver.factory(
clientSettings,
driverSettings,
bsonPlugin);
}
return MongoDriver.factory(
clientSettings,
driverSettings,
bsonPlugin);
}
```

To run this, you'll need a MongoDB replica set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mongodb.MongoClientSettings;
import io.vena.bosk.Bosk;
import io.vena.bosk.DriverFactory;
import io.vena.bosk.Identifier;
import io.vena.bosk.Reference;
import io.vena.bosk.annotations.ReferencePath;
import io.vena.bosk.drivers.mongo.BsonPlugin;
Expand All @@ -28,7 +27,7 @@ public interface Refs {
public final Refs refs = rootReference().buildReferences(Refs.class);

private static ExampleState defaultRoot() {
return new ExampleState(Identifier.from("example"), "world");
return new ExampleState("world");
}

private static DriverFactory<ExampleState> driverFactory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.vena.bosk.drivers.mongo.example;

import io.vena.bosk.Entity;
import io.vena.bosk.Identifier;
import io.vena.bosk.StateTreeNode;
import lombok.Value;

@Value
public class ExampleState implements Entity {
Identifier id;
public class ExampleState implements StateTreeNode {
// Add fields here as you need them
String name;
}

0 comments on commit 9eeae2d

Please sign in to comment.