SlideShare a Scribd company logo
1 of 41
Download to read offline
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
MySQL Connector/Node.js
and the X DevAPI
Rui Quelhas
Software Developer
Oracle, MySQL
October 25, 2018
Presented with
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, timing, and pricing of any
features or functionality described for Oracle’s products may change and remains at the
sole discretion of Oracle Corporation.
2
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 3
• Rui Quelhas
• MySQL Middleware and Clients
team (Connectors)
• Connector/Node.js Lead Developer
• rui.quelhas@oracle.com
• @ruiquelhas
whoami
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Outline
Building blocks for modern MySQL
Diving into the X DevAPI
X DevAPI via Connector/Node.js
Final thoughts
1
2
3
4
4
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Building blocks for modern MySQL
5
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X DevAPI
• High-level database API to develop modern applications powered by
InnoDB Cluster
• Application gateway for the underlying document-store infrastructure (X
Protocol, Router, X Plugin)
• Off-the-shelf support for CRUD NoSQL document operations
• Lower-level management via raw SQL and other advanced features
• Standard specification for all the official client implementations
• Available in connectors for the most popular languages as well as the
brand new MySQL Shell
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
MySQL Document Store
• It’s now possible to save and retrieve unstructured data using a MySQL
database (not talking about raw JSON)
• Data lives in a JSON column but everything is abstracted way from the user,
which only deals with documents
• No need to worry about schemas and data types
• Keeping logical consistency and ACID (it’s MySQL after all)
• At the same time, a schema can still mix in traditional tables
• ...and more https://lefred.be/content/top-10-reasons-for-nosql-with-
mysql/
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Component Overview
Client App
MySQL
Router
X DevAPI X Plugin
X Protocol
https://dev.mysql.com/doc/refman/8.0/en/document-store.html
Classic Protocol
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Router
• MySQL Router is an integral part of InnoDB Cluster
• Lightweight middleware that provides transparent routing between an
application and back-end MySQL servers
• Can be used for a wide variety of use cases, but mainly to address high
availability and scalability concerns
• Runs as a standalone component
• Packaged with the MySQL server for the usual platforms
• Documentation available at https://dev.mysql.com/doc/mysql-
router/8.0/en/
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X Plugin
• New MySQL server plugin (developed internally)
• Installed and enabled by default on MySQL 8.0 series
• Provides the document store infrastructure on top of plain MySQL servers
• Uses a different set of ports (33060 instead of the usual MySQL 3306)
• Implements a client-server protocol based on Google Protocol Buffers
• Accepts X DevAPI client requests and processes them using the core MySQL
engine
• Documentation available at
https://dev.mysql.com/doc/refman/8.0/en/document-store.html
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X Protocol
• New client-server protocol based on Google Protocol Buffers (easier for
ANYONE to extend and improve)
• Content awereness
– CRUD expression trees to establish operation boundaries
– Expectations to define conditions and create pipelined statements
– Placeholder assignment and value escaping
• Security baked in
– SSL/TLS by default
– No information leaked to unauthenticated users (e.g. server version)
• Open source at https://dev.mysql.com/doc/internals/en/x-protocol.html
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X Protocol
Message example
Mysqlx.Crud.Find {
collection { name: "collection_name", schema: "schema_name" }
data_model: DOCUMENT
criteria {
type: OPERATOR
operator {
name: "=="
param {
type: IDENT,
identifier { name: "_id" }
}
param {
type: LITERAL,
literal {
type: V_STRING,
v_string: { value: "some_string" }
}
}
}
}
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Building upon the X Protocol
• Each client offers a unified (up to some point) flavour of the X DevAPI to be
consumed by an application
• X DevAPI constructs are exposed to the user via contextual methods
operating on different database objects through the hierarchy
• Database interactions are encoded as X Protocol messages expressing the
respective action
• Level of abstraction varies depending on the task
• Represents the vision of the MySQL team
• However, anyone can create their own API flavour on top of the protocol
13
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Diving into the X DevAPI
14
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Overview
• Wraps powerful concepts in a simple API
– High-level session concept for writing code that transparently scales from single
MySQL Server to a multi-server environment
– Operations are simple, expressive and easy to understand
– Non-blocking, asynchronous calls follow common host language patterns.
• Introduces a new, modern and easy-to-learn way to work with data
– Documents are stored in Collections and have their dedicated CRUD operation set
– Work with your existing domain objects or generate code based on structure
definitions for strictly typed languages
– Focus is put on working with data via CRUD operations (even for regular tables)
– Semantic query-building preferred over traditional SQL string mashing
15
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
What do I get?
• Fluent API and flexible parameters
• Multiple connection modes (with support for connection pooling)
• Automatic connection failover and timeout management
• Security best practices with multiple SSL/TLS operation modes and
authentication mechanisms as well as improved SQL injection protection
• Small and portable expression-based query language (subset of SQL)
• Hybrid CRUD API for both Document store collections and regular tables
• SQL statements for everything else
• ACID (transactions and row locking)
16
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Fluent API
• Code flows from a single point of entry – getSession()
• Code becomes more readable, maintainable (and even testable)
• Operations encapsulated in specialized and semantic methods
• Nice scaffolding for refactor tasks
• First-class support for text-editor (or IDE) hints and auto-completion
• Smaller SQL injection surface area
• Common standard between different programming environments
17
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Flexible Parameters
• Most public API methods work with:
– Multiple individual parameters
– A single array of parameters
– An object with named properties (where it applies)
18
mysqlx.getSession('root@localhost')
mysqlx.getSession({ user: 'root' })
collection.add({ name: 'foo' }).add({ name: 'bar' })
collection.add([{ name: 'foo' }, { name: 'bar' }])
collection.find('name = :name').bind('name', 'foo')
collection.find('name = :name').bind({ name: 'foo' })
collection.find().fields('foo', 'bar')
collection.find().fields(['foo', 'bar'])
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Connection Types
• Each X DevAPI Session matches a single connection (regular mode)
• Pooling mode (X DevAPI Sessions create or re-use existing connections)
19
Different operation modes
const url = 'mysqlx://root@localhost:33060/defaultSchema'
// create a regular standalone connection
mysqlx.getSession(url)
const pooling = { enabled: true, maxIdleTime: 500, maxSize: 25, queueTimeout: 500 }
const client = mysqlx.getClient(url, { pooling })
// acquire a connection from the pool (creates one if it does not exist)
client.getSession()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Connection failover and timeout
• Automatic priority-based failover
• Different timeout management for single-host or multi-host settings
20
Pooling and non pooling setups
// single-host
mysqlx.getSession('mysqlx://root@localhost:33060?connect-timeout=1000');
// multi-host
// order-based priority
mysqlx.getSession('mysqlx://root@[localhost:33060, localhost:33061]?connect-timeout=1000')
mysqlx.getSession('root@[localhost:33060, localhost:33061] ')
// weight-based priority
mysqlx.getSession('mysqlx://root@[(localhost:33061, priority=99), (localhost:33060, priority=100)]')
mysqlx.getSession('root@[(localhost:33061, priority=99), (localhost:33060, priority=100)]?connect-timeout=1000')
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Security Options
• SSL/TLS is enabled by default, for TCP connections, when creating a session
• Local UNIX socket connections disable it (not needed) to be faster
• Additional server certificate validation
• Options can be overridden on-demand
21
mysqlx.getSession({ ssl: false })
mysqlx.getSession('mysqlx://root@localhost?ssl-mode=DISABLED')
mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem' } })
mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)')
mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem', crl: '/path/to/crl.pem' } })
mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)&ssl-crl=(/path/to/crl.pem)')
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Authentication Mechanisms
• SHA-1 and SHA-2 password hashing
• Supports authentication mechanisms for multiple server plugins:
– MYSQL41 for mysql_native_password
– PLAIN and SHA256_MEMORY for sha256_password and/or caching_sha2_password
22
mysqlx.getSession({ user: 'root', auth: 'MYSQL41' })
mysqlx.getSession('mysqlx://root@localhost?auth=MYSQL41')
mysqlx.getSession({ user: 'root', auth: 'PLAIN' })
mysqlx.getSession('mysqlx://root@localhost?auth=PLAIN')
mysqlx.getSession({ user: 'root', auth: 'SHA256_MEMORY' })
mysqlx.getSession('mysqlx://root@localhost?auth=SHA256_MEMORY')
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Hybrid CRUD API
• Operational methods return proper database object or operation instances
• CRUD operations: CollectionAdd, TableInsert, CollectionFind, TableSelect,
CollectionModify, TableUpdate, CollectionRemove, TableDelete
• Operation boundaries - filtering criteria, computed projections, placeholder
assignment, resultset ordering - established using an expression language
Table Collection
• insert()
• select()
• update()
• delete()
• add()
• find()
• modify()
• remove()
23
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Expression Strings
• Small subset of the SQL language
• Easy to grasp (everyone already knows SQL) but still powerful
• Expressive and human-readable
• Common standard between all the official connector implementations
// JavaScript
collection
.find("name = 'foo' AND age > 42")
.fields("name", "age")
.groupBy("name", "age")
.sort("name ASC", "age DESC")
.limit(4)
.offset(2)
.execute()
// Java
collection
.find("name = 'foo' AND age > 42")
.fields("name", "age")
.groupBy("name", "age")
.sort("name ASC", "age DESC")
.limit(4)
.offset(2)
.execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Placeholder assignment
• Placeholders are supported by the expression grammar
• Variable values are native to the protocol itself
• Additional server-side validation and escaping
• Mitigates SQL injection risks
25
collection.find('name = :name')
.bind('name', 'foo')
.execute()
collection.find('name = :name')
.bind({ name: 'foo' })
.execute()
Mysqlx.Crud.Find {
criteria {
operator {
param {
identifier { document_path: "name" }
}
param { position: 0 }
}
}
args { v_string: "foo" }
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Raw SQL
• For ETL, structured analytics and/or reporting, SQL is always an option
• Allows to tap into features not yet part of the X DevAPI, such as:
– DDL operations for relational tables
– Managing key constraints
– Using JOINs
26
// create a table
session.sql('CREATE TABLE foo (bar VARCHAR(3))').execute()
// add a unique constraint
session.sql('ALTER TABLE foo ADD COLUMN bar VARCHAR(3) GENERATED ALWAYS AS doc->>"$.bar" VIRTUAL UNIQUE KEY NOT
NULL').execute()
// execute a JOIN query
session.sql('SELECT DISTINCT t1.bar FROM foo t1 JOIN baz t2 ON t1.bar = t2.qux WHERE t1.qux = t2.quux').execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Transactions and Savepoints
• Session-level atomic operations
• Create, commit or rollback transactions in the scope of a session
• Create, release or rollback to intermediate savepoints in those transactions
27
try {
session.startTransaction()
// run some operations (1)
session.createSavepoint('foo')
// run more operations (2)
session.releaseSavepoint('foo')
session.commit()
} catch (err) {
try {
session.rollbackTo('foo') // go to (2)
} catch (err) {
session.rollback() // revert the entire thing
}
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Row Locking
• Reads determine isolation level in the presence of concurrent transactions
• Exclusive and Shared locks with different operation modes:
– DEFAULT (regular SELECT FOR ... UPDATE or SELECT FOR ... SHARE behavior)
– NOWAIT (query executes immediately and fails if row is locked)
– SKIP_LOCKED (query executes immediately and ignores row)
28
// works for table.select() as well
collection.find('name = "foo"').lockExclusive() // mysqlx.LockContention.DEFAULT
collection.find('name = "foo"').lockExclusive(mysqlx.LockContention.NOWAIT)
collection.find('name = "foo"').lockExclusive(mysqlx.LockContention.SKIP_LOCKED)
// works for table.select() as well
collection.find('name = "bar"').lockShared() // mysqlx.LockContention.DEFAULT
collection.find('name = "bar"').lockShared(mysqlx.LockContention.NOWAIT)
collection.find('name = "bar"').lockShared(mysqlx.LockContention.SKIP_LOCKED)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X DevAPI via Connector/Node.js
29
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Overview
• Currently the only Node.js driver for MySQL with out-of-the-box support
for 8.0 servers and, particularly, the X DevAPI
• Open source, available on GitHub and on npm
• Modern Node.js asynchronous API using Promises
• Covers a big chunk of the X DevAPI spec:
– Fluent API and query builder based on the common expression language
– Document store and relational CRUD methods alongside plain SQL support
– Secure by default (SSL/TLS and SHA256 authentication)
– Connection pooling and failover
– Logical consistency with transactions, savepoints and row-locking...
30
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Asynchronous API
• Operations are sent to the server when the execute() method is called
– receives an optional query operation callback push-based cursor) which runs for each
element in the result set
– returns a Promise which resolves to a Result instance containing details and metadata
about the operation, or fails with an Error (can be used with async/await)
collection
.remove()
.execute(/* no cursor will be needed */)
.then(result => {
// result contains details about the operation
})
.catch(err => {
// handle any errors
})
collection
.find()
.execute(doc => {
// do something with the document
})
.then(result => {
// result contains details about the operation
})
.catch(err => {
// handle any errors
})
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Up and running
$ npm install --save --save-exact @mysql/xdevapi
(async function () {
const session = await mysqlx.getSession({ user: 'root' });
const schema = session.getSchema('devapitalks');
if (!(await schema.existsInDatabase())) {
await session.createSchema('devapitalks');
}
// ...
})();
const mysqlx = require('@mysql/xdevapi');
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Data definition
// const schema = session.getSchema('devapitalks');
await schema.createCollection('sessions_nosql', { ReuseExistingObject: true });
// For now, table DDL operations are performed using SQL only
await session.sql('USE devapitalks').execute();
await session.sql(`CREATE TABLE sessions_sql (
_id VARCHAR(7) NOT NULL,
title VARCHAR(250),
PRIMARY KEY (_id)
)`).execute();
const collections = await schema.getCollections();
console.log(collections); // [{ schema: 'devapitalks', collection: 'sessions_nosql' }]
const tables = await schema.getTables();
console.log(tables); // [{ schema: 'devapitalks', table: 'sessions_sql' }]
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Saving data
const table = schema.getTable('sessions_sql');
const collection = schema.getCollection('sessions_nosql');
// single insert
await table.insert('_id', 'title').values('ODEV5985', 'This talk!').execute();
// batching inserts
const batchA = collection
.add({ _id: 'ODEV5986', title: 'Node.js and the MySQL Document Store', speakers: ['Rui Quelhas'] })
.add({ _id: 'ODEV6233', title: 'Connector/J Beyond JDBC...', speakers: ['Filipe Silva'] });
const batchB = table
.insert(['_id', 'title'])
.values(['ODEV5959', 'Python and the MySQL Document Store']);
const batchC = table
.insert('_id', 'title')
.values('OHOL1706', 'Developing Modern Applications with the MySQL Document Store and Node.js');
await Promise.all([batchA.execute(), batchB.execute(), batchC.execute()]);
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Retrieving data
// const collection = schema.getCollection('sessions_nosql');
await collection.find('title LIKE :pattern')
.bind({ pattern: '%Node.js%' })
.fields('_id', 'speakers')
.execute(console.log); // { _id: 'ODEV5985', speakers: ['Rui Quelhas'] }
// const table = schema.getTable('sessions_sql');
await table.select() // project specific columns if needed
.where('title LIKE :pattern')
.bind('pattern', '%MySQL%')
.orderBy('_id ASC')
.limit(1)
.offset(1)
.execute(console.log); // ['OHOL1706', 'Developing Modern Applications with...']
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Updating data
// const collection = schema.getCollection('sessions_nosql');
await collection.modify('_id = :id')
.bind('id', 'ODEV5986')
.set('title', 'The talk about Document Store!')
.execute();
const doc = await collection.getOne('ODEV5986');
console.log(doc); // { _id: 'ODEV5986', title: 'The talk about Document Store!', speakers: ['Rui Quelhas] }
// const table = schema.getTable('sessions_sql');
await table.update()
.where('_id = :id')
.bind({ id: 'ODEV5985' })
.set('title', 'MySQL Connector/Node.js and the X DevAPI')
.execute(console.log);
await table.select()
.where('_id = :id')
.bind('id', 'ODEV5985')
.execute(console.log) // ['ODEV5985', 'MySQL Connector/Node.js and the X DevAPI']
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Deleting data
// const collection = schema.getCollection('sessions_nosql');
await collection.remove('true').execute();
const docs = [];
const doc = await collection.find().execute(doc => docs.push(doc));
console.log(docs); // []
// const table = schema.getTable('sessions_sql');
await table.delete()
.where('title LIKE :pattern')
.bind('pattern', '%Node.js%')
.execute();
await table.select()
.execute(console.log) // ['ODEV5959', 'Python and the MySQL Document Store']
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Final thoughts
38
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Some takeaways
• Simplify and secure your database installation
• Reap the benefits of both SQL and NoSQL
• High-availability and scalability via InnoDB Cluster
• Fast prototyping but always with maintainability in mind
• No language or platform lock-in (portable API constructs)
• Stick to a modern simple Node.js workflow (async, npm)
• Deploy everywhere where Node.js excells (containers, microservices,
serverless and FaaS, desktop, IoT, etc.)
• It’s all open source
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Links and Resources
• https://dev.mysql.com/doc/x-devapi-userguide/en/
• https://github.com/mysql/mysql-connector-nodejs
• https://www.npmjs.com/package/@mysql/xdevapi
• https://dev.mysql.com/doc/dev/connector-nodejs/8.0/
• https://www.mysql.com/news-and-events/web-seminars/mysql-
document-store-and-node-js/
• https://www.mysql.com/news-and-events/web-seminars/mysql-
document-store-and-node-js/
40
MySQL Connector/Node.js and the X DevAPI

More Related Content

What's hot

Hadoop & no sql new generation database systems
Hadoop & no sql   new generation database systemsHadoop & no sql   new generation database systems
Hadoop & no sql new generation database systems
ramazan fırın
 
Query mechanisms for NoSQL databases
Query mechanisms for NoSQL databasesQuery mechanisms for NoSQL databases
Query mechanisms for NoSQL databases
ArangoDB Database
 
C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...
C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...
C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...
DataStax
 
Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...
Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...
Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...
DataStax
 

What's hot (20)

Big data for cio 2015
Big data for cio 2015Big data for cio 2015
Big data for cio 2015
 
Things Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Things Every Oracle DBA Needs To Know About The Hadoop EcosystemThings Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Things Every Oracle DBA Needs To Know About The Hadoop Ecosystem
 
Adding real time reporting to your database oracle db in memory
Adding real time reporting to your database oracle db in memoryAdding real time reporting to your database oracle db in memory
Adding real time reporting to your database oracle db in memory
 
BGOUG 2012 - Drag & drop and other stuff - Using your database as a file server
BGOUG 2012 - Drag & drop and other stuff - Using your database as a file serverBGOUG 2012 - Drag & drop and other stuff - Using your database as a file server
BGOUG 2012 - Drag & drop and other stuff - Using your database as a file server
 
Horizon for Big Data
Horizon for Big DataHorizon for Big Data
Horizon for Big Data
 
Hadoop & no sql new generation database systems
Hadoop & no sql   new generation database systemsHadoop & no sql   new generation database systems
Hadoop & no sql new generation database systems
 
Understanding Oracle GoldenGate 12c
Understanding Oracle GoldenGate 12cUnderstanding Oracle GoldenGate 12c
Understanding Oracle GoldenGate 12c
 
MongoDB Replication fundamentals - Desert Code Camp - October 2014
MongoDB Replication fundamentals - Desert Code Camp - October 2014MongoDB Replication fundamentals - Desert Code Camp - October 2014
MongoDB Replication fundamentals - Desert Code Camp - October 2014
 
Storlets fb session_16_9
Storlets fb session_16_9Storlets fb session_16_9
Storlets fb session_16_9
 
9. Document Oriented Databases
9. Document Oriented Databases9. Document Oriented Databases
9. Document Oriented Databases
 
Nosql data models
Nosql data modelsNosql data models
Nosql data models
 
Query mechanisms for NoSQL databases
Query mechanisms for NoSQL databasesQuery mechanisms for NoSQL databases
Query mechanisms for NoSQL databases
 
Hibernate
HibernateHibernate
Hibernate
 
NoSQL Slideshare Presentation
NoSQL Slideshare Presentation NoSQL Slideshare Presentation
NoSQL Slideshare Presentation
 
C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...
C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...
C* Keys: Partitioning, Clustering, & CrossFit (Adam Hutson, DataScale) | Cass...
 
NoSQL Introduction
NoSQL IntroductionNoSQL Introduction
NoSQL Introduction
 
Netezza online training at GoLogica
Netezza online training at GoLogicaNetezza online training at GoLogica
Netezza online training at GoLogica
 
The Evolution of the Fashion Retail Industry in the Age of AI with Kshitij Ku...
The Evolution of the Fashion Retail Industry in the Age of AI with Kshitij Ku...The Evolution of the Fashion Retail Industry in the Age of AI with Kshitij Ku...
The Evolution of the Fashion Retail Industry in the Age of AI with Kshitij Ku...
 
The Evolution of Open Source Databases
The Evolution of Open Source DatabasesThe Evolution of Open Source Databases
The Evolution of Open Source Databases
 
Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...
Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...
Building a Distributed Reservation System with Cassandra (Andrew Baker & Jeff...
 

Similar to MySQL Connector/Node.js and the X DevAPI

MySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document StoreMySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document Store
Olivier DASINI
 
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
Geir Høydalsvik
 

Similar to MySQL Connector/Node.js and the X DevAPI (20)

Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document StoreConnector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
 
MySQL 8 loves JavaScript
MySQL 8 loves JavaScript MySQL 8 loves JavaScript
MySQL 8 loves JavaScript
 
MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Document Store - A Document Store with all the benefts of a Transactona...MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Document Store - A Document Store with all the benefts of a Transactona...
 
MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?
 
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
 
MySQL Day Paris 2016 - MySQL as a Document Store
MySQL Day Paris 2016 - MySQL as a Document StoreMySQL Day Paris 2016 - MySQL as a Document Store
MySQL Day Paris 2016 - MySQL as a Document Store
 
OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
OUG Scotland 2014 - NoSQL and MySQL - The best of both worldsOUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
 
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
 
MySQL Shell: The DevOps Tool for MySQL
MySQL Shell: The DevOps Tool for MySQLMySQL Shell: The DevOps Tool for MySQL
MySQL Shell: The DevOps Tool for MySQL
 
MySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document StoreMySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document Store
 
Develop PHP Applications with MySQL X DevAPI
Develop PHP Applications with MySQL X DevAPIDevelop PHP Applications with MySQL X DevAPI
Develop PHP Applications with MySQL X DevAPI
 
20171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v120171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v1
 
MySQL Document Store
MySQL Document StoreMySQL Document Store
MySQL Document Store
 
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
 
Mysql NDB Cluster's Asynchronous Parallel Design for High Performance
Mysql NDB Cluster's Asynchronous Parallel Design for High PerformanceMysql NDB Cluster's Asynchronous Parallel Design for High Performance
Mysql NDB Cluster's Asynchronous Parallel Design for High Performance
 
What's New in MySQL 8.0 @ HKOSC 2017
What's New in MySQL 8.0 @ HKOSC 2017What's New in MySQL 8.0 @ HKOSC 2017
What's New in MySQL 8.0 @ HKOSC 2017
 
Solution Use Case Demo: The Power of Relationships in Your Big Data
Solution Use Case Demo: The Power of Relationships in Your Big DataSolution Use Case Demo: The Power of Relationships in Your Big Data
Solution Use Case Demo: The Power of Relationships in Your Big Data
 
OUGLS 2016: Guided Tour On The MySQL Source Code
OUGLS 2016: Guided Tour On The MySQL Source CodeOUGLS 2016: Guided Tour On The MySQL Source Code
OUGLS 2016: Guided Tour On The MySQL Source Code
 
Novinky v Oracle Database 18c
Novinky v Oracle Database 18cNovinky v Oracle Database 18c
Novinky v Oracle Database 18c
 
MySQL 8.0, what's new ? - Forum PHP 2018
MySQL 8.0, what's new ? - Forum PHP 2018MySQL 8.0, what's new ? - Forum PHP 2018
MySQL 8.0, what's new ? - Forum PHP 2018
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 

MySQL Connector/Node.js and the X DevAPI

  • 1. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | MySQL Connector/Node.js and the X DevAPI Rui Quelhas Software Developer Oracle, MySQL October 25, 2018 Presented with
  • 2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. 2
  • 3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 3 • Rui Quelhas • MySQL Middleware and Clients team (Connectors) • Connector/Node.js Lead Developer • rui.quelhas@oracle.com • @ruiquelhas whoami
  • 4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Outline Building blocks for modern MySQL Diving into the X DevAPI X DevAPI via Connector/Node.js Final thoughts 1 2 3 4 4
  • 5. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Building blocks for modern MySQL 5
  • 6. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X DevAPI • High-level database API to develop modern applications powered by InnoDB Cluster • Application gateway for the underlying document-store infrastructure (X Protocol, Router, X Plugin) • Off-the-shelf support for CRUD NoSQL document operations • Lower-level management via raw SQL and other advanced features • Standard specification for all the official client implementations • Available in connectors for the most popular languages as well as the brand new MySQL Shell
  • 7. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | MySQL Document Store • It’s now possible to save and retrieve unstructured data using a MySQL database (not talking about raw JSON) • Data lives in a JSON column but everything is abstracted way from the user, which only deals with documents • No need to worry about schemas and data types • Keeping logical consistency and ACID (it’s MySQL after all) • At the same time, a schema can still mix in traditional tables • ...and more https://lefred.be/content/top-10-reasons-for-nosql-with- mysql/
  • 8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Component Overview Client App MySQL Router X DevAPI X Plugin X Protocol https://dev.mysql.com/doc/refman/8.0/en/document-store.html Classic Protocol
  • 9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Router • MySQL Router is an integral part of InnoDB Cluster • Lightweight middleware that provides transparent routing between an application and back-end MySQL servers • Can be used for a wide variety of use cases, but mainly to address high availability and scalability concerns • Runs as a standalone component • Packaged with the MySQL server for the usual platforms • Documentation available at https://dev.mysql.com/doc/mysql- router/8.0/en/
  • 10. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X Plugin • New MySQL server plugin (developed internally) • Installed and enabled by default on MySQL 8.0 series • Provides the document store infrastructure on top of plain MySQL servers • Uses a different set of ports (33060 instead of the usual MySQL 3306) • Implements a client-server protocol based on Google Protocol Buffers • Accepts X DevAPI client requests and processes them using the core MySQL engine • Documentation available at https://dev.mysql.com/doc/refman/8.0/en/document-store.html
  • 11. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X Protocol • New client-server protocol based on Google Protocol Buffers (easier for ANYONE to extend and improve) • Content awereness – CRUD expression trees to establish operation boundaries – Expectations to define conditions and create pipelined statements – Placeholder assignment and value escaping • Security baked in – SSL/TLS by default – No information leaked to unauthenticated users (e.g. server version) • Open source at https://dev.mysql.com/doc/internals/en/x-protocol.html
  • 12. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X Protocol Message example Mysqlx.Crud.Find { collection { name: "collection_name", schema: "schema_name" } data_model: DOCUMENT criteria { type: OPERATOR operator { name: "==" param { type: IDENT, identifier { name: "_id" } } param { type: LITERAL, literal { type: V_STRING, v_string: { value: "some_string" } } } } } }
  • 13. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Building upon the X Protocol • Each client offers a unified (up to some point) flavour of the X DevAPI to be consumed by an application • X DevAPI constructs are exposed to the user via contextual methods operating on different database objects through the hierarchy • Database interactions are encoded as X Protocol messages expressing the respective action • Level of abstraction varies depending on the task • Represents the vision of the MySQL team • However, anyone can create their own API flavour on top of the protocol 13
  • 14. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Diving into the X DevAPI 14
  • 15. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Overview • Wraps powerful concepts in a simple API – High-level session concept for writing code that transparently scales from single MySQL Server to a multi-server environment – Operations are simple, expressive and easy to understand – Non-blocking, asynchronous calls follow common host language patterns. • Introduces a new, modern and easy-to-learn way to work with data – Documents are stored in Collections and have their dedicated CRUD operation set – Work with your existing domain objects or generate code based on structure definitions for strictly typed languages – Focus is put on working with data via CRUD operations (even for regular tables) – Semantic query-building preferred over traditional SQL string mashing 15
  • 16. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | What do I get? • Fluent API and flexible parameters • Multiple connection modes (with support for connection pooling) • Automatic connection failover and timeout management • Security best practices with multiple SSL/TLS operation modes and authentication mechanisms as well as improved SQL injection protection • Small and portable expression-based query language (subset of SQL) • Hybrid CRUD API for both Document store collections and regular tables • SQL statements for everything else • ACID (transactions and row locking) 16
  • 17. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Fluent API • Code flows from a single point of entry – getSession() • Code becomes more readable, maintainable (and even testable) • Operations encapsulated in specialized and semantic methods • Nice scaffolding for refactor tasks • First-class support for text-editor (or IDE) hints and auto-completion • Smaller SQL injection surface area • Common standard between different programming environments 17
  • 18. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Flexible Parameters • Most public API methods work with: – Multiple individual parameters – A single array of parameters – An object with named properties (where it applies) 18 mysqlx.getSession('root@localhost') mysqlx.getSession({ user: 'root' }) collection.add({ name: 'foo' }).add({ name: 'bar' }) collection.add([{ name: 'foo' }, { name: 'bar' }]) collection.find('name = :name').bind('name', 'foo') collection.find('name = :name').bind({ name: 'foo' }) collection.find().fields('foo', 'bar') collection.find().fields(['foo', 'bar'])
  • 19. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Connection Types • Each X DevAPI Session matches a single connection (regular mode) • Pooling mode (X DevAPI Sessions create or re-use existing connections) 19 Different operation modes const url = 'mysqlx://root@localhost:33060/defaultSchema' // create a regular standalone connection mysqlx.getSession(url) const pooling = { enabled: true, maxIdleTime: 500, maxSize: 25, queueTimeout: 500 } const client = mysqlx.getClient(url, { pooling }) // acquire a connection from the pool (creates one if it does not exist) client.getSession()
  • 20. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Connection failover and timeout • Automatic priority-based failover • Different timeout management for single-host or multi-host settings 20 Pooling and non pooling setups // single-host mysqlx.getSession('mysqlx://root@localhost:33060?connect-timeout=1000'); // multi-host // order-based priority mysqlx.getSession('mysqlx://root@[localhost:33060, localhost:33061]?connect-timeout=1000') mysqlx.getSession('root@[localhost:33060, localhost:33061] ') // weight-based priority mysqlx.getSession('mysqlx://root@[(localhost:33061, priority=99), (localhost:33060, priority=100)]') mysqlx.getSession('root@[(localhost:33061, priority=99), (localhost:33060, priority=100)]?connect-timeout=1000')
  • 21. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Security Options • SSL/TLS is enabled by default, for TCP connections, when creating a session • Local UNIX socket connections disable it (not needed) to be faster • Additional server certificate validation • Options can be overridden on-demand 21 mysqlx.getSession({ ssl: false }) mysqlx.getSession('mysqlx://root@localhost?ssl-mode=DISABLED') mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem' } }) mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)') mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem', crl: '/path/to/crl.pem' } }) mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)&ssl-crl=(/path/to/crl.pem)')
  • 22. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Authentication Mechanisms • SHA-1 and SHA-2 password hashing • Supports authentication mechanisms for multiple server plugins: – MYSQL41 for mysql_native_password – PLAIN and SHA256_MEMORY for sha256_password and/or caching_sha2_password 22 mysqlx.getSession({ user: 'root', auth: 'MYSQL41' }) mysqlx.getSession('mysqlx://root@localhost?auth=MYSQL41') mysqlx.getSession({ user: 'root', auth: 'PLAIN' }) mysqlx.getSession('mysqlx://root@localhost?auth=PLAIN') mysqlx.getSession({ user: 'root', auth: 'SHA256_MEMORY' }) mysqlx.getSession('mysqlx://root@localhost?auth=SHA256_MEMORY')
  • 23. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Hybrid CRUD API • Operational methods return proper database object or operation instances • CRUD operations: CollectionAdd, TableInsert, CollectionFind, TableSelect, CollectionModify, TableUpdate, CollectionRemove, TableDelete • Operation boundaries - filtering criteria, computed projections, placeholder assignment, resultset ordering - established using an expression language Table Collection • insert() • select() • update() • delete() • add() • find() • modify() • remove() 23
  • 24. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Expression Strings • Small subset of the SQL language • Easy to grasp (everyone already knows SQL) but still powerful • Expressive and human-readable • Common standard between all the official connector implementations // JavaScript collection .find("name = 'foo' AND age > 42") .fields("name", "age") .groupBy("name", "age") .sort("name ASC", "age DESC") .limit(4) .offset(2) .execute() // Java collection .find("name = 'foo' AND age > 42") .fields("name", "age") .groupBy("name", "age") .sort("name ASC", "age DESC") .limit(4) .offset(2) .execute()
  • 25. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Placeholder assignment • Placeholders are supported by the expression grammar • Variable values are native to the protocol itself • Additional server-side validation and escaping • Mitigates SQL injection risks 25 collection.find('name = :name') .bind('name', 'foo') .execute() collection.find('name = :name') .bind({ name: 'foo' }) .execute() Mysqlx.Crud.Find { criteria { operator { param { identifier { document_path: "name" } } param { position: 0 } } } args { v_string: "foo" } }
  • 26. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Raw SQL • For ETL, structured analytics and/or reporting, SQL is always an option • Allows to tap into features not yet part of the X DevAPI, such as: – DDL operations for relational tables – Managing key constraints – Using JOINs 26 // create a table session.sql('CREATE TABLE foo (bar VARCHAR(3))').execute() // add a unique constraint session.sql('ALTER TABLE foo ADD COLUMN bar VARCHAR(3) GENERATED ALWAYS AS doc->>"$.bar" VIRTUAL UNIQUE KEY NOT NULL').execute() // execute a JOIN query session.sql('SELECT DISTINCT t1.bar FROM foo t1 JOIN baz t2 ON t1.bar = t2.qux WHERE t1.qux = t2.quux').execute()
  • 27. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Transactions and Savepoints • Session-level atomic operations • Create, commit or rollback transactions in the scope of a session • Create, release or rollback to intermediate savepoints in those transactions 27 try { session.startTransaction() // run some operations (1) session.createSavepoint('foo') // run more operations (2) session.releaseSavepoint('foo') session.commit() } catch (err) { try { session.rollbackTo('foo') // go to (2) } catch (err) { session.rollback() // revert the entire thing } }
  • 28. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Row Locking • Reads determine isolation level in the presence of concurrent transactions • Exclusive and Shared locks with different operation modes: – DEFAULT (regular SELECT FOR ... UPDATE or SELECT FOR ... SHARE behavior) – NOWAIT (query executes immediately and fails if row is locked) – SKIP_LOCKED (query executes immediately and ignores row) 28 // works for table.select() as well collection.find('name = "foo"').lockExclusive() // mysqlx.LockContention.DEFAULT collection.find('name = "foo"').lockExclusive(mysqlx.LockContention.NOWAIT) collection.find('name = "foo"').lockExclusive(mysqlx.LockContention.SKIP_LOCKED) // works for table.select() as well collection.find('name = "bar"').lockShared() // mysqlx.LockContention.DEFAULT collection.find('name = "bar"').lockShared(mysqlx.LockContention.NOWAIT) collection.find('name = "bar"').lockShared(mysqlx.LockContention.SKIP_LOCKED)
  • 29. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X DevAPI via Connector/Node.js 29
  • 30. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Overview • Currently the only Node.js driver for MySQL with out-of-the-box support for 8.0 servers and, particularly, the X DevAPI • Open source, available on GitHub and on npm • Modern Node.js asynchronous API using Promises • Covers a big chunk of the X DevAPI spec: – Fluent API and query builder based on the common expression language – Document store and relational CRUD methods alongside plain SQL support – Secure by default (SSL/TLS and SHA256 authentication) – Connection pooling and failover – Logical consistency with transactions, savepoints and row-locking... 30
  • 31. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Asynchronous API • Operations are sent to the server when the execute() method is called – receives an optional query operation callback push-based cursor) which runs for each element in the result set – returns a Promise which resolves to a Result instance containing details and metadata about the operation, or fails with an Error (can be used with async/await) collection .remove() .execute(/* no cursor will be needed */) .then(result => { // result contains details about the operation }) .catch(err => { // handle any errors }) collection .find() .execute(doc => { // do something with the document }) .then(result => { // result contains details about the operation }) .catch(err => { // handle any errors })
  • 32. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Up and running $ npm install --save --save-exact @mysql/xdevapi (async function () { const session = await mysqlx.getSession({ user: 'root' }); const schema = session.getSchema('devapitalks'); if (!(await schema.existsInDatabase())) { await session.createSchema('devapitalks'); } // ... })(); const mysqlx = require('@mysql/xdevapi');
  • 33. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Data definition // const schema = session.getSchema('devapitalks'); await schema.createCollection('sessions_nosql', { ReuseExistingObject: true }); // For now, table DDL operations are performed using SQL only await session.sql('USE devapitalks').execute(); await session.sql(`CREATE TABLE sessions_sql ( _id VARCHAR(7) NOT NULL, title VARCHAR(250), PRIMARY KEY (_id) )`).execute(); const collections = await schema.getCollections(); console.log(collections); // [{ schema: 'devapitalks', collection: 'sessions_nosql' }] const tables = await schema.getTables(); console.log(tables); // [{ schema: 'devapitalks', table: 'sessions_sql' }]
  • 34. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Saving data const table = schema.getTable('sessions_sql'); const collection = schema.getCollection('sessions_nosql'); // single insert await table.insert('_id', 'title').values('ODEV5985', 'This talk!').execute(); // batching inserts const batchA = collection .add({ _id: 'ODEV5986', title: 'Node.js and the MySQL Document Store', speakers: ['Rui Quelhas'] }) .add({ _id: 'ODEV6233', title: 'Connector/J Beyond JDBC...', speakers: ['Filipe Silva'] }); const batchB = table .insert(['_id', 'title']) .values(['ODEV5959', 'Python and the MySQL Document Store']); const batchC = table .insert('_id', 'title') .values('OHOL1706', 'Developing Modern Applications with the MySQL Document Store and Node.js'); await Promise.all([batchA.execute(), batchB.execute(), batchC.execute()]);
  • 35. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Retrieving data // const collection = schema.getCollection('sessions_nosql'); await collection.find('title LIKE :pattern') .bind({ pattern: '%Node.js%' }) .fields('_id', 'speakers') .execute(console.log); // { _id: 'ODEV5985', speakers: ['Rui Quelhas'] } // const table = schema.getTable('sessions_sql'); await table.select() // project specific columns if needed .where('title LIKE :pattern') .bind('pattern', '%MySQL%') .orderBy('_id ASC') .limit(1) .offset(1) .execute(console.log); // ['OHOL1706', 'Developing Modern Applications with...']
  • 36. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Updating data // const collection = schema.getCollection('sessions_nosql'); await collection.modify('_id = :id') .bind('id', 'ODEV5986') .set('title', 'The talk about Document Store!') .execute(); const doc = await collection.getOne('ODEV5986'); console.log(doc); // { _id: 'ODEV5986', title: 'The talk about Document Store!', speakers: ['Rui Quelhas] } // const table = schema.getTable('sessions_sql'); await table.update() .where('_id = :id') .bind({ id: 'ODEV5985' }) .set('title', 'MySQL Connector/Node.js and the X DevAPI') .execute(console.log); await table.select() .where('_id = :id') .bind('id', 'ODEV5985') .execute(console.log) // ['ODEV5985', 'MySQL Connector/Node.js and the X DevAPI']
  • 37. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Deleting data // const collection = schema.getCollection('sessions_nosql'); await collection.remove('true').execute(); const docs = []; const doc = await collection.find().execute(doc => docs.push(doc)); console.log(docs); // [] // const table = schema.getTable('sessions_sql'); await table.delete() .where('title LIKE :pattern') .bind('pattern', '%Node.js%') .execute(); await table.select() .execute(console.log) // ['ODEV5959', 'Python and the MySQL Document Store']
  • 38. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Final thoughts 38
  • 39. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Some takeaways • Simplify and secure your database installation • Reap the benefits of both SQL and NoSQL • High-availability and scalability via InnoDB Cluster • Fast prototyping but always with maintainability in mind • No language or platform lock-in (portable API constructs) • Stick to a modern simple Node.js workflow (async, npm) • Deploy everywhere where Node.js excells (containers, microservices, serverless and FaaS, desktop, IoT, etc.) • It’s all open source
  • 40. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Links and Resources • https://dev.mysql.com/doc/x-devapi-userguide/en/ • https://github.com/mysql/mysql-connector-nodejs • https://www.npmjs.com/package/@mysql/xdevapi • https://dev.mysql.com/doc/dev/connector-nodejs/8.0/ • https://www.mysql.com/news-and-events/web-seminars/mysql- document-store-and-node-js/ • https://www.mysql.com/news-and-events/web-seminars/mysql- document-store-and-node-js/ 40