Skip to content

f4z4on/attendees.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Attendees

An example app to show Transit. It requires Node with Harmony support. Only development version support Harmony at the moment, e.g. 0.11.14.

Presentation

Filip Zrůst - Transferring application data efficiently with Transit: https://speakerdeck.com/frzng/transferring-application-data-efficiently-with-transit

Usage

Get the project

git clone https://github.com/frzng/attendees.js.git

Go at the beginning

See the simple app with pure JSON REST API:

git checkout step-1-simple

Check the source code of index.js and try it yourself. Run the app in one terminal:

npm install
npm start

And test it in other terminal. Get list of all attendees:

curl -s localhost:3000/attendees
[]

Add a new attendee:

curl -i -H 'Content-Type: application/json' -d '{ "email": "[email protected]", "name": "Filip Zrůst" }' localhost:3000/attendees
HTTP/1.1 201 Created
Location: /attendees/0

Get list of all attendees again:

curl -s localhost:3000/attendees
[{
  "email" : "[email protected]",
  "name" : "Filip Zrůst"
}]

You should have a clear understanding of we're trying to achieve. Storing data in global variable (not persistent). Data are attendee records with email and name keys.

Don't forget to kill the server when moving to the next step.

Transit in action

Now with Transit:

git checkout step-2-transit

Compare changes from step 1 to step 2. and try it yourself. Run the app in one terminal:

npm install
npm start

If you want to see how Transit uses JSON-Verbose for debugging and how it looks, you can start the server this way:

npm start -- -v

During the initialization, it will output its in-memory database of attendees in JSON-Verbose. It doesn't respond to or expect requests in that format.

And test it in other terminal. Get list of all attendees:

curl -s localhost:3000/attendees
[[
  "^ ",
  "~:name", "Filip Zrůst",
  "~:email", "~rmailto:[email protected]"
], [
  "^ ",
  "^0", "Stojan Jakotyč",
  "^1", "~rmailto:[email protected]"
]]

See how plain JavaScript object (i.e., map Transit semantic type) gets encoded to JSON array element with the "^ " tag indicating that it is in fact a map. You can also scalar elements with : and r tags for keyword and URI respectively.

Also notice how caching works for map keys.

Don't forget to kill the server when moving to the next step.

Note: This step doesn't properly support create and update actions. This is because co-body should be replaced with Transit reader.

Custom types in action

Now with custom composite type writer for Attendee record:

git checkout step-3-custom

Compare changes from step 2 to step 3. and try it yourself. Run the app in one terminal:

npm start

And test it in other terminal. Get list of all attendees:

curl -s localhost:3000/attendees
[[
  "~#att", [
    "^ ",
    "~:name", "Filip Zrůst",
    "~:email", "~rmailto:[email protected]"
  ]
], [
  "^0", [
    "^ ",
    "^1", "Stojan Jakotyč",
    "^2", "~rmailto:[email protected]"
  ]
]]

See the custom tag att for composite element and that even the tag gets cached (if it is longer than three characters).

Don't forget to kill the server when moving to the next step.

Note: This step doesn't properly support create and update actions. This is because co-body should be replaced with Transit reader.

Add and modify attendees

Now with custom composite type reader for Attendee record:

git checkout step-4-reader

Compare changes from step 3 to step 4. and try it yourself. Run the app in one terminal:

npm install
npm prune
npm start

And test it in other terminal. Get list of all attendees:

curl -s localhost:3000/attendees
[[
  "~#att", [
    "^ ",
    "~:name", "Filip Zrůst",
    "~:email", "~rmailto:[email protected]"
  ]
], [
  "^0", [
    "^ ",
    "^1", "Stojan Jakotyč",
    "^2", "~rmailto:[email protected]"
  ]
]]

Add a new attendee:

curl -i -H 'Content-Type: application/transit+json' -d '[ "~#att", [ "^ ", "~:name", "Tomáš Marný", "~:email", "~rmailto:[email protected]" ] ]' localhost:3000/attendees
HTTP/1.1 201 Created
Location: /attendees/2

Get list of all attendees again:

curl -s localhost:3000/attendees
[[
  "~#att", [
    "^ ",
    "~:name", "Filip Zrůst",
    "~:email", "~rmailto:[email protected]"
  ]
], [
  "^0", [
    "^ ",
    "^1", "Stojan Jakotyč",
    "^2", "~rmailto:[email protected]"
  ]
], [
  "^0", [
    "^ ",
    "^1", "Tomáš Marný",
    "^2", "~rmailto:[email protected]"
  ]
]]

Don't forget to kill the server when leaving…

About

An example app to show Transit.

Resources

License

Stars

Watchers

Forks

Packages

No packages published