Share
Embed
#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
Transcript
107 slides
-
The problem • Projets with short delivery time (<1 month)
• Repetitive back-end code • Lack of front-end integration standards -
Backend as a Service • baasbox/baasbox - Java • deployd/deployd
- NodeJS / MongoDB • hoodiehq/hoodie.js - NodeJS / CouchDB • parse.com - Closed source / $$$$$ • doubleleft/hook - PHP / MySQL -
hook • REST API • Data persistance (CRUD) • MySQL
/ PostgreSQL / MSSQL / MongoDB • User authentication • Extensible -
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 -
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/ -
Getting started • Back-end (doubleleft/hook) • Command-line Interface (doubleleft/hook- cli)
• Front-end (doubleleft/hook-javascript) -
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. -
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 -
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) -
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 -
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 -
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) -
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”) -
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”) -
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”) -
App Credentials • cli - deploy, seed, full-access • browser
- for web applications • device - for mobile applications • server - for server-to-server communication -
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 -
JavaScript Client var hook = new Hook.Client({ endpoint: "http://localhost:4665/", app_id:
1, key: "c2cee0f2575e3547315e123ddfbe8029" }); -
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 }); -
Collections • Database table • Typed attributes • Automatic migrations
• File upload • Security rules (security.yaml) -
Collections API • #create • #where • #update • #remove
• #then / #first -
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) }) -
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 -
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 -
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" }) -
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 -
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 }) -
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 -
hook.collection('books'). where("author", "Douglas Crockford"). where("name", "like", "%javascript%"). remove(function(data) { console.log(data.success)
// => 1 }) collection().remove Object -
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 }) -
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.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 -
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 -
Schema • Database definitions / migrations • Collection data types
• Collection relationships • Serves as documentation -
schema.yaml Relationships: - belongs_to - has_one - has_many - has_many
(though) View the documentation! -
Authentication API • #register • #login • #logout • #update
• #currentUser -
auth.register() hook.auth.register({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Registered!", auth)
}).otherwise(function(data) { console.log(data.error) }) -
auth.register() hook.auth.register({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Registered!", auth)
}).otherwise(function(data) { console.log(data.error) }) Object -
auth.register() hook.auth.register({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Registered!", auth)
}).otherwise(function(data) { console.log(data.error) }) Object -
auth.register() hook.auth.register({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Registered!", auth)
}).otherwise(function(data) { console.log(data.error) }) -
auth.register() hook.auth.register({ email: "[email protected]", password: "" }).then(function(auth) { console.log("Registered!", auth)
}).otherwise(function(data) { console.log(data.error) }) -
auth.login() hook.auth.login({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Logged in!",
auth) }).otherwise(function(data) { console.log(data.error) }) -
auth.login() hook.auth.login({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Logged in!",
auth) }).otherwise(function(data) { console.log(data.error) }) Object -
auth.login() hook.auth.login({ email: "[email protected]", password: "1234" }).then(function(auth) { console.log("Logged in!",
auth) }).otherwise(function(data) { console.log(data.error) }) Object -
auth.login() hook.auth.login({ email: "[email protected]", password: "12345678" }).then(function(auth) { console.log("Logged in!",
auth) }).otherwise(function(data) { console.log(data.error) }) -
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) }) -
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) }) -
Extending the back- end • Routes • Observers • Schedule
• Composer packages -
Routes $ hook generate:route last_posts ... Route created at ‘hook-ext/routes/
get_last_posts.php'. -
Collection observers • toArray • creating / created • updating
/ updated • saving / saved • deleting / deleted -
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(); } } -
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); -
Clients • JavaScript • Corona SDK • C# (Unity) •
Android • C++ • …