• I’m Here to Tell You a Story

  • Using the Power of Javascript
    in the database
    (to build a database)

  • WAT?

  • No.

    Really.

  • Prerequisites

    • PostgreSQL
    • PLV8
  • PLV8?

    That’s not Node!

  • But it is Javascript

    PostgreSQL has stored procedures

    CREATE OR REPLACE FUNCTION myCoolThing (arg1 VARCHAR, arg2 INT) RETURNS
    SETOF JSON AS $$
    
    return [ { "first": arg1, "second": arg2 ];
    
    $$ LANGUAGE plv8 STRICT;
  • Let’s Try It!

    // createdb pdxnode
    // psql pdxnode
    // create extension plv8
    // copy/paste
    // SELECT myCoolThing(‘abc’, 123);

  • This is POWER!

    // we’ve been given the most awesome database in the world
    // and we can do anything!

  • So I Built a Database

{"cards":[{"_id":"3d21209cd67fc38593000027","treeId":"3d212087d67fc38593000025","seq":1,"position":1,"parentId":null,"content":"# I'm Here to Tell You a Story\n\n\n"},{"_id":"3d21211ad67fc38593000028","treeId":"3d212087d67fc38593000025","seq":1,"position":1,"parentId":"3d21209cd67fc38593000027","content":"Using the Power of Javascript\nin the database\n(to build a database)\n"},{"_id":"3d217d3cd67fc38593000029","treeId":"3d212087d67fc38593000025","seq":1,"position":1,"parentId":"3d21211ad67fc38593000028","content":"# WAT?"},{"_id":"3d218b7ad67fc3859300002a","treeId":"3d212087d67fc38593000025","seq":1,"position":2,"parentId":"3d21211ad67fc38593000028","content":"# No. \n# Really."},{"_id":"3d2202fdd67fc38593000030","treeId":"3d212087d67fc38593000025","seq":1,"position":2,"parentId":"3d218b7ad67fc3859300002a","content":"# Prerequisites\n* PostgreSQL\n* PLV8\n"},{"_id":"3d21a688d67fc3859300002c","treeId":"3d212087d67fc38593000025","seq":1,"position":3,"parentId":"3d218b7ad67fc3859300002a","content":""},{"_id":"3d47c937d67fc38593000033","treeId":"3d212087d67fc38593000025","seq":1,"position":1.5,"parentId":"3d21a688d67fc3859300002c","content":"# PLV8?\n## That's not Node!\n\n"},{"_id":"3d484c44d67fc3859300003a","treeId":"3d212087d67fc38593000025","seq":1,"position":1,"parentId":"3d47c937d67fc38593000033","content":"# So I Built a Database"},{"_id":"3d47d207d67fc38593000035","treeId":"3d212087d67fc38593000025","seq":1,"position":3,"parentId":"3d21a688d67fc3859300002c","content":"# But it is Javascript\n\nPostgreSQL has stored procedures\n\n```\nCREATE OR REPLACE FUNCTION myCoolThing (arg1 VARCHAR, arg2 INT) RETURNS\nSETOF JSON AS $$\n\nreturn [ { \"first\": arg1, \"second\": arg2 ];\n\n$$ LANGUAGE plv8 STRICT;\n```\n"},{"_id":"3d4807c8d67fc38593000038","treeId":"3d212087d67fc38593000025","seq":1,"position":4,"parentId":"3d21a688d67fc3859300002c","content":"# Let's Try It!\n\n// createdb pdxnode\n// psql pdxnode\n// create extension plv8\n// copy/paste\n// SELECT myCoolThing('abc', 123);\n"},{"_id":"3d483744d67fc38593000039","treeId":"3d212087d67fc38593000025","seq":1,"position":5,"parentId":"3d21a688d67fc3859300002c","content":"# This is POWER!\n\n// we've been given the most awesome database in the world\n// and we can do anything!"}],"tree":{"_id":"3d212087d67fc38593000025","name":"MongoLike","publicUrl":"mongo-like"}}