Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 1.1 KB

connection.md

File metadata and controls

36 lines (25 loc) · 1.1 KB

Connection

DBX supports multi-connections to the database. It is just a light crystal-db overlay.

require "dbx"
require "pg" # <= PostgreSQL driver
require "sqlite3" # <= SQLite driver

# Create 4 DB connection pool (3 PostgreSQL and 1 SQLite)
DBX.open("app", "postgres://...")
DBX.open("reader", "postgres://...")
DBX.open("writer", "postgres://...")
DBX.open("local", "sqlite3://./data.db")

# Connection: app (using PostgreSQL)
DBX.db("app")

# Connection: local (using SQLite)
DBX.db("local")

# Connection: reader (using PostgreSQL)
DBX.db("reader")

# Connection: writer (using PostgreSQL)
DBX.db("writer")

See API: DBX connection

You can use each DBX connection as you do with crystal-db (example: DBX.db("app").query "select id, created_at, email from users").

Summary

The next chapter explores database querying, an essential chapter to take advantage of DBX's powerful querying capabilities.