• Stars
    star
    222
  • Rank 179,123 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐬 Pure Swift MySQL client built on non-blocking, event-driven sockets.

MySQL

Documentation Team Chat MIT License Continuous Integration Swift 5.2

🐬 Non-blocking, event-driven Swift client for MySQL.

Major Releases

The table below shows a list of MySQLKit major releases alongside their compatible NIO and Swift versions.

Version NIO Swift SPM
4.0 2.0 5.2+ from: "4.0.0"
3.0 1.0 4.0+ from: "3.0.0"
2.0 N/A 3.1+ from: "2.0.0"
1.0 N/A 3.1+ from: "1.0.0"

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

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

Supported Platforms

MySQLKit supports the following platforms:

  • Ubuntu 16.04+
  • macOS 10.15+

Overview

MySQLKit is a MySQL client library built on SQLKit. 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.

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.

More Repositories

1

vapor

πŸ’§ A server-side Swift HTTP web framework.
Swift
24,188
star
2

fluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
Swift
1,308
star
3

redis

Vapor provider for RediStack
Swift
458
star
4

console-kit

πŸ’» APIs for creating interactive CLI tools.
Swift
456
star
5

leaf

πŸƒ An expressive, performant, and extensible templating language built for Swift.
Swift
432
star
6

postgres-nio

🐘 Non-blocking, event-driven Swift client for PostgreSQL.
Swift
325
star
7

jwt

Vapor JWT provider
Swift
317
star
8

docs

πŸ“– Documentation markdown for all Vapor packages.
Swift
316
star
9

toolbox

Simplifies common command line tasks when using Vapor
Swift
283
star
10

websocket-kit

WebSocket client library built on SwiftNIO
Swift
272
star
11

sql-kit

*️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
Swift
239
star
12

http

πŸš€ Non-blocking, event-driven HTTP built on Swift NIO.
Swift
238
star
13

fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
Swift
206
star
14

postgres-kit

🐘 Non-blocking, event-driven Swift client for PostgreSQL.
Swift
187
star
15

jwt-kit

πŸ”‘ JSON Web Token (JWT) signing and verification (HMAC, ECDSA, EdDSA, RSA, PSS) with support for JWS and JWK
Swift
180
star
16

queues

A queue system for Vapor.
Swift
163
star
17

fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
Swift
145
star
18

api-template

πŸ’§ A starting point for Vapor APIs.
Swift
135
star
19

open-crypto

πŸ”‘ Hashing (BCrypt, SHA2, HMAC), encryption (AES), public-key (RSA), and random data generation.
Swift
134
star
20

multipart-kit

🏞 Parses and serializes multipart-encoded data with Codable support.
Swift
132
star
21

routing-kit

🚍 High-performance trie-node router.
Swift
122
star
22

apns

Helpful extensions and abstractions for using APNSwift
Swift
115
star
23

mysql-nio

🐬 Non-blocking, event-driven Swift client for MySQL.
Swift
88
star
24

service

πŸ“¦ Dependency injection / inversion of control framework.
Swift
85
star
25

core

🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.
Swift
80
star
26

fluent-mysql-driver

πŸ–‹πŸ¬ Swift ORM (queries, models, relations, etc) built on MySQL.
Swift
76
star
27

async-kit

Sugary extensions for the SwiftNIO library
Swift
71
star
28

template

Used by Vapor Toolbox’s new project command
Swift
70
star
29

fluent-sqlite-driver

Fluent driver for SQLite
Swift
67
star
30

sqlite-kit

Non-blocking SQLite client library with SQL builder built on SwiftNIO
Swift
58
star
31

sqlite-nio

Non-blocking wrapper for libsqlite3-dev using SwiftNIO
C
58
star
32

validation

βœ… Extensible data validation library (name, email, etc)
Swift
56
star
33

auth

πŸ‘€ Authentication and Authorization framework for Fluent.
Swift
53
star
34

leaf-kit

πŸƒ An expressive, performant, and extensible templating language built for Swift.
Swift
47
star
35

template-kit

πŸ“„ Easy-to-use foundation for building powerful templating languages in Swift.
Swift
45
star
36

website

Vapor's website running on Swift
Swift
45
star
37

docs-cn

πŸ‡¨πŸ‡³ Chinese translation of Vapor's documentation markdown.
HTML
43
star
38

web-template

A starting point for web applications
Swift
41
star
39

url-encoded-form

πŸ“ Parse and serialize url-encoded form data with Codable support.
Swift
41
star
40

database-kit

πŸ—„ Core services for creating database integrations.
Swift
40
star
41

auth-template

A starting point for Vapor applications using the auth provider.
Swift
35
star
42

university

Web application, iOS app, and API providing access to tutorials for the Vapor web framework.
Swift
24
star
43

fluent-mongo-driver

MongoDB support for Fluent built on MongoKittten.
Swift
24
star
44

queues-redis-driver

A Redis implementation for https://github.com/vapor/queues
Swift
22
star
45

design

Contains the reference designs and build pipeline to generate all design files for Vapor's sites
Swift
22
star
46

email

A common API for Vapor to integrate with different email providers
Swift
20
star
47

codable-kit

Conveniences for working with Swift's Codable protocols.
Swift
19
star
48

kafka

Swift Apacha Kafka (real-time data pipelines and streaming apps)
Swift
19
star
49

apt

Manage Vapor's Swift APT repository
Shell
17
star
50

blog

The Vapor Blog
Swift
16
star
51

penny-bot

The code that runs Penny πŸ€–
Swift
15
star
52

template-bare

A barebones template ready for use
Swift
12
star
53

docker

Swift
12
star
54

api-docs

Scripts and assets for Vapor's API documentation site at https://api.vapor.codes
Swift
11
star
55

redis-kit

Helpful extensions and abstractions for using RediStack
Swift
8
star
56

swift-getting-started-web-server

The source code for the Getting Started Guide on Vapor on swift.org
Swift
7
star
57

homebrew-tap

Homebrew Taps
Ruby
7
star
58

template-fluent-postgres

A template ready for use configured with Fluent and PostgreSQL
Swift
6
star
59

release-bot

Webhook-based GitHub bot that automatically tags new releases and posts to Discord when you merge PRs
Swift
6
star
60

ci

Support files and configurations for Vapor's CI
Swift
5
star
61

bot-github

A github bot to do things like interact with CI for the Vapor organization
Swift
5
star
62

template-fluent-postgres-leaf

A template ready for use configured with Leaf, Fluent and PostgreSQL
Swift
4
star
63

swiftly-action

Simple one-step access to the Swiftly toolchain manager from GitHub Actions workflows
3
star
64

swift-codecov-action

A GitHub Action which performs Codecov.io uploads with additional support for Swift projects
Swift
3
star
65

template-fluent-mysql

A template ready for use configured with Fluent and MySQL
Swift
2
star
66

docs-de

Deutsche Übersetzung der Vapor-Dokumentation
HTML
1
star