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
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
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