Factory functions for database adapter creation and configuration.
This namespace provides convenient factory functions for creating database adapters, data sources, and database contexts. It supports multiple database types (SQLite, PostgreSQL, MySQL, H2) through a unified configuration interface.
Key Features:
Usage: (require '[wagoe.platform.shell.adapters.database.factory :as dbf])
(def ctx (dbf/db-context {:adapter :sqlite :database-path "./app.db"})) (db/execute-query! ctx {:select [:*] :from [:users]})
Factory functions for database adapter creation and configuration.
This namespace provides convenient factory functions for creating database
adapters, data sources, and database contexts. It supports multiple database
types (SQLite, PostgreSQL, MySQL, H2) through a unified configuration interface.
Key Features:
- Database adapter creation from configuration
- Connection pool and datasource management
- Database context creation with adapter + datasource
- Resource management with automatic cleanup
- Configuration validation and error handling
Usage:
(require '[wagoe.platform.shell.adapters.database.factory :as dbf])
(def ctx (dbf/db-context {:adapter :sqlite :database-path "./app.db"}))
(db/execute-query! ctx {:select [:*] :from [:users]})(close-db-context! ctx)Close database context and its connection pool.
Args: ctx: Database context
Returns: nil
Close database context and its connection pool. Args: ctx: Database context Returns: nil
(create-adapter {:keys [adapter] :as db-config})Create database adapter from configuration.
Args: db-config: Database configuration map with :adapter key
Returns: Database adapter implementing DBAdapter protocol
Example: (create-adapter {:adapter :sqlite :database-path "./app.db"}) (create-adapter {:adapter :postgresql :host "localhost" :port 5432 :name "mydb" :username "user" :password "pass"})
Create database adapter from configuration.
Args:
db-config: Database configuration map with :adapter key
Returns:
Database adapter implementing DBAdapter protocol
Example:
(create-adapter {:adapter :sqlite :database-path "./app.db"})
(create-adapter {:adapter :postgresql :host "localhost" :port 5432
:name "mydb" :username "user" :password "pass"})(create-datasource db-config)Create connection pool datasource using adapter and configuration.
Args: db-config: Database configuration map
Returns: HikariDataSource connection pool
Example: (create-datasource {:adapter :sqlite :database-path "./app.db"})
Create connection pool datasource using adapter and configuration.
Args:
db-config: Database configuration map
Returns:
HikariDataSource connection pool
Example:
(create-datasource {:adapter :sqlite :database-path "./app.db"})(create-managed-contexts config-map)Create multiple database contexts with centralized lifecycle management.
Args: config-map: Map of context-name -> db-config
Returns: Map of context-name -> database context, plus a :close-all! function
Example: (def contexts (create-managed-contexts {:main {:adapter :sqlite :database-path "./main.db"} :cache {:adapter :h2 :database-path :memory}})) (core/execute-query! (:main contexts) {...}) ((:close-all! contexts)) ; Close all contexts
Create multiple database contexts with centralized lifecycle management.
Args:
config-map: Map of context-name -> db-config
Returns:
Map of context-name -> database context, plus a :close-all! function
Example:
(def contexts (create-managed-contexts
{:main {:adapter :sqlite :database-path "./main.db"}
:cache {:adapter :h2 :database-path :memory}}))
(core/execute-query! (:main contexts) {...})
((:close-all! contexts)) ; Close all contexts(db-config-from-env)Create database configuration from environment variables.
Expected environment variables:
Returns: Database configuration map
Throws: IllegalArgumentException if required environment variables are missing
Example: ;; With environment: DB_ADAPTER=sqlite DB_PATH=./app.db (db-config-from-env) ;; => {:adapter :sqlite :database-path "./app.db"}
Create database configuration from environment variables.
Expected environment variables:
- DB_ADAPTER: sqlite, postgresql, mysql, or h2
- DB_HOST: Database host (server DBs only)
- DB_PORT: Database port (server DBs only)
- DB_NAME: Database name (server DBs only)
- DB_USERNAME: Username (server DBs only)
- DB_PASSWORD: Password (server DBs only)
- DB_PATH: Database file path (SQLite/H2 only)
- DB_POOL_SIZE: Maximum pool size (optional, default varies by adapter)
Returns:
Database configuration map
Throws:
IllegalArgumentException if required environment variables are missing
Example:
;; With environment: DB_ADAPTER=sqlite DB_PATH=./app.db
(db-config-from-env) ;; => {:adapter :sqlite :database-path "./app.db"}(db-context db-config)Create database context with adapter and datasource.
A database context contains both the database adapter and the connection pool, providing everything needed for database operations through the core API.
Args: db-config: Database configuration map
Returns: Database context map {:adapter adapter :datasource datasource}
Example: (def ctx (db-context {:adapter :sqlite :database-path "./app.db"})) (core/execute-query! ctx {:select [:*] :from [:users]})
Create database context with adapter and datasource.
A database context contains both the database adapter and the connection
pool, providing everything needed for database operations through the core API.
Args:
db-config: Database configuration map
Returns:
Database context map {:adapter adapter :datasource datasource}
Example:
(def ctx (db-context {:adapter :sqlite :database-path "./app.db"}))
(core/execute-query! ctx {:select [:*] :from [:users]})(h2-config database-path)(h2-config database-path opts)Create H2 database configuration.
Args: database-path: String path to H2 database (or :memory for in-memory) opts: Optional map with :pool settings and other options
Returns: Database configuration map
Example: (h2-config "./data/app") (h2-config :memory) (h2-config "./data/app" {:pool {:maximum-pool-size 5}})
Create H2 database configuration.
Args:
database-path: String path to H2 database (or :memory for in-memory)
opts: Optional map with :pool settings and other options
Returns:
Database configuration map
Example:
(h2-config "./data/app")
(h2-config :memory)
(h2-config "./data/app" {:pool {:maximum-pool-size 5}})(in-memory-context)Create in-memory database context for testing.
Uses H2 in-memory database for fast, isolated testing.
Returns: Database context for in-memory H2 database
Create in-memory database context for testing. Uses H2 in-memory database for fast, isolated testing. Returns: Database context for in-memory H2 database
(list-supported-adapters)List all supported database adapters.
Returns: Vector of supported adapter keywords
List all supported database adapters. Returns: Vector of supported adapter keywords
(mysql-config host port database username password)(mysql-config host port database username password opts)Create MySQL database configuration.
Args: host: Database hostname port: Database port database: Database name username: Username password: Password opts: Optional map with :pool settings and other options
Returns: Database configuration map
Example: (mysql-config "localhost" 3306 "mydb" "user" "pass") (mysql-config "localhost" 3306 "mydb" "user" "pass" {:pool {:maximum-pool-size 15}})
Create MySQL database configuration.
Args:
host: Database hostname
port: Database port
database: Database name
username: Username
password: Password
opts: Optional map with :pool settings and other options
Returns:
Database configuration map
Example:
(mysql-config "localhost" 3306 "mydb" "user" "pass")
(mysql-config "localhost" 3306 "mydb" "user" "pass"
{:pool {:maximum-pool-size 15}})(postgresql-config host port database username password)(postgresql-config host port database username password opts)Create PostgreSQL database configuration.
Args: host: Database hostname port: Database port database: Database name username: Username password: Password opts: Optional map with :pool settings and other options
Returns: Database configuration map
Example: (postgresql-config "localhost" 5432 "mydb" "user" "pass") (postgresql-config "localhost" 5432 "mydb" "user" "pass" {:pool {:maximum-pool-size 20}})
Create PostgreSQL database configuration.
Args:
host: Database hostname
port: Database port
database: Database name
username: Username
password: Password
opts: Optional map with :pool settings and other options
Returns:
Database configuration map
Example:
(postgresql-config "localhost" 5432 "mydb" "user" "pass")
(postgresql-config "localhost" 5432 "mydb" "user" "pass"
{:pool {:maximum-pool-size 20}})(sqlite-config database-path)(sqlite-config database-path opts)Create SQLite database configuration.
Args: database-path: String path to SQLite database file opts: Optional map with :pool settings
Returns: Database configuration map
Example: (sqlite-config "./app.db") (sqlite-config "./app.db" {:pool {:maximum-pool-size 3}})
Create SQLite database configuration.
Args:
database-path: String path to SQLite database file
opts: Optional map with :pool settings
Returns:
Database configuration map
Example:
(sqlite-config "./app.db")
(sqlite-config "./app.db" {:pool {:maximum-pool-size 3}})(validate-connection db-config)Test database connection and validate configuration.
Args: db-config: Database configuration map
Returns: Map with connection status and database info
Example: (validate-connection {:adapter :sqlite :database-path "./app.db"})
Test database connection and validate configuration.
Args:
db-config: Database configuration map
Returns:
Map with connection status and database info
Example:
(validate-connection {:adapter :sqlite :database-path "./app.db"})(with-db db-config f)Execute function with temporary database context.
Creates a database context, executes the function with it, then automatically closes the connection pool. Useful for one-time operations or when you don't want to manage the lifecycle manually.
Args: db-config: Database configuration map f: Function that takes database context and returns result
Returns: Result of function execution
Example: (with-db {:adapter :sqlite :database-path "./temp.db"} (fn [ctx] (core/execute-query! ctx {:select [:*] :from [:users]})))
Execute function with temporary database context.
Creates a database context, executes the function with it, then
automatically closes the connection pool. Useful for one-time operations
or when you don't want to manage the lifecycle manually.
Args:
db-config: Database configuration map
f: Function that takes database context and returns result
Returns:
Result of function execution
Example:
(with-db {:adapter :sqlite :database-path "./temp.db"}
(fn [ctx]
(core/execute-query! ctx {:select [:*] :from [:users]})))cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |