databases and nodejs
TRANSCRIPT
![Page 1: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/1.jpg)
DATABASESNODEJS
![Page 2: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/2.jpg)
BEFORE WE BEGIN
Logistics
Slides: slideshare.net/rizafahmi
Subscribe: youtube, facebook live
Feedback:* https://www.youtube.com/c/hacktiv8
* https://www.facebook.com/hacktiv8id
![Page 3: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/3.jpg)
AGENDA
✅ What is database
✅ Types of database
✅ Common data types
✅ Database operations
✅ Object Relational Mapping
✅ Build something with database
![Page 4: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/4.jpg)
WAYS TO STORE AND RETRIEVE DATA
Store data in global variable[{ "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." }]
![Page 5: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/5.jpg)
WAYS TO STORE AND RETRIEVE DATA
Store data in global variable
PROS CONS
SUPER FAST
EASY
RESIDES IN MEMORY
LOSE DATA WHEN RESTART
![Page 6: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/6.jpg)
WAYS TO STORE AND RETRIEVE DATA
Store data in a file
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') }) })
![Page 7: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/7.jpg)
WAYS TO STORE AND RETRIEVE DATA
Store data in a file
PROS CONS
RESIDES IN STORAGEINFLEXIBLE ONE USER AT A
TIME
CHEAPER
DISTRIBUTION PROBLEM
![Page 8: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/8.jpg)
WHAT IS DATABASE
“A WAY TO STORE AND RETRIEVE DATA”
![Page 9: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/9.jpg)
WHAT IS DATABASE
Database Types
RELATIONAL DATABASE NON RELATIONAL DATABASE
SQL NOSQL
![Page 10: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/10.jpg)
WHAT IS DATABASE
Relational Database / SQL
Store in tables with rows and columns
We define a schema
![Page 11: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/11.jpg)
WHAT IS DATABASE
Examples
![Page 12: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/12.jpg)
WHAT IS DATABASE
Non Relational Database / NoSQL
No schema
Free Form Database
![Page 13: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/13.jpg)
WHAT IS DATABASE
Key-value store
Graph databases
![Page 14: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/14.jpg)
WHAT IS DATABASE
Column databases
Document databases
![Page 15: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/15.jpg)
RELATIONAL DATABASE
![Page 16: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/16.jpg)
DATA TYPES
Integer
Real
Text
Blob
2
71839
-38.2822
48
45e18
Hacktiv8
c
1
![Page 17: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/17.jpg)
DATA TYPES
PostgreSQL Data Types
![Page 18: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/18.jpg)
DATABASE OPERATIONSStructured Query Language
![Page 19: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/19.jpg)
Data Definition Language
Data Manipulation Language
![Page 20: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/20.jpg)
RELATIONAL DATABASE
Data Definition Language
/* 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;
![Page 21: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/21.jpg)
RELATIONAL DATABASE
Data Manipulation Language /* 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; CR
UD
![Page 22: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/22.jpg)
DATABASE OPERATIONS
![Page 23: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/23.jpg)
NODEJS
Data Definition Language
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()
![Page 24: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/24.jpg)
NODEJS
Data Manipulation Language
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;`
![Page 25: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/25.jpg)
NODEJS
Data Manipulation Languagedb.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()
![Page 26: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/26.jpg)
OBJECT-RELATIONAL MAPPING
![Page 27: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/27.jpg)
“Technique that lets you query and manipulate data from a relational database using an object-oriented paradigm.”
![Page 28: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/28.jpg)
Get Employee Addresses With SQL
SELECT name, city, street, state FROM Employee LEFT JOIN Address ON (Employee.address_id = Address.id)
![Page 29: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/29.jpg)
Get Employee Addresses With ORM
employee.getDetailAddress()
![Page 30: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/30.jpg)
ORM STANDARD FEATURES
![Page 31: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/31.jpg)
MAPPING TABLES TO OBJECTS
![Page 32: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/32.jpg)
CRUD Functionality
![Page 33: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/33.jpg)
EXECUTING CUSTOM QUERIES
![Page 34: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/34.jpg)
OTHER FEATURES
Seeding Data MigrationFlexibility
Data Validation Secure
![Page 35: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/35.jpg)
LIBRARIES
ORM For NodeJS
![Page 36: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/36.jpg)
SEQUELIZE
![Page 37: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/37.jpg)
“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
![Page 38: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/38.jpg)
![Page 39: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/39.jpg)
SEQUELIZE
Benefits
Secure
Less Code Abstraction No SQL
Tools
![Page 40: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/40.jpg)
SEQUELIZE
Limitations
Can be slow Extra Learning Curve
Documentation
![Page 41: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/41.jpg)
![Page 42: Databases and NodeJS](https://reader031.vdocuments.net/reader031/viewer/2022021922/5a6580117f8b9af3678b5231/html5/thumbnails/42.jpg)