mysql-kit

4.9.0

🐬 Pure Swift MySQL client built on non-blocking, event-driven sockets.
vapor/mysql-kit

What's New

4.9.0 - Actually support query logging

2024-05-29T18:20:46Z

What's Changed

Actually support query logging by @gwynne in #326

Now supports query logging with the same semantics as the other drivers. When a query is executed with query logging enabled at or above the logger’s current log level, the actual SQL query and its accompanying array of bound parameters (if any) are logged as structured metadata on the logger using a generic message. This follows the recommended guidelines for logging in libraries. Credit goes to @MahdiBM for the original suggestion.

Before:

2024-05-29T00:00:00Z debug codes.vapor.fluent : database-id=mysql [MySQLKit] SELECT * FROM foo WHERE a=? [["bar"]]

After:

2024-05-29T00:00:00Z debug codes.vapor.fluent : database-id=mysql sql=SELECT * FROM foo WHERE a=? binds=["bar"] [MySQLKit] Executing query
This patch was released by @gwynne

Full Changelog: 4.8.0...4.9.0

MySQLKit

Documentation Team Chat MIT License Continuous Integration Swift 5.8+


MySQLKit is an SQLKit driver for MySQL clients. It supports building and serializing MySQL-dialect SQL queries. MySQLKit uses MySQLNIO to connect and communicate with the database server asynchronously. AsyncKit is used to provide connection pooling.

Usage

Use the SPM string to easily include the dependendency in your Package.swift file.

.package(url: "https://github.com/vapor/mysql-kit.git", from: "4.0.0")

Supported Platforms

MySQLKit supports the following platforms:

  • Ubuntu 20.04+
  • macOS 10.15+

Configuration

Database connection options and credentials are specified using a MySQLConfiguration struct.

import MySQLKit

let configuration = MySQLConfiguration(
    hostname: "localhost",
    port: 3306,
    username: "vapor_username",
    password: "vapor_password",
    database: "vapor_database"
)

URL string based configuration is also supported.

guard let configuration = MySQLConfiguration(url: "mysql://...") else {
    ...
}

To connect via unix-domain sockets, use unixDomainSocketPath instead of hostname and port.

let configuration = MySQLConfiguration(
    unixDomainSocketPath: "/path/to/socket",
    username: "vapor_username",
    password: "vapor_password",
    database: "vapor_database"
)

Connection Pool

Once you have a MySQLConfiguration, you can use it to create a connection source and pool.

let eventLoopGroup: EventLoopGroup = ...
defer { try! eventLoopGroup.syncShutdown() }

let pools = EventLoopGroupConnectionPool(
    source: MySQLConnectionSource(configuration: configuration), 
    on: eventLoopGroup
)
defer { pools.shutdown() }

First create a MySQLConnectionSource using the configuration struct. This type is responsible for creating new connections to your database server as needed.

Next, use the connection source to create an EventLoopGroupConnectionPool. You will also need to pass an EventLoopGroup. For more information on creating an EventLoopGroup, visit SwiftNIO's documentation. Make sure to shutdown the connection pool before it deinitializes.

EventLoopGroupConnectionPool is a collection of pools for each event loop. When using EventLoopGroupConnectionPool directly, random event loops will be chosen as needed.

pools.withConnection { conn 
    print(conn) // MySQLConnection on randomly chosen event loop
}

To get a pool for a specific event loop, use pool(for:). This returns an EventLoopConnectionPool.

let eventLoop: EventLoop = ...
let pool = pools.pool(for: eventLoop)

pool.withConnection { conn
    print(conn) // MySQLConnection on eventLoop
}

MySQLDatabase

Both EventLoopGroupConnectionPool and EventLoopConnectionPool can be used to create instances of MySQLDatabase.

let mysql = pool.database(logger: ...) // MySQLDatabase
let rows = try mysql.simpleQuery("SELECT @@version;").wait()

Visit MySQLNIO's docs for more information on using MySQLDatabase.

SQLDatabase

A MySQLDatabase can be used to create an instance of SQLDatabase.

let sql = mysql.sql() // SQLDatabase
let planets = try sql.select().column("*").from("planets").all().wait()

Visit SQLKit's docs for more information on using SQLDatabase.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Sun Jun 09 2024 01:23:55 GMT-0900 (Hawaii-Aleutian Daylight Time)