SlideShare a Scribd company logo
1 of 33
Download to read offline
Len Chang
08.18 2019
1
▪ Me
▪ The introduction of knex.js
▪ What’s the problem that schema change ?
▪ Create seed data and migration files
▪ How to solve the problem ?
▪ Integrate it with Bitbucket pipeline
▪ Q & A
2
▪ Len Chang
▪ Engineer
▪ Like..
▪ Baseball / Beer / Finance
▪ Coding
▪ JavaScript / Typescript
▪ DB
▪ PostgreSQL / Elasticsearch
▪ Volunteer
▪ Nantou Code Geek
▪ You can find me..
▪ LinkedIn
▪ Facebook
▪ PPT Online
3
Library Docs (Link)
4
▪ Knex.js is a "batteries included" SQL query builder
for Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon
Redshift designed to be flexible, portable, and fun to use.
▪ Basically it’s the major choice when you use nodejs and RDBMS to do the backend
server.
▪ Features
▪ Query Builder
▪ Pooling
▪ Transactions
▪ Migrations
▪ Seed files
5
▪ Query Builder
▪ The heart of the library, the knex query builder is the interface used for building and
executing standard SQL queries, such as select, insert, update, delete.
▪ Example
knex({ a: 'table', b: 'table' })
.select({
aTitle: 'a.title',
bTitle: 'b.title'
})
.whereRaw('?? = ??', ['a.column_1', 'b.column_2’])
// select "a"."title" as "aTitle", "b"."title" as "bTitle" from "table" as "a", "table" as "b" where "a"."column_1" = "b"."column_2"
6
Application
▪ Pooling
▪ The client created by the configuration initializes a connection pool, using the tarn.js
library. This connection pool has a default setting of a min: 2, max: 10
7
Pooling
Action
Action
Action
PG
Application
Action
Action
Action
PG
▪ Transactions
▪ All queries within a transaction are executed on the same database connection, and run
the entire set of queries as a single unit of work. Any failure will mean the database will
rollback any queries executed on that connection to the pre-transaction state.
▪ Methods
1. As a transaction object
2. As a query builder
8
as a transaction object
▪ Transactions
▪ Methods
1. As a transaction object
2. As a query builder
9
as a transaction object
▪ Migrations
▪ Migrations allow for you to define sets of schema changes so upgrading a database is a
breeze.
10
▪ Seed files
▪ Seed files allow you to populate your database with test or seed data independent of your
migration files.
11
Run seed
12
Testing
1. How to avoid crashing when you
update schema ?
▪ Testing
2. How to avoid operation error ?
▪ Deploy system automatically
13
DB
(version + 1)
Script:
Updating schema
Success (Commit) Failed (Rollback)
DB
(version + 1)
DB
(version)
Dev DB
Script:
Updating schema
QA DB
PROD DB
Dev DB
Dev DB
QA DB
Script:
Updating schemaScript:
Updating schema
3. Doesn’t need to use another tool or code language to do it.
▪ Just JavaScript
14
▪ Summary
▪ How to avoid crashing when you updated schema ?
▪ Testing
▪ How to avoid operation error ?
▪ Deploying system automatically
▪ Doesn’t need to use another tool or code language to do it
▪ Just JavaScript
15
16
▪ Migration
1. knex init => knexfile.ts
17
▪ Migration
2. knex migrate:make migration
▪ Up / Down Functions
18
▪ Migration
3. knex migrate:latest
19
Check migration record
Run
unrecorded
files
▪ Migration Process
20
knexfile.ts
20190814170951_migration.ts
Success (Commit)Failed (Rollback)
Run Migration
▪ Seed data
1. knex init => knexfile.ts
21
▪ Seed data
2. Create seed data
22
▪ Seed data
3. Create seed handling script
and Running
23
▪ Seed Process
24
Seed data of tables
Seed handling Scripts
knex seed:run
DB
Testing, deploying system automatically and just javascript !
25
▪ Testing
▪ Embedded seed testing in migration
Process
26
Transaction
Transaction
▪ Testing
▪ Dividing Environment
▪ All migration script is knex migrate:latest, but
it run different script by different env.
27
▪ deploying system automatically
28
▪ Just javascript
▪ …well…. Just you said that….only javascript.
29
30
31
▪ Advantage
▪ Reduce operation error !
▪ Deploy schema simpler and safer.
▪ Do it by JS developer.
32
LinkedIn
Facebook
PPT Online
33

More Related Content

What's hot

Building Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDBBuilding Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDB
Ashnikbiz
 

What's hot (20)

Is It Fast? : Measuring MongoDB Performance
Is It Fast? : Measuring MongoDB PerformanceIs It Fast? : Measuring MongoDB Performance
Is It Fast? : Measuring MongoDB Performance
 
dba_lounge_Iasi: Everybody likes redis
dba_lounge_Iasi: Everybody likes redisdba_lounge_Iasi: Everybody likes redis
dba_lounge_Iasi: Everybody likes redis
 
When is MyRocks good?
When is MyRocks good? When is MyRocks good?
When is MyRocks good?
 
InnoDB Scalability improvements in MySQL 8.0
InnoDB Scalability improvements in MySQL 8.0InnoDB Scalability improvements in MySQL 8.0
InnoDB Scalability improvements in MySQL 8.0
 
Couch Db
Couch DbCouch Db
Couch Db
 
MySQL on AWS RDS
MySQL on AWS RDSMySQL on AWS RDS
MySQL on AWS RDS
 
Webpack
WebpackWebpack
Webpack
 
WiredTiger Overview
WiredTiger OverviewWiredTiger Overview
WiredTiger Overview
 
MySQL topology healing at OLA.
MySQL topology healing at OLA.MySQL topology healing at OLA.
MySQL topology healing at OLA.
 
What is new in Galera 4 ?
What is new in Galera 4 ?What is new in Galera 4 ?
What is new in Galera 4 ?
 
Redis
RedisRedis
Redis
 
SQL, NoSQL, NewSQL? What's a developer to do?
SQL, NoSQL, NewSQL? What's a developer to do?SQL, NoSQL, NewSQL? What's a developer to do?
SQL, NoSQL, NewSQL? What's a developer to do?
 
NewSQL overview, Feb 2015
NewSQL overview, Feb 2015NewSQL overview, Feb 2015
NewSQL overview, Feb 2015
 
Evolution of MySQL Parallel Replication
Evolution of MySQL Parallel Replication Evolution of MySQL Parallel Replication
Evolution of MySQL Parallel Replication
 
Redis Overview
Redis OverviewRedis Overview
Redis Overview
 
MySQL Performance Schema in Action
MySQL Performance Schema in Action MySQL Performance Schema in Action
MySQL Performance Schema in Action
 
Building Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDBBuilding Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDB
 
Redis IU
Redis IURedis IU
Redis IU
 
MongoDB basics & Introduction
MongoDB basics & IntroductionMongoDB basics & Introduction
MongoDB basics & Introduction
 
Making MySQL Agile-ish
Making MySQL Agile-ishMaking MySQL Agile-ish
Making MySQL Agile-ish
 

Similar to COSCUP 2019 - The discussion between Knex.js and PostgreSQL

001 hbase introduction
001 hbase introduction001 hbase introduction
001 hbase introduction
Scott Miao
 
kranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High loadkranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High load
Krivoy Rog IT Community
 
Encompassing Information Integration
Encompassing Information IntegrationEncompassing Information Integration
Encompassing Information Integration
nguyenfilip
 
Meetup#2: Building responsive Symbology & Suggest WebService
Meetup#2: Building responsive Symbology & Suggest WebServiceMeetup#2: Building responsive Symbology & Suggest WebService
Meetup#2: Building responsive Symbology & Suggest WebService
Minsk MongoDB User Group
 

Similar to COSCUP 2019 - The discussion between Knex.js and PostgreSQL (20)

The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
 
Handling scale on AWS
Handling scale on AWSHandling scale on AWS
Handling scale on AWS
 
001 hbase introduction
001 hbase introduction001 hbase introduction
001 hbase introduction
 
MyRocks introduction and production deployment
MyRocks introduction and production deploymentMyRocks introduction and production deployment
MyRocks introduction and production deployment
 
Plantilla oracle
Plantilla oraclePlantilla oracle
Plantilla oracle
 
kranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High loadkranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High load
 
Achieving cyber mission assurance with near real-time impact
Achieving cyber mission assurance with near real-time impactAchieving cyber mission assurance with near real-time impact
Achieving cyber mission assurance with near real-time impact
 
Middleware upgrade to Oracle Fusion Middleware(FMW) 12c.Real Case stories.
Middleware upgrade to Oracle Fusion Middleware(FMW) 12c.Real Case stories. Middleware upgrade to Oracle Fusion Middleware(FMW) 12c.Real Case stories.
Middleware upgrade to Oracle Fusion Middleware(FMW) 12c.Real Case stories.
 
Serverless Compose vs hurtownia danych
Serverless Compose vs hurtownia danychServerless Compose vs hurtownia danych
Serverless Compose vs hurtownia danych
 
Encompassing Information Integration
Encompassing Information IntegrationEncompassing Information Integration
Encompassing Information Integration
 
Meetup#2: Building responsive Symbology & Suggest WebService
Meetup#2: Building responsive Symbology & Suggest WebServiceMeetup#2: Building responsive Symbology & Suggest WebService
Meetup#2: Building responsive Symbology & Suggest WebService
 
Lessons from Building Large-Scale, Multi-Cloud, SaaS Software at Databricks
Lessons from Building Large-Scale, Multi-Cloud, SaaS Software at DatabricksLessons from Building Large-Scale, Multi-Cloud, SaaS Software at Databricks
Lessons from Building Large-Scale, Multi-Cloud, SaaS Software at Databricks
 
Ow
OwOw
Ow
 
Level 101 for Presto: What is PrestoDB?
Level 101 for Presto: What is PrestoDB?Level 101 for Presto: What is PrestoDB?
Level 101 for Presto: What is PrestoDB?
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
Ow
OwOw
Ow
 
Operational foundation for the sql server dba
Operational foundation for the sql server dbaOperational foundation for the sql server dba
Operational foundation for the sql server dba
 
Experiences with Evangelizing Java Within the Database
Experiences with Evangelizing Java Within the DatabaseExperiences with Evangelizing Java Within the Database
Experiences with Evangelizing Java Within the Database
 
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
19. Cloud Native Computing - Kubernetes - Bratislava - Databases in K8s world
 

More from Len Chang

More from Len Chang (8)

The issue discussion between dml and ddl deployment
The issue discussion between dml and ddl deploymentThe issue discussion between dml and ddl deployment
The issue discussion between dml and ddl deployment
 
DevOps Taiwan meetup #19
DevOps Taiwan meetup #19DevOps Taiwan meetup #19
DevOps Taiwan meetup #19
 
BI in Xuenn
BI in XuennBI in Xuenn
BI in Xuenn
 
2014 Pixnet Hackathonh - EXIF Mining
2014 Pixnet Hackathonh - EXIF Mining2014 Pixnet Hackathonh - EXIF Mining
2014 Pixnet Hackathonh - EXIF Mining
 
Hadoop con2016 - Implement Real-time Centralized logging System by Elastic Stack
Hadoop con2016 - Implement Real-time Centralized logging System by Elastic StackHadoop con2016 - Implement Real-time Centralized logging System by Elastic Stack
Hadoop con2016 - Implement Real-time Centralized logging System by Elastic Stack
 
Agile scrum in startup
Agile scrum in startup  Agile scrum in startup
Agile scrum in startup
 
Hadoop Con2015 - The Data Scientist’s Toolbox
Hadoop Con2015 - The Data Scientist’s ToolboxHadoop Con2015 - The Data Scientist’s Toolbox
Hadoop Con2015 - The Data Scientist’s Toolbox
 
Spam user detection report
Spam user detection reportSpam user detection report
Spam user detection report
 

Recently uploaded

Recently uploaded (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

COSCUP 2019 - The discussion between Knex.js and PostgreSQL

  • 2. ▪ Me ▪ The introduction of knex.js ▪ What’s the problem that schema change ? ▪ Create seed data and migration files ▪ How to solve the problem ? ▪ Integrate it with Bitbucket pipeline ▪ Q & A 2
  • 3. ▪ Len Chang ▪ Engineer ▪ Like.. ▪ Baseball / Beer / Finance ▪ Coding ▪ JavaScript / Typescript ▪ DB ▪ PostgreSQL / Elasticsearch ▪ Volunteer ▪ Nantou Code Geek ▪ You can find me.. ▪ LinkedIn ▪ Facebook ▪ PPT Online 3
  • 5. ▪ Knex.js is a "batteries included" SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use. ▪ Basically it’s the major choice when you use nodejs and RDBMS to do the backend server. ▪ Features ▪ Query Builder ▪ Pooling ▪ Transactions ▪ Migrations ▪ Seed files 5
  • 6. ▪ Query Builder ▪ The heart of the library, the knex query builder is the interface used for building and executing standard SQL queries, such as select, insert, update, delete. ▪ Example knex({ a: 'table', b: 'table' }) .select({ aTitle: 'a.title', bTitle: 'b.title' }) .whereRaw('?? = ??', ['a.column_1', 'b.column_2’]) // select "a"."title" as "aTitle", "b"."title" as "bTitle" from "table" as "a", "table" as "b" where "a"."column_1" = "b"."column_2" 6
  • 7. Application ▪ Pooling ▪ The client created by the configuration initializes a connection pool, using the tarn.js library. This connection pool has a default setting of a min: 2, max: 10 7 Pooling Action Action Action PG Application Action Action Action PG
  • 8. ▪ Transactions ▪ All queries within a transaction are executed on the same database connection, and run the entire set of queries as a single unit of work. Any failure will mean the database will rollback any queries executed on that connection to the pre-transaction state. ▪ Methods 1. As a transaction object 2. As a query builder 8 as a transaction object
  • 9. ▪ Transactions ▪ Methods 1. As a transaction object 2. As a query builder 9 as a transaction object
  • 10. ▪ Migrations ▪ Migrations allow for you to define sets of schema changes so upgrading a database is a breeze. 10
  • 11. ▪ Seed files ▪ Seed files allow you to populate your database with test or seed data independent of your migration files. 11 Run seed
  • 12. 12
  • 13. Testing 1. How to avoid crashing when you update schema ? ▪ Testing 2. How to avoid operation error ? ▪ Deploy system automatically 13 DB (version + 1) Script: Updating schema Success (Commit) Failed (Rollback) DB (version + 1) DB (version) Dev DB Script: Updating schema QA DB PROD DB Dev DB Dev DB QA DB Script: Updating schemaScript: Updating schema
  • 14. 3. Doesn’t need to use another tool or code language to do it. ▪ Just JavaScript 14
  • 15. ▪ Summary ▪ How to avoid crashing when you updated schema ? ▪ Testing ▪ How to avoid operation error ? ▪ Deploying system automatically ▪ Doesn’t need to use another tool or code language to do it ▪ Just JavaScript 15
  • 16. 16
  • 17. ▪ Migration 1. knex init => knexfile.ts 17
  • 18. ▪ Migration 2. knex migrate:make migration ▪ Up / Down Functions 18
  • 19. ▪ Migration 3. knex migrate:latest 19 Check migration record Run unrecorded files
  • 21. ▪ Seed data 1. knex init => knexfile.ts 21
  • 22. ▪ Seed data 2. Create seed data 22
  • 23. ▪ Seed data 3. Create seed handling script and Running 23
  • 24. ▪ Seed Process 24 Seed data of tables Seed handling Scripts knex seed:run DB
  • 25. Testing, deploying system automatically and just javascript ! 25
  • 26. ▪ Testing ▪ Embedded seed testing in migration Process 26 Transaction Transaction
  • 27. ▪ Testing ▪ Dividing Environment ▪ All migration script is knex migrate:latest, but it run different script by different env. 27
  • 28. ▪ deploying system automatically 28
  • 29. ▪ Just javascript ▪ …well…. Just you said that….only javascript. 29
  • 30. 30
  • 31. 31
  • 32. ▪ Advantage ▪ Reduce operation error ! ▪ Deploy schema simpler and safer. ▪ Do it by JS developer. 32