Mito, a successor of Integral

53
MITO, A SUCCESSOR OF INTEGRAL LISP MEETUP #43 Aug 30, 2016

Transcript of Mito, a successor of Integral

Page 1: Mito, a successor of Integral

MITO, A SUCCESSOR OF INTEGRAL

LISP MEETUP #43 Aug 30, 2016

Page 2: Mito, a successor of Integral

I’m Eitaro Fukamachi @nitro_idiot fukamachi

Page 3: Mito, a successor of Integral

Do you use RDBMS?

Page 4: Mito, a successor of Integral

Do you know O/R Mapper?

Page 5: Mito, a successor of Integral

RDBMS

Record

Record

Record

TableTable

Table

Page 6: Mito, a successor of Integral

RDBMS

Record

Record

Record

TableTable

Table

CLOS class CLOS class

CLOS class

Page 7: Mito, a successor of Integral

RDBMS

Record

Record

Record

TableTable

Table

CLOS class CLOS class

CLOS class

make-instance

Page 8: Mito, a successor of Integral

RDBMS

Record

Record

Record

TableTable

Table

CLOS class CLOS class

CLOS class

CLOS Object

CLOS Object

CLOS Object

make-instance

Page 9: Mito, a successor of Integral

CLOS class CLOS class

CLOS class

CLOS Object

CLOS Object

CLOS Object

make-instance

Page 10: Mito, a successor of Integral

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

Page 11: Mito, a successor of Integral

Do you know O/R Mapper?

ORM Examples

▸ CLSQL

▸ Postmodern

Page 12: Mito, a successor of Integral

Do you know O/R Mapper?

ORM Examples

▸ CLSQL

▸ Postmodern

▸ Integral

Page 13: Mito, a successor of Integral

Do you know O/R Mapper?

ORM Examples

▸ CLSQL

▸ Postmodern

▸ Integral

A talk about “Integral”

2 years ago

Page 14: Mito, a successor of Integral

Do you know O/R Mapper?

ORM Examples

▸ CLSQL

▸ Postmodern

▸ Integral

▸ Crane

A talk about “Integral”

2 years ago

Page 15: Mito, a successor of Integral

Do you know O/R Mapper?

ORM Examples

▸ CLSQL

▸ Postmodern

▸ Integral (Not recommended)

▸ Crane

▸ Mito

A talk about “Integral”

2 years ago

NEW!

Page 16: Mito, a successor of Integral

What’s new in Mito?

Page 17: Mito, a successor of Integral

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

Page 18: Mito, a successor of Integral

Implicit columns, auto-pk & record-timestamps

Page 19: Mito, a successor of Integral

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

Page 20: Mito, a successor of Integral

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

Page 21: Mito, a successor of Integral

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

Page 22: Mito, a successor of Integral

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

Page 23: Mito, a successor of Integral

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

Page 24: Mito, a successor of Integral

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

Page 25: Mito, a successor of Integral

Reference between DB table classes

Page 26: Mito, a successor of Integral

Reference between DB table classes

(defclass user () … (:metaclass mito:dao-table-class))

Page 27: Mito, a successor of Integral

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

Page 28: Mito, a successor of Integral

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

Page 29: Mito, a successor of Integral

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

Page 30: Mito, a successor of Integral

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

Page 31: Mito, a successor of Integral

It may cause a performance issue: N+1 query.

Page 32: Mito, a successor of Integral

Eager loading helps.

Page 33: Mito, a successor of Integral

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

Page 34: Mito, a successor of Integral

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

Page 35: Mito, a successor of Integral

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

Page 36: Mito, a successor of Integral

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

Page 37: Mito, a successor of Integral

Inheritance of DB table classes

Page 39: Mito, a successor of Integral

Inheritance of DB table classes

Ex) SQL Antipatterns: Logical Deletion

slideshare.net/t_wada/ronsakucasual

slideshare.net/SoudaiSone/postgre-sql-54919575

PostgreSQL Antipatterns

Page 40: Mito, a successor of Integral

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

Page 41: Mito, a successor of Integral

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 )

Page 42: Mito, a successor of Integral

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 )

Page 43: Mito, a successor of Integral

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.

Page 45: Mito, a successor of Integral

Inheritance of DB table classes - Mixin

Mixin

▸ Injects columns and methods

▸ record-timestamps is an actual example

▸ Adds created_at & updated_at

Page 46: Mito, a successor of Integral

Inheritance of DB table classes - Mixin

Ex) mito-auth

▸ Managing user passwords is dull

▸ Stores hashed passwords

▸ Authenticates

github.com/fukamachi/mito-auth

Page 47: Mito, a successor of Integral

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

Page 48: Mito, a successor of Integral

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 “[email protected]” :password “c0mmon-l1sp”)

github.com/fukamachi/mito-auth

Page 49: Mito, a successor of Integral

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 “[email protected]” :password “c0mmon-l1sp”)

Stores a hashed password and salt, not a plain text.

github.com/fukamachi/mito-auth

Page 50: Mito, a successor of Integral

What wasn’t mentioned▸ Migrations

▸ Schema versioning

▸ Inflation/deflation

▸ See https://github.com/fukamachi/mito

Page 51: Mito, a successor of Integral

Thanks.

Page 52: Mito, a successor of Integral

EITARO FUKAMACHI 8arrow.org @nitro_idiot fukamachi

Page 53: Mito, a successor of Integral

Thanks.

See Also

▸ https://github.com/fukamachi/mito

▸ https://github.com/fukamachi/mito-auth

▸ https://github.com/fukamachi/mito-attachment