Covers:
- Three-schema approach (overview of Conceptual, Logical and Physical Data Model)
- DEMO: Express with raw SQL (pitfalls of not using an ORM)
- What good is an Object Relational Mapper (ORM)?
- DEMO: Express with Sequelize (models)
- What good are migrations?
- DEMO: Express with Sequelize (migrations)
- Sequelize ecosystem, strengths and weaknesses
2. Agenda
• Three-schema approach
• DEMO: Express w/ SQL
• What good is an “ORM”?
• DEMO: Express w/ Sequelize (models)
• What good are migrations?
• DEMO: Express w/ Sequelize (migrations)
Follow along in Github:
https://github.com/rmharrison/prez-sequelize
2
14. Migrations: Define schema
14
demodb=# d people
Table "public.people"
Column | Type | Collation | Nullable | Default
------------+--------------------------+-----------+----------+-----------------------------------
id | integer | | not null |
nextval('people_id_seq'::regclass)
created_at | timestamp with time zone | | not null | now()
updated_at | timestamp with time zone | | not null | now()
full_name | character varying(255) | | |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
15. Migrations: Define schema
15
demodb=# d people
Table "public.people"
Column | Type | Collation | Nullable | Default
------------+--------------------------+-----------+----------+-----------------------------------
id | integer | | not null |
nextval('people_id_seq'::regclass)
created_at | timestamp with time zone | | not null | now()
updated_at | timestamp with time zone | | not null | now()
first_name | character varying(255) | | |
last_name | character varying(255) | | |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
16. Migrations: Define schema
16
Time Jan 2018 Feb 2018 Mar 2018
release v0.1.0 v0.2.0 v0.3.0
models sales.js
product.js
sales.js
product.js (13 ++++++-----)
store.js
sales.js (100 +++---)
product.js (50 ++++)
store.js (20 +-----)
migrations 201801-migration.js 201802-migration.js
201801-migration.js
201803-migration.js
201802-migration.js
201801-migration.js
17. Seeds: Define data
17
demodb=# SELECT * FROM people;
id | created_at | updated_at | first_name |
last_name
----+----------------------------+----------------------------+------------+-----------
1 | 2019-01-25 09:20:11.392+00 | 2019-01-25 09:20:11.392+00 | John | Watts
2 | 2019-01-25 09:20:11.392+00 | 2019-01-25 09:20:11.392+00 | Charles | Ingram
18. DEMO: Express w/ Sequelize (migrations)
• full_name → first_name, last_name
• Generate migrations and seeds from CLI
18
19. Strengths
● Model-driven development
● Sequelize Models are Javascript classes
○ all the goodies available, inc. Prototype, extends, etc.
● Life-cycle hooks
○ beforeSave, afterValidate, et al.
19
20. Weaknesses addressed ecosystem
● Generate models from existing DB schema
○ npm:sequelize-auto (1400+ stars; officially supported)
● Generate migrations from models
○ Long-standing known issue: https://github.com/sequelize/cli/issues/157
○ npm:sequelize-auto-migrations (incomplete, only handles up)
● Model versioning
○ npm:sequelize-version (low adoption)
● Nested hierarchies
○ npm:sequelize-hierarchy
● Fixtures
○ npm:sequelize-fixtures (maintainer wants to abandon)
20
21. Gotchas
● Cannot link a seed with a specific migration
○ Convention: Assume “latest” for seed data
○ Check SequelizeMeta in seed script
● “Version” is not what you think
○ NB: version in Sequelize is for OptimisticLocking [PR] not model versions
● Poor error messages in migrations
21
== 20190125055636-full-to-first-last: migrating =======
ERROR: Unexpected identifier