Mito, a successor of Integral

715 views

Published on

At Lisp Meetup #43

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
715
On SlideShare
0
From Embeds
0
Number of Embeds
127
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mito, a successor of Integral

  1. 1. MITO, A SUCCESSOR OF INTEGRAL LISP MEETUP #43 Aug 30, 2016
  2. 2. I’m Eitaro Fukamachi @nitro_idiot fukamachi
  3. 3. Do you use RDBMS?
  4. 4. Do you know O/R Mapper?
  5. 5. RDBMS Record Record Record Table Table Table
  6. 6. RDBMS Record Record Record Table Table Table CLOS class CLOS class CLOS class
  7. 7. RDBMS Record Record Record Table Table Table CLOS class CLOS class CLOS class make-instance
  8. 8. RDBMS Record Record Record Table Table Table CLOS class CLOS class CLOS class CLOS Object CLOS Object CLOS Object make-instance
  9. 9. CLOS class CLOS class CLOS class CLOS Object CLOS Object CLOS Object make-instance
  10. 10. CLOS class CLOS class CLOS class CLOS Object CLOS Object CLOS Object make-instance ▸ Abstracts RDBMS including SQL ▸ Maps RDB tables to CLOS classes ▸ Maps RDB records to CLOS objects ▸ Can add methods ▸ Accelerates development
  11. 11. Do you know O/R Mapper? ORM Examples ▸ CLSQL ▸ Postmodern
  12. 12. Do you know O/R Mapper? ORM Examples ▸ CLSQL ▸ Postmodern ▸ Integral
  13. 13. Do you know O/R Mapper? ORM Examples ▸ CLSQL ▸ Postmodern ▸ Integral A talk about “Integral” 2 years ago
  14. 14. Do you know O/R Mapper? ORM Examples ▸ CLSQL ▸ Postmodern ▸ Integral ▸ Crane A talk about “Integral” 2 years ago
  15. 15. Do you know O/R Mapper? ORM Examples ▸ CLSQL ▸ Postmodern ▸ Integral (Not recommended) ▸ Crane ▸ Mito A talk about “Integral” 2 years ago NEW!
  16. 16. What’s new in Mito?
  17. 17. What’s new in Mito? Mito, a successor of Integral ▸ Supports PostgreSQL, as well as MySQL & SQLite3 ▸ Implicit columns (auto-pk & record-timestamps) ▸ Reference between DB table classes ▸ Eager loading ▸ Inheritance of DB table classes ▸ Migrations ▸ Schema versioning
  18. 18. Implicit columns, auto-pk & record-timestamps
  19. 19. (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class))
  20. 20. CREATE TABLE user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), created_at TIME, updated_at TIME ) (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class))
  21. 21. CREATE TABLE user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), created_at TIME, updated_at TIME ) (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class))
  22. 22. CREATE TABLE user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), created_at TIME, updated_at TIME ) (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class))
  23. 23. CREATE TABLE user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), created_at TIME, updated_at TIME ) (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class))
  24. 24. CREATE TABLE user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), created_at TIME, updated_at TIME ) (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) auto-pk record-timestamps
  25. 25. Reference between DB table classes
  26. 26. Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class))
  27. 27. (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user-id :col-type (:bigint () :unsigned) :initarg :user-id :accessor tweet-user-id)) (:metaclass mito:dao-table-class)) Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class))
  28. 28. (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user-id :col-type (:bigint () :unsigned) :initarg :user-id :accessor tweet-user-id)) (:metaclass mito:dao-table-class)) Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defun tweet-user (tweet) (mito:find-dao ‘user :id (tweet-user-id tweet)))
  29. 29. (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user-id :col-type (:bigint () :unsigned) :initarg :user-id :accessor tweet-user-id)) (:metaclass mito:dao-table-class)) Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defun tweet-user (tweet) (mito:find-dao ‘user :id (tweet-user-id tweet))) These kind of accessors are quite common.
  30. 30. Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user :col-type user :initarg :user :accessor tweet-user)) (:metaclass mito:dao-table-class))
  31. 31. It may cause a performance issue: N+1 query.
  32. 32. Eager loading helps.
  33. 33. Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user :col-type user :initarg :user :accessor tweet-user)) (:metaclass mito:dao-table-class)) ;; Tweets contain “Japan” (select-dao 'tweet (where (:like :status “%Japan%"))) ;; Getting names of tweeted users. (mapcar (lambda (tweet) (user-name (tweet-user tweet))) *) BAD EXAMPLE
  34. 34. Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user :col-type user :initarg :user :accessor tweet-user)) (:metaclass mito:dao-table-class)) ;; Tweets contain “Japan” (select-dao 'tweet (where (:like :status “%Japan%"))) ;; Getting names of tweeted users. (mapcar (lambda (tweet) (user-name (tweet-user tweet))) *) SQL execution for each records. (N times) BAD EXAMPLE
  35. 35. Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user :col-type user :initarg :user :accessor tweet-user)) (:metaclass mito:dao-table-class)) ;; Tweets contain “Japan” (select-dao 'tweet (includes 'user) (where (:like :status “%Japan%"))) ;; No additional SQLs will be executed. (tweet-user (first *)) GOOD EXAMPLE
  36. 36. Reference between DB table classes (defclass user () … (:metaclass mito:dao-table-class)) (defclass tweet () ((status :col-type :text :initarg :status :accessor tweet-status) (user :col-type user :initarg :user :accessor tweet-user)) (:metaclass mito:dao-table-class)) ;; Tweets contain “Japan” (select-dao 'tweet (includes 'user) (where (:like :status “%Japan%"))) ;; No additional SQLs will be executed. (tweet-user (first *)) Throw 1 query to retrieve users beforehand. GOOD EXAMPLE
  37. 37. Inheritance of DB table classes
  38. 38. Sometimes similar DB tables are needed. Photo by Andrey Shipilov
  39. 39. Inheritance of DB table classes Ex) SQL Antipatterns: Logical Deletion slideshare.net/t_wada/ronsakucasual slideshare.net/SoudaiSone/postgre-sql-54919575 PostgreSQL Antipatterns
  40. 40. Inheritance of DB table classes Ex) SQL Antipatterns: Logical Deletion (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) (defclass deleted-user (user) ((deleted-at :col-type :datetime :initarg :deleted-at :initform (local-time:now) :accessor user-deleted-at)) (:metaclass mito:dao-table-class))
  41. 41. Inheritance of DB table classes Ex) SQL Antipatterns: Logical Deletion (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) (defclass deleted-user (user) ((deleted-at :col-type :datetime :initarg :deleted-at :initform (local-time:now) :accessor user-deleted-at)) (:metaclass mito:dao-table-class)) CREATE TABLE deleted_user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), deleted_at TIME NOT NULL, created_at TIME, updated_at TIME )
  42. 42. Inheritance of DB table classes Ex) SQL Antipatterns: Logical Deletion (defclass user () ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) (defclass deleted-user (user) ((deleted-at :col-type :datetime :initarg :deleted-at :initform (local-time:now) :accessor user-deleted-at)) (:metaclass mito:dao-table-class)) CREATE TABLE deleted_user ( id BIGSERIAL NOT NULL PRIMARY KEY, name VARCHAR(64) NOT NULL, email VARCHAR(128), deleted_at TIME NOT NULL, created_at TIME, updated_at TIME )
  43. 43. Inheritance of DB table classes Ex) SQL Antipatterns: Logical Deletion (defmethod mito:delete-dao :before ((user user)) (mito:create-dao ‘deleted-user :id (object-id user) :name (user-name user) :email (user-email user) :created-at (object-created-at user) :updated-at (object-updated-at user))) Copy “user” records to “deleted_user” before deleting.
  44. 44. Mixin DB table injection Photo by Steve Snodgrass
  45. 45. Inheritance of DB table classes - Mixin Mixin ▸ Injects columns and methods ▸ record-timestamps is an actual example ▸ Adds created_at & updated_at
  46. 46. Inheritance of DB table classes - Mixin Ex) mito-auth ▸ Managing user passwords is dull ▸ Stores hashed passwords ▸ Authenticates github.com/fukamachi/mito-auth
  47. 47. Inheritance of DB table classes - Mixin Ex) mito-auth (defclass user (has-secure-password) ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) github.com/fukamachi/mito-auth
  48. 48. Inheritance of DB table classes - Mixin Ex) mito-auth (defclass user (has-secure-password) ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) (mito:create-dao ‘user :name “fukamachi” :email “e.arrows@gmail.com” :password “c0mmon-l1sp”) github.com/fukamachi/mito-auth
  49. 49. Inheritance of DB table classes - Mixin Ex) mito-auth (defclass user (has-secure-password) ((name :col-type (:varchar 64) :initarg :name :accessor user-name) (email :col-type (or (:varchar 128) :null) :initarg :email :accessor user-email)) (:metaclass mito:dao-table-class)) (mito:create-dao ‘user :name “fukamachi” :email “e.arrows@gmail.com” :password “c0mmon-l1sp”) Stores a hashed password and salt, not a plain text. github.com/fukamachi/mito-auth
  50. 50. What wasn’t mentioned ▸ Migrations ▸ Schema versioning ▸ Inflation/deflation ▸ See https://github.com/fukamachi/mito
  51. 51. Thanks.
  52. 52. EITARO FUKAMACHI 8arrow.org @nitro_idiot fukamachi
  53. 53. Thanks. See Also ▸ https://github.com/fukamachi/mito ▸ https://github.com/fukamachi/mito-auth ▸ https://github.com/fukamachi/mito-attachment

×