Database: Getting Started
Introduction
Modern applications depend on reliable, efficient database interactions. Elegant simplifies this process by providing a clean, expressive interface for working with databases. Whether you need raw SQL power, a fluent query builder, or an elegant ORM, this toolkit has you covered.
Elegant offers native support for four popular database systems:
MySQL - The world's most popular open-source database
MariaDB - A performance-enhanced MySQL fork
PostgreSQL - Advanced open-source relational database
SQLite - Lightweight, file-based database perfect for development
Configuration
Setting Up Your Database Connection
Database configuration is managed through the elegant.config.js file in your project root. This file defines your database connections and specifies which one to use by default.
Here's a complete configuration example:
Environment Variables
It's best practice to store sensitive database credentials in environment variables. Create a .env file in your project root:
You may override the default config and migrations path with Elegant specific environment variables.
Executing SQL Queries
Establishing a Connection
Before running queries, establish a connection to your database:
In most applications, a single database instance is created and shared across multiple services throughout the application lifecycle. Elegant elegantly implements the singleton design pattern to ensure efficient resource management and connection reuse:
To use a specific connection (other than the default):
Gracefully disconnect from all Elegant database instances that were instantiated using the connection method:
Disconnect from all Elegant database instances that were created with the singleton method:
SELECT Queries
Retrieve data using the select method:
Parameters:
First argument: SQL query string
Second argument: Array of parameter bindings
The select method returns an array of results. Each result is a plain object with column names as properties.
Type-Safe Queries
Leverage TypeScript's type system for better code safety and IDE support:
Retrieving Scalar Values
When you need a single value instead of a full result set, use the scalar method:
UPDATE Queries
Modify existing records with the update method, which returns the number of affected rows:
DELETE Queries
Remove records using the delete method:
Raw Queries Without Binding
Parameter Binding
Elegant uses parameterized queries to protect against SQL injection. Parameters are represented by ? or $n placeholders depending the database dialect.
MySQL and MariaDB and SQLite use ? to represent parameters while Postgres use $ followd by the parameter number.
Accessing the Underlying Driver
For advanced use cases, access the raw database driver:
Database Transactions
Transactions ensure data integrity by grouping multiple operations into an atomic unit:
Manual transaction control:
Closing Connections
Close database connections when your application shuts down:
Database-Specific Notes
MySQL and MariaDB
Uses
?for parameter placeholdersSupports all standard features
Recommended charset:
utf8mb4Collation:
utf8mb4_unicode_ci
PostgreSQL
Uses
$1,$2, etc. for parameter placeholdersCase-sensitive table and column names by default
Supports advanced features like arrays and JSON operators
Boolean type is native (not emulated)
SQLite
Uses
?for parameter placeholdersLightweight, serverless database
Limited ALTER TABLE support (some column modifications require table recreation)
Foreign keys must be explicitly enabled in connection settings
Additional Resources
For more information and support:
GitHub Repository: https://github.com/joshua-williams/elegant
Report Issues: https://github.com/joshua-williams/elegant/issues
TypeScript Type Definitions: Included with package
Next Steps
Now that you understand the basics of database operations with Elegant, explore more advanced features:
Query Builder - Build complex queries with a fluent interface
Migrations - Version control for your database schema
Elegant ORM - Work with models instead of raw SQL