SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Insert Picture Here
1
Developing for Node.JS with
MySQL and NoSQL
 J.D. Duncan, john.duncan@oracle.com
 Craig Russell, craig.russell@oracle.com
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2
MySQL Cluster Architecture
MySQL Cluster Data Nodes
Data Layer
Clients
Application Layer
Management
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3
MySQL Cluster Architecture
MySQL Cluster Data Nodes
Data Layer
Clients
Application Layer
Management
Management
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4
MySQL Cluster Architecture
MySQL Cluster Data Nodes
Data Layer
Application Layer
Management
Management
Clients
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5
mysql-js: a MySQL connector for Node.JS
 Included in MySQL Cluster 7.3
 in storage/ndb/nodejs
 Also at github:
 https://github.com/mysql/mysql-js
 One JavaScript API with two back-end adapters
 The MySQL back end uses node-mysql (Felix Geisendorfer's all-
JavaScript MySQL connector)
 The NDB (MySQL Cluster) back end uses native NDBAPI
 Twitter-like demo application in samples/tweet/
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6
What is Node.JS
 A server-side web application platform
 Single-threaded* and event-driven
 Application code is written in JavaScript
 JavaScript code runs in a very fast VM
 Node.JS includes a useful set of standard libraries
* One JavaScript thread;
a pool of background worker threads perform blocking I/O.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7
Why Node.JS
 Servers like lighttpd, nginx, and tornado can solve the C10K problem
for static or cached content
 Node.JS can solve the C10K problem for a dynamic web application
– ... but what about 10,000 back-end database connections?
 mysql-js with NDB aims to solve the C10K problem for a dynamic and
database-driven web application
The "C10K" problem: how to handle 10,000+ client connections
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.8
Sample Code
Node HTTP Server
function handleRequest(request, response) {
var page = getContent(request.url);
response.statusCode = page ? 200 : 404;
response.write(page);
response.end();
}
function startWebServer() {
http.createServer(handleRequest).listen(8080);
console.log("Server started");
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.9
Sample Code with database access
Node HTTP Server and mysql-js
function dbConnected(connectionError, sessionFactory) {
function handleRequest(request, response) {
function onDbResponse(err, data) {
var page = renderPage(data);
response.write(page);
response.end();
}
function onDbSession(err, session) {
runDbOperation(session, request, onDbResponse);
}
sessionFactory.openSession(null, onDbSession);
}
http.createServer(handleRequest).listen(8080);
}
nosql.connect(... , dbConnected);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
User's View
10
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Twitter-like Demo Application
11
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Twitter-like Demo Application
12
 Create user
 Find user by name
 Delete user by name
(cascading)
 Create a tweet
 Find tweet by id
 Delete tweet by id (cascading)
 Make User A a follower of
User B
 List followers of user
 List who user is following
 Get the most recent tweets
 Get the 20 latest tweets by a
user
 Get the 20 latest tweets
@mentioning a user
 Get the 20 latest tweets
containing a #hashtag
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.13
JavaScript Constructor
function Tweet(author, message) {
if(author !== undefined) {
this.date_created = new Date();
this.author = author;
this.message = message;
}
}
Creates an instance of a Domain Object
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.14
SQL Table
CREATE TABLE tweet (
id bigint unsigned auto_increment not null,
author varchar(20),
message varchar(140),
date_created timestamp(2),
...
)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.15
Mapping
var nosql = require("../mysql-js");
// Map SQL Tables to JS Constructors
var mappings = [];
mappings.push(
new nosql.TableMapping('tweet').
applyToClass(Tweet)
);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.16
Explicit Mappings are not required
 All operations take either a constructor or table name
 If table name is used, default mapping is created
 Default mapping maps all columns to JS properties, where the JS type
of each property is the default for its corresponding SQL Data Type
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.17
connect function
 connect is asynchronous
 First parameter is object with connection properties
 Second parameter is array of table names or constructors
– Metadata for these will be resolved before returning session factory
 Third parameter is callback (err, sessionFactory)
 err will be falsy if no error
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18
connect Example
/* Connection Properties */
dbProperties = {
adapter : "ndb",
ndb_connectstring : "localhost:1186"
};
// Map SQL Tables to JS Constructors
mappings = [];
mappings.push(new nosql.TableMapping('tweet').
applyToClass(Tweet));
// Connect
nosql.connect(dbProperties, mappings,
runCmdlineOperation, operation);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19
The connect callback
function dbConnected(connectionError, sessionFactory) {
function handleRequest(request, response) {
function onDbResponse(err, data) {
var page = renderPage(data);
response.write(page);
response.end();
}
function onDbSession(err, session) {
runDbOperation(session, request, onDbResponse);
}
sessionFactory.openSession(null, onDbSession);
}
http.createServer(handleRequest).listen(8080);
}
nosql.connect(dbProperties, mappings, dbConnected);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.21
find Example
function findAuthor(name, callback) {
session.find(Author, name, function(err, result) {
if (err) {
callback(err);
} else {
// result is the row in the author table
callback(null, result);
});
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.22
find function on Session
 find is asynchronous
 First parameter is table name or constructor (mapped class)
 Second parameter is key to use locating the record
– Primary key
– Unique key
 Third parameter is callback (err, result)
– err will be falsy if no error
– result will be new object with mapped properties
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23
persist Example
function persistAuthor(name, full, callback) {
var author = new Author(name, full);
session.persist(author, function(err) {
if (err) {
callback(err);
} else {
callback(null, author);
}
});
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.24
persist function on Session
 persist is asynchronous
 First parameter is instance of mapped class
 Second parameter callback (err)
– err will be falsy if no error
 Variants allow persist into default mapped table (no constructor)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.25
update Example
function updateAuthor(author, new_full_name,
callback) {
author.full_name = new_full_name;
session.update(author, callback);
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.26
update function on Session
 update is asynchronous
 First parameter is instance of mapped class
– Modified properties are written to database
 Second parameter callback (err)
– err will be falsy if no error
 Variants allow update of default mapped table (no constructor)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.27
remove Example
function removeByAuthorName(name, callback) {
session.remove(Author, name, function(err, result)
{
if (err) {
callback(err);
} else {
// row has been removed
callback(null);
});
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28
remove function on Session
 remove is asynchronous
– First parameter is table name or constructor (mapped class)
– Second parameter is key to use locating the record
 Primary key
 Unique key
– Third parameter is callback (err)
 err will be falsy if no error
 Alternately
– First parameter is instance of constructor
– Second parameter is callback (err)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.29
Transactions
/* Insert a tweet.
- Start a transaction.
- Persist the tweet.
- Create & persist #hashtag & @user records
- Increment the author's tweet count.
- Then commit the transaction.
*/
function InsertTweetOperation(params, data) {
session.currentTransaction().begin();
...
session.currentTransaction().commit();
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.30
Query Example
// Last 20 tweets @user
TweetsAtUserOperation.run = function(tag) {
if(tag.charAt(0) == "@") tag = tag.substring(1);
this.session.createQuery(Mention,
function(error, query) {
var queryParams = { "order" : "desc",
"limit" : 20 ,
"tag" : tag};
query.where(query.at_user.eq(query.param("tag")));
query.execute(queryParams, fetchTweetsInBatch);
});
};
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.31
query function
 Session is Query factory using asynchronous api
 query has a filter based on constructor (mapped class) properties
 Filter has comparators and boolean operations
– eq, ne, gt, ge, le, lt, between,isNull,isNotNull comparators
– and, or, not,andNot,orNot operations
 Query execution is asynchronous
– Filter determines query strategy
 Primary/unique key lookup; index scan; table scan
– Properties govern query execution
– Results are given in callback
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.32
Query Comparators
 Comparators compare properties to parameters
 Query Domain Type property names correspond to Constructor field
names (properties)
 Parameters are created by name
– qdt.param('date_low')
 Properties are referenced by field name in Constructor
– qdt.date_created
 Comparators are properties of qdt properties
– qdt.date_created.gt(qdt.param('date_low'));
 Comparators return predicates
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.33
Query Operators
 Predicates are used as query filters via where function
 Predicates are results of comparators or operators
 Operators combine predicates using and, or, andNot, orNot, not
– var predicate1 =
qdt.date_created.gt(qdt.param('date_low'));
– var predicate2 =
qdt.date_created.lt(qdt.param('date_high'));
– var predicate = predicate1.and(predicate2);
– var predicate = predicate3.andNot(predicate4);
– qdt.where(predicate)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.34
Batching
function fetchTweetsInBatch(error, mentions,
finalCallback) {
var resultData = [];
function addTweetToResults(err, tweet) {
if(tweet && ! err) resultData.push(tweet);
}
var batch = session.createBatch();
while(var mention = mentions.pop());
batch.find(Tweet, mention.id, addTweetToResults);
batch.execute(finalCallback, resultData);
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.35
Batching
 Batching allows parallel database operations
– Any combination of find, persist, update, remove
 Batch is created using session.createBatch()
 Operations (with their own callbacks) are added to the batch
 Batch is executed with its own callback
– Operation callbacks are run first
– Then the batch callback
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.36
Get mysql-js & run the Tweet demo
### First install MySQL Cluster 7.3
### Then get the latest code from github:
% git clone https://github.com/mysql/mysql-js.git
### build it
% node configure.js
% node-gyp configure build
### Go to the demo directory
% cd samples/tweet
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.37
Get mysql-js & run the Tweet demo
### create the schema
% mysql -u root < tweet.sql
### Run with the mysql adapter
% node tweet.js -a mysql put user mr_jdd 
'John David Duncan'
### Run with the ndb adapter
% node tweet.js -a ndb get user mr_jdd
### Look at some more examples
% cat test_tweet.sh

Contenu connexe

Tendances

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
Database Cloud Services Office Hours : Oracle sharding hyperscale globally d...
Database Cloud Services Office Hours : Oracle sharding  hyperscale globally d...Database Cloud Services Office Hours : Oracle sharding  hyperscale globally d...
Database Cloud Services Office Hours : Oracle sharding hyperscale globally d...Tammy Bednar
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJSAll Things Open
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use casesFabio Biondi
 
What is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | EdurekaWhat is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | EdurekaEdureka!
 
Modificateurs d'accès en java
Modificateurs d'accès en javaModificateurs d'accès en java
Modificateurs d'accès en javaMohamed Bah
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Hibernate
HibernateHibernate
HibernateAjay K
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WSKatrien Verbert
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
Oracle REST Data Services: Options for your Web Services
Oracle REST Data Services: Options for your Web ServicesOracle REST Data Services: Options for your Web Services
Oracle REST Data Services: Options for your Web ServicesJeff Smith
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 
Resource Bundle
Resource BundleResource Bundle
Resource BundleSunil OS
 

Tendances (20)

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
 
Database Cloud Services Office Hours : Oracle sharding hyperscale globally d...
Database Cloud Services Office Hours : Oracle sharding  hyperscale globally d...Database Cloud Services Office Hours : Oracle sharding  hyperscale globally d...
Database Cloud Services Office Hours : Oracle sharding hyperscale globally d...
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
What is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | EdurekaWhat is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | Edureka
 
Modificateurs d'accès en java
Modificateurs d'accès en javaModificateurs d'accès en java
Modificateurs d'accès en java
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
React js
React jsReact js
React js
 
Hibernate
HibernateHibernate
Hibernate
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WS
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Oracle REST Data Services: Options for your Web Services
Oracle REST Data Services: Options for your Web ServicesOracle REST Data Services: Options for your Web Services
Oracle REST Data Services: Options for your Web Services
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Resource Bundle
Resource BundleResource Bundle
Resource Bundle
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 

Similaire à Developing for Node.JS with MySQL and NoSQL

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
GraphQL & Prisma from Scratch
GraphQL & Prisma from ScratchGraphQL & Prisma from Scratch
GraphQL & Prisma from ScratchNikolas Burk
 
Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleRaimonds Simanovskis
 
Database access and JDBC
Database access and JDBCDatabase access and JDBC
Database access and JDBCFulvio Corno
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...TAISEEREISA
 
Innovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and MonitoringInnovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and MonitoringCary Millsap
 
Using Ruby on Rails with legacy Oracle databases
Using Ruby on Rails with legacy Oracle databasesUsing Ruby on Rails with legacy Oracle databases
Using Ruby on Rails with legacy Oracle databasesRaimonds Simanovskis
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js frameworkBen Lin
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemyInada Naoki
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'sAntônio Roberto Silva
 
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...Oracle Developers
 
Php and MySQL Web Development
Php and MySQL Web DevelopmentPhp and MySQL Web Development
Php and MySQL Web Developmentw3ondemand
 
Tuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperTuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperVinay Kumar
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBCWebStackAcademy
 

Similaire à Developing for Node.JS with MySQL and NoSQL (20)

Con4445 jesus
Con4445 jesusCon4445 jesus
Con4445 jesus
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
Database programming
Database programmingDatabase programming
Database programming
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
GraphQL & Prisma from Scratch
GraphQL & Prisma from ScratchGraphQL & Prisma from Scratch
GraphQL & Prisma from Scratch
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on Oracle
 
Database access and JDBC
Database access and JDBCDatabase access and JDBC
Database access and JDBC
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
 
Innovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and MonitoringInnovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and Monitoring
 
Using Ruby on Rails with legacy Oracle databases
Using Ruby on Rails with legacy Oracle databasesUsing Ruby on Rails with legacy Oracle databases
Using Ruby on Rails with legacy Oracle databases
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
Reactive Java Programming: A new Asynchronous Database Access API by Kuassi M...
 
Php and MySQL Web Development
Php and MySQL Web DevelopmentPhp and MySQL Web Development
Php and MySQL Web Development
 
Tuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperTuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paper
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBC
 

Dernier

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

Developing for Node.JS with MySQL and NoSQL

  • 1. Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Picture Here 1 Developing for Node.JS with MySQL and NoSQL  J.D. Duncan, john.duncan@oracle.com  Craig Russell, craig.russell@oracle.com
  • 2. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2 MySQL Cluster Architecture MySQL Cluster Data Nodes Data Layer Clients Application Layer Management
  • 3. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3 MySQL Cluster Architecture MySQL Cluster Data Nodes Data Layer Clients Application Layer Management Management
  • 4. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4 MySQL Cluster Architecture MySQL Cluster Data Nodes Data Layer Application Layer Management Management Clients
  • 5. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5 mysql-js: a MySQL connector for Node.JS  Included in MySQL Cluster 7.3  in storage/ndb/nodejs  Also at github:  https://github.com/mysql/mysql-js  One JavaScript API with two back-end adapters  The MySQL back end uses node-mysql (Felix Geisendorfer's all- JavaScript MySQL connector)  The NDB (MySQL Cluster) back end uses native NDBAPI  Twitter-like demo application in samples/tweet/
  • 6. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6 What is Node.JS  A server-side web application platform  Single-threaded* and event-driven  Application code is written in JavaScript  JavaScript code runs in a very fast VM  Node.JS includes a useful set of standard libraries * One JavaScript thread; a pool of background worker threads perform blocking I/O.
  • 7. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7 Why Node.JS  Servers like lighttpd, nginx, and tornado can solve the C10K problem for static or cached content  Node.JS can solve the C10K problem for a dynamic web application – ... but what about 10,000 back-end database connections?  mysql-js with NDB aims to solve the C10K problem for a dynamic and database-driven web application The "C10K" problem: how to handle 10,000+ client connections
  • 8. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.8 Sample Code Node HTTP Server function handleRequest(request, response) { var page = getContent(request.url); response.statusCode = page ? 200 : 404; response.write(page); response.end(); } function startWebServer() { http.createServer(handleRequest).listen(8080); console.log("Server started"); }
  • 9. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.9 Sample Code with database access Node HTTP Server and mysql-js function dbConnected(connectionError, sessionFactory) { function handleRequest(request, response) { function onDbResponse(err, data) { var page = renderPage(data); response.write(page); response.end(); } function onDbSession(err, session) { runDbOperation(session, request, onDbResponse); } sessionFactory.openSession(null, onDbSession); } http.createServer(handleRequest).listen(8080); } nosql.connect(... , dbConnected);
  • 10. Copyright © 2013, Oracle and/or its affiliates. All rights reserved. User's View 10
  • 11. Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Twitter-like Demo Application 11
  • 12. Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Twitter-like Demo Application 12  Create user  Find user by name  Delete user by name (cascading)  Create a tweet  Find tweet by id  Delete tweet by id (cascading)  Make User A a follower of User B  List followers of user  List who user is following  Get the most recent tweets  Get the 20 latest tweets by a user  Get the 20 latest tweets @mentioning a user  Get the 20 latest tweets containing a #hashtag
  • 13. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.13 JavaScript Constructor function Tweet(author, message) { if(author !== undefined) { this.date_created = new Date(); this.author = author; this.message = message; } } Creates an instance of a Domain Object
  • 14. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.14 SQL Table CREATE TABLE tweet ( id bigint unsigned auto_increment not null, author varchar(20), message varchar(140), date_created timestamp(2), ... )
  • 15. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.15 Mapping var nosql = require("../mysql-js"); // Map SQL Tables to JS Constructors var mappings = []; mappings.push( new nosql.TableMapping('tweet'). applyToClass(Tweet) );
  • 16. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.16 Explicit Mappings are not required  All operations take either a constructor or table name  If table name is used, default mapping is created  Default mapping maps all columns to JS properties, where the JS type of each property is the default for its corresponding SQL Data Type
  • 17. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.17 connect function  connect is asynchronous  First parameter is object with connection properties  Second parameter is array of table names or constructors – Metadata for these will be resolved before returning session factory  Third parameter is callback (err, sessionFactory)  err will be falsy if no error
  • 18. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18 connect Example /* Connection Properties */ dbProperties = { adapter : "ndb", ndb_connectstring : "localhost:1186" }; // Map SQL Tables to JS Constructors mappings = []; mappings.push(new nosql.TableMapping('tweet'). applyToClass(Tweet)); // Connect nosql.connect(dbProperties, mappings, runCmdlineOperation, operation);
  • 19. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19 The connect callback function dbConnected(connectionError, sessionFactory) { function handleRequest(request, response) { function onDbResponse(err, data) { var page = renderPage(data); response.write(page); response.end(); } function onDbSession(err, session) { runDbOperation(session, request, onDbResponse); } sessionFactory.openSession(null, onDbSession); } http.createServer(handleRequest).listen(8080); } nosql.connect(dbProperties, mappings, dbConnected);
  • 20. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20
  • 21. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.21 find Example function findAuthor(name, callback) { session.find(Author, name, function(err, result) { if (err) { callback(err); } else { // result is the row in the author table callback(null, result); }); }
  • 22. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.22 find function on Session  find is asynchronous  First parameter is table name or constructor (mapped class)  Second parameter is key to use locating the record – Primary key – Unique key  Third parameter is callback (err, result) – err will be falsy if no error – result will be new object with mapped properties
  • 23. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23 persist Example function persistAuthor(name, full, callback) { var author = new Author(name, full); session.persist(author, function(err) { if (err) { callback(err); } else { callback(null, author); } }); }
  • 24. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.24 persist function on Session  persist is asynchronous  First parameter is instance of mapped class  Second parameter callback (err) – err will be falsy if no error  Variants allow persist into default mapped table (no constructor)
  • 25. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.25 update Example function updateAuthor(author, new_full_name, callback) { author.full_name = new_full_name; session.update(author, callback); }
  • 26. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.26 update function on Session  update is asynchronous  First parameter is instance of mapped class – Modified properties are written to database  Second parameter callback (err) – err will be falsy if no error  Variants allow update of default mapped table (no constructor)
  • 27. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.27 remove Example function removeByAuthorName(name, callback) { session.remove(Author, name, function(err, result) { if (err) { callback(err); } else { // row has been removed callback(null); }); }
  • 28. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28 remove function on Session  remove is asynchronous – First parameter is table name or constructor (mapped class) – Second parameter is key to use locating the record  Primary key  Unique key – Third parameter is callback (err)  err will be falsy if no error  Alternately – First parameter is instance of constructor – Second parameter is callback (err)
  • 29. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.29 Transactions /* Insert a tweet. - Start a transaction. - Persist the tweet. - Create & persist #hashtag & @user records - Increment the author's tweet count. - Then commit the transaction. */ function InsertTweetOperation(params, data) { session.currentTransaction().begin(); ... session.currentTransaction().commit(); }
  • 30. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.30 Query Example // Last 20 tweets @user TweetsAtUserOperation.run = function(tag) { if(tag.charAt(0) == "@") tag = tag.substring(1); this.session.createQuery(Mention, function(error, query) { var queryParams = { "order" : "desc", "limit" : 20 , "tag" : tag}; query.where(query.at_user.eq(query.param("tag"))); query.execute(queryParams, fetchTweetsInBatch); }); };
  • 31. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.31 query function  Session is Query factory using asynchronous api  query has a filter based on constructor (mapped class) properties  Filter has comparators and boolean operations – eq, ne, gt, ge, le, lt, between,isNull,isNotNull comparators – and, or, not,andNot,orNot operations  Query execution is asynchronous – Filter determines query strategy  Primary/unique key lookup; index scan; table scan – Properties govern query execution – Results are given in callback
  • 32. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.32 Query Comparators  Comparators compare properties to parameters  Query Domain Type property names correspond to Constructor field names (properties)  Parameters are created by name – qdt.param('date_low')  Properties are referenced by field name in Constructor – qdt.date_created  Comparators are properties of qdt properties – qdt.date_created.gt(qdt.param('date_low'));  Comparators return predicates
  • 33. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.33 Query Operators  Predicates are used as query filters via where function  Predicates are results of comparators or operators  Operators combine predicates using and, or, andNot, orNot, not – var predicate1 = qdt.date_created.gt(qdt.param('date_low')); – var predicate2 = qdt.date_created.lt(qdt.param('date_high')); – var predicate = predicate1.and(predicate2); – var predicate = predicate3.andNot(predicate4); – qdt.where(predicate)
  • 34. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.34 Batching function fetchTweetsInBatch(error, mentions, finalCallback) { var resultData = []; function addTweetToResults(err, tweet) { if(tweet && ! err) resultData.push(tweet); } var batch = session.createBatch(); while(var mention = mentions.pop()); batch.find(Tweet, mention.id, addTweetToResults); batch.execute(finalCallback, resultData); }
  • 35. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.35 Batching  Batching allows parallel database operations – Any combination of find, persist, update, remove  Batch is created using session.createBatch()  Operations (with their own callbacks) are added to the batch  Batch is executed with its own callback – Operation callbacks are run first – Then the batch callback
  • 36. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.36 Get mysql-js & run the Tweet demo ### First install MySQL Cluster 7.3 ### Then get the latest code from github: % git clone https://github.com/mysql/mysql-js.git ### build it % node configure.js % node-gyp configure build ### Go to the demo directory % cd samples/tweet
  • 37. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.37 Get mysql-js & run the Tweet demo ### create the schema % mysql -u root < tweet.sql ### Run with the mysql adapter % node tweet.js -a mysql put user mr_jdd 'John David Duncan' ### Run with the ndb adapter % node tweet.js -a ndb get user mr_jdd ### Look at some more examples % cat test_tweet.sh

Notes de l'éditeur

  1. JS is prototypal Constructor is like a class Creates
  2. runCmdlineOperation is a callback function that will get the connection operation is an extra argument
  3. And the pattern continues ... another callback