Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Databases and NodeJS

300 views

Published on

Mini Hacktiv8 workshop introducing database with NodeJS.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Databases and NodeJS

  1. 1. DATABASES NODEJS
  2. 2. BEFORE WE BEGIN Logistics Slides: slideshare.net/rizafahmi Subscribe: youtube, facebook live Feedback:
 * https://www.youtube.com/c/hacktiv8 * https://www.facebook.com/hacktiv8id
  3. 3. AGENDA ✅ Whatisdatabase ✅ Typesofdatabase ✅ Commondatatypes ✅ Databaseoperations ✅ ObjectRelationalMapping ✅ Buildsomethingwithdatabase
  4. 4. WAYS TO STORE AND RETRIEVE DATA Storedatainglobalvariable [{ "id": 1, "image": "/img/1-jlol_sp_ed_bb-8_sphero_force_band.jpg", "description": "The Force is strong with you. Channel it with the Special Edition Battle-Worn BB-8 and included Star Wars Force Band by Sphero. Like a Jedi Knight, you can control your BB-8 App-Enabled Droid with just a wave of your hand and expand your abilities with Force Training." }, { "id": 2, "image": "/img/ilhi_3d_sw_kylo_ren.gif", "description": "The Dark Side is a very turbulent place. Sure they have cookies, but there are nooks and crannies in which darkness reigns so supreme, if you merely enter, you're likely to stub your toe." }]
  5. 5. WAYS TO STORE AND RETRIEVE DATA Storedatainglobalvariable PROS CONS SUPER FAST EASY RESIDES IN MEMORY LOSE DATA WHEN RESTART
  6. 6. WAYS TO STORE AND RETRIEVE DATA Storedatainafile const fs = require('fs') app.post('/users', function (req, res) { const user = req.body fs.appendFile('users.txt', JSON.stringify({ name: user.name, age: user.age }), (err) => { res.send('successfully registered') }) })
  7. 7. WAYS TO STORE AND RETRIEVE DATA Storedatainafile PROS CONS RESIDES IN STORAGE INFLEXIBLE ONE USER AT A TIME CHEAPER DISTRIBUTION PROBLEM
  8. 8. WHAT IS DATABASE “A WAY TO STORE AND RETRIEVE DATA”
  9. 9. WHAT IS DATABASE DatabaseTypes RELATIONAL DATABASE NON RELATIONAL DATABASE SQL NOSQL
  10. 10. WHAT IS DATABASE RelationalDatabase/SQL Store in tables with rows and columns We define a schema
  11. 11. WHAT IS DATABASE Examples
  12. 12. WHAT IS DATABASE NonRelationalDatabase/NoSQL No schema Free Form Database
  13. 13. WHAT IS DATABASE Key-value store Graph databases
  14. 14. WHAT IS DATABASE Column databases Document databases
  15. 15. RELATIONAL DATABASE
  16. 16. DATA TYPES Integer Real Text Blob 2 71839 -38.2822 48 45e18 Hacktiv8 c 1
  17. 17. DATA TYPES PostgreSQLDataTypes
  18. 18. DATABASEOPERATIONSStructuredQueryLanguage
  19. 19. Data Definition Language Data Manipulation Language
  20. 20. RELATIONAL DATABASE DataDefinitionLanguage /* Define a table schema */ CREATE TABLE items( id INTEGER PRIMARY KEY AUTOINCREMENT, image VARCHAR(100), description TEXT); /* Change a table schema */ ALTER TABLE items ADD qty INTEGER; ALTER TABLE items RENAME TO barang; /* Delete a table */ DROP TABLE users;
  21. 21. RELATIONAL DATABASE DataManipulationLanguage /* Insert a data */ INSERT INTO items (id, image, description) VALUES (1, "/img/1-jlol_sp_ed_bb- 8_sphero_force_band.jpg", "New item for today."); /* Read data */ SELECT * FROM items; /* Change data */ UPDATE items SET description=“Hacktivcash items” /* Remove data */ DELETE FROM items WHERE id=1; CRU D
  22. 22. DATABASEOPERATIONS
  23. 23. NODEJS DataDefinitionLanguage const sqlite3 = require('sqlite3').verbose() const db = new sqlite3.Database('./data/try.db') const CREATE_TABLE = `CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(100), password TEXT);` const ALTER_TABLE = `ALTER TABLE users ADD dateCreated DATE;` const DROP_TABLE = `DROP TABLE users;` db.serialize(() => { db.run(CREATE_TABLE) db.run(ALTER_TABLE) db.run(DROP_TABLE) }) db.close()
  24. 24. NODEJS DataManipulationLanguage const sqlite3 = require('sqlite3').verbose() const db = new sqlite3.Database(‘./data/try.db') const CREATE_TABLE = `CREATE TABLE IF NOT EXISTS items(id INTEGER PRIMARY KEY AUTOINCREMENT, image VARCHAR(100), description TEXT);` const DROP_TABLE = `DROP TABLE IF EXISTS items;` const INSERT_ITEM = `INSERT INTO items (id, image, description) VALUES (1, "/img/1- jlol_sp_ed_bb-8_sphero_force_band.jpg", "abc");` const SELECT_ITEM = `SELECT * FROM items;` const UPDATE_ITEM = `UPDATE items SET description='HACKTIVCAST new items'` const DELETE_ITEM = `DELETE FROM items WHERE id=1;`
  25. 25. NODEJS DataManipulationLanguage db.serialize(() => { db.run(DROP_TABLE) db.run(CREATE_TABLE) db.run(INSERT_ITEM) db.each(SELECT_ITEM, (err, row) => { console.log(row.id, ': ', row.description) }) db.run(UPDATE_ITEM) db.each(SELECT_ITEM, (err, row) => { console.log(row.id, ': ', row.description) }) db.run(DELETE_ITEM) db.each(SELECT_ITEM, (err, row) => { console.log('After DELETE: ', row.id, ': ', row.description) }) }) db.close()
  26. 26. OBJECT-RELATIONAL MAPPING
  27. 27. “Techniquethatletsyouqueryandmanipulatedatafroma relationaldatabaseusinganobject-orientedparadigm.”
  28. 28. GetEmployeeAddressesWithSQL SELECT name, city, street, state FROM Employee LEFT JOIN Address ON (Employee.address_id = Address.id)
  29. 29. GetEmployeeAddressesWithORM employee.getDetailAddress()
  30. 30. ORMSTANDARDFEATURES
  31. 31. MAPPINGTABLESTO OBJECTS
  32. 32. CRUDFunctionality
  33. 33. EXECUTINGCUSTOM QUERIES
  34. 34. OTHERFEATURES Seeding Data Migration Flexibility Data Validation Secure
  35. 35. LIBRARIES ORMForNodeJS
  36. 36. SEQUELIZE
  37. 37. “SEQUELIZE IS A PROMISE-BASED ORM FOR NODE.JS V4 AND UP.” “IT ENABLES JS DEVELOPERS TO WORK WITH RELATIONAL DATA MORE EASILY.” -- ALEX BOOKER
  38. 38. SEQUELIZE Benefits Secure Less Code Abstraction No SQL Tools
  39. 39. SEQUELIZE Limitations Can be slow Extra Learning Curve Documentation

×