Upgrade to Pro — share decks privately, control downloads, hide ads and more …

#TDC2015: Know hook and how to use it

#TDC2015: Know hook and how to use it

My speech about hook at The Developers Conference 2015, Brazil (Front-end track)

https://github.com/doubleleft/hook

Endel Dreyer

July 21, 2015
Tweet

More Decks by Endel Dreyer

Other Decks in Programming

Transcript

  1. The problem • Projets with short delivery time (<1 month)

    • Repetitive back-end code • Lack of front-end integration standards
  2. Backend as a Service • baasbox/baasbox - Java • deployd/deployd

    - NodeJS / MongoDB • hoodiehq/hoodie.js - NodeJS / CouchDB • parse.com - Closed source / $$$$$ • doubleleft/hook - PHP / MySQL
  3. hook • REST API • Data persistance (CRUD) • MySQL

    / PostgreSQL / MSSQL / MongoDB • User authentication • Extensible
  4. Installation • Dependencies: PHP 5.5+ / node 0.12+ • github.com/doubleleft/hook

    $ curl -sSL https:// raw.githubusercontent.com/ doubleleft/hook/master/scripts/ install.sh | bash
  5. Static Web Apps • “Static Web Applications run independently of

    the need for server-side dynamic processing.” • “Static web architecture eases common web development headaches without introducing additional complexity.” • http://www.staticapps.org/
  6. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully.
  7. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Collection’s exposure level
  8. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Back-end dependencies (Composer)
  9. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Crontab
  10. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Database migration
  11. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Back-end configurations (smtp, oauth, etc)
  12. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Configurations per environment (“development”)
  13. Getting started $ hook app:new tdc2015 create hook-ext/security.yaml create hook-ext/packages.yaml

    create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.development.yaml create hook-ext/credentials/development/cli.json create hook-ext/credentials/development/browser.json create hook-ext/credentials/development/device.json create hook-ext/credentials/development/server.json Application created successfully. Application credentials (“development”)
  14. Getting started $ hook app:new tdc2015 —environment production create hook-ext/security.yaml

    create hook-ext/packages.yaml create hook-ext/schedule.yaml create hook-ext/schema.yaml create hook-ext/config/config.yaml create hook-ext/config/config.production.yaml create hook-ext/credentials/production/cli.json create hook-ext/credentials/production/browser.json create hook-ext/credentials/production/device.json create hook-ext/credentials/production/server.json Application created successfully. Environment-specific files (“production”)
  15. App Credentials • cli - deploy, seed, full-access • browser

    - for web applications • device - for mobile applications • server - for server-to-server communication
  16. App Credentials • cli - deploy, seed, full-access • browser

    - para uso em webapps • device - para uso em mobiles • server - para comunicação server-to-server KEEP IT SAFE!!!!!1111
  17. JavaScript Client var hook = new Hook.Client({ endpoint: process.env.HOOK_ENDPOINT, app_id:

    process.env.HOOK_APP_ID, key: process.env.HOOK_BROWSER_KEY });
  18. Collections • Database table • Typed attributes • Automatic migrations

    • File upload • Security rules (security.yaml)
  19. collection().create hook.collection('books').create({ name: "JavaScript: The Good Parts", author: "Douglas Crockford"

    }).then(function(book) { console.log(book._id, book.name) console.log(book.author) console.log(book.created_at) }).otherwise(function(error) { console.log(error) })
  20. collection().create hook.collection('books').create({ name: "JavaScript: The Good Parts", author: "Douglas Crockford"

    }).then(function(book) { console.log(book._id, book.name) console.log(book.author) console.log(book.created_at) }).otherwise(function(error) { console.log(error) }) Success
  21. collection().create hook.collection('books').create({ name: "JavaScript: The Good Parts", author: "Douglas Crockford"

    }).then(function(book) { console.log(book._id, book.name) console.log(book.author) console.log(book.created_at) }).otherwise(function(error) { console.log(error) }) Error
  22. collection().then hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). then(function(rows) {

    console.log(rows.length) // => 1 console.log(rows[0].name) // => "JavaScript: The Good Parts" })
  23. collection().then hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). then(function(rows) {

    console.log(rows.length) // => 1 console.log(rows[0].name) // => "JavaScript: The Good Parts" }) Array
  24. collection().first hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). first(function(row) {

    console.log(row.name) // => "JavaScript: The Good Parts" console.log(row._id) // => 1 }) Object
  25. collection().update hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). update({ isbn:

    "978-0-596-51774-8" }). then(function(data) { console.log(data.success) // => true console.log(data.affected) // => 1 })
  26. hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). update({ isbn: "978-0-596-51774-8"

    }). then(function(data) { console.log(data.success) // => true console.log(data.affected) // => 1 }) collection().update
  27. hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). update({ isbn: "978-0-596-51774-8"

    }). then(function(data) { console.log(data.success) // => true console.log(data.affected) // => 1 }) collection().update hook-ext/security.yaml
  28. hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). update({ isbn: "978-0-596-51774-8"

    }). then(function(data) { console.log(data.success) // => true console.log(data.affected) // => 1 }) collection().update Object
  29. Schema • Database definitions / migrations • Collection data types

    • Collection relationships • Serves as documentation
  30. auth.login() hook.auth.login({ email: "[email protected]", password: "not my password" }).then(function(auth) {

    console.log("Logged in!", auth) }).otherwise(function(data) { console.log(data.error) })
  31. OAuth • JavaScript Plugin • 3rd party service configuration (hook-ext/

    config.yaml) • Dependency (hook-ext/packages.yaml) hook.oauth.popup('facebook').then(function(auth) { console.log("Logged in!", auth) }).otherwise(function(data) { console.log(data.error) })
  32. Collection observers • toArray • creating / created • updating

    / updated • saving / saved • deleting / deleted
  33. Observers $ hook generate:observer auth <?php class Auths { public

    function created($model) { $message = Mail::message('Welcome ' . $model->name); $message->from(Config::get('email.from')) ->to($model->email) ->subject("Welcome") ->send(); } }
  34. Observers $ hook generate:observer auth <?php class Auths { public

    function toArray($model, $array) { unset($array['endereco']); unset($array['cpf']); return $array; } public function creating($model) { $message = Mail::message('Welcome ' . $model->name);