O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

COSCUP 2019 - The discussion between Knex.js and PostgreSQL

231 visualizações

Publicada em

The discussion between knex.js and postgreSQL

Publicada em: Tecnologia
  • Seja o primeiro a comentar

COSCUP 2019 - The discussion between Knex.js and PostgreSQL

  1. 1. Len Chang 08.18 2019 1
  2. 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. 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
  4. 4. Library Docs (Link) 4
  5. 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. 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. 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. 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. 9. ▪ Transactions ▪ Methods 1. As a transaction object 2. As a query builder 9 as a transaction object
  10. 10. ▪ Migrations ▪ Migrations allow for you to define sets of schema changes so upgrading a database is a breeze. 10
  11. 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. 12
  13. 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. 14. 3. Doesn’t need to use another tool or code language to do it. ▪ Just JavaScript 14
  15. 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. 16
  17. 17. ▪ Migration 1. knex init => knexfile.ts 17
  18. 18. ▪ Migration 2. knex migrate:make migration ▪ Up / Down Functions 18
  19. 19. ▪ Migration 3. knex migrate:latest 19 Check migration record Run unrecorded files
  20. 20. ▪ Migration Process 20 knexfile.ts 20190814170951_migration.ts Success (Commit)Failed (Rollback) Run Migration
  21. 21. ▪ Seed data 1. knex init => knexfile.ts 21
  22. 22. ▪ Seed data 2. Create seed data 22
  23. 23. ▪ Seed data 3. Create seed handling script and Running 23
  24. 24. ▪ Seed Process 24 Seed data of tables Seed handling Scripts knex seed:run DB
  25. 25. Testing, deploying system automatically and just javascript ! 25
  26. 26. ▪ Testing ▪ Embedded seed testing in migration Process 26 Transaction Transaction
  27. 27. ▪ Testing ▪ Dividing Environment ▪ All migration script is knex migrate:latest, but it run different script by different env. 27
  28. 28. ▪ deploying system automatically 28
  29. 29. ▪ Just javascript ▪ …well…. Just you said that….only javascript. 29
  30. 30. 30
  31. 31. 31
  32. 32. ▪ Advantage ▪ Reduce operation error ! ▪ Deploy schema simpler and safer. ▪ Do it by JS developer. 32
  33. 33. LinkedIn Facebook PPT Online 33

×