Browse files

finish (?) example.

  • Loading branch information...
1 parent 5f37130 commit e7f4af70962af3083ab78f6f854d27a3b4bcc69e @robey committed May 5, 2013
Showing with 38 additions and 6 deletions.
  1. +38 −6 docs/
@@ -70,6 +70,7 @@ fauna = require 'fauna'
class User extends fauna.Class
+ @field "name"
class Message extends fauna.Class
@field "text"
@@ -82,8 +83,9 @@ class ChatRoom extends fauna.Class
This defines a class `User` and marks it as a native fauna model, so it will
live in the fauna namespace instead of being created as a user-defined class.
-`User` is usually the only class that needs this special treatment. We could
-add our own fields and references here, just like any other class.
+`User` is usually the only class that needs this special treatment. We can
+add our own fields and references here, just like any other class, so we add
+a display name.
`Message` has a text field for the message content, and a reference named
"author". Marking it as a reference lets fauna know that it's a reference to
@@ -109,6 +111,7 @@ function User() {
extends(User, fauna.Class);
function Message() {
fauna.Class.apply(this, arguments);
@@ -170,7 +173,7 @@ and read the last 20 messages posted. Simple!
If no users exist yet, we should create one:
-var user = new User({ email: "", password: "acornz" });
+var user = new User({ name, "Rocky", email: "", password: "acornz" });
user.persist().then(function (user) {
console.log("Created a new user: " + user);
console.log("The new user's id is: " +;
@@ -192,15 +195,16 @@ them manually:
var user = new User(); = "Rocky"; = "";
user.password = "acornz";
user.persist().then( ... );
For user objects, fauna-js knows that `email` and `password` are built-in
-fields. If we had defined other fields or references on the class, we could
-set them the same way in the constructor, and fauna-js would put them into the
-`data` and `references` sections of the object inside the fauna database.
+fields. For other fields or references on the class (like `name` in our
+example) fauna-js fills in the `data` and `references` sections of the object
+inside the fauna database.
The `persist` call may look a little magical, but it's using the `FaunaClient`
that the `User` prototype was registered with. If you're juggling multiple
@@ -259,8 +263,36 @@ identifier and put it in the "references" section of the object.
## Read recent messages
+To read the most recent 20 messages, we ask for the most recent events on the
+chat room's "messages" event set. Fauna time values are in microseconds, and
+javascript's are in milliseconds, so we have to multiply the current time by
+1000, and ask for events before that time (now).
+var now = * 1000;{ count: 20, before: now }).then(function (page) {
+ var messages = page.toArray().reverse();
+ for (var i = 0; i < messages.length; i++) {
+ console.log("<" + messages[i] + "> " + messages[i].text);
+ }
+The `page` method requests a page of events from the event set, and returns an
+event set object with `before`, `after`, and `events` fields. Since we aren't
+going to keep paging through the events, we ignore the `before` and `after`
+fields, and call `toArray` to convert the event list into a list of objects.
+(If the events included modifications or deletions, `toArray` would only
+return items that still exist after matching "create" and "delete" operations.
+So for a true stream of events, you would want to use the `` array
+directly. Our littlest chat server never deletes messages, and treats all
+messages as immutable, so we know all of the events are "create" events.)
+The objects are in reverse time order, so we reverse them back to display the
+oldest messages first. Fauna helpfully supplies the user object for our
+`author` field, so we dig out the user's name and display it next to their
+And that's it! We're chatting, like 1990s IRC all over again!

0 comments on commit e7f4af7

Please sign in to comment.